Skip to content

Commit

Permalink
Merge pull request #3287 from opral/lixdk-290-rename-plugin_key-lix_o…
Browse files Browse the repository at this point in the history
…wn_entity-to-lix_own_change_control

refactor: rename plugin_key: `lix_own_entity` to `lix_own_change_control`
  • Loading branch information
samuelstroschein authored Dec 18, 2024
2 parents 0c46eef + 9f1765a commit 81f4a62
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 22 deletions.
7 changes: 7 additions & 0 deletions .changeset/itchy-parents-live.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion packages/lix-sdk/src/change/apply-changes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions packages/lix-sdk/src/change/apply-changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
};

Expand Down Expand Up @@ -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",
};

Expand Down Expand Up @@ -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"
);
});

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: (
Expand All @@ -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
Expand All @@ -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;
`;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/lix-sdk/src/version/switch-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
) {
Expand Down

0 comments on commit 81f4a62

Please sign in to comment.