Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent actions for many-cardinality link fields #132

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,19 @@ export const getSystemModels = (models: Array<Model>, model: Model): Array<Model
slug: 'source',
type: 'link',
target: model.slug,
actions: {
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
},
},
{
slug: 'target',
type: 'link',
target: relatedModel.slug,
actions: {
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
},
},
],
});
Expand Down
10 changes: 9 additions & 1 deletion src/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export type ModelField = ModelFieldBasics &
/** The target model of the relationship that is being established. */
target: string;
/** Whether the field should be related to one record, or many records. */
kind?: 'one' | 'many';
kind?: 'one';
/**
* If the target record is updated or deleted, the defined actions maybe executed.
*/
Expand All @@ -101,6 +101,14 @@ export type ModelField = ModelFieldBasics &
onUpdate?: ModelFieldLinkAction;
};
}
| {
/** The kind of value that should be stored inside the field. */
type: 'link';
/** The target model of the relationship that is being established. */
target: string;
/** Whether the field should be related to one record, or many records. */
kind: 'many';
}
);

/** An extended version of `ModelField`, for internal use within the compiler. */
Expand Down
4 changes: 2 additions & 2 deletions tests/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ test('create new model that has system models associated with it', () => {
const transaction = new Transaction(queries, { models });

expect(transaction.statements[1]).toEqual({
statement: `CREATE TABLE "ronin_link_account_followers" ("id" TEXT PRIMARY KEY DEFAULT ('ron_' || lower(substr(hex(randomblob(12)), 1, 16))), "ronin.locked" BOOLEAN, "ronin.createdAt" DATETIME DEFAULT (strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z'), "ronin.createdBy" TEXT, "ronin.updatedAt" DATETIME DEFAULT (strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z'), "ronin.updatedBy" TEXT, "source" TEXT REFERENCES accounts("id"), "target" TEXT REFERENCES accounts("id"))`,
statement: `CREATE TABLE "ronin_link_account_followers" ("id" TEXT PRIMARY KEY DEFAULT ('ron_' || lower(substr(hex(randomblob(12)), 1, 16))), "ronin.locked" BOOLEAN, "ronin.createdAt" DATETIME DEFAULT (strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z'), "ronin.createdBy" TEXT, "ronin.updatedAt" DATETIME DEFAULT (strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z'), "ronin.updatedBy" TEXT, "source" TEXT REFERENCES accounts("id") ON DELETE CASCADE ON UPDATE CASCADE, "target" TEXT REFERENCES accounts("id") ON DELETE CASCADE ON UPDATE CASCADE)`,
params: [],
});
});
Expand Down Expand Up @@ -750,7 +750,7 @@ test('create new field with many-cardinality relationship', () => {

expect(transaction.statements).toEqual([
{
statement: `CREATE TABLE "ronin_link_account_followers" ("id" TEXT PRIMARY KEY DEFAULT ('ron_' || lower(substr(hex(randomblob(12)), 1, 16))), "ronin.locked" BOOLEAN, "ronin.createdAt" DATETIME DEFAULT (strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z'), "ronin.createdBy" TEXT, "ronin.updatedAt" DATETIME DEFAULT (strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z'), "ronin.updatedBy" TEXT, "source" TEXT REFERENCES accounts("id"), "target" TEXT REFERENCES accounts("id"))`,
statement: `CREATE TABLE "ronin_link_account_followers" ("id" TEXT PRIMARY KEY DEFAULT ('ron_' || lower(substr(hex(randomblob(12)), 1, 16))), "ronin.locked" BOOLEAN, "ronin.createdAt" DATETIME DEFAULT (strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z'), "ronin.createdBy" TEXT, "ronin.updatedAt" DATETIME DEFAULT (strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z'), "ronin.updatedBy" TEXT, "source" TEXT REFERENCES accounts("id") ON DELETE CASCADE ON UPDATE CASCADE, "target" TEXT REFERENCES accounts("id") ON DELETE CASCADE ON UPDATE CASCADE)`,
params: [],
},
{
Expand Down
Loading