Add option to publish but not persist events (#9448)

* Add option to publish but not persist events

* tsc
This commit is contained in:
Tom Moor
2025-06-14 10:46:01 -04:00
committed by GitHub
parent 8b6d9589f9
commit 75f173c6ff
4 changed files with 65 additions and 48 deletions
+1 -1
View File
@@ -391,7 +391,7 @@ class GroupMembership extends ParanoidModel<
) {
const hookContext = {
...ctx,
event: { name, data, create: true },
event: { name, data, publish: true },
} as HookContext;
if (this.collectionId) {
+1 -1
View File
@@ -371,7 +371,7 @@ class UserMembership extends IdModel<
) {
const hookContext = {
...ctx,
event: { name, data, create: true },
event: { name, data, publish: true },
} as HookContext;
if (this.collectionId) {
+58 -45
View File
@@ -30,13 +30,17 @@ type EventOverrideOptions = {
name?: string;
/** Additional data to publish in the event. */
data?: Record<string, unknown>;
/**
* Whether to persist the event to the database. Defaults to true when using any `withCtx` methods.
*/
persist?: boolean;
};
type EventOptions = EventOverrideOptions & {
/**
* Whether to publish event to the job queue. Defaults to true when using any `withCtx` methods.
*/
create: boolean;
publish: boolean;
};
export type HookContext = APIContext["context"] & { event?: EventOptions };
@@ -62,7 +66,7 @@ class Model<
...ctx.context,
event: {
...eventOpts,
create: true,
publish: true,
},
};
return this.save({ ...options, ...hookContext });
@@ -80,7 +84,7 @@ class Model<
...ctx.context,
event: {
...eventOpts,
create: true,
publish: true,
},
};
this.set(keys);
@@ -96,7 +100,7 @@ class Model<
...ctx.context,
event: {
...eventOpts,
create: true,
publish: true,
},
};
return this.destroy(hookContext);
@@ -110,7 +114,7 @@ class Model<
...ctx.context,
event: {
...eventOpts,
create: true,
publish: true,
},
};
return this.restore(hookContext);
@@ -130,7 +134,7 @@ class Model<
...ctx.context,
event: {
...eventOpts,
create: true,
publish: true,
},
};
return this.findOrCreate({
@@ -152,7 +156,7 @@ class Model<
...ctx.context,
event: {
...eventOpts,
create: true,
publish: true,
},
};
return this.create(values, hookContext);
@@ -218,7 +222,7 @@ class Model<
const namespace = this.eventNamespace ?? this.tableName;
const models = this.sequelize!.models;
if (!context.event?.create) {
if (!context.event?.publish) {
return;
}
@@ -234,44 +238,53 @@ class Model<
});
}
return models.event.create(
{
name: `${namespace}.${context.event.name ?? name}`,
modelId: "modelId" in model ? model.modelId : model.id,
collectionId:
"collectionId" in model
? model.collectionId
: model instanceof models.collection
? model.id
: undefined,
documentId:
"documentId" in model
? model.documentId
: model instanceof models.document
? model.id
: undefined,
userId:
"userId" in model
? model.userId
: model instanceof models.user
? model.id
: undefined,
teamId:
"teamId" in model
? model.teamId
: model instanceof models.team
? model.id
: context.auth?.user.teamId,
actorId: context.auth?.user?.id,
authType: context.auth?.type,
ip: context.ip,
changes: model.previousChangeset,
data: context.event.data,
},
{
const attrs = {
name: `${namespace}.${context.event.name ?? name}`,
modelId: "modelId" in model ? model.modelId : model.id,
collectionId:
"collectionId" in model
? model.collectionId
: model instanceof models.collection
? model.id
: undefined,
documentId:
"documentId" in model
? model.documentId
: model instanceof models.document
? model.id
: undefined,
userId:
"userId" in model
? model.userId
: model instanceof models.user
? model.id
: undefined,
teamId:
"teamId" in model
? model.teamId
: model instanceof models.team
? model.id
: context.auth?.user.teamId,
actorId: context.auth?.user?.id,
authType: context.auth?.type,
ip: context.ip,
changes: model.previousChangeset,
data: context.event.data,
};
if (context.event?.persist !== false) {
return models.event.create(attrs, {
transaction: context.transaction,
}
);
});
} else if (context.transaction) {
(context.transaction.parent || context.transaction).afterCommit(() =>
// @ts-expect-error Event class
models.event.schedule(attrs)
);
} else {
// @ts-expect-error Event class
return models.event.schedule(attrs);
}
}
/**
+5 -1
View File
@@ -175,7 +175,11 @@ export default abstract class APIImportTask<
createContext({
user: associatedImport.createdBy,
transaction,
})
}),
undefined,
{
persist: false,
}
);
});