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

Refactored indexes & triggers object structures #44

Merged
merged 2 commits into from
Feb 11, 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
12 changes: 6 additions & 6 deletions examples/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ export const Account = model({
birthday: date(),
},

indexes: [
{
indexes: {
name: {
fields: [{ slug: 'name', order: 'ASC', collation: 'BINARY' }],
unique: true,
},
],
},

triggers: [
{
triggers: {
afterInsert: {
when: 'AFTER',
action: 'INSERT',
fields: [{ slug: 'name' }],
// @ts-expect-error: The queries need to be adjusted in the TS client.
effects: () => [add.member.with({ name: 'admin', email: '[email protected]' })],
},
],
},
});

export const Profile = model({
Expand Down
4 changes: 2 additions & 2 deletions src/schema/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ export interface Model<Fields = RecordWithoutForbiddenKeys<Primitives>>
/**
* Database indexes to optimize query performance.
*/
indexes?: Array<ModelIndex<Array<ModelField & { slug: keyof Fields }>>>;
indexes?: Record<string, ModelIndex<Array<ModelField & { slug: keyof Fields }>>>;

/**
* Queries that run automatically in response to other queries.
*/
triggers?: Array<ModelTrigger<Array<ModelField & { slug: keyof Fields }>>>;
triggers?: Record<string, ModelTrigger<Array<ModelField & { slug: keyof Fields }>>>;

/**
* Predefined query instructions that can be reused across multiple different queries.
Expand Down
36 changes: 18 additions & 18 deletions tests/schema/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,11 @@ describe('models', () => {
fields: {
name: string({ required: true }),
},
indexes: [
{
indexes: {
name: {
fields: [{ slug: 'name', order: 'ASC', collation: 'BINARY' }],
},
],
},
});
expect(Account).toBeTypeOf('object');
// @ts-expect-error: The Account object has 'fields'.
Expand All @@ -476,11 +476,11 @@ describe('models', () => {
required: true,
},
],
indexes: [
{
indexes: {
name: {
fields: [{ slug: 'name', order: 'ASC', collation: 'BINARY' }],
},
],
},
});
});

Expand All @@ -493,14 +493,14 @@ describe('models', () => {
fields: {
name: string({ required: true }),
},
indexes: [
{
indexes: {
name: {
fields: [
// @ts-expect-error This is intended.
{ slug: 'thisFieldDoesNotExist', order: 'ASC', collation: 'BINARY' },
],
},
],
},
});
} catch (err) {
const error = err as Error;
Expand All @@ -520,11 +520,11 @@ describe('models', () => {
fields: {
name: string({ required: true }),
},
indexes: [
{
indexes: {
name: {
fields: [],
},
],
},
});
} catch (err) {
const error = err as Error;
Expand All @@ -543,15 +543,15 @@ describe('models', () => {
fields: {
name: string({ required: true }),
},
triggers: [
{
triggers: {
afterInsert: {
action: 'INSERT',
when: 'AFTER',
fields: [{ slug: 'name' }],
// @ts-expect-error: The queries need to be adjusted in the TS client.
effects: [add.account.with({ name: 'Lorena' })],
},
],
},
}));

expect(Account).toBeTypeOf('object');
Expand All @@ -570,8 +570,8 @@ describe('models', () => {
required: true,
},
],
triggers: [
{
triggers: {
afterInsert: {
action: 'INSERT',
when: 'AFTER',
effects: [
Expand All @@ -589,7 +589,7 @@ describe('models', () => {
],
fields: [{ slug: 'name' }],
},
],
},
});
});

Expand Down
Loading