import type { EntityStateByVersionColumns } from "./entity-state-by-version.js"; import type { EntityStateColumns } from "./entity-state.js"; import type { StateEntityHistoryColumns } from "./entity-state-history.js"; import type { Generated as KyselyGenerated } from "kysely"; import type { LixGenerated, LixInsertable, LixUpdateable, LixSelectable } from "../../schema-definition/definition.js"; /** * Check if a type has the LixGenerated brand */ type IsLixGenerated = T extends { readonly __lixGenerated?: true; } ? true : false; /** * Extract the base type from LixGenerated * Since LixGenerated = T & { brand }, we need to extract T */ type ExtractFromGenerated = T extends LixGenerated ? U : T; /** * Extract the select type from LixGenerated or return the type as-is */ type SelectType = ExtractFromGenerated; /** * Convert our LixGenerated types to Kysely's Generated types. * This adapter is used at the database boundary. */ export type ToKysely = { [K in keyof T]: IsLixGenerated extends true ? KyselyGenerated> : T[K]; }; /** * View type for entities in the active version only. * * This type combines your entity properties with Lix operational columns * (file_id, timestamps, etc.) while preserving LixGenerated markers for * database schema compatibility. * * Use this type when defining database views that work with the current * active version only. * * @example * ```typescript * // Define a view type for key-value entities * type KeyValueView = EntityStateView; * * // The resulting type includes both entity properties and operational columns * // { key: string, value: any, lixcol_file_id: LixGenerated, ... } * ``` */ export type EntityStateView = T & EntityStateColumns; /** * View type for entities across all versions. * * This type combines your entity properties with Lix operational columns * including the version_id, allowing you to query and manipulate entities * in specific versions. * * Use this type when defining database views that need to work across * multiple versions. * * @example * ```typescript * // Define a view type for key-value entities across versions * type KeyValueByVersionView = EntityStateByVersionView; * * // Query entities in a specific version * await lix.db * .selectFrom("key_value_by_version") * .where("lixcol_version_id", "=", "v2") * .selectAll() * .execute(); * ``` */ export type EntityStateByVersionView = T & EntityStateByVersionColumns; /** * View type for entity history (read-only). * * This type combines your entity properties with historical tracking columns, * allowing you to see how entities evolved over time through different change sets. * * History views are read-only and include change tracking information like * change_id, change_set_id, and depth for blame functionality. * * @example * ```typescript * // Define a history view type for key-value entities * type KeyValueHistoryView = EntityStateHistoryView; * * // Query entity state at a specific commit * await lix.db * .selectFrom("key_value_history") * .where("lixcol_commit_id", "=", commitId) * .where("lixcol_depth", "=", 0) * .selectAll() * .execute(); * ``` */ export type EntityStateHistoryView = T & StateEntityHistoryColumns; /** * Type for querying entities from the active version. * * This type unwraps all LixGenerated markers, giving you the actual engine types * for entity properties and operational columns. Use this when working with * query results from the database or passing data to UI components. * * All properties are required and have their actual types (no LixGenerated wrappers). * * @example * ```typescript * // Use State type for UI components that display entity data * interface SettingsCardProps { * setting: State; * } * * function SettingsCard({ setting }: SettingsCardProps) { * return ( *
*

{setting.key}

*

{setting.value}

* *
* ); * } * * // Query and pass to UI * const settings = await lix.db * .selectFrom("key_value") * .selectAll() * .execute(); * * settings.map(setting => ); * ``` */ export type State = LixSelectable>; /** * Type for querying entities across all versions. * * This type unwraps all LixGenerated markers and includes the version_id column, * allowing you to work with entities from any version in the database. * * All properties are required and have their actual types (no LixGenerated wrappers). * * @example * ```typescript * // Use StateByVersion for version comparison UI * interface VersionDiffProps { * oldValue: StateByVersion; * newValue: StateByVersion; * } * * function VersionDiff({ oldValue, newValue }: VersionDiffProps) { * return ( *
*

{oldValue.key}

*
Version {oldValue.lixcol_version_id}: {oldValue.value}
*
Version {newValue.lixcol_version_id}: {newValue.value}
*
* ); * } * ``` */ export type StateByVersion = LixSelectable>; /** * Type for querying entity history. * * This type unwraps all LixGenerated markers and includes historical tracking * columns like change_id, change_set_id, and depth. Use this for blame * functionality and understanding how entities evolved. * * History queries are read-only. * * @example * ```typescript * // Use StateHistory for blame UI * interface BlameViewProps { * history: StateHistory[]; * } * * function BlameView({ history }: BlameViewProps) { * return ( *
    * {history.map(state => ( *
  • * Depth {state.lixcol_depth}: {state.value} *
    * Change: {state.lixcol_change_id} *
  • * ))} *
* ); * } * ``` */ export type StateHistory = LixSelectable>; /** * Type for creating new entities in the active version. * * This type makes all LixGenerated columns optional (like id, timestamps), * while keeping other required fields mandatory. The database will * automatically populate generated fields if not provided. * * @example * ```typescript * // Use NewState for form data types * interface SettingsFormData { * setting: NewState; * } * * async function createSetting(formData: SettingsFormData) { * // Only key and value are required * const newSetting: NewState = { * key: formData.setting.key, * value: formData.setting.value * // lixcol_created_at, lixcol_file_id etc. are auto-generated * }; * * await lix.db * .insertInto("key_value") * .values(newSetting) * .execute(); * } * ``` */ export type NewState = LixInsertable>; /** * Type for updating entities in the active version. * * This type makes all columns optional, allowing partial updates. * Only include the fields you want to change - the database will * preserve existing values for omitted fields. * * @example * ```typescript * // Use StateUpdate for update form data * interface UpdateSettingData { * updates: StateUpdate; * } * * async function updateSetting(key: string, updates: UpdateSettingData) { * // Only update the fields that changed * const patch: StateUpdate = { * value: updates.updates.value * // key, timestamps, etc. remain unchanged * }; * * await lix.db * .updateTable("key_value") * .set(patch) * .where("key", "=", key) * .execute(); * } * ``` */ export type StateUpdate = LixUpdateable>; /** * Type for creating new entities with version control. * * This type includes lixcol_version_id for creating entities in specific * versions. Like NewState, it makes LixGenerated columns optional. * * @example * ```typescript * // Use NewStateByVersion for version-specific creation * async function createFeatureFlag(versionId: string, flag: { * key: string; * value: boolean; * }) { * const newFlag: NewStateByVersion = { * key: flag.key, * value: flag.value, * lixcol_version_id: versionId // Create in specific version * }; * * await lix.db * .insertInto("key_value_by_version") * .values(newFlag) * .execute(); * } * ``` */ export type NewStateByVersion = LixInsertable>; /** * Type for updating entities in specific versions. * * This type makes all columns optional for partial updates and includes * version control. You can update entities in any version or change * an entity's version. * * @example * ```typescript * // Use StateByVersionUpdate for version-specific updates * async function toggleFeatureFlag( * key: string, * versionId: string, * enabled: boolean * ) { * const updates: StateByVersionUpdate = { * value: enabled * }; * * await lix.db * .updateTable("key_value_by_version") * .set(updates) * .where("key", "=", key) * .where("lixcol_version_id", "=", versionId) * .execute(); * } * ``` */ export type StateByVersionUpdate = LixUpdateable>; export {}; //# sourceMappingURL=types.d.ts.map