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

Allow for passing trigger effects as query symbols #146

Merged
merged 3 commits into from
Feb 19, 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
2 changes: 1 addition & 1 deletion src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ export const transformMetaQuery = (

// Compile the effect queries into SQL statements.
const effectStatements = trigger.effects.map((effectQuery) => {
return compileQueryInput(effectQuery, models, null, {
return compileQueryInput(effectQuery[QUERY_SYMBOLS.QUERY], models, null, {
returning: false,
parentModel: existingModel,
inlineDefaults: options.inlineDefaults,
Expand Down
3 changes: 2 additions & 1 deletion src/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
Query,
WithInstruction,
} from '@/src/types/query';
import type { QUERY_SYMBOLS } from '@/src/utils/helpers';

type ModelFieldCollation = 'BINARY' | 'NOCASE' | 'RTRIM';

Expand Down Expand Up @@ -179,7 +180,7 @@ export type ModelTrigger<
/** When the trigger should fire in the case that a matching query is executed. */
when: 'BEFORE' | 'DURING' | 'AFTER';
/** A list of queries that should be executed when the trigger fires. */
effects: Array<Query>;
effects: Array<Record<typeof QUERY_SYMBOLS.QUERY, Query>>;
/** A list of field slugs for which the trigger should fire. */
fields?: Array<ModelTriggerField<T>>;
/**
Expand Down
110 changes: 65 additions & 45 deletions tests/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ test('create new model', () => {
action: 'INSERT',
effects: [
{
add: {
signup: {
with: {
year: 2000,
[QUERY_SYMBOLS.QUERY]: {
add: {
signup: {
with: {
year: 2000,
},
},
},
},
Expand Down Expand Up @@ -1269,10 +1271,12 @@ test('create new trigger for creating records', () => {
action: 'INSERT',
effects: [
{
add: {
signup: {
with: {
year: 2000,
[QUERY_SYMBOLS.QUERY]: {
add: {
signup: {
with: {
year: 2000,
},
},
},
},
Expand Down Expand Up @@ -1325,10 +1329,12 @@ test('create new trigger for creating records with targeted fields', () => {
action: 'UPDATE',
effects: [
{
add: {
signup: {
with: {
year: 2000,
[QUERY_SYMBOLS.QUERY]: {
add: {
signup: {
with: {
year: 2000,
},
},
},
},
Expand Down Expand Up @@ -1389,19 +1395,23 @@ test('create new trigger for creating records with multiple effects', () => {
action: 'INSERT',
effects: [
{
add: {
signup: {
with: {
year: 2000,
[QUERY_SYMBOLS.QUERY]: {
add: {
signup: {
with: {
year: 2000,
},
},
},
},
},
{
add: {
candidate: {
with: {
year: 2020,
[QUERY_SYMBOLS.QUERY]: {
add: {
candidate: {
with: {
year: 2020,
},
},
},
},
Expand Down Expand Up @@ -1460,14 +1470,16 @@ test('create new per-record trigger for creating records', () => {
action: 'INSERT',
effects: [
{
add: {
member: {
with: {
account: {
[QUERY_SYMBOLS.EXPRESSION]: `${QUERY_SYMBOLS.FIELD_PARENT_NEW}createdBy`,
[QUERY_SYMBOLS.QUERY]: {
add: {
member: {
with: {
account: {
[QUERY_SYMBOLS.EXPRESSION]: `${QUERY_SYMBOLS.FIELD_PARENT_NEW}createdBy`,
},
role: 'owner',
pending: false,
},
role: 'owner',
pending: false,
},
},
},
Expand Down Expand Up @@ -1525,11 +1537,13 @@ test('create new per-record trigger for removing records', () => {
action: 'DELETE',
effects: [
{
remove: {
members: {
with: {
account: {
[QUERY_SYMBOLS.EXPRESSION]: `${QUERY_SYMBOLS.FIELD_PARENT_OLD}createdBy`,
[QUERY_SYMBOLS.QUERY]: {
remove: {
members: {
with: {
account: {
[QUERY_SYMBOLS.EXPRESSION]: `${QUERY_SYMBOLS.FIELD_PARENT_OLD}createdBy`,
},
},
},
},
Expand Down Expand Up @@ -1589,14 +1603,16 @@ test('create new per-record trigger with filters for creating records', () => {
action: 'INSERT',
effects: [
{
add: {
member: {
with: {
account: {
[QUERY_SYMBOLS.EXPRESSION]: `${QUERY_SYMBOLS.FIELD_PARENT_NEW}createdBy`,
[QUERY_SYMBOLS.QUERY]: {
add: {
member: {
with: {
account: {
[QUERY_SYMBOLS.EXPRESSION]: `${QUERY_SYMBOLS.FIELD_PARENT_NEW}createdBy`,
},
role: 'owner',
pending: false,
},
role: 'owner',
pending: false,
},
},
},
Expand Down Expand Up @@ -1674,7 +1690,9 @@ test('drop existing trigger', () => {
action: 'INSERT',
effects: [
{
add: { member: { with: { account: 'test' } } },
[QUERY_SYMBOLS.QUERY]: {
add: { member: { with: { account: 'test' } } },
},
},
],
},
Expand Down Expand Up @@ -2000,12 +2018,14 @@ test('try to drop a system field', () => {
});

test('try to create new trigger with targeted fields and wrong action', () => {
const effectQueries = [
const effectQueries: ModelTrigger['effects'] = [
{
add: {
signup: {
with: {
year: 2000,
[QUERY_SYMBOLS.QUERY]: {
add: {
signup: {
with: {
year: 2000,
},
},
},
},
Expand Down
Loading