@open-mercato/core 0.6.7-develop.6775.1.c2313bb8a3 → 0.6.7-develop.6785.1.1dd7cfac55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/AGENTS.md +3 -3
- package/agentic/standalone-guide.md +13 -0
- package/dist/generated/entities/organization/index.js +2 -0
- package/dist/generated/entities/organization/index.js.map +2 -2
- package/dist/generated/entity-fields-registry.js +1 -0
- package/dist/generated/entity-fields-registry.js.map +2 -2
- package/dist/modules/auth/cli.js +1 -1
- package/dist/modules/auth/cli.js.map +2 -2
- package/dist/modules/auth/lib/backendChrome.js +4 -6
- package/dist/modules/auth/lib/backendChrome.js.map +2 -2
- package/dist/modules/auth/lib/setup-app.js +1 -0
- package/dist/modules/auth/lib/setup-app.js.map +2 -2
- package/dist/modules/auth/services/rbacService.js +29 -44
- package/dist/modules/auth/services/rbacService.js.map +2 -2
- package/dist/modules/catalog/widgets/injection/product-seo/widget.js +1 -1
- package/dist/modules/catalog/widgets/injection/product-seo/widget.js.map +2 -2
- package/dist/modules/communication_channels/lib/access-control.js +2 -2
- package/dist/modules/communication_channels/lib/access-control.js.map +2 -2
- package/dist/modules/customer_accounts/api/invitations/accept.js +1 -2
- package/dist/modules/customer_accounts/api/invitations/accept.js.map +2 -2
- package/dist/modules/customer_accounts/api/login.js +4 -2
- package/dist/modules/customer_accounts/api/login.js.map +2 -2
- package/dist/modules/customer_accounts/api/magic-link/verify.js +4 -2
- package/dist/modules/customer_accounts/api/magic-link/verify.js.map +2 -2
- package/dist/modules/customer_accounts/api/portal/feature-check.js +5 -4
- package/dist/modules/customer_accounts/api/portal/feature-check.js.map +2 -2
- package/dist/modules/customer_accounts/api/portal/nav.js +6 -4
- package/dist/modules/customer_accounts/api/portal/nav.js.map +2 -2
- package/dist/modules/customer_accounts/api/portal/profile.js +5 -1
- package/dist/modules/customer_accounts/api/portal/profile.js.map +2 -2
- package/dist/modules/customer_accounts/api/portal/sessions-refresh.js +3 -3
- package/dist/modules/customer_accounts/api/portal/sessions-refresh.js.map +2 -2
- package/dist/modules/customer_accounts/lib/customerAuth.js +7 -2
- package/dist/modules/customer_accounts/lib/customerAuth.js.map +2 -2
- package/dist/modules/customer_accounts/lib/customerAuthServer.js +2 -1
- package/dist/modules/customer_accounts/lib/customerAuthServer.js.map +2 -2
- package/dist/modules/customer_accounts/services/customerRbacService.js +12 -3
- package/dist/modules/customer_accounts/services/customerRbacService.js.map +2 -2
- package/dist/modules/customer_accounts/setup.js +6 -0
- package/dist/modules/customer_accounts/setup.js.map +2 -2
- package/dist/modules/customer_accounts/widgets/injection/portal-ai-assistant-trigger/widget.client.js +1 -1
- package/dist/modules/customer_accounts/widgets/injection/portal-ai-assistant-trigger/widget.client.js.map +2 -2
- package/dist/modules/customers/lib/visibilityFilter.js +6 -4
- package/dist/modules/customers/lib/visibilityFilter.js.map +2 -2
- package/dist/modules/dashboards/api/layout/[itemId]/route.js +6 -3
- package/dist/modules/dashboards/api/layout/[itemId]/route.js.map +2 -2
- package/dist/modules/dashboards/api/layout/route.js +22 -9
- package/dist/modules/dashboards/api/layout/route.js.map +2 -2
- package/dist/modules/dashboards/api/roles/widgets/route.js +9 -3
- package/dist/modules/dashboards/api/roles/widgets/route.js.map +2 -2
- package/dist/modules/dashboards/api/users/widgets/route.js +9 -3
- package/dist/modules/dashboards/api/users/widgets/route.js.map +2 -2
- package/dist/modules/dashboards/api/widgets/catalog.js +6 -3
- package/dist/modules/dashboards/api/widgets/catalog.js.map +2 -2
- package/dist/modules/dashboards/lib/access.js +5 -3
- package/dist/modules/dashboards/lib/access.js.map +2 -2
- package/dist/modules/directory/api/organization-branding/route.js +10 -3
- package/dist/modules/directory/api/organization-branding/route.js.map +2 -2
- package/dist/modules/directory/api/organizations/route.js +7 -0
- package/dist/modules/directory/api/organizations/route.js.map +3 -3
- package/dist/modules/directory/backend/directory/branding/page.js +31 -8
- package/dist/modules/directory/backend/directory/branding/page.js.map +2 -2
- package/dist/modules/directory/commands/organizations.js +8 -1
- package/dist/modules/directory/commands/organizations.js.map +2 -2
- package/dist/modules/directory/data/entities.js +4 -0
- package/dist/modules/directory/data/entities.js.map +2 -2
- package/dist/modules/directory/data/validators.js +2 -0
- package/dist/modules/directory/data/validators.js.map +2 -2
- package/dist/modules/directory/migrations/Migration20260626145500_directory_logo_preserve_aspect_ratio.js +13 -0
- package/dist/modules/directory/migrations/Migration20260626145500_directory_logo_preserve_aspect_ratio.js.map +7 -0
- package/dist/modules/entities/lib/entityAcl.js +47 -15
- package/dist/modules/entities/lib/entityAcl.js.map +3 -3
- package/dist/modules/inbox_ops/ai-tools.js +5 -2
- package/dist/modules/inbox_ops/ai-tools.js.map +2 -2
- package/dist/modules/messages/lib/routeHelpers.js +1 -3
- package/dist/modules/messages/lib/routeHelpers.js.map +2 -2
- package/dist/modules/notifications/lib/notificationRecipients.js +6 -7
- package/dist/modules/notifications/lib/notificationRecipients.js.map +2 -2
- package/dist/modules/query_index/di.js +3 -0
- package/dist/modules/query_index/di.js.map +2 -2
- package/dist/modules/query_index/lib/coverage.js +2 -2
- package/dist/modules/query_index/lib/coverage.js.map +2 -2
- package/dist/modules/query_index/lib/indexer.js +83 -17
- package/dist/modules/query_index/lib/indexer.js.map +2 -2
- package/dist/modules/query_index/lib/search-tokens.js +13 -7
- package/dist/modules/query_index/lib/search-tokens.js.map +2 -2
- package/dist/modules/query_index/subscribers/coverage_refresh.js +4 -0
- package/dist/modules/query_index/subscribers/coverage_refresh.js.map +2 -2
- package/dist/modules/query_index/subscribers/delete_one.js +53 -1
- package/dist/modules/query_index/subscribers/delete_one.js.map +3 -3
- package/dist/modules/query_index/subscribers/upsert_one.js +60 -0
- package/dist/modules/query_index/subscribers/upsert_one.js.map +3 -3
- package/dist/modules/sales/commands/documents.js +17 -0
- package/dist/modules/sales/commands/documents.js.map +2 -2
- package/dist/modules/staff/api/interceptors.js +5 -3
- package/dist/modules/staff/api/interceptors.js.map +2 -2
- package/dist/modules/sync_excel/api/upload/route.js +15 -2
- package/dist/modules/sync_excel/api/upload/route.js.map +2 -2
- package/dist/modules/workflows/lib/activity-executor.js +7 -7
- package/dist/modules/workflows/lib/activity-executor.js.map +2 -2
- package/generated/entities/organization/index.ts +1 -0
- package/generated/entity-fields-registry.ts +1 -0
- package/package.json +7 -7
- package/src/modules/auth/AGENTS.md +5 -3
- package/src/modules/auth/cli.ts +1 -1
- package/src/modules/auth/lib/backendChrome.tsx +5 -11
- package/src/modules/auth/lib/setup-app.ts +1 -0
- package/src/modules/auth/services/rbacService.ts +41 -46
- package/src/modules/catalog/widgets/injection/product-seo/widget.ts +1 -1
- package/src/modules/communication_channels/lib/access-control.ts +3 -3
- package/src/modules/customer_accounts/AGENTS.md +3 -3
- package/src/modules/customer_accounts/api/invitations/accept.ts +1 -2
- package/src/modules/customer_accounts/api/login.ts +4 -2
- package/src/modules/customer_accounts/api/magic-link/verify.ts +4 -2
- package/src/modules/customer_accounts/api/portal/feature-check.ts +5 -7
- package/src/modules/customer_accounts/api/portal/nav.ts +5 -3
- package/src/modules/customer_accounts/api/portal/profile.ts +5 -1
- package/src/modules/customer_accounts/api/portal/sessions-refresh.ts +3 -3
- package/src/modules/customer_accounts/lib/customerAuth.ts +8 -2
- package/src/modules/customer_accounts/lib/customerAuthServer.ts +1 -0
- package/src/modules/customer_accounts/services/customerRbacService.ts +16 -3
- package/src/modules/customer_accounts/setup.ts +7 -0
- package/src/modules/customer_accounts/widgets/injection/portal-ai-assistant-trigger/widget.client.tsx +1 -3
- package/src/modules/customers/lib/visibilityFilter.ts +7 -5
- package/src/modules/dashboards/api/layout/[itemId]/route.ts +6 -3
- package/src/modules/dashboards/api/layout/route.ts +22 -9
- package/src/modules/dashboards/api/roles/widgets/route.ts +9 -3
- package/src/modules/dashboards/api/users/widgets/route.ts +9 -3
- package/src/modules/dashboards/api/widgets/catalog.ts +6 -3
- package/src/modules/dashboards/lib/access.ts +5 -3
- package/src/modules/directory/api/organization-branding/route.ts +12 -1
- package/src/modules/directory/api/organizations/route.ts +7 -0
- package/src/modules/directory/backend/directory/branding/page.tsx +34 -9
- package/src/modules/directory/commands/organizations.ts +9 -1
- package/src/modules/directory/data/entities.ts +3 -0
- package/src/modules/directory/data/validators.ts +2 -0
- package/src/modules/directory/i18n/de.json +3 -1
- package/src/modules/directory/i18n/en.json +3 -1
- package/src/modules/directory/i18n/es.json +3 -1
- package/src/modules/directory/i18n/pl.json +3 -1
- package/src/modules/directory/migrations/.snapshot-open-mercato.json +18 -1
- package/src/modules/directory/migrations/Migration20260626145500_directory_logo_preserve_aspect_ratio.ts +13 -0
- package/src/modules/entities/lib/entityAcl.ts +47 -18
- package/src/modules/inbox_ops/ai-tools.ts +5 -3
- package/src/modules/messages/lib/routeHelpers.ts +4 -6
- package/src/modules/notifications/lib/notificationRecipients.ts +6 -8
- package/src/modules/query_index/di.ts +6 -0
- package/src/modules/query_index/lib/coverage.ts +9 -6
- package/src/modules/query_index/lib/indexer.ts +105 -19
- package/src/modules/query_index/lib/search-tokens.ts +19 -8
- package/src/modules/query_index/subscribers/coverage_refresh.ts +4 -0
- package/src/modules/query_index/subscribers/delete_one.ts +65 -1
- package/src/modules/query_index/subscribers/upsert_one.ts +68 -1
- package/src/modules/sales/commands/documents.ts +27 -0
- package/src/modules/sales/i18n/de.json +1 -0
- package/src/modules/sales/i18n/en.json +1 -0
- package/src/modules/sales/i18n/es.json +1 -0
- package/src/modules/sales/i18n/pl.json +1 -0
- package/src/modules/staff/api/interceptors.ts +6 -4
- package/src/modules/sync_excel/api/upload/route.ts +15 -2
- package/src/modules/workflows/lib/activity-executor.ts +12 -10
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { resolveEntityTableName } from "@open-mercato/shared/lib/query/engine";
|
|
2
2
|
import { resolveTenantEncryptionService } from "@open-mercato/shared/lib/encryption/customFieldValues";
|
|
3
3
|
import { decryptIndexDocForSearch, encryptIndexDocForStorage } from "@open-mercato/shared/lib/encryption/indexDoc";
|
|
4
|
+
import {
|
|
5
|
+
buildCustomFieldDefinitionIndexFromRows,
|
|
6
|
+
normalizeDefinitionKey,
|
|
7
|
+
selectDefinitionForRecord
|
|
8
|
+
} from "@open-mercato/shared/lib/crud/custom-field-definition-index";
|
|
4
9
|
import { sql } from "kysely";
|
|
5
10
|
import { createLogger } from "@open-mercato/shared/lib/logger";
|
|
6
11
|
import { replaceSearchTokensForRecord, deleteSearchTokensForRecord } from "./search-tokens.js";
|
|
@@ -62,8 +67,61 @@ async function buildIndexDoc(em, params) {
|
|
|
62
67
|
if (!cfMap[cfKey]) cfMap[cfKey] = [];
|
|
63
68
|
cfMap[cfKey].push(val);
|
|
64
69
|
}
|
|
70
|
+
const multiKeys = /* @__PURE__ */ new Set();
|
|
71
|
+
const fieldKeys = Object.keys(cfMap).map((key) => key.slice(3));
|
|
72
|
+
if (fieldKeys.length > 0) {
|
|
73
|
+
let defQuery = db.selectFrom("custom_field_defs").select([
|
|
74
|
+
"key",
|
|
75
|
+
"kind",
|
|
76
|
+
"config_json",
|
|
77
|
+
"organization_id",
|
|
78
|
+
"tenant_id",
|
|
79
|
+
"deleted_at",
|
|
80
|
+
"updated_at"
|
|
81
|
+
]).where("entity_id", "=", params.entityType).where("key", "in", fieldKeys).where("is_active", "=", true).where("deleted_at", "is", null);
|
|
82
|
+
if (params.organizationId != null) {
|
|
83
|
+
defQuery = defQuery.where((eb) => eb.or([
|
|
84
|
+
eb("organization_id", "=", params.organizationId),
|
|
85
|
+
eb("organization_id", "is", null)
|
|
86
|
+
]));
|
|
87
|
+
} else {
|
|
88
|
+
defQuery = defQuery.where("organization_id", "is", null);
|
|
89
|
+
}
|
|
90
|
+
if (params.tenantId != null) {
|
|
91
|
+
defQuery = defQuery.where((eb) => eb.or([
|
|
92
|
+
eb("tenant_id", "=", params.tenantId),
|
|
93
|
+
eb("tenant_id", "is", null)
|
|
94
|
+
]));
|
|
95
|
+
} else {
|
|
96
|
+
defQuery = defQuery.where("tenant_id", "is", null);
|
|
97
|
+
}
|
|
98
|
+
const defRows = await defQuery.execute();
|
|
99
|
+
const definitionIndex = buildCustomFieldDefinitionIndexFromRows(
|
|
100
|
+
defRows.map((row) => ({
|
|
101
|
+
key: String(row.key ?? ""),
|
|
102
|
+
entityId: params.entityType,
|
|
103
|
+
kind: typeof row.kind === "string" ? row.kind : null,
|
|
104
|
+
configJson: row.config_json,
|
|
105
|
+
organizationId: row.organization_id ?? null,
|
|
106
|
+
tenantId: row.tenant_id ?? null,
|
|
107
|
+
deletedAt: row.deleted_at ?? null,
|
|
108
|
+
updatedAt: row.updated_at ?? null
|
|
109
|
+
})),
|
|
110
|
+
{ organizationIds: params.organizationId ? [params.organizationId] : [] }
|
|
111
|
+
);
|
|
112
|
+
for (const key of fieldKeys) {
|
|
113
|
+
const definitions = definitionIndex.get(normalizeDefinitionKey(key)) ?? [];
|
|
114
|
+
const definition = selectDefinitionForRecord(
|
|
115
|
+
definitions,
|
|
116
|
+
params.organizationId ?? null,
|
|
117
|
+
params.tenantId ?? null
|
|
118
|
+
);
|
|
119
|
+
if (definition?.multi) multiKeys.add(key);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
65
122
|
for (const [key, arr] of Object.entries(cfMap)) {
|
|
66
|
-
|
|
123
|
+
const fieldKey = key.slice(3);
|
|
124
|
+
doc[key] = multiKeys.has(fieldKey) || arr.length > 1 ? arr : arr[0];
|
|
67
125
|
}
|
|
68
126
|
try {
|
|
69
127
|
const translationRow = await db.selectFrom("entity_translations").select(["translations"]).where("entity_type", "=", params.entityType).where("entity_id", "=", String(params.recordId)).where(sql`tenant_id is not distinct from ${params.tenantId ?? null}`).where(sql`organization_id is not distinct from ${params.organizationId ?? null}`).executeTakeFirst();
|
|
@@ -109,8 +167,9 @@ function scopeEntityIndexes(q, args) {
|
|
|
109
167
|
}
|
|
110
168
|
async function upsertIndexRow(em, args) {
|
|
111
169
|
const db = em.getKysely();
|
|
170
|
+
const executor = args.trx ?? db;
|
|
112
171
|
const existing = await scopeEntityIndexes(
|
|
113
|
-
|
|
172
|
+
executor.selectFrom("entity_indexes").select(["id", "deleted_at"]),
|
|
114
173
|
args
|
|
115
174
|
).executeTakeFirst();
|
|
116
175
|
const existed = !!existing;
|
|
@@ -124,14 +183,16 @@ async function upsertIndexRow(em, args) {
|
|
|
124
183
|
recordId: args.recordId,
|
|
125
184
|
organizationId: args.organizationId ?? null,
|
|
126
185
|
tenantId: args.tenantId ?? null,
|
|
127
|
-
doc: null
|
|
186
|
+
doc: null,
|
|
187
|
+
trx: args.trx
|
|
128
188
|
});
|
|
129
|
-
} catch {
|
|
189
|
+
} catch (error) {
|
|
190
|
+
if (args.trx) throw error;
|
|
130
191
|
}
|
|
131
192
|
}
|
|
132
193
|
if (existed) {
|
|
133
194
|
await scopeEntityIndexes(
|
|
134
|
-
|
|
195
|
+
executor.deleteFrom("entity_indexes"),
|
|
135
196
|
args
|
|
136
197
|
).execute();
|
|
137
198
|
}
|
|
@@ -148,7 +209,7 @@ async function upsertIndexRow(em, args) {
|
|
|
148
209
|
deleted_at: null
|
|
149
210
|
};
|
|
150
211
|
try {
|
|
151
|
-
await
|
|
212
|
+
await executor.insertInto("entity_indexes").values({ ...payload, created_at: sql`now()` }).onConflict((oc) => oc.columns(["entity_type", "entity_id", "organization_id_coalesced"]).doUpdateSet({
|
|
152
213
|
tenant_id: args.tenantId ?? null,
|
|
153
214
|
doc: sql`${JSON.stringify(doc)}::jsonb`,
|
|
154
215
|
index_version: 1,
|
|
@@ -157,13 +218,14 @@ async function upsertIndexRow(em, args) {
|
|
|
157
218
|
})).execute();
|
|
158
219
|
} catch {
|
|
159
220
|
const updated = await scopeEntityIndexes(
|
|
160
|
-
|
|
221
|
+
executor.updateTable("entity_indexes").set(payload),
|
|
161
222
|
args
|
|
162
223
|
).executeTakeFirst();
|
|
163
224
|
if (!updated || Number(updated.numUpdatedRows ?? 0) === 0) {
|
|
164
225
|
try {
|
|
165
|
-
await
|
|
166
|
-
} catch {
|
|
226
|
+
await executor.insertInto("entity_indexes").values({ ...payload, created_at: sql`now()` }).execute();
|
|
227
|
+
} catch (error) {
|
|
228
|
+
if (args.trx) throw error;
|
|
167
229
|
}
|
|
168
230
|
}
|
|
169
231
|
}
|
|
@@ -177,9 +239,11 @@ async function upsertIndexRow(em, args) {
|
|
|
177
239
|
organizationId: args.organizationId ?? null,
|
|
178
240
|
tenantId: args.tenantId ?? null,
|
|
179
241
|
doc,
|
|
180
|
-
searchTokenDoc: args.searchTokenDoc ?? null
|
|
242
|
+
searchTokenDoc: args.searchTokenDoc ?? null,
|
|
243
|
+
trx: args.trx
|
|
181
244
|
});
|
|
182
|
-
} catch {
|
|
245
|
+
} catch (error) {
|
|
246
|
+
if (args.trx) throw error;
|
|
183
247
|
}
|
|
184
248
|
}
|
|
185
249
|
return { doc, existed, wasDeleted, created, revived };
|
|
@@ -192,7 +256,7 @@ async function reindexSearchTokensForRecord(em, args) {
|
|
|
192
256
|
recordId: args.recordId,
|
|
193
257
|
organizationId: args.organizationId ?? null,
|
|
194
258
|
tenantId: args.tenantId ?? null
|
|
195
|
-
});
|
|
259
|
+
}, { trx: args.trx });
|
|
196
260
|
return;
|
|
197
261
|
}
|
|
198
262
|
const tokenDoc = args.searchTokenDoc ?? (() => {
|
|
@@ -212,12 +276,13 @@ async function reindexSearchTokensForRecord(em, args) {
|
|
|
212
276
|
organizationId: args.organizationId ?? null,
|
|
213
277
|
tenantId: args.tenantId ?? null,
|
|
214
278
|
doc: await tokenDoc
|
|
215
|
-
});
|
|
279
|
+
}, { trx: args.trx });
|
|
216
280
|
}
|
|
217
281
|
async function markDeleted(em, args) {
|
|
218
282
|
const db = em.getKysely();
|
|
283
|
+
const executor = args.trx ?? db;
|
|
219
284
|
const existing = await scopeEntityIndexes(
|
|
220
|
-
|
|
285
|
+
executor.selectFrom("entity_indexes").select(["deleted_at"]),
|
|
221
286
|
args
|
|
222
287
|
).executeTakeFirst();
|
|
223
288
|
const wasActive = !!existing && existing.deleted_at == null;
|
|
@@ -228,11 +293,12 @@ async function markDeleted(em, args) {
|
|
|
228
293
|
recordId: args.recordId,
|
|
229
294
|
organizationId: args.organizationId ?? null,
|
|
230
295
|
tenantId: args.tenantId ?? null
|
|
231
|
-
});
|
|
232
|
-
} catch {
|
|
296
|
+
}, { trx: args.trx });
|
|
297
|
+
} catch (error) {
|
|
298
|
+
if (args.trx) throw error;
|
|
233
299
|
}
|
|
234
300
|
await scopeEntityIndexes(
|
|
235
|
-
|
|
301
|
+
executor.deleteFrom("entity_indexes"),
|
|
236
302
|
args
|
|
237
303
|
).execute();
|
|
238
304
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/query_index/lib/indexer.ts"],
|
|
4
|
-
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { resolveEntityTableName } from '@open-mercato/shared/lib/query/engine'\nimport { resolveTenantEncryptionService } from '@open-mercato/shared/lib/encryption/customFieldValues'\nimport { decryptIndexDocForSearch, encryptIndexDocForStorage } from '@open-mercato/shared/lib/encryption/indexDoc'\nimport { sql } from 'kysely'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport { replaceSearchTokensForRecord, deleteSearchTokensForRecord } from './search-tokens'\nimport { attachAggregateSearchField } from './document'\n\nconst logger = createLogger('query_index').child({ component: 'indexer' })\n\ntype BuildDocParams = {\n entityType: string // '<module>:<entity>'\n recordId: string\n organizationId?: string | null\n tenantId?: string | null\n}\n\nexport async function buildIndexDoc(em: EntityManager, params: BuildDocParams): Promise<Record<string, any> | null> {\n const db = (em as any).getKysely()\n const baseTable = resolveEntityTableName(em, params.entityType)\n\n // Fetch base row\n const baseRow = await db\n .selectFrom(baseTable as any)\n .selectAll()\n .where('id' as any, '=', params.recordId)\n .executeTakeFirst() as Record<string, any> | undefined\n if (!baseRow) return null\n const docSources: Array<Record<string, any>> = []\n\n // Attach the core customer entity when indexing customer profiles so search tokens see the combined row\n let parentEntityRow: Record<string, any> | null = null\n if (params.entityType === 'customers:customer_person_profile' || params.entityType === 'customers:customer_company_profile') {\n const entityId = (baseRow as any).entity_id ?? (baseRow as any).entityId\n if (entityId) {\n const entityRow = await db\n .selectFrom('customer_entities' as any)\n .selectAll()\n .where('id' as any, '=', entityId)\n .executeTakeFirst() as Record<string, any> | undefined\n if (entityRow) {\n docSources.push(entityRow)\n parentEntityRow = entityRow\n }\n }\n }\n void parentEntityRow\n\n // Build base document (snake_case keys as in DB)\n let doc: Record<string, any> = {}\n docSources.push(baseRow)\n for (const source of docSources) {\n for (const [k, v] of Object.entries(source)) doc[k] = v\n }\n\n // Attach custom fields under flat keys 'cf:<key>'\n let cfQuery = db\n .selectFrom('custom_field_values' as any)\n .select([\n 'field_key' as any,\n 'value_text' as any,\n 'value_multiline' as any,\n 'value_int' as any,\n 'value_float' as any,\n 'value_bool' as any,\n ])\n .where('entity_id' as any, '=', params.entityType)\n .where('record_id' as any, '=', String(params.recordId))\n\n if (params.organizationId != null) {\n cfQuery = cfQuery.where((eb: any) => eb.or([\n eb('organization_id' as any, '=', params.organizationId),\n eb('organization_id' as any, 'is', null),\n ]))\n } else {\n cfQuery = cfQuery.where('organization_id' as any, 'is', null)\n }\n\n if (params.tenantId != null) {\n cfQuery = cfQuery.where((eb: any) => eb.or([\n eb('tenant_id' as any, '=', params.tenantId),\n eb('tenant_id' as any, 'is', null),\n ]))\n } else {\n cfQuery = cfQuery.where('tenant_id' as any, 'is', null)\n }\n\n const cfRows = await cfQuery.execute() as Array<Record<string, any>>\n\n const cfMap: Record<string, any[]> = {}\n for (const r of cfRows) {\n const key = String(r.field_key)\n const cfKey = `cf:${key}`\n const val = r.value_bool ?? r.value_int ?? r.value_float ?? r.value_text ?? r.value_multiline ?? null\n if (!cfMap[cfKey]) cfMap[cfKey] = []\n cfMap[cfKey].push(val)\n }\n for (const [key, arr] of Object.entries(cfMap)) {\n // Store singletons as simple value; multis as array\n doc[key] = arr.length <= 1 ? arr[0] : arr\n }\n\n // Attach translations under flat keys 'l10n:{locale}:{field}'\n try {\n const translationRow = await db\n .selectFrom('entity_translations' as any)\n .select(['translations' as any])\n .where('entity_type' as any, '=', params.entityType)\n .where('entity_id' as any, '=', String(params.recordId))\n .where(sql`tenant_id is not distinct from ${params.tenantId ?? null}`)\n .where(sql`organization_id is not distinct from ${params.organizationId ?? null}`)\n .executeTakeFirst() as { translations: Record<string, Record<string, unknown>> | null } | undefined\n\n if (translationRow?.translations && typeof translationRow.translations === 'object') {\n for (const [locale, fields] of Object.entries(translationRow.translations)) {\n if (!fields || typeof fields !== 'object') continue\n for (const [field, value] of Object.entries(fields as Record<string, unknown>)) {\n if (typeof value === 'string' && value.length > 0) {\n doc[`l10n:${locale}:${field}`] = value\n }\n }\n }\n }\n } catch {}\n\n // Kept outside the guard below: a failure while building the aggregate search field is a\n // bug in the aggregation or its configuration, and must surface instead of being mistaken\n // for an encryption failure and silently skipping encryption.\n doc = attachAggregateSearchField(doc, { entityType: params.entityType })\n\n try {\n const encryption = resolveTenantEncryptionService(em as any)\n doc = await encryptIndexDocForStorage(\n params.entityType,\n doc,\n { tenantId: params.tenantId ?? null, organizationId: params.organizationId ?? null },\n encryption,\n )\n } catch (error) {\n // `encryptIndexDocForStorage` already returns the document untouched when encryption is\n // absent or disabled, so a throw here is always a genuine encryption failure. Returning\n // `doc` would hand the caller the plaintext document to write into `entity_indexes`,\n // which must stay encrypted at rest; returning `null` would mean \"record gone\" and make\n // `upsertIndexRow` delete a healthy index row. Fail loudly instead: the projection keeps\n // its previous, correctly encrypted contents and the `query_index.upsert_one` subscriber\n // persists the error via `recordIndexerError`.\n logger.error('Index document encryption failed; refusing to return an unencrypted document', {\n entityType: params.entityType,\n recordId: params.recordId,\n tenantId: params.tenantId ?? null,\n organizationId: params.organizationId ?? null,\n err: error,\n })\n throw error\n }\n\n return doc\n}\n\nexport type UpsertIndexResult = {\n doc: Record<string, any> | null\n existed: boolean\n wasDeleted: boolean\n created: boolean\n revived: boolean\n}\n\nfunction scopeEntityIndexes<QB extends { where: (...args: any[]) => QB }>(\n q: QB,\n args: { entityType: string; recordId: string; organizationId?: string | null; tenantId?: string | null },\n): QB {\n let chain = q.where('entity_type' as any, '=', args.entityType)\n chain = chain.where('entity_id' as any, '=', String(args.recordId))\n chain = args.organizationId == null\n ? chain.where('organization_id' as any, 'is', null as any)\n : chain.where('organization_id' as any, '=', args.organizationId)\n chain = chain.where(sql`tenant_id is not distinct from ${args.tenantId ?? null}`)\n return chain\n}\n\nexport async function upsertIndexRow(\n em: EntityManager,\n args: { entityType: string; recordId: string; organizationId?: string | null; tenantId?: string | null; searchTokenDoc?: Record<string, unknown> | null; deferSearchTokens?: boolean }\n): Promise<UpsertIndexResult> {\n const db = (em as any).getKysely()\n\n const existing = await scopeEntityIndexes(\n db.selectFrom('entity_indexes' as any).select(['id' as any, 'deleted_at' as any]),\n args,\n ).executeTakeFirst() as { id: string; deleted_at: Date | null } | undefined\n\n const existed = !!existing\n const wasDeleted = !!existing && existing.deleted_at != null\n\n const doc = await buildIndexDoc(em, args)\n if (!doc) {\n // When the caller defers token work it owns the matching token cleanup; the\n // projection-row removal below stays synchronous so list reads converge immediately.\n if (!args.deferSearchTokens) {\n try {\n await reindexSearchTokensForRecord(em, {\n entityType: args.entityType,\n recordId: args.recordId,\n organizationId: args.organizationId ?? null,\n tenantId: args.tenantId ?? null,\n doc: null,\n })\n } catch {}\n }\n if (existed) {\n await scopeEntityIndexes(\n db.deleteFrom('entity_indexes' as any) as any,\n args,\n ).execute()\n }\n return { doc: null, existed, wasDeleted, created: false, revived: false }\n }\n\n const payload = {\n entity_type: args.entityType,\n entity_id: String(args.recordId),\n organization_id: args.organizationId ?? null,\n tenant_id: args.tenantId ?? null,\n doc: sql`${JSON.stringify(doc)}::jsonb`,\n index_version: 1,\n updated_at: sql`now()`,\n deleted_at: null,\n }\n\n // Prefer modern upsert keyed by coalesced org id when available; fallback to update-then-insert\n try {\n await db\n .insertInto('entity_indexes' as any)\n .values({ ...payload, created_at: sql`now()` } as any)\n .onConflict((oc: any) => oc\n .columns(['entity_type', 'entity_id', 'organization_id_coalesced'])\n .doUpdateSet({\n tenant_id: args.tenantId ?? null,\n doc: sql`${JSON.stringify(doc)}::jsonb`,\n index_version: 1,\n updated_at: sql`now()`,\n deleted_at: null,\n } as any))\n .execute()\n } catch {\n // Fallback for schemas without organization_id_coalesced column/index\n const updated = await scopeEntityIndexes(\n db.updateTable('entity_indexes' as any).set(payload as any) as any,\n args,\n ).executeTakeFirst() as { numUpdatedRows?: bigint | number } | undefined\n if (!updated || Number(updated.numUpdatedRows ?? 0) === 0) {\n try {\n await db\n .insertInto('entity_indexes' as any)\n .values({ ...payload, created_at: sql`now()` } as any)\n .execute()\n } catch {}\n }\n }\n\n const created = !existed\n const revived = existed && wasDeleted\n // The search-token rebuild (DELETE + chunked INSERT) is the heavy tail of indexing.\n // Callers that defer it (the upsert subscriber) run `reindexSearchTokensForRecord`\n // asynchronously after this projection update so write latency stays bounded.\n if (!args.deferSearchTokens) {\n try {\n await reindexSearchTokensForRecord(em, {\n entityType: args.entityType,\n recordId: args.recordId,\n organizationId: args.organizationId ?? null,\n tenantId: args.tenantId ?? null,\n doc,\n searchTokenDoc: args.searchTokenDoc ?? null,\n })\n } catch {}\n }\n return { doc, existed, wasDeleted, created, revived }\n}\n\n/**\n * Rebuilds (or clears, when `doc` is null) the search-token rows for a single record.\n * This is the asynchronous-friendly tail of `upsertIndexRow`: it does not touch the\n * `entity_indexes` projection that list endpoints read, so it can run out-of-band\n * without making query-index reads inconsistent.\n */\nexport async function reindexSearchTokensForRecord(\n em: EntityManager,\n args: {\n entityType: string\n recordId: string\n organizationId?: string | null\n tenantId?: string | null\n doc: Record<string, any> | null\n searchTokenDoc?: Record<string, unknown> | null\n },\n): Promise<void> {\n const db = (em as any).getKysely()\n if (!args.doc) {\n await deleteSearchTokensForRecord(db, {\n entityType: args.entityType,\n recordId: args.recordId,\n organizationId: args.organizationId ?? null,\n tenantId: args.tenantId ?? null,\n })\n return\n }\n const tokenDoc = args.searchTokenDoc ?? (() => {\n const encryption = resolveTenantEncryptionService(em as any)\n const dekKeyCache = new Map<string | null, string | null>()\n return decryptIndexDocForSearch(\n args.entityType,\n args.doc,\n { tenantId: args.tenantId ?? null, organizationId: args.organizationId ?? null },\n encryption,\n dekKeyCache,\n )\n })()\n await replaceSearchTokensForRecord(db, {\n entityType: args.entityType,\n recordId: args.recordId,\n organizationId: args.organizationId ?? null,\n tenantId: args.tenantId ?? null,\n doc: await tokenDoc,\n })\n}\n\nexport async function markDeleted(\n em: EntityManager,\n args: { entityType: string; recordId: string; organizationId?: string | null; tenantId?: string | null }\n): Promise<{ wasActive: boolean }> {\n const db = (em as any).getKysely()\n const existing = await scopeEntityIndexes(\n db.selectFrom('entity_indexes' as any).select(['deleted_at' as any]),\n args,\n ).executeTakeFirst() as { deleted_at: Date | null } | undefined\n\n const wasActive = !!existing && existing.deleted_at == null\n\n if (existing) {\n try {\n await deleteSearchTokensForRecord(db, {\n entityType: args.entityType,\n recordId: args.recordId,\n organizationId: args.organizationId ?? null,\n tenantId: args.tenantId ?? null,\n })\n } catch {}\n await scopeEntityIndexes(\n db.deleteFrom('entity_indexes' as any) as any,\n args,\n ).execute()\n }\n\n return { wasActive }\n}\n"],
|
|
5
|
-
"mappings": "AACA,SAAS,8BAA8B;AACvC,SAAS,sCAAsC;AAC/C,SAAS,0BAA0B,iCAAiC;AACpE,
|
|
4
|
+
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { resolveEntityTableName } from '@open-mercato/shared/lib/query/engine'\nimport { resolveTenantEncryptionService } from '@open-mercato/shared/lib/encryption/customFieldValues'\nimport { decryptIndexDocForSearch, encryptIndexDocForStorage } from '@open-mercato/shared/lib/encryption/indexDoc'\nimport {\n buildCustomFieldDefinitionIndexFromRows,\n normalizeDefinitionKey,\n selectDefinitionForRecord,\n type CustomFieldDefinitionRow,\n} from '@open-mercato/shared/lib/crud/custom-field-definition-index'\nimport { type Kysely, type Transaction, sql } from 'kysely'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport { replaceSearchTokensForRecord, deleteSearchTokensForRecord } from './search-tokens'\nimport { attachAggregateSearchField } from './document'\n\nconst logger = createLogger('query_index').child({ component: 'indexer' })\n\ntype QueryIndexExecutor = Kysely<any> | Transaction<any>\n\ntype BuildDocParams = {\n entityType: string // '<module>:<entity>'\n recordId: string\n organizationId?: string | null\n tenantId?: string | null\n}\n\nexport async function buildIndexDoc(em: EntityManager, params: BuildDocParams): Promise<Record<string, any> | null> {\n const db = (em as any).getKysely()\n const baseTable = resolveEntityTableName(em, params.entityType)\n\n // Fetch base row\n const baseRow = await db\n .selectFrom(baseTable as any)\n .selectAll()\n .where('id' as any, '=', params.recordId)\n .executeTakeFirst() as Record<string, any> | undefined\n if (!baseRow) return null\n const docSources: Array<Record<string, any>> = []\n\n // Attach the core customer entity when indexing customer profiles so search tokens see the combined row\n let parentEntityRow: Record<string, any> | null = null\n if (params.entityType === 'customers:customer_person_profile' || params.entityType === 'customers:customer_company_profile') {\n const entityId = (baseRow as any).entity_id ?? (baseRow as any).entityId\n if (entityId) {\n const entityRow = await db\n .selectFrom('customer_entities' as any)\n .selectAll()\n .where('id' as any, '=', entityId)\n .executeTakeFirst() as Record<string, any> | undefined\n if (entityRow) {\n docSources.push(entityRow)\n parentEntityRow = entityRow\n }\n }\n }\n void parentEntityRow\n\n // Build base document (snake_case keys as in DB)\n let doc: Record<string, any> = {}\n docSources.push(baseRow)\n for (const source of docSources) {\n for (const [k, v] of Object.entries(source)) doc[k] = v\n }\n\n // Attach custom fields under flat keys 'cf:<key>'\n let cfQuery = db\n .selectFrom('custom_field_values' as any)\n .select([\n 'field_key' as any,\n 'value_text' as any,\n 'value_multiline' as any,\n 'value_int' as any,\n 'value_float' as any,\n 'value_bool' as any,\n ])\n .where('entity_id' as any, '=', params.entityType)\n .where('record_id' as any, '=', String(params.recordId))\n\n if (params.organizationId != null) {\n cfQuery = cfQuery.where((eb: any) => eb.or([\n eb('organization_id' as any, '=', params.organizationId),\n eb('organization_id' as any, 'is', null),\n ]))\n } else {\n cfQuery = cfQuery.where('organization_id' as any, 'is', null)\n }\n\n if (params.tenantId != null) {\n cfQuery = cfQuery.where((eb: any) => eb.or([\n eb('tenant_id' as any, '=', params.tenantId),\n eb('tenant_id' as any, 'is', null),\n ]))\n } else {\n cfQuery = cfQuery.where('tenant_id' as any, 'is', null)\n }\n\n const cfRows = await cfQuery.execute() as Array<Record<string, any>>\n\n const cfMap: Record<string, any[]> = {}\n for (const r of cfRows) {\n const key = String(r.field_key)\n const cfKey = `cf:${key}`\n const val = r.value_bool ?? r.value_int ?? r.value_float ?? r.value_text ?? r.value_multiline ?? null\n if (!cfMap[cfKey]) cfMap[cfKey] = []\n cfMap[cfKey].push(val)\n }\n\n const multiKeys = new Set<string>()\n const fieldKeys = Object.keys(cfMap).map((key) => key.slice(3))\n if (fieldKeys.length > 0) {\n let defQuery = db\n .selectFrom('custom_field_defs' as any)\n .select([\n 'key' as any,\n 'kind' as any,\n 'config_json' as any,\n 'organization_id' as any,\n 'tenant_id' as any,\n 'deleted_at' as any,\n 'updated_at' as any,\n ])\n .where('entity_id' as any, '=', params.entityType)\n .where('key' as any, 'in', fieldKeys)\n .where('is_active' as any, '=', true)\n .where('deleted_at' as any, 'is', null)\n\n if (params.organizationId != null) {\n defQuery = defQuery.where((eb: any) => eb.or([\n eb('organization_id' as any, '=', params.organizationId),\n eb('organization_id' as any, 'is', null),\n ]))\n } else {\n defQuery = defQuery.where('organization_id' as any, 'is', null)\n }\n\n if (params.tenantId != null) {\n defQuery = defQuery.where((eb: any) => eb.or([\n eb('tenant_id' as any, '=', params.tenantId),\n eb('tenant_id' as any, 'is', null),\n ]))\n } else {\n defQuery = defQuery.where('tenant_id' as any, 'is', null)\n }\n\n const defRows = await defQuery.execute() as Array<Record<string, any>>\n const definitionIndex = buildCustomFieldDefinitionIndexFromRows(\n defRows.map((row): CustomFieldDefinitionRow => ({\n key: String(row.key ?? ''),\n entityId: params.entityType,\n kind: typeof row.kind === 'string' ? row.kind : null,\n configJson: row.config_json,\n organizationId: row.organization_id ?? null,\n tenantId: row.tenant_id ?? null,\n deletedAt: row.deleted_at ?? null,\n updatedAt: row.updated_at ?? null,\n })),\n { organizationIds: params.organizationId ? [params.organizationId] : [] },\n )\n for (const key of fieldKeys) {\n const definitions = definitionIndex.get(normalizeDefinitionKey(key)) ?? []\n const definition = selectDefinitionForRecord(\n definitions,\n params.organizationId ?? null,\n params.tenantId ?? null,\n )\n if (definition?.multi) multiKeys.add(key)\n }\n }\n\n for (const [key, arr] of Object.entries(cfMap)) {\n // Cardinality belongs to the field definition, not the current row count:\n // a multi field containing one value must still hydrate as an array.\n const fieldKey = key.slice(3)\n doc[key] = multiKeys.has(fieldKey) || arr.length > 1 ? arr : arr[0]\n }\n\n // Attach translations under flat keys 'l10n:{locale}:{field}'\n try {\n const translationRow = await db\n .selectFrom('entity_translations' as any)\n .select(['translations' as any])\n .where('entity_type' as any, '=', params.entityType)\n .where('entity_id' as any, '=', String(params.recordId))\n .where(sql`tenant_id is not distinct from ${params.tenantId ?? null}`)\n .where(sql`organization_id is not distinct from ${params.organizationId ?? null}`)\n .executeTakeFirst() as { translations: Record<string, Record<string, unknown>> | null } | undefined\n\n if (translationRow?.translations && typeof translationRow.translations === 'object') {\n for (const [locale, fields] of Object.entries(translationRow.translations)) {\n if (!fields || typeof fields !== 'object') continue\n for (const [field, value] of Object.entries(fields as Record<string, unknown>)) {\n if (typeof value === 'string' && value.length > 0) {\n doc[`l10n:${locale}:${field}`] = value\n }\n }\n }\n }\n } catch {}\n\n // Kept outside the guard below: a failure while building the aggregate search field is a\n // bug in the aggregation or its configuration, and must surface instead of being mistaken\n // for an encryption failure and silently skipping encryption.\n doc = attachAggregateSearchField(doc, { entityType: params.entityType })\n\n try {\n const encryption = resolveTenantEncryptionService(em as any)\n doc = await encryptIndexDocForStorage(\n params.entityType,\n doc,\n { tenantId: params.tenantId ?? null, organizationId: params.organizationId ?? null },\n encryption,\n )\n } catch (error) {\n // `encryptIndexDocForStorage` already returns the document untouched when encryption is\n // absent or disabled, so a throw here is always a genuine encryption failure. Returning\n // `doc` would hand the caller the plaintext document to write into `entity_indexes`,\n // which must stay encrypted at rest; returning `null` would mean \"record gone\" and make\n // `upsertIndexRow` delete a healthy index row. Fail loudly instead: the projection keeps\n // its previous, correctly encrypted contents and the `query_index.upsert_one` subscriber\n // persists the error via `recordIndexerError`.\n logger.error('Index document encryption failed; refusing to return an unencrypted document', {\n entityType: params.entityType,\n recordId: params.recordId,\n tenantId: params.tenantId ?? null,\n organizationId: params.organizationId ?? null,\n err: error,\n })\n throw error\n }\n\n return doc\n}\n\nexport type UpsertIndexResult = {\n doc: Record<string, any> | null\n existed: boolean\n wasDeleted: boolean\n created: boolean\n revived: boolean\n}\n\nfunction scopeEntityIndexes<QB extends { where: (...args: any[]) => QB }>(\n q: QB,\n args: { entityType: string; recordId: string; organizationId?: string | null; tenantId?: string | null },\n): QB {\n let chain = q.where('entity_type' as any, '=', args.entityType)\n chain = chain.where('entity_id' as any, '=', String(args.recordId))\n chain = args.organizationId == null\n ? chain.where('organization_id' as any, 'is', null as any)\n : chain.where('organization_id' as any, '=', args.organizationId)\n chain = chain.where(sql`tenant_id is not distinct from ${args.tenantId ?? null}`)\n return chain\n}\n\nexport async function upsertIndexRow(\n em: EntityManager,\n args: { entityType: string; recordId: string; organizationId?: string | null; tenantId?: string | null; searchTokenDoc?: Record<string, unknown> | null; deferSearchTokens?: boolean; trx?: QueryIndexExecutor }\n): Promise<UpsertIndexResult> {\n const db = (em as any).getKysely()\n const executor = args.trx ?? db\n\n const existing = await scopeEntityIndexes(\n executor.selectFrom('entity_indexes' as any).select(['id' as any, 'deleted_at' as any]),\n args,\n ).executeTakeFirst() as { id: string; deleted_at: Date | null } | undefined\n\n const existed = !!existing\n const wasDeleted = !!existing && existing.deleted_at != null\n\n const doc = await buildIndexDoc(em, args)\n if (!doc) {\n // When the caller defers token work it owns the matching token cleanup; the\n // projection-row removal below stays synchronous so list reads converge immediately.\n if (!args.deferSearchTokens) {\n try {\n await reindexSearchTokensForRecord(em, {\n entityType: args.entityType,\n recordId: args.recordId,\n organizationId: args.organizationId ?? null,\n tenantId: args.tenantId ?? null,\n doc: null,\n trx: args.trx,\n })\n } catch (error) {\n if (args.trx) throw error\n }\n }\n if (existed) {\n await scopeEntityIndexes(\n executor.deleteFrom('entity_indexes' as any) as any,\n args,\n ).execute()\n }\n return { doc: null, existed, wasDeleted, created: false, revived: false }\n }\n\n const payload = {\n entity_type: args.entityType,\n entity_id: String(args.recordId),\n organization_id: args.organizationId ?? null,\n tenant_id: args.tenantId ?? null,\n doc: sql`${JSON.stringify(doc)}::jsonb`,\n index_version: 1,\n updated_at: sql`now()`,\n deleted_at: null,\n }\n\n // Prefer modern upsert keyed by coalesced org id when available; fallback to update-then-insert\n try {\n await executor\n .insertInto('entity_indexes' as any)\n .values({ ...payload, created_at: sql`now()` } as any)\n .onConflict((oc: any) => oc\n .columns(['entity_type', 'entity_id', 'organization_id_coalesced'])\n .doUpdateSet({\n tenant_id: args.tenantId ?? null,\n doc: sql`${JSON.stringify(doc)}::jsonb`,\n index_version: 1,\n updated_at: sql`now()`,\n deleted_at: null,\n } as any))\n .execute()\n } catch {\n // Fallback for schemas without organization_id_coalesced column/index\n const updated = await scopeEntityIndexes(\n executor.updateTable('entity_indexes' as any).set(payload as any) as any,\n args,\n ).executeTakeFirst() as { numUpdatedRows?: bigint | number } | undefined\n if (!updated || Number(updated.numUpdatedRows ?? 0) === 0) {\n try {\n await executor\n .insertInto('entity_indexes' as any)\n .values({ ...payload, created_at: sql`now()` } as any)\n .execute()\n } catch (error) {\n if (args.trx) throw error\n }\n }\n }\n\n const created = !existed\n const revived = existed && wasDeleted\n // The search-token rebuild (DELETE + chunked INSERT) is the heavy tail of indexing.\n // Callers that defer it (the upsert subscriber) run `reindexSearchTokensForRecord`\n // asynchronously after this projection update so write latency stays bounded.\n if (!args.deferSearchTokens) {\n try {\n await reindexSearchTokensForRecord(em, {\n entityType: args.entityType,\n recordId: args.recordId,\n organizationId: args.organizationId ?? null,\n tenantId: args.tenantId ?? null,\n doc,\n searchTokenDoc: args.searchTokenDoc ?? null,\n trx: args.trx,\n })\n } catch (error) {\n if (args.trx) throw error\n }\n }\n return { doc, existed, wasDeleted, created, revived }\n}\n\n/**\n * Rebuilds (or clears, when `doc` is null) the search-token rows for a single record.\n * This is the asynchronous-friendly tail of `upsertIndexRow`: it does not touch the\n * `entity_indexes` projection that list endpoints read, so it can run out-of-band\n * without making query-index reads inconsistent.\n */\nexport async function reindexSearchTokensForRecord(\n em: EntityManager,\n args: {\n entityType: string\n recordId: string\n organizationId?: string | null\n tenantId?: string | null\n doc: Record<string, any> | null\n searchTokenDoc?: Record<string, unknown> | null\n trx?: QueryIndexExecutor\n },\n): Promise<void> {\n const db = (em as any).getKysely()\n if (!args.doc) {\n await deleteSearchTokensForRecord(db, {\n entityType: args.entityType,\n recordId: args.recordId,\n organizationId: args.organizationId ?? null,\n tenantId: args.tenantId ?? null,\n }, { trx: args.trx })\n return\n }\n const tokenDoc = args.searchTokenDoc ?? (() => {\n const encryption = resolveTenantEncryptionService(em as any)\n const dekKeyCache = new Map<string | null, string | null>()\n return decryptIndexDocForSearch(\n args.entityType,\n args.doc,\n { tenantId: args.tenantId ?? null, organizationId: args.organizationId ?? null },\n encryption,\n dekKeyCache,\n )\n })()\n await replaceSearchTokensForRecord(db, {\n entityType: args.entityType,\n recordId: args.recordId,\n organizationId: args.organizationId ?? null,\n tenantId: args.tenantId ?? null,\n doc: await tokenDoc,\n }, { trx: args.trx })\n}\n\nexport async function markDeleted(\n em: EntityManager,\n args: { entityType: string; recordId: string; organizationId?: string | null; tenantId?: string | null; trx?: QueryIndexExecutor }\n): Promise<{ wasActive: boolean }> {\n const db = (em as any).getKysely()\n const executor = args.trx ?? db\n const existing = await scopeEntityIndexes(\n executor.selectFrom('entity_indexes' as any).select(['deleted_at' as any]),\n args,\n ).executeTakeFirst() as { deleted_at: Date | null } | undefined\n\n const wasActive = !!existing && existing.deleted_at == null\n\n if (existing) {\n try {\n await deleteSearchTokensForRecord(db, {\n entityType: args.entityType,\n recordId: args.recordId,\n organizationId: args.organizationId ?? null,\n tenantId: args.tenantId ?? null,\n }, { trx: args.trx })\n } catch (error) {\n if (args.trx) throw error\n }\n await scopeEntityIndexes(\n executor.deleteFrom('entity_indexes' as any) as any,\n args,\n ).execute()\n }\n\n return { wasActive }\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,8BAA8B;AACvC,SAAS,sCAAsC;AAC/C,SAAS,0BAA0B,iCAAiC;AACpE;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP,SAAwC,WAAW;AACnD,SAAS,oBAAoB;AAC7B,SAAS,8BAA8B,mCAAmC;AAC1E,SAAS,kCAAkC;AAE3C,MAAM,SAAS,aAAa,aAAa,EAAE,MAAM,EAAE,WAAW,UAAU,CAAC;AAWzE,eAAsB,cAAc,IAAmB,QAA6D;AAClH,QAAM,KAAM,GAAW,UAAU;AACjC,QAAM,YAAY,uBAAuB,IAAI,OAAO,UAAU;AAG9D,QAAM,UAAU,MAAM,GACnB,WAAW,SAAgB,EAC3B,UAAU,EACV,MAAM,MAAa,KAAK,OAAO,QAAQ,EACvC,iBAAiB;AACpB,MAAI,CAAC,QAAS,QAAO;AACrB,QAAM,aAAyC,CAAC;AAGhD,MAAI,kBAA8C;AAClD,MAAI,OAAO,eAAe,uCAAuC,OAAO,eAAe,sCAAsC;AAC3H,UAAM,WAAY,QAAgB,aAAc,QAAgB;AAChE,QAAI,UAAU;AACZ,YAAM,YAAY,MAAM,GACrB,WAAW,mBAA0B,EACrC,UAAU,EACV,MAAM,MAAa,KAAK,QAAQ,EAChC,iBAAiB;AACpB,UAAI,WAAW;AACb,mBAAW,KAAK,SAAS;AACzB,0BAAkB;AAAA,MACpB;AAAA,IACF;AAAA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A,MACL,KAAK;AAAA,MACL,EAAE,UAAU,KAAK,YAAY,MAAM,gBAAgB,KAAK,kBAAkB,KAAK;AAAA,MAC/E;AAAA,MACA;AAAA,IACF;AAAA,EACF,GAAG;AACH,QAAM,6BAA6B,IAAI;AAAA,IACrC,YAAY,KAAK;AAAA,IACjB,UAAU,KAAK;AAAA,IACf,gBAAgB,KAAK,kBAAkB;AAAA,IACvC,UAAU,KAAK,YAAY;AAAA,IAC3B,KAAK,MAAM;AAAA,EACb,GAAG,EAAE,KAAK,KAAK,IAAI,CAAC;AACtB;AAEA,eAAsB,YACpB,IACA,MACiC;AACjC,QAAM,KAAM,GAAW,UAAU;AACjC,QAAM,WAAW,KAAK,OAAO;AAC7B,QAAM,WAAW,MAAM;AAAA,IACrB,SAAS,WAAW,gBAAuB,EAAE,OAAO,CAAC,YAAmB,CAAC;AAAA,IACzE;AAAA,EACF,EAAE,iBAAiB;AAEnB,QAAM,YAAY,CAAC,CAAC,YAAY,SAAS,cAAc;AAEvD,MAAI,UAAU;AACZ,QAAI;AACF,YAAM,4BAA4B,IAAI;AAAA,QACpC,YAAY,KAAK;AAAA,QACjB,UAAU,KAAK;AAAA,QACf,gBAAgB,KAAK,kBAAkB;AAAA,QACvC,UAAU,KAAK,YAAY;AAAA,MAC7B,GAAG,EAAE,KAAK,KAAK,IAAI,CAAC;AAAA,IACtB,SAAS,OAAO;AACd,UAAI,KAAK,IAAK,OAAM;AAAA,IACtB;AACA,UAAM;AAAA,MACJ,SAAS,WAAW,gBAAuB;AAAA,MAC3C;AAAA,IACF,EAAE,QAAQ;AAAA,EACZ;AAEA,SAAO,EAAE,UAAU;AACrB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -109,15 +109,15 @@ function buildFieldPairs(recordId, doc) {
|
|
|
109
109
|
}
|
|
110
110
|
return pairs;
|
|
111
111
|
}
|
|
112
|
-
async function replaceSearchTokensForRecord(db, params) {
|
|
112
|
+
async function replaceSearchTokensForRecord(db, params, options) {
|
|
113
113
|
const rows = buildSearchTokenRows(params);
|
|
114
114
|
const config = params.config ?? resolveSearchConfig();
|
|
115
115
|
if (!config.enabled) return;
|
|
116
116
|
const organizationId = params.organizationId ?? null;
|
|
117
117
|
const tenantId = params.tenantId ?? null;
|
|
118
118
|
const fieldPairs = buildFieldPairs(String(params.recordId), params.doc);
|
|
119
|
-
|
|
120
|
-
let deleteQuery =
|
|
119
|
+
const writeTokens = async (executor) => {
|
|
120
|
+
let deleteQuery = executor.deleteFrom("search_tokens").where("entity_type", "=", params.entityType).where(sql`organization_id is not distinct from ${organizationId}`).where(sql`tenant_id is not distinct from ${tenantId}`);
|
|
121
121
|
if (fieldPairs.length) {
|
|
122
122
|
deleteQuery = deleteQuery.where((eb) => eb.or(
|
|
123
123
|
fieldPairs.map(([rid, field]) => eb.and([
|
|
@@ -132,14 +132,20 @@ async function replaceSearchTokensForRecord(db, params) {
|
|
|
132
132
|
if (!rows.length) return;
|
|
133
133
|
const payloads = rows.map((row) => ({ ...row, created_at: sql`now()` }));
|
|
134
134
|
for (const batch of chunk(payloads, INSERT_BATCH_SIZE)) {
|
|
135
|
-
await
|
|
135
|
+
await executor.insertInto("search_tokens").values(batch).execute();
|
|
136
136
|
}
|
|
137
|
-
}
|
|
137
|
+
};
|
|
138
|
+
if (options?.trx) {
|
|
139
|
+
await writeTokens(options.trx);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
await db.transaction().execute(writeTokens);
|
|
138
143
|
}
|
|
139
|
-
async function deleteSearchTokensForRecord(db, params) {
|
|
144
|
+
async function deleteSearchTokensForRecord(db, params, options) {
|
|
140
145
|
const organizationId = params.organizationId ?? null;
|
|
141
146
|
const tenantId = params.tenantId ?? null;
|
|
142
|
-
|
|
147
|
+
const executor = options?.trx ?? db;
|
|
148
|
+
await executor.deleteFrom("search_tokens").where("entity_type", "=", params.entityType).where("entity_id", "=", String(params.recordId)).where(sql`organization_id is not distinct from ${organizationId}`).where(sql`tenant_id is not distinct from ${tenantId}`).execute();
|
|
143
149
|
}
|
|
144
150
|
async function replaceSearchTokensForBatch(db, payloads) {
|
|
145
151
|
if (!payloads.length) return;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/query_index/lib/search-tokens.ts"],
|
|
4
|
-
"sourcesContent": ["import { type Kysely, sql } from 'kysely'\nimport {\n isSearchFieldBlocklisted,\n resolveSearchConfig,\n type SearchConfig,\n} from '@open-mercato/shared/lib/search/config'\nimport { tokenizeText } from '@open-mercato/shared/lib/search/tokenize'\nimport { parseBooleanToken } from '@open-mercato/shared/lib/boolean'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst logger = createLogger('query_index').child({ component: 'search-tokens' })\n\nconst INSERT_BATCH_SIZE = 500\n\nfunction chunk<T>(items: T[], size: number): T[][] {\n if (size <= 0) return [items]\n const out: T[][] = []\n for (let i = 0; i < items.length; i += size) out.push(items.slice(i, i + size))\n return out\n}\n\nexport type SearchTokenRow = {\n entity_type: string\n entity_id: string\n organization_id: string | null\n tenant_id: string | null\n field: string\n token_hash: string\n token?: string | null\n}\n\ntype BuildTokenOptions = {\n entityType: string\n recordId: string\n organizationId?: string | null\n tenantId?: string | null\n doc?: Record<string, unknown> | null\n config?: SearchConfig\n}\n\nconst DEFAULT_SCOPE = { organizationId: null, tenantId: null }\ntype EntityFieldPair = [string, string]\n\nexport const isSearchDebugEnabled = (): boolean => {\n return parseBooleanToken(process.env.OM_SEARCH_DEBUG ?? '') === true\n}\n\nconst debug = (event: string, payload: Record<string, unknown>) => {\n if (!isSearchDebugEnabled()) return\n try {\n logger.debug('Search token event', { event, payload })\n } catch {\n // ignore\n }\n}\n\nfunction collectTextValues(value: unknown): string[] {\n if (typeof value === 'string') return [value]\n if (Array.isArray(value)) {\n const out: string[] = []\n for (const entry of value) {\n if (typeof entry === 'string') out.push(entry)\n }\n return out\n }\n return []\n}\n\nfunction shouldIndexField(\n field: string,\n value: unknown,\n config: SearchConfig,\n entityType: string | null,\n): boolean {\n if (typeof value !== 'string' && !Array.isArray(value)) return false\n const lower = field.toLowerCase()\n if (lower === 'id' || lower.endsWith('_id') || lower.endsWith('.id')) return false\n if (lower.endsWith('_at')) return false\n if (['created_at', 'updated_at', 'deleted_at', 'tenant_id', 'organization_id'].includes(lower)) return false\n if (isSearchFieldBlocklisted(field, entityType, config)) return false\n const values = collectTextValues(value)\n if (!values.length) return false\n return values.some((text) => tokenizeText(text, config).tokens.length > 0)\n}\n\nexport function buildSearchTokenRows(params: BuildTokenOptions): SearchTokenRow[] {\n const config = params.config ?? resolveSearchConfig()\n if (!config.enabled) return []\n if (!params.doc) return []\n const tokens: SearchTokenRow[] = []\n const capturePairs = isSearchDebugEnabled() && params.entityType === 'customers:customer_deal'\n const debugPairs: Array<{ field: string; hash: string }> = []\n const scope = {\n organizationId: params.organizationId ?? DEFAULT_SCOPE.organizationId,\n tenantId: params.tenantId ?? DEFAULT_SCOPE.tenantId,\n }\n\n for (const [field, rawValue] of Object.entries(params.doc)) {\n if (!shouldIndexField(field, rawValue, config, params.entityType)) continue\n const values = collectTextValues(rawValue)\n const seen = new Set<string>()\n for (const text of values) {\n const { tokens: textTokens, hashes } = tokenizeText(text, config)\n for (let i = 0; i < textTokens.length; i += 1) {\n const token = textTokens[i]\n const hash = hashes[i]\n const dedupeKey = `${field}|${hash}`\n if (seen.has(dedupeKey)) continue\n seen.add(dedupeKey)\n debug('token.generated', { entityType: params.entityType, recordId: params.recordId, field, hash })\n tokens.push({\n entity_type: params.entityType,\n entity_id: String(params.recordId),\n organization_id: scope.organizationId,\n tenant_id: scope.tenantId,\n field,\n token_hash: hash,\n token: config.storeRawTokens ? token : null,\n })\n if (capturePairs) {\n debugPairs.push({ field, hash })\n }\n }\n }\n }\n if (capturePairs) {\n debug('deal.tokens', {\n entityType: params.entityType,\n recordId: params.recordId,\n tokenCount: debugPairs.length,\n tokens: debugPairs,\n })\n }\n debug('doc.completed', { entityType: params.entityType, recordId: params.recordId, tokenCount: tokens.length })\n\n return tokens\n}\n\nfunction buildFieldPairs(recordId: string, doc?: Record<string, unknown> | null): EntityFieldPair[] {\n if (!doc) return []\n const pairs: EntityFieldPair[] = []\n const dedupe = new Set<string>()\n for (const field of Object.keys(doc)) {\n const key = `${recordId}|${field}`\n if (dedupe.has(key)) continue\n dedupe.add(key)\n pairs.push([recordId, field])\n }\n return pairs\n}\n\nexport async function replaceSearchTokensForRecord(\n db: Kysely<any>,\n params: BuildTokenOptions
|
|
5
|
-
"mappings": "AAAA,
|
|
4
|
+
"sourcesContent": ["import { type Kysely, type Transaction, sql } from 'kysely'\nimport {\n isSearchFieldBlocklisted,\n resolveSearchConfig,\n type SearchConfig,\n} from '@open-mercato/shared/lib/search/config'\nimport { tokenizeText } from '@open-mercato/shared/lib/search/tokenize'\nimport { parseBooleanToken } from '@open-mercato/shared/lib/boolean'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst logger = createLogger('query_index').child({ component: 'search-tokens' })\n\nconst INSERT_BATCH_SIZE = 500\n\nfunction chunk<T>(items: T[], size: number): T[][] {\n if (size <= 0) return [items]\n const out: T[][] = []\n for (let i = 0; i < items.length; i += size) out.push(items.slice(i, i + size))\n return out\n}\n\nexport type SearchTokenRow = {\n entity_type: string\n entity_id: string\n organization_id: string | null\n tenant_id: string | null\n field: string\n token_hash: string\n token?: string | null\n}\n\ntype BuildTokenOptions = {\n entityType: string\n recordId: string\n organizationId?: string | null\n tenantId?: string | null\n doc?: Record<string, unknown> | null\n config?: SearchConfig\n}\n\nconst DEFAULT_SCOPE = { organizationId: null, tenantId: null }\ntype EntityFieldPair = [string, string]\ntype SearchTokenExecutor = Kysely<any> | Transaction<any>\n\nexport const isSearchDebugEnabled = (): boolean => {\n return parseBooleanToken(process.env.OM_SEARCH_DEBUG ?? '') === true\n}\n\nconst debug = (event: string, payload: Record<string, unknown>) => {\n if (!isSearchDebugEnabled()) return\n try {\n logger.debug('Search token event', { event, payload })\n } catch {\n // ignore\n }\n}\n\nfunction collectTextValues(value: unknown): string[] {\n if (typeof value === 'string') return [value]\n if (Array.isArray(value)) {\n const out: string[] = []\n for (const entry of value) {\n if (typeof entry === 'string') out.push(entry)\n }\n return out\n }\n return []\n}\n\nfunction shouldIndexField(\n field: string,\n value: unknown,\n config: SearchConfig,\n entityType: string | null,\n): boolean {\n if (typeof value !== 'string' && !Array.isArray(value)) return false\n const lower = field.toLowerCase()\n if (lower === 'id' || lower.endsWith('_id') || lower.endsWith('.id')) return false\n if (lower.endsWith('_at')) return false\n if (['created_at', 'updated_at', 'deleted_at', 'tenant_id', 'organization_id'].includes(lower)) return false\n if (isSearchFieldBlocklisted(field, entityType, config)) return false\n const values = collectTextValues(value)\n if (!values.length) return false\n return values.some((text) => tokenizeText(text, config).tokens.length > 0)\n}\n\nexport function buildSearchTokenRows(params: BuildTokenOptions): SearchTokenRow[] {\n const config = params.config ?? resolveSearchConfig()\n if (!config.enabled) return []\n if (!params.doc) return []\n const tokens: SearchTokenRow[] = []\n const capturePairs = isSearchDebugEnabled() && params.entityType === 'customers:customer_deal'\n const debugPairs: Array<{ field: string; hash: string }> = []\n const scope = {\n organizationId: params.organizationId ?? DEFAULT_SCOPE.organizationId,\n tenantId: params.tenantId ?? DEFAULT_SCOPE.tenantId,\n }\n\n for (const [field, rawValue] of Object.entries(params.doc)) {\n if (!shouldIndexField(field, rawValue, config, params.entityType)) continue\n const values = collectTextValues(rawValue)\n const seen = new Set<string>()\n for (const text of values) {\n const { tokens: textTokens, hashes } = tokenizeText(text, config)\n for (let i = 0; i < textTokens.length; i += 1) {\n const token = textTokens[i]\n const hash = hashes[i]\n const dedupeKey = `${field}|${hash}`\n if (seen.has(dedupeKey)) continue\n seen.add(dedupeKey)\n debug('token.generated', { entityType: params.entityType, recordId: params.recordId, field, hash })\n tokens.push({\n entity_type: params.entityType,\n entity_id: String(params.recordId),\n organization_id: scope.organizationId,\n tenant_id: scope.tenantId,\n field,\n token_hash: hash,\n token: config.storeRawTokens ? token : null,\n })\n if (capturePairs) {\n debugPairs.push({ field, hash })\n }\n }\n }\n }\n if (capturePairs) {\n debug('deal.tokens', {\n entityType: params.entityType,\n recordId: params.recordId,\n tokenCount: debugPairs.length,\n tokens: debugPairs,\n })\n }\n debug('doc.completed', { entityType: params.entityType, recordId: params.recordId, tokenCount: tokens.length })\n\n return tokens\n}\n\nfunction buildFieldPairs(recordId: string, doc?: Record<string, unknown> | null): EntityFieldPair[] {\n if (!doc) return []\n const pairs: EntityFieldPair[] = []\n const dedupe = new Set<string>()\n for (const field of Object.keys(doc)) {\n const key = `${recordId}|${field}`\n if (dedupe.has(key)) continue\n dedupe.add(key)\n pairs.push([recordId, field])\n }\n return pairs\n}\n\nexport async function replaceSearchTokensForRecord(\n db: Kysely<any>,\n params: BuildTokenOptions,\n options?: { trx?: SearchTokenExecutor },\n): Promise<void> {\n const rows = buildSearchTokenRows(params)\n const config = params.config ?? resolveSearchConfig()\n if (!config.enabled) return\n const organizationId = params.organizationId ?? null\n const tenantId = params.tenantId ?? null\n const fieldPairs = buildFieldPairs(String(params.recordId), params.doc)\n\n const writeTokens = async (executor: SearchTokenExecutor): Promise<void> => {\n let deleteQuery = executor\n .deleteFrom('search_tokens' as any)\n .where('entity_type' as any, '=', params.entityType)\n .where(sql<boolean>`organization_id is not distinct from ${organizationId}`)\n .where(sql<boolean>`tenant_id is not distinct from ${tenantId}`)\n if (fieldPairs.length) {\n deleteQuery = deleteQuery.where((eb: any) => eb.or(\n fieldPairs.map(([rid, field]) => eb.and([\n eb('entity_id' as any, '=', rid),\n eb('field' as any, '=', field),\n ])),\n ))\n } else {\n deleteQuery = deleteQuery.where('entity_id' as any, '=', String(params.recordId))\n }\n await deleteQuery.execute()\n if (!rows.length) return\n const payloads = rows.map((row) => ({ ...row, created_at: sql`now()` }))\n for (const batch of chunk(payloads, INSERT_BATCH_SIZE)) {\n await executor.insertInto('search_tokens' as any).values(batch as any).execute()\n }\n }\n\n if (options?.trx) {\n await writeTokens(options.trx)\n return\n }\n\n await db.transaction().execute(writeTokens)\n}\n\nexport async function deleteSearchTokensForRecord(\n db: Kysely<any>,\n params: { entityType: string; recordId: string; organizationId?: string | null; tenantId?: string | null },\n options?: { trx?: SearchTokenExecutor },\n): Promise<void> {\n const organizationId = params.organizationId ?? null\n const tenantId = params.tenantId ?? null\n const executor = options?.trx ?? db\n await executor\n .deleteFrom('search_tokens' as any)\n .where('entity_type' as any, '=', params.entityType)\n .where('entity_id' as any, '=', String(params.recordId))\n .where(sql<boolean>`organization_id is not distinct from ${organizationId}`)\n .where(sql<boolean>`tenant_id is not distinct from ${tenantId}`)\n .execute()\n}\n\nexport async function replaceSearchTokensForBatch(\n db: Kysely<any>,\n payloads: Array<BuildTokenOptions & { doc: Record<string, unknown> }>\n): Promise<void> {\n if (!payloads.length) return\n const config = resolveSearchConfig()\n if (!config.enabled) return\n\n const rows = payloads.flatMap((payload) => buildSearchTokenRows({ ...payload, config }))\n if (!rows.length) {\n const entityType = payloads[0]?.entityType\n if (!entityType) return\n const ids = payloads.map((p) => String(p.recordId))\n await db\n .deleteFrom('search_tokens' as any)\n .where('entity_type' as any, '=', entityType)\n .where('entity_id' as any, 'in', ids)\n .execute()\n return\n }\n\n const scopeKey = (org: string | null, tenant: string | null) => `${org ?? '__null__'}|${tenant ?? '__null__'}`\n const scopeBuckets = new Map<string, { organizationId: string | null; tenantId: string | null; ids: Set<string> }>()\n\n for (const payload of payloads) {\n const org = payload.organizationId ?? null\n const tenant = payload.tenantId ?? null\n const key = scopeKey(org, tenant)\n const bucket = scopeBuckets.get(key) ?? { organizationId: org, tenantId: tenant, ids: new Set<string>() }\n bucket.ids.add(String(payload.recordId))\n scopeBuckets.set(key, bucket)\n }\n\n await db.transaction().execute(async (trx) => {\n for (const [, bucket] of scopeBuckets.entries()) {\n // Delete by entity_id: a batch replaces all of a record's tokens, and a per-field OR over the\n // whole batch overflows the query compiler's call stack on large batches.\n const deleteQuery = trx\n .deleteFrom('search_tokens' as any)\n .where('entity_type' as any, '=', payloads[0].entityType)\n .where(sql<boolean>`organization_id is not distinct from ${bucket.organizationId}`)\n .where(sql<boolean>`tenant_id is not distinct from ${bucket.tenantId}`)\n .where('entity_id' as any, 'in', Array.from(bucket.ids))\n await deleteQuery.execute()\n }\n const payloadWithTimestamps = rows.map((row) => ({ ...row, created_at: sql`now()` }))\n for (const batch of chunk(payloadWithTimestamps, INSERT_BATCH_SIZE)) {\n await trx.insertInto('search_tokens' as any).values(batch as any).execute()\n }\n })\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAwC,WAAW;AACnD;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP,SAAS,oBAAoB;AAC7B,SAAS,yBAAyB;AAClC,SAAS,oBAAoB;AAE7B,MAAM,SAAS,aAAa,aAAa,EAAE,MAAM,EAAE,WAAW,gBAAgB,CAAC;AAE/E,MAAM,oBAAoB;AAE1B,SAAS,MAAS,OAAY,MAAqB;AACjD,MAAI,QAAQ,EAAG,QAAO,CAAC,KAAK;AAC5B,QAAM,MAAa,CAAC;AACpB,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,KAAM,KAAI,KAAK,MAAM,MAAM,GAAG,IAAI,IAAI,CAAC;AAC9E,SAAO;AACT;AAqBA,MAAM,gBAAgB,EAAE,gBAAgB,MAAM,UAAU,KAAK;AAItD,MAAM,uBAAuB,MAAe;AACjD,SAAO,kBAAkB,QAAQ,IAAI,mBAAmB,EAAE,MAAM;AAClE;AAEA,MAAM,QAAQ,CAAC,OAAe,YAAqC;AACjE,MAAI,CAAC,qBAAqB,EAAG;AAC7B,MAAI;AACF,WAAO,MAAM,sBAAsB,EAAE,OAAO,QAAQ,CAAC;AAAA,EACvD,QAAQ;AAAA,EAER;AACF;AAEA,SAAS,kBAAkB,OAA0B;AACnD,MAAI,OAAO,UAAU,SAAU,QAAO,CAAC,KAAK;AAC5C,MAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,UAAM,MAAgB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C;AACvC,iBAAa,IAAI,KAAK,MAAM;AAAA,EAC9B;AAEA,QAAM,GAAG,YAAY,EAAE,QAAQ,OAAO,QAAQ;AAC5C,eAAW,CAAC,EAAE,MAAM,KAAK,aAAa,QAAQ,GAAG;AAG/C,YAAM,cAAc,IACjB,WAAW,eAAsB,EACjC,MAAM,eAAsB,KAAK,SAAS,CAAC,EAAE,UAAU,EACvD,MAAM,2CAAoD,OAAO,cAAc,EAAE,EACjF,MAAM,qCAA8C,OAAO,QAAQ,EAAE,EACrE,MAAM,aAAoB,MAAM,MAAM,KAAK,OAAO,GAAG,CAAC;AACzD,YAAM,YAAY,QAAQ;AAAA,IAC5B;AACA,UAAM,wBAAwB,KAAK,IAAI,CAAC,SAAS,EAAE,GAAG,KAAK,YAAY,WAAW,EAAE;AACpF,eAAW,SAAS,MAAM,uBAAuB,iBAAiB,GAAG;AACnE,YAAM,IAAI,WAAW,eAAsB,EAAE,OAAO,KAAY,EAAE,QAAQ;AAAA,IAC5E;AAAA,EACF,CAAC;AACH;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { recordIndexerError } from "@open-mercato/shared/lib/indexers/error-log";
|
|
2
|
+
import { isReadProjectionAlwaysConsistent } from "@open-mercato/shared/lib/data/consistency";
|
|
2
3
|
import { refreshCoverageSnapshot } from "../lib/coverage.js";
|
|
3
4
|
import { createLogger } from "@open-mercato/shared/lib/logger";
|
|
4
5
|
const logger = createLogger("query_index").child({ component: "coverage-refresh" });
|
|
@@ -51,6 +52,9 @@ async function handle(payload, ctx) {
|
|
|
51
52
|
payload
|
|
52
53
|
}
|
|
53
54
|
);
|
|
55
|
+
if (isReadProjectionAlwaysConsistent()) {
|
|
56
|
+
throw err;
|
|
57
|
+
}
|
|
54
58
|
}
|
|
55
59
|
};
|
|
56
60
|
if (delayMs === 0) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/query_index/subscribers/coverage_refresh.ts"],
|
|
4
|
-
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { recordIndexerError } from '@open-mercato/shared/lib/indexers/error-log'\nimport { refreshCoverageSnapshot } from '../lib/coverage'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst logger = createLogger('query_index').child({ component: 'coverage-refresh' })\n\nexport const metadata = { event: 'query_index.coverage.refresh', persistent: false }\n\ntype Payload = {\n entityType?: string\n tenantId?: string | null\n organizationId?: string | null\n withDeleted?: boolean\n delayMs?: number\n}\n\nconst DEFAULT_DELAY_MS = 0\nconst pending = new Map<string, NodeJS.Timeout>()\n\nfunction forkRefreshEntityManager(em: EntityManager): EntityManager {\n const fork = (em as unknown as { fork?: (options?: Record<string, unknown>) => EntityManager }).fork\n if (typeof fork !== 'function') return em\n return fork.call(em, { clear: true, freshEventManager: true, useContext: false })\n}\n\nfunction scopeKey(input: Payload): string {\n const entity = String(input.entityType || '')\n const tenant = input.tenantId ?? '__null__'\n const org = input.organizationId ?? '__null__'\n const deleted = input.withDeleted ? '1' : '0'\n\n return `${entity}|${tenant}|${org}|${deleted}`\n}\n\nexport default async function handle(payload: Payload, ctx: { resolve: <T = any>(name: string) => T }) {\n const entityType = String(payload?.entityType || '')\n if (!entityType) {\n return\n }\n\n const tenantId = payload?.tenantId ?? null\n const organizationId = payload?.organizationId ?? null\n const withDeleted = payload?.withDeleted === true\n const delayMs = typeof payload?.delayMs === 'number' && payload.delayMs >= 0 ? payload.delayMs : DEFAULT_DELAY_MS\n\n const key = scopeKey({ entityType, tenantId, organizationId, withDeleted })\n\n const handleRefresh = async () => {\n const em = forkRefreshEntityManager(ctx.resolve<EntityManager>('em'))\n try {\n await refreshCoverageSnapshot(em, { entityType, tenantId, organizationId, withDeleted })\n } catch (err) {\n logger.warn('Failed to refresh coverage snapshot', {\n entityType,\n tenantId,\n organizationId,\n withDeleted,\n error: err instanceof Error ? err.message : err,\n })\n await recordIndexerError(\n { em },\n {\n source: 'query_index',\n handler: 'event:query_index.coverage.refresh',\n error: err,\n entityType,\n tenantId,\n organizationId,\n payload,\n },\n )\n }\n }\n\n if (delayMs === 0) {\n await handleRefresh()\n return\n }\n\n const existing = pending.get(key)\n if (existing) {\n clearTimeout(existing)\n }\n\n const timer = setTimeout(() => {\n pending.delete(key)\n void handleRefresh()\n }, delayMs)\n if (typeof timer.unref === 'function') {\n timer.unref()\n }\n pending.set(key, timer)\n}\n"],
|
|
5
|
-
"mappings": "AACA,SAAS,0BAA0B;AACnC,SAAS,+BAA+B;AACxC,SAAS,oBAAoB;AAE7B,MAAM,SAAS,aAAa,aAAa,EAAE,MAAM,EAAE,WAAW,mBAAmB,CAAC;AAE3E,MAAM,WAAW,EAAE,OAAO,gCAAgC,YAAY,MAAM;AAUnF,MAAM,mBAAmB;AACzB,MAAM,UAAU,oBAAI,IAA4B;AAEhD,SAAS,yBAAyB,IAAkC;AAClE,QAAM,OAAQ,GAAkF;AAChG,MAAI,OAAO,SAAS,WAAY,QAAO;AACvC,SAAO,KAAK,KAAK,IAAI,EAAE,OAAO,MAAM,mBAAmB,MAAM,YAAY,MAAM,CAAC;AAClF;AAEA,SAAS,SAAS,OAAwB;AACxC,QAAM,SAAS,OAAO,MAAM,cAAc,EAAE;AAC5C,QAAM,SAAS,MAAM,YAAY;AACjC,QAAM,MAAM,MAAM,kBAAkB;AACpC,QAAM,UAAU,MAAM,cAAc,MAAM;AAE1C,SAAO,GAAG,MAAM,IAAI,MAAM,IAAI,GAAG,IAAI,OAAO;AAC9C;AAEA,eAAO,OAA8B,SAAkB,KAAgD;AACrG,QAAM,aAAa,OAAO,SAAS,cAAc,EAAE;AACnD,MAAI,CAAC,YAAY;AACf;AAAA,EACF;AAEA,QAAM,WAAW,SAAS,YAAY;AACtC,QAAM,iBAAiB,SAAS,kBAAkB;AAClD,QAAM,cAAc,SAAS,gBAAgB;AAC7C,QAAM,UAAU,OAAO,SAAS,YAAY,YAAY,QAAQ,WAAW,IAAI,QAAQ,UAAU;AAEjG,QAAM,MAAM,SAAS,EAAE,YAAY,UAAU,gBAAgB,YAAY,CAAC;AAE1E,QAAM,gBAAgB,YAAY;AAChC,UAAM,KAAK,yBAAyB,IAAI,QAAuB,IAAI,CAAC;AACpE,QAAI;AACF,YAAM,wBAAwB,IAAI,EAAE,YAAY,UAAU,gBAAgB,YAAY,CAAC;AAAA,IACzF,SAAS,KAAK;AACZ,aAAO,KAAK,uCAAuC;AAAA,QACjD;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA,OAAO,eAAe,QAAQ,IAAI,UAAU;AAAA,MAC9C,CAAC;AACD,YAAM;AAAA,QACJ,EAAE,GAAG;AAAA,QACL;AAAA,UACE,QAAQ;AAAA,UACR,SAAS;AAAA,UACT,OAAO;AAAA,UACP;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,MAAI,YAAY,GAAG;AACjB,UAAM,cAAc;AACpB;AAAA,EACF;AAEA,QAAM,WAAW,QAAQ,IAAI,GAAG;AAChC,MAAI,UAAU;AACZ,iBAAa,QAAQ;AAAA,EACvB;AAEA,QAAM,QAAQ,WAAW,MAAM;AAC7B,YAAQ,OAAO,GAAG;AAClB,SAAK,cAAc;AAAA,EACrB,GAAG,OAAO;AACV,MAAI,OAAO,MAAM,UAAU,YAAY;AACrC,UAAM,MAAM;AAAA,EACd;AACA,UAAQ,IAAI,KAAK,KAAK;AACxB;",
|
|
4
|
+
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { recordIndexerError } from '@open-mercato/shared/lib/indexers/error-log'\nimport { isReadProjectionAlwaysConsistent } from '@open-mercato/shared/lib/data/consistency'\nimport { refreshCoverageSnapshot } from '../lib/coverage'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst logger = createLogger('query_index').child({ component: 'coverage-refresh' })\n\nexport const metadata = { event: 'query_index.coverage.refresh', persistent: false }\n\ntype Payload = {\n entityType?: string\n tenantId?: string | null\n organizationId?: string | null\n withDeleted?: boolean\n delayMs?: number\n}\n\nconst DEFAULT_DELAY_MS = 0\nconst pending = new Map<string, NodeJS.Timeout>()\n\nfunction forkRefreshEntityManager(em: EntityManager): EntityManager {\n const fork = (em as unknown as { fork?: (options?: Record<string, unknown>) => EntityManager }).fork\n if (typeof fork !== 'function') return em\n return fork.call(em, { clear: true, freshEventManager: true, useContext: false })\n}\n\nfunction scopeKey(input: Payload): string {\n const entity = String(input.entityType || '')\n const tenant = input.tenantId ?? '__null__'\n const org = input.organizationId ?? '__null__'\n const deleted = input.withDeleted ? '1' : '0'\n\n return `${entity}|${tenant}|${org}|${deleted}`\n}\n\nexport default async function handle(payload: Payload, ctx: { resolve: <T = any>(name: string) => T }) {\n const entityType = String(payload?.entityType || '')\n if (!entityType) {\n return\n }\n\n const tenantId = payload?.tenantId ?? null\n const organizationId = payload?.organizationId ?? null\n const withDeleted = payload?.withDeleted === true\n const delayMs = typeof payload?.delayMs === 'number' && payload.delayMs >= 0 ? payload.delayMs : DEFAULT_DELAY_MS\n\n const key = scopeKey({ entityType, tenantId, organizationId, withDeleted })\n\n const handleRefresh = async () => {\n const em = forkRefreshEntityManager(ctx.resolve<EntityManager>('em'))\n try {\n await refreshCoverageSnapshot(em, { entityType, tenantId, organizationId, withDeleted })\n } catch (err) {\n logger.warn('Failed to refresh coverage snapshot', {\n entityType,\n tenantId,\n organizationId,\n withDeleted,\n error: err instanceof Error ? err.message : err,\n })\n await recordIndexerError(\n { em },\n {\n source: 'query_index',\n handler: 'event:query_index.coverage.refresh',\n error: err,\n entityType,\n tenantId,\n organizationId,\n payload,\n },\n )\n if (isReadProjectionAlwaysConsistent()) {\n throw err\n }\n }\n }\n\n if (delayMs === 0) {\n await handleRefresh()\n return\n }\n\n const existing = pending.get(key)\n if (existing) {\n clearTimeout(existing)\n }\n\n const timer = setTimeout(() => {\n pending.delete(key)\n void handleRefresh()\n },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AY,UAAU,gBAAgB,YAAY,CAAC;AAAA,IACzF,SAAS,KAAK;AACZ,aAAO,KAAK,uCAAuC;AAAA,QACjD;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA,OAAO,eAAe,QAAQ,IAAI,UAAU;AAAA,MAC9C,CAAC;AACD,YAAM;AAAA,QACJ,EAAE,GAAG;AAAA,QACL;AAAA,UACE,QAAQ;AAAA,UACR,SAAS;AAAA,UACT,OAAO;AAAA,UACP;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAAA,MACF;AACA,UAAI,iCAAiC,GAAG;AACtC,cAAM;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,MAAI,YAAY,GAAG;AACjB,UAAM,cAAc;AACpB;AAAA,EACF;AAEA,QAAM,WAAW,QAAQ,IAAI,GAAG;AAChC,MAAI,UAAU;AACZ,iBAAa,QAAQ;AAAA,EACvB;AAEA,QAAM,QAAQ,WAAW,MAAM;AAC7B,YAAQ,OAAO,GAAG;AAClB,SAAK,cAAc;AAAA,EACrB,GAAG,OAAO;AACV,MAAI,OAAO,MAAM,UAAU,YAAY;AACrC,UAAM,MAAM;AAAA,EACd;AACA,UAAQ,IAAI,KAAK,KAAK;AACxB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { recordIndexerError } from "@open-mercato/shared/lib/indexers/error-log";
|
|
2
|
+
import { isReadProjectionAlwaysConsistent } from "@open-mercato/shared/lib/data/consistency";
|
|
2
3
|
import { sql } from "kysely";
|
|
3
4
|
import { markDeleted } from "../lib/indexer.js";
|
|
4
5
|
import { applyCoverageAdjustments, createCoverageAdjustments } from "../lib/coverage.js";
|
|
@@ -27,7 +28,9 @@ async function handle(payload, ctx) {
|
|
|
27
28
|
if (!entityType || !recordId) return;
|
|
28
29
|
let organizationId = payload?.organizationId ?? null;
|
|
29
30
|
let tenantId = payload?.tenantId ?? null;
|
|
31
|
+
const suppressCoverage = payload?.suppressCoverage === true;
|
|
30
32
|
const coverageDelayMs = typeof payload?.coverageDelayMs === "number" ? payload.coverageDelayMs : void 0;
|
|
33
|
+
const alwaysConsistent = isReadProjectionAlwaysConsistent();
|
|
31
34
|
try {
|
|
32
35
|
const hasPayloadOrganizationId = Object.prototype.hasOwnProperty.call(payload ?? {}, "organizationId");
|
|
33
36
|
const hasPayloadTenantId = Object.prototype.hasOwnProperty.call(payload ?? {}, "tenantId");
|
|
@@ -42,6 +45,56 @@ async function handle(payload, ctx) {
|
|
|
42
45
|
});
|
|
43
46
|
organizationId = resolvedScope.organizationId;
|
|
44
47
|
tenantId = resolvedScope.tenantId;
|
|
48
|
+
if (alwaysConsistent) {
|
|
49
|
+
const db = em.getKysely();
|
|
50
|
+
await db.transaction().execute(async (trx) => {
|
|
51
|
+
const { wasActive: wasActive2 } = await markDeleted(em, { entityType, recordId, organizationId, tenantId, trx });
|
|
52
|
+
let baseQuery = trx.selectFrom(source.table).select(["deleted_at"]).where("id", "=", recordId);
|
|
53
|
+
if (source.organizationColumn) {
|
|
54
|
+
baseQuery = baseQuery.where(
|
|
55
|
+
source.organizationColumn,
|
|
56
|
+
organizationId === null ? "is" : "=",
|
|
57
|
+
organizationId
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
if (source.tenantColumn) {
|
|
61
|
+
baseQuery = baseQuery.where(sql`${sql.ref(source.tenantColumn)} is not distinct from ${tenantId}`);
|
|
62
|
+
}
|
|
63
|
+
const row = await baseQuery.executeTakeFirst();
|
|
64
|
+
const baseDelta2 = !row || row.deleted_at != null ? -1 : 0;
|
|
65
|
+
const baseDeltaOverride2 = typeof payload?.coverageBaseDelta === "number" ? payload.coverageBaseDelta : void 0;
|
|
66
|
+
const indexDeltaOverride2 = typeof payload?.coverageIndexDelta === "number" ? payload.coverageIndexDelta : void 0;
|
|
67
|
+
let effectiveBaseDelta2 = baseDeltaOverride2 ?? baseDelta2;
|
|
68
|
+
let effectiveIndexDelta2 = indexDeltaOverride2 ?? (wasActive2 ? -1 : 0);
|
|
69
|
+
if (!Number.isFinite(effectiveBaseDelta2)) effectiveBaseDelta2 = 0;
|
|
70
|
+
if (!Number.isFinite(effectiveIndexDelta2)) effectiveIndexDelta2 = 0;
|
|
71
|
+
if (effectiveBaseDelta2 !== 0 || effectiveIndexDelta2 !== 0) {
|
|
72
|
+
const adjustments = createCoverageAdjustments({
|
|
73
|
+
entityType,
|
|
74
|
+
tenantId: tenantId ?? null,
|
|
75
|
+
organizationId: organizationId ?? null,
|
|
76
|
+
baseDelta: effectiveBaseDelta2,
|
|
77
|
+
indexDelta: effectiveIndexDelta2
|
|
78
|
+
});
|
|
79
|
+
if (adjustments.length) {
|
|
80
|
+
await applyCoverageAdjustments(em, adjustments, { trx });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
const bus = ctx.resolve("eventBus");
|
|
85
|
+
const shouldRefreshCoverage2 = !suppressCoverage && (coverageDelayMs === void 0 || coverageDelayMs >= 0);
|
|
86
|
+
const coverageRefreshDelay2 = coverageDelayMs ?? 0;
|
|
87
|
+
if (shouldRefreshCoverage2) {
|
|
88
|
+
await bus.emitEvent("query_index.coverage.refresh", {
|
|
89
|
+
entityType,
|
|
90
|
+
tenantId: tenantId ?? null,
|
|
91
|
+
organizationId: organizationId ?? null,
|
|
92
|
+
delayMs: coverageRefreshDelay2
|
|
93
|
+
}, { rethrowHandlerErrors: true });
|
|
94
|
+
}
|
|
95
|
+
await bus.emitEvent("search.delete_record", { entityId: entityType, recordId, organizationId, tenantId }, { rethrowHandlerErrors: true });
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
45
98
|
const { wasActive } = await markDeleted(em, { entityType, recordId, organizationId, tenantId });
|
|
46
99
|
let baseDelta = 0;
|
|
47
100
|
let baseCheckSucceeded = false;
|
|
@@ -84,7 +137,6 @@ async function handle(payload, ctx) {
|
|
|
84
137
|
await applyCoverageAdjustments(em, adjustments);
|
|
85
138
|
}
|
|
86
139
|
}
|
|
87
|
-
const suppressCoverage = payload?.suppressCoverage === true;
|
|
88
140
|
const explicitDelayRequested = typeof payload?.coverageDelayMs === "number";
|
|
89
141
|
const shouldRefreshCoverage = !suppressCoverage && (coverageDelayMs === void 0 || coverageDelayMs >= 0) && (explicitDelayRequested || shouldAllowDeleteCoverageRefresh(entityType, tenantId));
|
|
90
142
|
const coverageRefreshDelay = coverageDelayMs ?? 0;
|