Usage
Short Manual
Section titled “Short Manual”Reading and Writing data
Section titled “Reading and Writing data”Reading
// Get a single instanceroot.get("oven") // Creates a new Oven instanceroot.get("oven") // Gets a cached Oven instance
await node.get("kitchen") // { kitchen: Kitchen } also cachedawait node.items.kitchen // same as above
// Plain deletionnode.delete("kitchen")
// Get multiple instances at onceawait root.getContainerSet(["oven", "userManual"]) // { userManual: '...', oven: Oven }await root.getContainerSet((c) => [c.userManual, c.oven]) // same as above
// Subscribe to container changesnode.subscribeToContainer("oven", (oven) => {})node.subscribeToContainerSet(["oven", "kitchen"], ({ oven, kitchen }) => {})// prettier-ignorenode.subscribeToContainerSet((c) => [c.kitchen], ({ oven, kitchen }) => {})node.on("containerUpdated", ({ key, newItem }) => {})node.on("containerUpserted", ({ key, newItem }) => {})node.on("containerDeleted", ({ key, newItem }) => {})
// Disposingnode .add({ dbConnection: () => connectToDb(process.env.dbUrl) }) .addDisposer({ dbConnection: (db) => db.disconnect() }) // waits for promiseawait node.dispose("dbConnection")await node.disposeAll()
Writing
let node1 = createContainer() .add({ userManual: "Please preheat before use", oven: () => new Oven(), }) .upsert((containers, node) => ({ userManual: "Works better when hot", preheatedOven: async () => { await containers.oven.preheat() return containers.oven }, }))
// `add` is typesafe and a runtime safe method. Hence we've used `upsert`try { node1.add({ // @ts-expect-error userManual: "You shall not pass", // Type Error: (property) userManual: "You are overwriting this token. It is not safe. Use an unsafe `upsert` method" })} catch (err) { err.message // Error Tokens already exist: ['userManual']}