Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -20,10 +20,11 @@ export type { OutboxEvent } from "../generated/prisma/client/client.js";
|
||||
|
||||
/**
|
||||
* Event types follow the `<domain>.<entity>.<verb>` convention (lowercase,
|
||||
* dot-separated). The dispatcher routes by the first segment, e.g.
|
||||
* dot-separated, with optional internal hyphens per segment). The dispatcher
|
||||
* routes by the first segment, e.g.
|
||||
* `billing.subscription.updated` → `notification` queue.
|
||||
*/
|
||||
export const OUTBOX_EVENT_TYPE_PATTERN = /^[a-z][a-z0-9]*(\.[a-z][a-z0-9]*){2,}$/u;
|
||||
export const OUTBOX_EVENT_TYPE_PATTERN = /^[a-z][a-z0-9]*(\.[a-z][a-z0-9]*(?:-[a-z0-9]+)*){2,}$/u;
|
||||
|
||||
/**
|
||||
* dispatchKey must be safe to use as a unique row key and must not leak
|
||||
@@ -70,12 +71,24 @@ export type OutboxEventWriterTx = {
|
||||
};
|
||||
};
|
||||
|
||||
export type OutboxEventBatchWriterTx = {
|
||||
outboxEvent: {
|
||||
createMany(args: {
|
||||
data: Prisma.OutboxEventCreateManyInput[];
|
||||
skipDuplicates?: boolean;
|
||||
}): Promise<{ count: number }>;
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Constrained TransactionClient type: the helper only accepts a
|
||||
* `Prisma.TransactionClient`, never a full `PrismaClient`. This is the
|
||||
* type-system guard that enforces "publish must run inside a transaction".
|
||||
*/
|
||||
export type OutboxPublisherTransaction = Prisma.TransactionClient & OutboxEventWriterTx;
|
||||
export type OutboxBatchPublisherTransaction = Prisma.TransactionClient & OutboxEventBatchWriterTx;
|
||||
|
||||
export const OUTBOX_PUBLISH_BATCH_SIZE = 100;
|
||||
|
||||
export class OutboxPublishError extends Error {
|
||||
public override readonly name = "OutboxPublishError";
|
||||
@@ -140,6 +153,20 @@ function isUniqueViolation(error: unknown): boolean {
|
||||
return candidate.code === "P2002";
|
||||
}
|
||||
|
||||
function toOutboxCreateData(args: OutboxPublishArgs): Prisma.OutboxEventCreateManyInput {
|
||||
validateEventType(args.type);
|
||||
validateDispatchKey(args.dispatchKey);
|
||||
validatePayload(args.payload);
|
||||
return {
|
||||
tenantId: args.tenantId ?? null,
|
||||
eventType: args.type,
|
||||
payload: args.payload,
|
||||
dispatchKey: args.dispatchKey ?? null,
|
||||
...(args.scheduledAt ? { scheduledAt: args.scheduledAt } : {}),
|
||||
...(typeof args.priority === "number" ? { priority: args.priority } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert an OutboxEvent row inside the surrounding Prisma transaction.
|
||||
*
|
||||
@@ -155,20 +182,25 @@ export async function publishOutboxEvent(
|
||||
tx: OutboxPublisherTransaction,
|
||||
args: OutboxPublishArgs
|
||||
): Promise<OutboxEvent> {
|
||||
validateEventType(args.type);
|
||||
validateDispatchKey(args.dispatchKey);
|
||||
validatePayload(args.payload);
|
||||
const data = toOutboxCreateData(args);
|
||||
|
||||
// Look the dispatch key up BEFORE inserting. Postgres aborts the whole
|
||||
// transaction on a unique violation, so the recovery below can never run
|
||||
// inside one: every statement after the failed INSERT — including the lookup
|
||||
// itself — fails with "current transaction is aborted". The catch stays as a
|
||||
// last resort for a genuine race, where it is still the lesser evil.
|
||||
if (args.dispatchKey) {
|
||||
const existing = await tx.outboxEvent.findUnique({
|
||||
where: { dispatchKey: args.dispatchKey },
|
||||
});
|
||||
if (existing) {
|
||||
return existing;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return await tx.outboxEvent.create({
|
||||
data: {
|
||||
tenantId: args.tenantId ?? null,
|
||||
eventType: args.type,
|
||||
payload: args.payload,
|
||||
dispatchKey: args.dispatchKey ?? null,
|
||||
...(args.scheduledAt ? { scheduledAt: args.scheduledAt } : {}),
|
||||
...(typeof args.priority === "number" ? { priority: args.priority } : {}),
|
||||
},
|
||||
data,
|
||||
});
|
||||
} catch (error) {
|
||||
if (isUniqueViolation(error) && args.dispatchKey) {
|
||||
@@ -182,3 +214,32 @@ export async function publishOutboxEvent(
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bulk variant for high-volume producers. Every item is validated before the
|
||||
* first write, then inserted in bounded statements with dispatch-key conflict
|
||||
* deduplication. Callers still provide a TransactionClient so all batches roll
|
||||
* back together with their domain mutation when any statement fails.
|
||||
*/
|
||||
export async function publishOutboxEvents(
|
||||
tx: OutboxBatchPublisherTransaction,
|
||||
args: readonly OutboxPublishArgs[],
|
||||
options: { batchSize?: number } = {}
|
||||
): Promise<{ attempted: number; inserted: number }> {
|
||||
const data = args.map(toOutboxCreateData);
|
||||
const requestedBatchSize = options.batchSize ?? OUTBOX_PUBLISH_BATCH_SIZE;
|
||||
const batchSize = Number.isFinite(requestedBatchSize)
|
||||
? Math.max(1, Math.min(1_000, Math.floor(requestedBatchSize)))
|
||||
: OUTBOX_PUBLISH_BATCH_SIZE;
|
||||
let inserted = 0;
|
||||
|
||||
for (let index = 0; index < data.length; index += batchSize) {
|
||||
const result = await tx.outboxEvent.createMany({
|
||||
data: data.slice(index, index + batchSize),
|
||||
skipDuplicates: true,
|
||||
});
|
||||
inserted += result.count;
|
||||
}
|
||||
|
||||
return { attempted: data.length, inserted };
|
||||
}
|
||||
Reference in new issue
Block a user