Own persistent storage
Import persistence from @bedrock-core/db and let its consuming addon or library own the database lifetime.
Install
- npm
- yarn
- pnpm
npm install @bedrock-core/db
yarn add @bedrock-core/db
pnpm add @bedrock-core/db
Bind storage to a declaration
A declaration creates storage during installation and stops it during cleanup:
import type { Declaration } from '@bedrock-core/server';
import { players, schema, type Collection } from '@bedrock-core/db';
import { createEngineDb } from '@bedrock-core/db/minecraft';
import type { Player } from '@minecraft/server';
interface Balance { gold: number }
export function registerBalances(): Declaration<Collection<Balance, Player>> {
return {
install(core, lifetime) {
const db = createEngineDb(core.namespace);
lifetime.defer(() => db.stop());
const value = db.collection('balances', {
schema: schema<Balance>({ defaults: { gold: 0 } }),
accept: players(),
});
return { value };
},
};
}
Pass the declaration to core.register({ manifest, balances: registerBalances() }) or install it later with core.install. The returned accessor is the collection. Deferred cleanup also runs if collection setup throws, when registration rolls back, when the declaration is uninstalled or when the addon stops.
The namespace prefixes storage keys. Use one owner for each collection in a namespace; expose shared collections through the owning library's service accessor. Config owns its own namespaced database and scope subscriptions.
Standalone users can call createEngineDb(namespace) directly and must call db.stop() themselves. Stopping flushes queued writes, releases subscriptions and detaches engine hooks. Cross-addon reads and writes require an explicit addon API.
Next steps
- Collections — documents, schemas and database lifetime
- Declarations — installation and cleanup ownership