Skip to content

Commit

Permalink
Refactored indexes & triggers object structures (#44)
Browse files Browse the repository at this point in the history
* Refactored model indexes & triggers from arrays to objects

* Updated model unit tests
  • Loading branch information
NuroDev authored Feb 11, 2025
1 parent 14ca1fa commit f03a45b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
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

0 comments on commit f03a45b

Please sign in to comment.