I'm trying to build an m:n relationship with react, dexie and dexie-relationsships.
I have tags and blocks, and each tag can link to many blocks, and each block can link to many tags
You can find the example code here: https://codesandbox.io/s/dexie-relations-lpxg6
import React from "react";
import "./styles.css";
import Dexie from "dexie";
import relationships from "dexie-relationships";
export default function App() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
f = async () => {
const tagName = "first";
Dexie.delete("db");
const db = new Dexie("db", { addons: [relationships] });
db.version(1).stores({
tags: `id++, &name, dateCreated`,
blocks: `id++, dateCreated, content`,
relations: `id++, tagID -> tags.id, blockID -> blocks.id, dateLinked`
});
db.open().catch(function (err) {
console.error("Failed to open db: " + (err.stack || err));
});
db.on("ready", async function () {
// on('ready') event will fire when database is open but
// before any other queued operations start executing.
// By returning a Promise from this event,
// the framework will wait until promise completes before
// resuming any queued database operations.
await db.tags.add({ id: 1, name: tagName, dateCreated: Date.now() });
await db.blocks.add({ id: 1, content: "bla bla", dateCreated: Date.now() });
await db.relations.add({
tagID: 1,
blockID: 1,
dateLinked: Date.now(),
id: 1
});
console.log(`tags: ${JSON.stringify(await db.tags.toArray(), null, 4)}`);
console.log(
`blocks: ${JSON.stringify(await db.blocks.toArray(), null, 4)}`
);
console.log(
`relations: ${JSON.stringify(await db.relations.toArray(), null, 4)}`
);
const rel = await db.relations.where({ tagID: 1 }).with({ tags: "tags" });
console.log(`relations joined with tags: ${JSON.stringify(rel, null, 4)}`);
console.log(`rel.tags should be defined: ${rel.tags}`);
});
};
f();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…