Function: createEntityViewsIfNotExists()

createEntityViewsIfNotExists(args: { defaultValues?: Record<string, () => any | (row: Record<string, any>) => any>; engine: Pick<LixEngine, "sqlite">; hardcodedFileId?: string; hardcodedVersionId?: string; overrideName?: string; pluginKey: string; readOnly?: boolean; schema: LixSchemaDefinition; validation?: ValidationCallbacks; }): void

Creates SQL views and CRUD triggers for an entity based on its schema definition.

This function automatically generates three views:

  • Primary view (e.g., "key_value") - uses state (active version only)
  • All view (e.g., "key_value_all") - uses state (all versions)
  • History view (e.g., "key_value_history") - uses state_history (historical states)

Each view includes:

  • A view that extracts JSON properties from the respective table
  • CRUD triggers for entity and entity_all views (history view is read-only)

Example

// Basic usage for key-value entities
createEntityViewsIfNotExists({
  lix,
  schema: LixKeyValueSchema,
  overrideName: "key_value",
  pluginKey: "lix_key_value",
  hardcodedFileId: "lix"
});
// Creates: key_value, key_value_all, and key_value_history

// With default values for account entities
createEntityViewsIfNotExists({
  lix,
  schema: LixAccountSchema,
  overrideName: "account",
  pluginKey: "lix_own_entity",
  hardcodedFileId: "lix",
  defaultValues: {
    id: (row) => nanoid()
  }
});
// Creates: account, account_all, and account_history

Parameters

ParameterTypeDescription
args{ defaultValues?: Record<string, () => any | (row: Record<string, any>) => any>; engine: Pick<LixEngine, "sqlite">; hardcodedFileId?: string; hardcodedVersionId?: string; overrideName?: string; pluginKey: string; readOnly?: boolean; schema: LixSchemaDefinition; validation?: ValidationCallbacks; }-
args.defaultValues?Record<string, () => any | (row: Record<string, any>) => any>Object mapping property names to functions that generate default values
args.enginePick<LixEngine, "sqlite">-
args.hardcodedFileId?stringOptional hardcoded file_id (if not provided, uses lixcol_file_id from mutations)
args.hardcodedVersionId?stringOptional hardcoded version_id (if not provided, uses lixcol_version_id from mutations or active version)
args.overrideName?stringOverrides the view name which defaults to schema["x-lix-key"]
args.pluginKeystringPlugin identifier for the entity
args.readOnly?booleanIf true, creates read-only views (no INSERT/UPDATE/DELETE triggers)
args.schemaLixSchemaDefinition-
args.validation?ValidationCallbacksCustom validation logic for entity operations

Returns

void

Throws

Error if schema type is not "object" or x-lix-primary-key is not defined