Skip to content

Schema Known Only at Runtime

Some schema can’t be in a catalog, because it isn’t known when the catalogs are read: a vector index whose dimensions arrive with an embedding model configured after startup, or an index for each label a tenant declares. Give it to ensure, under the owner it belongs to:

await schemaManager.ensure('dice', vectorIndex('Proposition', 'embedding', model.dimensions));

With Nest, inject SchemaManager. The module provides one only when given schema, so an application whose schema is all known at runtime gives it schema: { catalogs: [] }.

The items join what that owner’s catalogs declared, for the life of the manager, and are applied as enforce applies the owner’s:

  • Under the same options. mode, indexBuildTimeout, recreateOnDrift, replaceBlockingIndex and violationSampleSize apply. Only the items given are applied, and the results come back by database. ensure leaves a change of the owner’s version to enforce, and recreateOnStartup doesn’t apply to it.
  • In the owner’s inventory. The owner’s _DrivineSchema marker records them as ensured at runtime. A later enforce() applies them with the rest, and recreateAll() recreates them. An enforce by a manager built later (the next process, before the item is ensured again) keeps them in the inventory and doesn’t report them as orphans.
  • Recreated with the owner. When that later manager recreates the owner’s items (its version changed, recreateOnStartup, or recreateAll), it has no spec for a runtime item, so it marks it, and the next ensure of the item recreates it.
  • Checked against other owners. A declaration that conflicts with another owner’s, or with the owner’s own, fails naming both, and nothing is kept or applied on any database. One identical to another owner’s is co-owned.

Ensuring an item again with the same kind, label and properties replaces it: of another shape, it’s drift. To target databases, give a catalog: ensure(SchemaCatalog.of(spec).named('dice').forDatabase('users')). A runtime catalog can’t carry a version, since the owner’s version is its startup catalogs’. The marker is drivine4j’s: either library keeps what the other ensured at runtime.

remove takes items back: schemaManager.remove('dice', spec), or a catalog to target databases. Each item is found by its kind, label and properties, whatever its name. It is forgotten among what ensure gave the owner, so a later enforce doesn’t make it again, and taken out of the owner’s marker. It is dropped, unless a startup catalog (of any owner) or another owner’s runtime items declare it, or another owner’s _DrivineSchema marker in the database records it, whichever manager or process wrote it: then it is left in place, with a warning naming who declares it. An item that isn’t there is forgotten all the same.