Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* Helpers for domain services to publish search-indexing events through
|
||||
* the outbox pattern. Domain code calls one of these helpers immediately
|
||||
* after the canonical DB write — inside the same Prisma transaction so
|
||||
* the event row is atomic with the source mutation.
|
||||
*
|
||||
* Event type convention: `search.index.<entity>.<verb>` so the outbox
|
||||
* event router (apps/worker/src/outbox/event-router.ts) maps every
|
||||
* search event onto the `outbox.search-index` BullMQ queue.
|
||||
*/
|
||||
|
||||
import type { SearchIndexSchema } from "./schemas/index.js";
|
||||
import { sanitizeDocument } from "./sanitize.js";
|
||||
|
||||
/**
|
||||
* Loose alias matching the @toolctrl/db OutboxPublisherTransaction shape.
|
||||
* We avoid the direct import to keep packages/search free of a Prisma /
|
||||
* @toolctrl/db dependency — the helper here is a thin wrapper around the
|
||||
* caller-supplied publish function.
|
||||
*/
|
||||
export interface SearchOutboxPublisher {
|
||||
(event: {
|
||||
tenantId: string;
|
||||
type: string;
|
||||
payload: Record<string, unknown>;
|
||||
dispatchKey?: string | null;
|
||||
}): Promise<unknown>;
|
||||
}
|
||||
|
||||
export type SearchOutboxVerb = "upsert" | "delete" | "bulk-reindex";
|
||||
|
||||
export interface PublishSearchUpsertEventInput {
|
||||
readonly publish: SearchOutboxPublisher;
|
||||
readonly schema: SearchIndexSchema;
|
||||
readonly tenantId: string;
|
||||
readonly entityName: string;
|
||||
readonly document: Record<string, unknown> & { id: string | number; tenantId: string };
|
||||
}
|
||||
|
||||
export interface PublishSearchDeleteEventInput {
|
||||
readonly publish: SearchOutboxPublisher;
|
||||
readonly schema: SearchIndexSchema;
|
||||
readonly tenantId: string;
|
||||
readonly entityName: string;
|
||||
readonly documentId: string | number;
|
||||
}
|
||||
|
||||
export interface PublishSearchBulkReindexEventInput {
|
||||
readonly publish: SearchOutboxPublisher;
|
||||
readonly schema: SearchIndexSchema;
|
||||
readonly tenantId: string;
|
||||
readonly entityName: string;
|
||||
}
|
||||
|
||||
export function searchEventType(entityName: string, verb: SearchOutboxVerb): string {
|
||||
const normalized = entityName.toLowerCase().replace(/[^a-z0-9]/gu, "");
|
||||
// The outbox event-type regex requires at least three lowercase
|
||||
// segments; we emit four: `search.index.<entity>.<verb>`. Hyphens are
|
||||
// not allowed in any segment so the verb becomes `bulkreindex` rather
|
||||
// than `bulk-reindex`.
|
||||
const verbSlug = verb === "bulk-reindex" ? "bulkreindex" : verb;
|
||||
return `search.index.${normalized}.${verbSlug}`;
|
||||
}
|
||||
|
||||
export async function publishSearchUpsertEvent(input: PublishSearchUpsertEventInput): Promise<void> {
|
||||
const sanitized = sanitizeDocument(input.schema, input.document);
|
||||
await input.publish({
|
||||
tenantId: input.tenantId,
|
||||
type: searchEventType(input.entityName, "upsert"),
|
||||
payload: {
|
||||
action: "upsert",
|
||||
indexName: input.schema.name,
|
||||
tenantId: input.tenantId,
|
||||
document: sanitized,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function publishSearchDeleteEvent(input: PublishSearchDeleteEventInput): Promise<void> {
|
||||
await input.publish({
|
||||
tenantId: input.tenantId,
|
||||
type: searchEventType(input.entityName, "delete"),
|
||||
payload: {
|
||||
action: "delete",
|
||||
indexName: input.schema.name,
|
||||
tenantId: input.tenantId,
|
||||
documentId: input.documentId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function publishSearchBulkReindexEvent(input: PublishSearchBulkReindexEventInput): Promise<void> {
|
||||
await input.publish({
|
||||
tenantId: input.tenantId,
|
||||
type: searchEventType(input.entityName, "bulk-reindex"),
|
||||
payload: {
|
||||
action: "bulk-reindex",
|
||||
indexName: input.schema.name,
|
||||
tenantId: input.tenantId,
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user