Skip to content

Commit

Permalink
adds unique and default human readable version names
Browse files Browse the repository at this point in the history
- removes nullability of `version.name` which eases conditional logic.
- apps don't need custom default version naming logic anymore
  • Loading branch information
samuelstroschein committed Dec 21, 2024
1 parent 7e2dbf6 commit 4d9d980
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .changeset/nine-seahorses-love.md
Original file line number Diff line number Diff line change
@@ -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
9 changes: 1 addition & 8 deletions packages/lix-sdk/src/database/apply-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
15 changes: 15 additions & 0 deletions packages/lix-sdk/src/database/init-db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
7 changes: 7 additions & 0 deletions packages/lix-sdk/src/database/init-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 }),
});
}

0 comments on commit 4d9d980

Please sign in to comment.