diff --git a/.changeset/nine-seahorses-love.md b/.changeset/nine-seahorses-love.md new file mode 100644 index 0000000000..6ef4a089b9 --- /dev/null +++ b/.changeset/nine-seahorses-love.md @@ -0,0 +1,8 @@ +--- +"@lix-js/sdk": minor +--- + +adds unique and default human readable version names + +- removes nullability of `version.name` which eases conditional logic. +- apps don't need custom default version naming logic anymore \ No newline at end of file diff --git a/packages/lix-sdk/src/database/apply-schema.ts b/packages/lix-sdk/src/database/apply-schema.ts index ee96f796c8..39e23e4021 100644 --- a/packages/lix-sdk/src/database/apply-schema.ts +++ b/packages/lix-sdk/src/database/apply-schema.ts @@ -201,14 +201,7 @@ export function applySchema(args: { sqlite: SqliteDatabase }): SqliteDatabase { CREATE TABLE IF NOT EXISTS version ( id TEXT PRIMARY KEY DEFAULT (uuid_v7()), - -- name is optional. - -- - -- "anonymous" versiones can ease workflows. - -- For example, a user can create a version - -- without a name to experiment with - -- changes with no mental overhead of - -- naming the version. - name TEXT + name TEXT NOT NULL UNIQUE DEFAULT (human_id()) ) STRICT; CREATE TABLE IF NOT EXISTS version_change ( diff --git a/packages/lix-sdk/src/database/init-db.test.ts b/packages/lix-sdk/src/database/init-db.test.ts index 990962c60b..4295146500 100644 --- a/packages/lix-sdk/src/database/init-db.test.ts +++ b/packages/lix-sdk/src/database/init-db.test.ts @@ -524,3 +524,18 @@ test("version_change must have a unique entity_id, file_id, version_id, and sche .values({ ...versionChange, version_id: version1.id }) .execute(); }); + + +test("versions have a unique and default human readable name", async () => { + const lix = await openLixInMemory({}); + + const version0 = await createVersion({ lix }); + const version1 = await createVersion({ lix }); + + expect(version0.name).not.toBe(version1.name); + + await expect( + createVersion({ lix, name: version0.name }), + "version.name is unique" + ).rejects.toThrow(); +}); \ No newline at end of file diff --git a/packages/lix-sdk/src/database/init-db.ts b/packages/lix-sdk/src/database/init-db.ts index 8ba622ffd6..23994e7df1 100644 --- a/packages/lix-sdk/src/database/init-db.ts +++ b/packages/lix-sdk/src/database/init-db.ts @@ -9,6 +9,7 @@ import { ParseJsonBPluginV1 } from "./kysely-plugin/parse-jsonb-plugin-v1.js"; import { SerializeJsonBPlugin } from "./kysely-plugin/serialize-jsonb-plugin.js"; import { createSession } from "./mutation-log/lix-session.js"; import { applyOwnChangeControlTriggers } from "../own-change-control/database-triggers.js"; +import { humanId } from "human-id"; export function initDb(args: { sqlite: SqliteDatabase; @@ -91,4 +92,10 @@ function initFunctions(args: { sqlite: SqliteDatabase }) { arity: 0, xFunc: () => lixSession.sessionClockTick(), }); + + args.sqlite.createFunction({ + name: "human_id", + arity: 0, + xFunc: () => humanId({ separator: "-", capitalize: false }), + }); }