Usage
Short Manual
Section titled “Short Manual”Reading and Writing data
Section titled “Reading and Writing data”Reading
// Get a single instance (async)await root.get("oven") // Creates a new Oven instanceawait root.get("oven") // Gets a cached Oven instance
// Get a single instance (sync) - ✨ New in v0.8.0root.getSync("oven") // Returns cached instance or undefined
await node.get("kitchen") // Kitchen instanceawait node.items.kitchen // same as above
// Plain deletionnode.delete("kitchen")
// Get multiple instances at once (async)await root.getItems(["oven", "userManual"]) // { userManual: '...', oven: Oven }await root.getItems((c) => [c.userManual, c.oven]) // same as above
// Get multiple instances at once (sync) - ✨ New in v0.8.0root.getItemsSync(["oven", "userManual"]) // Returns items if cached, otherwise Promise
// Subscribe to item changes (new API)node.subscribeToItem("oven", (oven) => {})node.subscribeToItems(["oven", "kitchen"], ({ oven, kitchen }) => {})// prettier-ignorenode.subscribeToItems((c) => [c.kitchen], ({ oven, kitchen }) => {})
// Item-focused events (recommended) - ✨ New in v0.8.0node.on("itemUpdated", ({ key, newItem }) => {})node.on("itemUpserted", ({ key, newItem }) => {})node.on("itemDeleted", ({ key, newItem }) => {})node.on("itemDisposed", ({ key }) => {})
// Legacy container events (still supported but deprecated)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 cont1 = 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 { cont1.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']}