Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving table blocks in Google Firestore #2636

Closed
lewisdonovan opened this issue Feb 25, 2024 · 1 comment
Closed

Saving table blocks in Google Firestore #2636

lewisdonovan opened this issue Feb 25, 2024 · 1 comment

Comments

@lewisdonovan
Copy link

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

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

// 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

// 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

@neSpecc
Copy link
Member

neSpecc commented Feb 25, 2024

This issue is not related to the Editor.js core. Consider reopening it in the Table tool repo: https://github.com/editor-js/table

@neSpecc neSpecc closed this as completed Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants