Skip to content

Declarative: Catalogs, Enforced at Startup

Give the module a schema, and it is enforced before the application starts, indexes before constraints:

DrivineModule.withOptions({
connectionProviders: [DatabaseRegistry.buildOrResolveFromEnv()],
schema: {
catalogs: [
// vector dimensions come from your embedding model, at run time
SchemaCatalog.fromFragments(() => embeddings.dimensions, PropositionNode, MessageNode),
SchemaCatalog.of(rangeIndex('Session', 'lastActivityAt'), uniquenessConstraint('Session', 'id'))
]
}
});

Without Nest, enforce the schema yourself before the application serves:

await new SchemaManager(graphObjectManagers, { catalogs: [/* … */] }).enforce();

A catalog’s items give each declaration once, its indexes before its constraints, as drivine4j’s catalogs give them, so that each library plans the same schema from the same declarations. Catalogs merge, identical declarations count once, and conflicting ones for the same kind, label and properties fail startup. A catalog applies to every database that supports schema, unless you narrow it: .forDefaultDatabase(), .forDatabase('users') or .forDatabases('a', 'b'). A named target is strict: an unknown database, or one without schema support, fails startup.

A fragment’s @NodeId gets a range index. Each save, and each load by id, finds its node by the @NodeId property under the fragment’s primary label, and with no index over it a store reads every node of the label. So fromFragments gives each fragment with a @NodeId a range index over that property, after the indexes it declares, unless an item declared serves it: a range index over that property alone, or a uniqueness constraint over it alone, neither ignoring case, from any fragment of the catalog, or any other catalog (or item given to ensure) the SchemaManager applies on that database. It is an item like any other, made by enforce and listed in the owner’s marker. Once made, it stays when a @Unique is later declared on the id (reported as an orphan of its owner), and on Neo4j it is in the way of that constraint (BLOCKED_BY_INDEX): enforce once with replaceBlockingIndex: true, or drop the index first. drivine4j and drivine-python give the same index, so a store indexed by one is up to date for the others. To go without it:

  • one fragment: @NodeFragment({ labels: ['Person'], nodeIdIndex: false }) (of that class alone: a subclass is given the index unless it says so too);
  • one catalog: SchemaCatalog.fromFragments(...).withoutNodeIdIndexes();
  • every catalog a manager is given: schema: { catalogs, indexNodeIds: false }.

A fragment never read into a SchemaCatalog gets none. A fragment whose @NodeId no index serves, under any of its labels, is warned of once per database, at WARN (set DRIVINE_LOG_LEVEL=WARN to see it), naming the label, the property and the fix, and the database where there is more than one: after an enforce (or recreateAll), from what it lists of the store, or, where no SchemaManager with a catalog is built, 30 seconds after the fragment’s first use, unless a range index or uniqueness constraint over the @NodeId alone, not ignoring case, under one of its labels, was ensured by hand through indexes or constraints by then. A fragment some catalog was read from (with fromFragments, its default index on or off), or under whose labels some catalog has an item, is checked only on the databases those catalogs are for, and any other on every database. The check never reads the store in a save, and never fails one.

A vector index in a catalog over a fragment field that has no @VectorIndex fails too, naming the class, the field and the spec. Without the decorator FalkorDB stores the field as a plain list, and loadNearest finds no index for the fragment. Declare both when a catalog spec tunes the index. The check runs when SchemaManager applies the catalog, or is given the spec by ensure, and when the fragment class is first used. A field is matched by the property it’s stored under, on any of the fragment’s labels. Only fields the class decorates or initializes can be seen, so embedding?: number[] with neither isn’t checked, unless the compiler emits class fields (useDefineForClassFields, the default from target ES2022). A spec for a label no fragment maps, and one given to indexes.ensure directly, aren’t checked either.

Option Meaning
mode FAIL_FAST (the default) throws on drift, a violation, an index in the way of a constraint, or an index whose build failed or did not end within indexBuildTimeout; WARN logs it and carries on.
indexBuildTimeout How long, in milliseconds, an index Neo4j or FalkorDB builds in the background is waited on to be built. Defaults to 60,000.
recreateOnDrift Destructive: replace an item whose shape drifted.
recreateOnStartup Destructive: drop and make every declared item on every startup.
replaceBlockingIndex Destructive: drop a plain index in the way of a uniqueness constraint (Neo4j) and make the constraint in its place.
violationSampleSize How many keys held more than once a violation reports, and how many of each one’s nodes; 0 reports none, though the data is still read to confirm a violation, which then has an empty sample. Defaults to 10.
enabled false skips the startup run; SchemaManager can still be injected.
indexNodeIds false applies the catalogs without the range index each fragment’s @NodeId is given by default, leaving only what is declared. Defaults to true.

SchemaManager is injectable, to change the schema as the application runs: enforce() is safe to call again, and recreateAll() drops and remakes every declared item.

The manager reads its catalogs once, when it is built: a catalog made later isn’t seen, and a later enforce() applies the same list. Schema known later is given to ensure (below). With nothing to apply, in its catalogs or given to ensure, it says so at info level: Schema management found no SchemaCatalog when it was given none, or found 2 SchemaCatalog(s), all empty: 'dice', unnamed when every one came out empty. So a missing index can be traced to a catalog that never arrived.