editor-js / editor-js/table

Saving to Google Firestore

Open
#145 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
146
Forks
162
Avg merge
5m
Merged PRs (30d)
1

Description

Hi guys, thanks for the awesome tool!

I noticed in a recent project that Google Firestore doesn't allow storage of nested arrays, so any documents that include table blocks get rejected.

I've written a couple of functions to structure the nested arrays as objects on save, and decode them back to nested arrays on fetch (see below), I was wondering if this is something we could consider making a native feature (or an option in the config)?

utils.ts
```typescript
export function encodeBlocksForFirestore(blocks: OutputBlockData[]) {
if (!blocks || !blocks.length) return blocks;
return blocks.map(block => {
if (!block || block.type !== "table") return block;
const newContent = Object.fromEntries(block.data.content);
block.data.content = newContent
return block;
});
}
export function decodeBlocksFromFirestore(blocks: OutputBlockData[]) {
if (!blocks || !blocks.length) return blocks;
return blocks.map(block => {
if (!block || block.type !== "table") return block;
const newContent = Object.keys(block.data.content).map(k => {
return [ k, block.data.content[k] ]
});
block.data.content = newContent
return block;
})
}
```

And then to make use of them on your server:

POST or PATCH
```typescript
// Get payload from client...

// Encode blocks
if (json.content.blocks && json.content.blocks.length) {
const fixedBlocks = encodeBlocksForFirestore(json.content.blocks);
json.content.blocks = fixedBlocks;
}

// Save json to DB...
```

GET
```typescript
// Retrieve data from DB...

// Decode blocks
if (json.content && json.content.blocks.length) {
const fixedBlocks = decodeBlocksFromFirestore(json.content.blocks);
json.content.blocks = fixedBlocks
}

// Return json to client...
```

Thanks

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.