From 9f1765ab0d8327f18d90db6d84842c069c981da6 Mon Sep 17 00:00:00 2001 From: Samuel Stroschein <35429197+samuelstroschein@users.noreply.github.com> Date: Wed, 18 Dec 2024 17:02:16 -0500 Subject: [PATCH] refactor: rename plugin_key: `lix_own_entity` to `lix_own_change_control` Closes https://github.com/opral/lix-sdk/issues/197 --- .changeset/itchy-parents-live.md | 7 +++++++ .../lix-sdk/src/change/apply-changes.test.ts | 2 +- packages/lix-sdk/src/change/apply-changes.ts | 7 +++++-- .../apply-own-entity-change.test.ts | 18 +++++++++--------- .../apply-own-entity-change.ts | 4 ++-- .../database-triggers.test.ts | 2 +- .../database-triggers.ts | 12 ++++++------ packages/lix-sdk/src/version/switch-version.ts | 2 +- 8 files changed, 32 insertions(+), 22 deletions(-) create mode 100644 .changeset/itchy-parents-live.md diff --git a/.changeset/itchy-parents-live.md b/.changeset/itchy-parents-live.md new file mode 100644 index 0000000000..27818b0284 --- /dev/null +++ b/.changeset/itchy-parents-live.md @@ -0,0 +1,7 @@ +--- +"@lix-js/sdk": patch +--- + +refactor: rename plugin_key: `lix_own_entity` to `lix_own_change_control` + +Closes https://github.com/opral/lix-sdk/issues/197 diff --git a/packages/lix-sdk/src/change/apply-changes.test.ts b/packages/lix-sdk/src/change/apply-changes.test.ts index 8c9fa2d594..2baa6e3b9a 100644 --- a/packages/lix-sdk/src/change/apply-changes.test.ts +++ b/packages/lix-sdk/src/change/apply-changes.test.ts @@ -156,7 +156,7 @@ test("it applies own entity changes", async () => { id: "change0", entity_id: "mock-key", file_id: "null", - plugin_key: "lix_own_entity", + plugin_key: "lix_own_change_control", schema_key: "lix_key_value_table", snapshot_id: snapshot.id, created_at: "2021-01-01T00:00:00Z", diff --git a/packages/lix-sdk/src/change/apply-changes.ts b/packages/lix-sdk/src/change/apply-changes.ts index 35fb1ed9e8..a610e7ee67 100644 --- a/packages/lix-sdk/src/change/apply-changes.ts +++ b/packages/lix-sdk/src/change/apply-changes.ts @@ -52,8 +52,11 @@ export async function applyChanges(args: { continue; } // Skip own entity changes which have a file id 'null' and - // plugin key 'lix_own_entity' as they are not associated with a file - if (fileId === "null" && changes[0]?.plugin_key === "lix_own_entity") { + // plugin key 'lix_own_change_control' as they are not associated with a file + if ( + fileId === "null" && + changes[0]?.plugin_key === "lix_own_change_control" + ) { await applyOwnEntityChanges({ lix: { ...args.lix, db: trx }, changes }); continue; } diff --git a/packages/lix-sdk/src/own-entity-change-control/apply-own-entity-change.test.ts b/packages/lix-sdk/src/own-entity-change-control/apply-own-entity-change.test.ts index 22f8295b1e..7961284036 100644 --- a/packages/lix-sdk/src/own-entity-change-control/apply-own-entity-change.test.ts +++ b/packages/lix-sdk/src/own-entity-change-control/apply-own-entity-change.test.ts @@ -21,7 +21,7 @@ test("it should apply insert changes correctly", async () => { id: "change1", entity_id: "key1", schema_key: "lix_key_value_table", - plugin_key: "lix_own_entity", + plugin_key: "lix_own_change_control", file_id: "null", snapshot_id: snapshot.id, created_at: "2021-01-01T00:00:00.000Z", @@ -62,7 +62,7 @@ test("it should apply update changes correctly", async () => { entity_id: "key1", file_id: "null", created_at: "2021-01-01T00:00:00.000Z", - plugin_key: "lix_own_entity", + plugin_key: "lix_own_change_control", snapshot_id: snapshot.id, }; @@ -98,7 +98,7 @@ test("it should apply delete changes correctly", async () => { file_id: "null", created_at: "2021-01-01T00:00:00.000Z", entity_id: "key1", - plugin_key: "lix_own_entity", + plugin_key: "lix_own_change_control", snapshot_id: "no-content", }; @@ -141,7 +141,7 @@ test("it should throw an error for invalid plugin key", async () => { await expect( applyOwnEntityChanges({ lix, changes: [change] }) ).rejects.toThrow( - "Expected 'lix_own_entity' as plugin key but received invalid-plugin" + "Expected 'lix_own_change_control' as plugin key but received invalid-plugin" ); }); @@ -189,7 +189,7 @@ test("file.data is not changed by applyOwnEntityChanges", async () => { id: "change1", entity_id: file.id, schema_key: "lix_file_table", - plugin_key: "lix_own_entity", + plugin_key: "lix_own_change_control", file_id: "null", snapshot_id: snapshot.id, created_at: "2021-01-01T00:00:00.000Z", @@ -230,7 +230,7 @@ test("foreign key constraints are deferred to make the order of applying changes id: "change2", entity_id: "change-set-1,change0", schema_key: "lix_change_set_element_table", - plugin_key: "lix_own_entity", + plugin_key: "lix_own_change_control", file_id: "null", snapshot_id: snapshots[1].id, created_at: "2021-01-01T00:00:00.000Z", @@ -239,7 +239,7 @@ test("foreign key constraints are deferred to make the order of applying changes id: "change1", entity_id: "change-set-1", schema_key: "lix_change_set_table", - plugin_key: "lix_own_entity", + plugin_key: "lix_own_change_control", file_id: "null", snapshot_id: snapshots[0].id, created_at: "2021-01-01T00:00:00.000Z", @@ -291,7 +291,7 @@ test("foreign key constraints are obeyed", async () => { // the change set for this change does not exist entity_id: "change-set-1,change0", schema_key: "lix_change_set_element_table", - plugin_key: "lix_own_entity", + plugin_key: "lix_own_change_control", file_id: "null", snapshot_id: snapshots[0].id, created_at: "2021-01-01T00:00:00.000Z", @@ -351,7 +351,7 @@ test("applying own entity changes doesn't lead to the creation of new changes", id: "change0", entity_id: "mock-key", file_id: "null", - plugin_key: "lix_own_entity", + plugin_key: "lix_own_change_control", schema_key: "lix_key_value_table", snapshot_id: snapshot.id, created_at: "2021-01-01T00:00:00Z", diff --git a/packages/lix-sdk/src/own-entity-change-control/apply-own-entity-change.ts b/packages/lix-sdk/src/own-entity-change-control/apply-own-entity-change.ts index 753b4e1786..a2338d40a1 100644 --- a/packages/lix-sdk/src/own-entity-change-control/apply-own-entity-change.ts +++ b/packages/lix-sdk/src/own-entity-change-control/apply-own-entity-change.ts @@ -31,9 +31,9 @@ export async function applyOwnEntityChanges(args: { await Promise.all( args.changes.map(async (change) => { - if (change.plugin_key !== "lix_own_entity") { + if (change.plugin_key !== "lix_own_change_control") { throw new Error( - "Expected 'lix_own_entity' as plugin key but received " + + "Expected 'lix_own_change_control' as plugin key but received " + change.plugin_key ); } diff --git a/packages/lix-sdk/src/own-entity-change-control/database-triggers.test.ts b/packages/lix-sdk/src/own-entity-change-control/database-triggers.test.ts index b04a108a92..1a2e5b031c 100644 --- a/packages/lix-sdk/src/own-entity-change-control/database-triggers.test.ts +++ b/packages/lix-sdk/src/own-entity-change-control/database-triggers.test.ts @@ -36,7 +36,7 @@ test("it works for inserts, updates and deletions", async () => { for (const change of changes) { expect(change.entity_id).toBe("key1"); expect(change.file_id).toBe("null"); - expect(change.plugin_key).toBe("lix_own_entity"); + expect(change.plugin_key).toBe("lix_own_change_control"); expect(change.schema_key).toBe("lix_key_value_table"); } diff --git a/packages/lix-sdk/src/own-entity-change-control/database-triggers.ts b/packages/lix-sdk/src/own-entity-change-control/database-triggers.ts index 012be21450..0f12b98faa 100644 --- a/packages/lix-sdk/src/own-entity-change-control/database-triggers.ts +++ b/packages/lix-sdk/src/own-entity-change-control/database-triggers.ts @@ -24,7 +24,7 @@ export function applyOwnEntityChangeControlTriggers( } sqlite.createFunction({ - name: "handle_lix_own_entity_change", + name: "handle_lix_own_change_control", arity: -1, // @ts-expect-error - dynamic function xFunc: ( @@ -51,7 +51,7 @@ export function applyOwnEntityChangeControlTriggers( CREATE TEMP TRIGGER IF NOT EXISTS ${table}_change_control_insert AFTER INSERT ON ${table} BEGIN - SELECT handle_lix_own_entity_change('${table}', 'insert', ${tableInfo.map((c) => "NEW." + c.name).join(", ")}); + SELECT handle_lix_own_change_control('${table}', 'insert', ${tableInfo.map((c) => "NEW." + c.name).join(", ")}); END; CREATE TEMP TRIGGER IF NOT EXISTS ${table}_change_control_update @@ -69,13 +69,13 @@ export function applyOwnEntityChangeControlTriggers( : "" } BEGIN - SELECT handle_lix_own_entity_change('${table}', 'update', ${tableInfo.map((c) => "NEW." + c.name).join(", ")}); + SELECT handle_lix_own_change_control('${table}', 'update', ${tableInfo.map((c) => "NEW." + c.name).join(", ")}); END; CREATE TEMP TRIGGER IF NOT EXISTS ${table}_change_control_delete AFTER DELETE ON ${table} BEGIN - SELECT handle_lix_own_entity_change('${table}', 'delete', ${tableInfo.map((c) => "OLD." + c.name).join(", ")}); + SELECT handle_lix_own_change_control('${table}', 'delete', ${tableInfo.map((c) => "OLD." + c.name).join(", ")}); END; `; @@ -120,7 +120,7 @@ function handleLixOwnEntityChange( .select("plugin_key"), })[0]; - if (change?.plugin_key === "lix_own_entity") { + if (change?.plugin_key === "lix_own_change_control") { return; } @@ -178,7 +178,7 @@ function handleLixOwnEntityChange( version: currentVersion, entityId, fileId: "null", - pluginKey: "lix_own_entity", + pluginKey: "lix_own_change_control", schemaKey: `lix_${tableName}_table`, snapshotContent, }, diff --git a/packages/lix-sdk/src/version/switch-version.ts b/packages/lix-sdk/src/version/switch-version.ts index 3f77ca1c4f..9d66bdfed9 100644 --- a/packages/lix-sdk/src/version/switch-version.ts +++ b/packages/lix-sdk/src/version/switch-version.ts @@ -75,7 +75,7 @@ export async function switchVersion(args: { // need to remove the entity when switching the version else { if ( - change.plugin_key === "lix_own_entity" && + change.plugin_key === "lix_own_change_control" && (change.schema_key === "lix_account_table" || change.schema_key === "lix_version_table") ) {