dexie / Dexie.js

A Minimalistic Wrapper for IndexedDB
https://dexie.org
Apache License 2.0
11.59k stars 642 forks source link

dexie-cluod: on populate error #1365

Open maor700 opened 3 years ago

maor700 commented 3 years ago

Here the code:

treesDB.on("populate", async () => {
  const treeId = await treesDB.createNewTree("Categories");
  if (!treeId) return;
  const { id, parentPath } = (await treesDB.getRoot(treeId)) || {};
  const finalParentPath = parentPath + id + "/";
  return await treesDB.treesItems.bulkAdd(([
    { treeId, name: "Action", parentPath: finalParentPath } as TreeItem,
    { treeId, name: "Comedy", parentPath: finalParentPath } as TreeItem,
    { treeId, name: "Drama", parentPath: finalParentPath } as TreeItem,
    { treeId, name: "Fantasy", parentPath: finalParentPath } as TreeItem,
    { treeId, name: "Horror", parentPath: finalParentPath } as TreeItem,
    { treeId, name: "Mystery", parentPath: finalParentPath } as TreeItem,
  ]))
})

The code causes an error. Same code only with the event "ready" works well.

Error: DatabaseClosedError: DataError Failed to execute 'add' on 'IDBObjectStore': Evaluating the object store's key path did not yield a value. DataError: Failed to execute 'add' on 'IDBObjectStore': Evaluating the object store's key path did not yield a value.

dfahlander commented 3 years ago

I see the reason as populate event is lower level and run before on ready. Thanks. Might inactivate populate event when using dexie cloud (document not to use it) as populating data should be done using import script rather than client side. But it shouldn't fail like it's doing here.

maor700 commented 3 years ago

So how do I initiate the db with content for the first time?

dfahlander commented 3 years ago

You can use npx dexie-cloud import to import data that needs to be in the db. Try npx dexie-cloud export to examine the format.

Regarding per-user data we haven't figured out the final solution yet as we want to avoid the situation of having duplicated data to be populated in the situation when a session starts off unauthorized and later on logs in to existing account. There will be a solution for it, it's just not decided yet the exact api but there might be some kind of data templates for per-user singletons with dedicated ids that are same for every user but represents different objects per user.