@open-mercato/shared 0.10.1-develop.8004.1.a8a79ee43f → 0.10.1-develop.8024.1.02dbc16b49
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/dist/lib/data/engine.js +40 -44
- package/dist/lib/data/engine.js.map +2 -2
- package/dist/lib/db/recordScope.js +72 -0
- package/dist/lib/db/recordScope.js.map +7 -0
- package/dist/lib/version.js +1 -1
- package/dist/lib/version.js.map +1 -1
- package/package.json +2 -2
- package/src/lib/data/__tests__/engine.custom-entity-encryption-sort.integration.test.ts +4 -0
- package/src/lib/data/__tests__/engine.custom-entity-encryption.integration.test.ts +4 -0
- package/src/lib/data/__tests__/engine.custom-entity-encryption.test.ts +4 -0
- package/src/lib/data/__tests__/engine.custom-entity-require-active.test.ts +4 -0
- package/src/lib/data/__tests__/engine.custom-entity-storage-scope.test.ts +4 -0
- package/src/lib/data/__tests__/engine.custom-entity-storage-upsert.test.ts +138 -0
- package/src/lib/data/__tests__/engine.custom-entity-storage.pg.test.ts +152 -0
- package/src/lib/data/__tests__/engine.pin-organization-id.test.ts +4 -0
- package/src/lib/data/__tests__/engine.set-custom-fields-signal.test.ts +100 -0
- package/src/lib/data/__tests__/engine.table-exists-cache.test.ts +4 -0
- package/src/lib/data/engine.ts +63 -59
- package/src/lib/db/__tests__/recordScope.test.ts +116 -0
- package/src/lib/db/recordScope.ts +130 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
jest.mock('../../db/advisoryLock', () => ({
|
|
2
|
+
withAdvisoryXactLock: async <T>(em: unknown, _key: string, work: (txEm: unknown) => Promise<T>): Promise<T> => work(em),
|
|
3
|
+
}))
|
|
4
|
+
|
|
5
|
+
import fs from 'node:fs'
|
|
6
|
+
import path from 'node:path'
|
|
7
|
+
import { DefaultDataEngine, clearDataEngineTableExistsCache } from '../engine'
|
|
8
|
+
import { registerEntityIds } from '../../encryption/entityIds'
|
|
9
|
+
|
|
10
|
+
const ENTITY_ID = 'example:todo'
|
|
11
|
+
const RECORD_ID = '11111111-1111-4111-8111-111111111111'
|
|
12
|
+
const ORG_ID = '22222222-2222-4222-8222-222222222222'
|
|
13
|
+
const TENANT_ID = '33333333-3333-4333-8333-333333333333'
|
|
14
|
+
const MIGRATIONS_DIR = path.resolve(__dirname, '../../../../../core/src/modules/entities/migrations')
|
|
15
|
+
|
|
16
|
+
type SnapshotIndex = { keyName: string; unique: boolean; primary: boolean; expression?: string | null }
|
|
17
|
+
type SnapshotTable = { name: string; indexes: SnapshotIndex[] }
|
|
18
|
+
|
|
19
|
+
type Operation = 'table-check' | 'select' | 'insert' | 'update' | 'delete'
|
|
20
|
+
type QueryCall = { operation: Operation; scopes: Array<[string, string, unknown]>; onConflict: boolean }
|
|
21
|
+
|
|
22
|
+
function buildEngine(options: { updatedRows: number }) {
|
|
23
|
+
const queries: QueryCall[] = []
|
|
24
|
+
const buildQuery = (operation: Operation) => {
|
|
25
|
+
const query: QueryCall = { operation, scopes: [], onConflict: false }
|
|
26
|
+
queries.push(query)
|
|
27
|
+
const chain = {
|
|
28
|
+
select: () => chain,
|
|
29
|
+
values: () => chain,
|
|
30
|
+
set: () => chain,
|
|
31
|
+
where: (column: string, operator: string, value: unknown) => {
|
|
32
|
+
query.scopes.push([column, operator, value])
|
|
33
|
+
return chain
|
|
34
|
+
},
|
|
35
|
+
onConflict: () => {
|
|
36
|
+
query.onConflict = true
|
|
37
|
+
return chain
|
|
38
|
+
},
|
|
39
|
+
executeTakeFirst: async () => {
|
|
40
|
+
if (operation === 'table-check') return { present: 1 }
|
|
41
|
+
return { numUpdatedRows: BigInt(options.updatedRows) }
|
|
42
|
+
},
|
|
43
|
+
execute: async () => [],
|
|
44
|
+
}
|
|
45
|
+
return chain
|
|
46
|
+
}
|
|
47
|
+
const db = {
|
|
48
|
+
selectFrom: (table: string) => buildQuery(table === 'information_schema.tables' ? 'table-check' : 'select'),
|
|
49
|
+
insertInto: () => buildQuery('insert'),
|
|
50
|
+
updateTable: () => buildQuery('update'),
|
|
51
|
+
deleteFrom: () => buildQuery('delete'),
|
|
52
|
+
}
|
|
53
|
+
const em = {
|
|
54
|
+
getKysely: () => db,
|
|
55
|
+
getMetadata: () => ({ find: () => undefined, getAll: () => [] }),
|
|
56
|
+
find: async () => [],
|
|
57
|
+
persist: () => undefined,
|
|
58
|
+
flush: async () => undefined,
|
|
59
|
+
}
|
|
60
|
+
return { engine: new DefaultDataEngine(em as never, {} as never), queries }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function readStorageTableSnapshot(): SnapshotTable {
|
|
64
|
+
const snapshot = JSON.parse(fs.readFileSync(path.join(MIGRATIONS_DIR, '.snapshot-open-mercato.json'), 'utf8')) as { tables: SnapshotTable[] }
|
|
65
|
+
const table = snapshot.tables.find((entry) => entry.name === 'custom_entities_storage')
|
|
66
|
+
if (!table) throw new Error('[internal] custom_entities_storage missing from entities snapshot')
|
|
67
|
+
return table
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function readMigrationSql(): string {
|
|
71
|
+
return fs
|
|
72
|
+
.readdirSync(MIGRATIONS_DIR)
|
|
73
|
+
.filter((file) => /^Migration.*\.ts$/.test(file))
|
|
74
|
+
.map((file) => fs.readFileSync(path.join(MIGRATIONS_DIR, file), 'utf8'))
|
|
75
|
+
.join('\n')
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
describe('custom_entities_storage write strategy is pinned to its index definition', () => {
|
|
79
|
+
beforeEach(() => {
|
|
80
|
+
registerEntityIds({ example: { todo: ENTITY_ID } })
|
|
81
|
+
clearDataEngineTableExistsCache()
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
afterEach(() => {
|
|
85
|
+
registerEntityIds({})
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('the table has no unique arbiter index an ON CONFLICT target could match', () => {
|
|
89
|
+
const table = readStorageTableSnapshot()
|
|
90
|
+
const uniqueNonPrimary = table.indexes.filter((index) => index.unique && !index.primary)
|
|
91
|
+
expect(uniqueNonPrimary).toEqual([])
|
|
92
|
+
expect(table.indexes.find((index) => index.keyName === 'custom_entities_storage_unique_idx')?.unique).toBe(false)
|
|
93
|
+
|
|
94
|
+
const sql = readMigrationSql()
|
|
95
|
+
expect(sql).toContain('create index "custom_entities_storage_unique_idx" on "custom_entities_storage" ("entity_type", "entity_id", "organization_id");')
|
|
96
|
+
expect(sql).not.toMatch(/create unique index[^;]*on "custom_entities_storage"/i)
|
|
97
|
+
expect(sql).not.toMatch(/alter table "custom_entities_storage"[^;]*add constraint[^;]*unique/i)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test.each([
|
|
101
|
+
['organization + tenant scope', ORG_ID, TENANT_ID],
|
|
102
|
+
['global scope', null, null],
|
|
103
|
+
] as const)('create never issues ON CONFLICT and updates in place when the row exists (%s)', async (_label, organizationId, tenantId) => {
|
|
104
|
+
const { engine, queries } = buildEngine({ updatedRows: 1 })
|
|
105
|
+
|
|
106
|
+
await engine.createCustomEntityRecord({ entityId: ENTITY_ID, recordId: RECORD_ID, organizationId, tenantId, values: {} })
|
|
107
|
+
|
|
108
|
+
expect(queries.some((query) => query.onConflict)).toBe(false)
|
|
109
|
+
expect(queries.filter((query) => query.operation === 'insert')).toHaveLength(0)
|
|
110
|
+
const update = queries.find((query) => query.operation === 'update')!
|
|
111
|
+
expect(update.scopes).toEqual([
|
|
112
|
+
['entity_type', '=', ENTITY_ID],
|
|
113
|
+
['entity_id', '=', RECORD_ID],
|
|
114
|
+
organizationId === null ? ['organization_id', 'is', null] : ['organization_id', '=', organizationId],
|
|
115
|
+
tenantId === null ? ['tenant_id', 'is', null] : ['tenant_id', '=', tenantId],
|
|
116
|
+
])
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
test('create inserts exactly once when no row matches the scoped update', async () => {
|
|
120
|
+
const { engine, queries } = buildEngine({ updatedRows: 0 })
|
|
121
|
+
|
|
122
|
+
await engine.createCustomEntityRecord({ entityId: ENTITY_ID, recordId: RECORD_ID, organizationId: ORG_ID, tenantId: TENANT_ID, values: {} })
|
|
123
|
+
|
|
124
|
+
expect(queries.some((query) => query.onConflict)).toBe(false)
|
|
125
|
+
const writes = queries.filter((query) => query.operation === 'update' || query.operation === 'insert').map((query) => query.operation)
|
|
126
|
+
expect(writes).toEqual(['update', 'insert'])
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
test('update shares the same scoped update-then-insert write', async () => {
|
|
130
|
+
const { engine, queries } = buildEngine({ updatedRows: 0 })
|
|
131
|
+
|
|
132
|
+
await engine.updateCustomEntityRecord({ entityId: ENTITY_ID, recordId: RECORD_ID, organizationId: ORG_ID, tenantId: TENANT_ID, values: {} })
|
|
133
|
+
|
|
134
|
+
expect(queries.some((query) => query.onConflict)).toBe(false)
|
|
135
|
+
const writes = queries.filter((query) => query.operation === 'update' || query.operation === 'insert').map((query) => query.operation)
|
|
136
|
+
expect(writes).toEqual(['update', 'insert'])
|
|
137
|
+
})
|
|
138
|
+
})
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
import { EntitySchema, MikroORM } from '@mikro-orm/core'
|
|
3
|
+
import { PostgreSqlDriver, type EntityManager } from '@mikro-orm/postgresql'
|
|
4
|
+
import { Pool } from 'pg'
|
|
5
|
+
import { DefaultDataEngine, clearDataEngineTableExistsCache } from '../engine'
|
|
6
|
+
|
|
7
|
+
const databaseUrl = process.env.OM_CUSTOM_ENTITY_STORAGE_PG_URL
|
|
8
|
+
const describePostgres = databaseUrl ? describe : describe.skip
|
|
9
|
+
const entityId = 'user:storage_contention'
|
|
10
|
+
const Probe = new EntitySchema<{ id: string }>({
|
|
11
|
+
name: 'StorageContentionProbe',
|
|
12
|
+
properties: { id: { type: 'uuid', primary: true } },
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
describePostgres('custom entity storage writes against PostgreSQL', () => {
|
|
16
|
+
let orm: MikroORM<PostgreSqlDriver, EntityManager<PostgreSqlDriver>>
|
|
17
|
+
let admin: Pool
|
|
18
|
+
let schema: string
|
|
19
|
+
const previousEncryption = process.env.TENANT_DATA_ENCRYPTION
|
|
20
|
+
const previousBackcompat = process.env.ENTITIES_BACKCOMPAT_EAV_FOR_CUSTOM
|
|
21
|
+
|
|
22
|
+
beforeAll(async () => {
|
|
23
|
+
schema = `storage_contention_${randomUUID().replaceAll('-', '')}`
|
|
24
|
+
admin = new Pool({ connectionString: databaseUrl })
|
|
25
|
+
await admin.query(`create schema "${schema}"`)
|
|
26
|
+
orm = await MikroORM.init<PostgreSqlDriver, EntityManager<PostgreSqlDriver>>({
|
|
27
|
+
driver: PostgreSqlDriver,
|
|
28
|
+
clientUrl: databaseUrl,
|
|
29
|
+
entities: [Probe],
|
|
30
|
+
driverOptions: { options: `-c search_path=${schema},public` },
|
|
31
|
+
pool: { min: 0, max: 5 },
|
|
32
|
+
})
|
|
33
|
+
const em = orm.em.fork()
|
|
34
|
+
await em.execute(`create table custom_entities_storage (
|
|
35
|
+
id uuid primary key default gen_random_uuid(),
|
|
36
|
+
entity_type text not null, entity_id uuid not null,
|
|
37
|
+
organization_id uuid, tenant_id uuid, doc jsonb not null,
|
|
38
|
+
created_at timestamptz not null, updated_at timestamptz not null, deleted_at timestamptz
|
|
39
|
+
)`)
|
|
40
|
+
await em.execute(`create function delay_storage_insert() returns trigger language plpgsql as $$
|
|
41
|
+
begin perform pg_sleep(0.15); return new; end $$`)
|
|
42
|
+
await em.execute(`create trigger delay_storage_insert before insert on custom_entities_storage
|
|
43
|
+
for each row execute function delay_storage_insert()`)
|
|
44
|
+
process.env.TENANT_DATA_ENCRYPTION = 'no'
|
|
45
|
+
process.env.ENTITIES_BACKCOMPAT_EAV_FOR_CUSTOM = 'no'
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
afterAll(async () => {
|
|
49
|
+
if (previousEncryption === undefined) delete process.env.TENANT_DATA_ENCRYPTION
|
|
50
|
+
else process.env.TENANT_DATA_ENCRYPTION = previousEncryption
|
|
51
|
+
if (previousBackcompat === undefined) delete process.env.ENTITIES_BACKCOMPAT_EAV_FOR_CUSTOM
|
|
52
|
+
else process.env.ENTITIES_BACKCOMPAT_EAV_FOR_CUSTOM = previousBackcompat
|
|
53
|
+
await orm?.close(true)
|
|
54
|
+
if (admin) {
|
|
55
|
+
await admin.query(`drop schema if exists "${schema}" cascade`)
|
|
56
|
+
await admin.end()
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
function engine(): DefaultDataEngine {
|
|
61
|
+
const em = orm.em.fork()
|
|
62
|
+
jest.spyOn(em, 'find').mockResolvedValue([])
|
|
63
|
+
return new DefaultDataEngine(em, {} as never)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
beforeEach(() => clearDataEngineTableExistsCache())
|
|
67
|
+
|
|
68
|
+
test.each([
|
|
69
|
+
['global', null, null],
|
|
70
|
+
['tenant-only', randomUUID(), null],
|
|
71
|
+
['organization', randomUUID(), randomUUID()],
|
|
72
|
+
] as const)('serializes upserts, missing-record updates and create-only contenders in %s scope', async (_label, tenantId, organizationId) => {
|
|
73
|
+
for (const operation of ['upsert', 'update', 'mixed'] as const) {
|
|
74
|
+
const recordId = randomUUID()
|
|
75
|
+
const options = { entityId, recordId, tenantId, organizationId, values: {} }
|
|
76
|
+
const first = engine()
|
|
77
|
+
const second = engine()
|
|
78
|
+
const outcomes = await Promise.allSettled([
|
|
79
|
+
operation === 'mixed'
|
|
80
|
+
? first.createCustomEntityRecord({ ...options, createOnly: true })
|
|
81
|
+
: operation === 'update' ? first.updateCustomEntityRecord(options) : first.createCustomEntityRecord(options),
|
|
82
|
+
operation === 'update' ? second.updateCustomEntityRecord(options) : second.createCustomEntityRecord(options),
|
|
83
|
+
])
|
|
84
|
+
const rejected = outcomes.filter((outcome) => outcome.status === 'rejected')
|
|
85
|
+
if (operation === 'mixed') {
|
|
86
|
+
for (const outcome of rejected) {
|
|
87
|
+
if (outcome.status === 'rejected') expect(outcome.reason).toMatchObject({ status: 409, body: { code: 'custom_entity_record_exists' } })
|
|
88
|
+
}
|
|
89
|
+
} else expect(rejected).toEqual([])
|
|
90
|
+
await second.createCustomEntityRecord(options)
|
|
91
|
+
const rows = await orm.em.fork().execute<Array<{ count: number }>>(
|
|
92
|
+
'select count(*)::int as count from custom_entities_storage where entity_type = ? and entity_id = ?',
|
|
93
|
+
[entityId, recordId],
|
|
94
|
+
)
|
|
95
|
+
expect(rows[0].count).toBe(1)
|
|
96
|
+
await expect(first.createCustomEntityRecord({ ...options, createOnly: true })).rejects.toMatchObject({ status: 409 })
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test.each(['create', 'update'] as const)('%s remains atomic with an enclosing caller transaction', async (operation) => {
|
|
101
|
+
const recordId = randomUUID()
|
|
102
|
+
const options = { entityId, recordId, values: {} }
|
|
103
|
+
await expect(orm.em.fork().transactional(async (txEm) => {
|
|
104
|
+
const transactionalEngine = new DefaultDataEngine(txEm, {} as never)
|
|
105
|
+
if (operation === 'create') await transactionalEngine.createCustomEntityRecord(options)
|
|
106
|
+
else await transactionalEngine.updateCustomEntityRecord(options)
|
|
107
|
+
throw new Error('[internal] Roll back the owning transaction')
|
|
108
|
+
})).rejects.toThrow('[internal] Roll back the owning transaction')
|
|
109
|
+
const rows = await orm.em.fork().execute<Array<{ count: number }>>(
|
|
110
|
+
'select count(*)::int as count from custom_entities_storage where entity_type = ? and entity_id = ?',
|
|
111
|
+
[entityId, recordId],
|
|
112
|
+
)
|
|
113
|
+
expect(rows[0].count).toBe(0)
|
|
114
|
+
await engine().createCustomEntityRecord(options)
|
|
115
|
+
const retried = await orm.em.fork().execute<Array<{ count: number }>>(
|
|
116
|
+
'select count(*)::int as count from custom_entities_storage where entity_type = ? and entity_id = ?',
|
|
117
|
+
[entityId, recordId],
|
|
118
|
+
)
|
|
119
|
+
expect(retried[0].count).toBe(1)
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
test('releases the lock after a failed insert so a retry writes exactly one row', async () => {
|
|
123
|
+
const recordId = randomUUID()
|
|
124
|
+
const em = orm.em.fork()
|
|
125
|
+
await em.execute(`alter table custom_entities_storage add constraint reject_retry_record
|
|
126
|
+
check (entity_id <> '${recordId}'::uuid)`)
|
|
127
|
+
const options = { entityId, recordId, values: {} }
|
|
128
|
+
try {
|
|
129
|
+
await expect(engine().createCustomEntityRecord(options)).rejects.toMatchObject({ code: '23514' })
|
|
130
|
+
} finally {
|
|
131
|
+
await em.execute('alter table custom_entities_storage drop constraint reject_retry_record')
|
|
132
|
+
}
|
|
133
|
+
await Promise.all([engine().createCustomEntityRecord(options), engine().createCustomEntityRecord(options)])
|
|
134
|
+
const rows = await em.execute<Array<{ count: number }>>(
|
|
135
|
+
'select count(*)::int as count from custom_entities_storage where entity_type = ? and entity_id = ?',
|
|
136
|
+
[entityId, recordId],
|
|
137
|
+
)
|
|
138
|
+
expect(rows[0].count).toBe(1)
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
test('an upsert in another tenant neither replaces nor merges the original record', async () => {
|
|
142
|
+
const recordId = randomUUID()
|
|
143
|
+
const organizationId = randomUUID()
|
|
144
|
+
for (const tenantId of [randomUUID(), randomUUID()]) {
|
|
145
|
+
await engine().createCustomEntityRecord({ entityId, recordId, organizationId, tenantId, values: {} })
|
|
146
|
+
}
|
|
147
|
+
const rows = await orm.em.fork().execute<Array<{ tenant_id: string }>>(
|
|
148
|
+
'select tenant_id from custom_entities_storage where entity_type = ? and entity_id = ?', [entityId, recordId],
|
|
149
|
+
)
|
|
150
|
+
expect(new Set(rows.map((row) => row.tenant_id)).size).toBe(2)
|
|
151
|
+
})
|
|
152
|
+
})
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
jest.mock('../../db/advisoryLock', () => ({
|
|
2
|
+
withAdvisoryXactLock: async <T>(em: unknown, _key: string, work: (txEm: unknown) => Promise<T>): Promise<T> => work(em),
|
|
3
|
+
}))
|
|
4
|
+
|
|
1
5
|
import { registerEntityIds } from '../../encryption/entityIds'
|
|
2
6
|
|
|
3
7
|
const setRecordCustomFieldsMock = jest.fn(async () => undefined)
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { AwilixContainer } from 'awilix'
|
|
2
|
+
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
3
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
4
|
+
|
|
5
|
+
jest.mock('@open-mercato/shared/lib/logger', () => {
|
|
6
|
+
const mocked = {
|
|
7
|
+
debug: jest.fn(),
|
|
8
|
+
info: jest.fn(),
|
|
9
|
+
warn: jest.fn(),
|
|
10
|
+
error: jest.fn(),
|
|
11
|
+
child: jest.fn(),
|
|
12
|
+
}
|
|
13
|
+
mocked.child.mockImplementation(() => mocked)
|
|
14
|
+
return { createLogger: jest.fn(() => mocked) }
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
jest.mock('@open-mercato/core/modules/entities/lib/helpers', () => ({
|
|
18
|
+
setRecordCustomFields: jest.fn(async () => undefined),
|
|
19
|
+
encryptCustomEntityRecordDocument: jest.fn(async (_em: unknown, opts: { doc: unknown }) => opts.doc),
|
|
20
|
+
}))
|
|
21
|
+
|
|
22
|
+
jest.mock('@open-mercato/core/modules/entities/lib/validation', () => ({
|
|
23
|
+
validateCustomFieldValuesServer: jest.fn(async () => ({ ok: true, fieldErrors: {} })),
|
|
24
|
+
}))
|
|
25
|
+
|
|
26
|
+
jest.mock('@open-mercato/core/modules/entities/lib/htmlRichTextSanitizer', () => ({
|
|
27
|
+
sanitizeCustomFieldHtmlRichTextValuesServer: jest.fn(async (_em: unknown, opts: { values: unknown }) => opts.values),
|
|
28
|
+
}))
|
|
29
|
+
|
|
30
|
+
const { DefaultDataEngine, __resetUndeclaredEventWarningsForTests } = require('../engine')
|
|
31
|
+
|
|
32
|
+
const loggerWarn = createLogger('shared').warn as jest.Mock
|
|
33
|
+
|
|
34
|
+
type EmittedEvent = { name: string; payload: unknown; options?: unknown }
|
|
35
|
+
|
|
36
|
+
function makeFixture() {
|
|
37
|
+
const emitted: EmittedEvent[] = []
|
|
38
|
+
const bus = {
|
|
39
|
+
emitEvent: jest.fn(async (name: string, payload: unknown, options?: unknown) => {
|
|
40
|
+
emitted.push({ name, payload, options })
|
|
41
|
+
}),
|
|
42
|
+
}
|
|
43
|
+
const container = {
|
|
44
|
+
resolve: (name: string) => {
|
|
45
|
+
if (name === 'eventBus') return bus
|
|
46
|
+
throw new Error(`[internal] ${name} not registered`)
|
|
47
|
+
},
|
|
48
|
+
} as unknown as AwilixContainer
|
|
49
|
+
const engine = new DefaultDataEngine({} as unknown as EntityManager, container)
|
|
50
|
+
return { engine, emitted }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
describe('DataEngine.setCustomFields change signal', () => {
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
__resetUndeclaredEventWarningsForTests()
|
|
56
|
+
loggerWarn.mockClear()
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it.each([
|
|
60
|
+
'catalog:catalog_product',
|
|
61
|
+
'catalog:catalog_product_variant',
|
|
62
|
+
'customers:customer_deal',
|
|
63
|
+
'customers:customer_activity',
|
|
64
|
+
'user:runtime_custom_entity',
|
|
65
|
+
])('emits the entity-keyed signal for %s without an undeclared-event warning', async (entityId) => {
|
|
66
|
+
const { engine, emitted } = makeFixture()
|
|
67
|
+
const [mod, ent] = entityId.split(':')
|
|
68
|
+
|
|
69
|
+
await engine.setCustomFields({
|
|
70
|
+
entityId,
|
|
71
|
+
recordId: 'rec-1',
|
|
72
|
+
organizationId: 'org-1',
|
|
73
|
+
tenantId: 'tenant-1',
|
|
74
|
+
values: { priority: 3 },
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
expect(emitted).toEqual([
|
|
78
|
+
{
|
|
79
|
+
name: `${mod}.${ent}.updated`,
|
|
80
|
+
payload: { id: 'rec-1', organizationId: 'org-1', tenantId: 'tenant-1' },
|
|
81
|
+
options: { persistent: true, tenantId: 'tenant-1', organizationId: 'org-1', emitterModuleId: mod },
|
|
82
|
+
},
|
|
83
|
+
])
|
|
84
|
+
expect(loggerWarn).not.toHaveBeenCalled()
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('emits nothing when notify is false', async () => {
|
|
88
|
+
const { engine, emitted } = makeFixture()
|
|
89
|
+
|
|
90
|
+
await engine.setCustomFields({
|
|
91
|
+
entityId: 'catalog:catalog_product',
|
|
92
|
+
recordId: 'rec-1',
|
|
93
|
+
values: { priority: 3 },
|
|
94
|
+
notify: false,
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
expect(emitted).toEqual([])
|
|
98
|
+
expect(loggerWarn).not.toHaveBeenCalled()
|
|
99
|
+
})
|
|
100
|
+
})
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
jest.mock('../../db/advisoryLock', () => ({
|
|
2
|
+
withAdvisoryXactLock: async <T>(em: unknown, _key: string, work: (txEm: unknown) => Promise<T>): Promise<T> => work(em),
|
|
3
|
+
}))
|
|
4
|
+
|
|
1
5
|
import { DefaultDataEngine, clearDataEngineTableExistsCache } from '../engine'
|
|
2
6
|
import { registerEntityIds } from '../../encryption/entityIds'
|
|
3
7
|
|
package/src/lib/data/engine.ts
CHANGED
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
} from '../crud/types'
|
|
16
16
|
import type { BulkImportSuppression } from '../commands/types'
|
|
17
17
|
import { conflict, CrudHttpError, isUniqueViolation, notFound } from '../crud/errors'
|
|
18
|
-
import { withAdvisoryXactLock } from '../db/advisoryLock'
|
|
18
|
+
import { AdvisoryLockUnavailableError, withAdvisoryXactLock } from '../db/advisoryLock'
|
|
19
19
|
import { resolveRegisteredEntityTableName } from '../query/engine'
|
|
20
20
|
import { getEntityIds } from '../encryption/entityIds'
|
|
21
21
|
import { normalizeCustomFieldValues } from '../custom-fields/normalize'
|
|
@@ -112,7 +112,14 @@ export interface DataEngine {
|
|
|
112
112
|
organizationId?: string | null
|
|
113
113
|
tenantId?: string | null
|
|
114
114
|
values: Record<string, string | number | boolean | null | undefined | Array<string | number | boolean | null | undefined>>
|
|
115
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Default true: emits the entity-keyed custom-field change signal `<module>.<entity>.updated`,
|
|
117
|
+
* derived from `entityId` (e.g. `catalog:catalog_product` → `catalog.catalog_product.updated`).
|
|
118
|
+
* It is not a module domain event — the query_index bridge subscribes to the same derivation for
|
|
119
|
+
* every entity carrying custom fields, including runtime custom entities — so it is deliberately
|
|
120
|
+
* not checked against the `events.ts` registry.
|
|
121
|
+
*/
|
|
122
|
+
notify?: boolean
|
|
116
123
|
// Forwarded to `setRecordCustomFields` — see its doc for when a caller must pass
|
|
117
124
|
// true (a recordId that is not unique across organizations for this entityId).
|
|
118
125
|
pinOrganizationId?: boolean
|
|
@@ -320,7 +327,6 @@ export class DefaultDataEngine implements DataEngine {
|
|
|
320
327
|
const [mod, ent] = (entityId || '').split(':')
|
|
321
328
|
if (mod && ent) {
|
|
322
329
|
const eventName = `${mod}.${ent}.updated`
|
|
323
|
-
warnIfUndeclaredEvent(eventName, 'setCustomFields')
|
|
324
330
|
try {
|
|
325
331
|
await bus.emitEvent(eventName, { id: recordId, organizationId, tenantId }, {
|
|
326
332
|
persistent: true,
|
|
@@ -415,9 +421,59 @@ export class DefaultDataEngine implements DataEngine {
|
|
|
415
421
|
}
|
|
416
422
|
}
|
|
417
423
|
|
|
424
|
+
private async withCustomEntityStorageLock<T>(lockKey: string, work: (txEm: EntityManager) => Promise<T>): Promise<T> {
|
|
425
|
+
if (!this.em.isInTransaction?.()) return withAdvisoryXactLock(this.em, lockKey, work)
|
|
426
|
+
const deadlineAt = Date.now() + 15_000
|
|
427
|
+
while (true) {
|
|
428
|
+
const rows = await this.em.execute<Array<{ locked: boolean }>>(
|
|
429
|
+
'select pg_try_advisory_xact_lock(hashtextextended(?, 0)) as locked',
|
|
430
|
+
[lockKey],
|
|
431
|
+
)
|
|
432
|
+
if (rows[0]?.locked) return work(this.em)
|
|
433
|
+
const remainingMs = deadlineAt - Date.now()
|
|
434
|
+
if (remainingMs <= 0) throw new AdvisoryLockUnavailableError('deadline', lockKey)
|
|
435
|
+
await new Promise<void>((resolve) => setTimeout(resolve, Math.min(50, remainingMs)))
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
private async writeCustomEntityStorageDoc(target: {
|
|
440
|
+
entityType: string
|
|
441
|
+
entityId: string
|
|
442
|
+
organizationId: string | null
|
|
443
|
+
tenantId: string | null
|
|
444
|
+
doc: Record<string, unknown>
|
|
445
|
+
requireActive?: boolean
|
|
446
|
+
}): Promise<void> {
|
|
447
|
+
const lockKey = `custom_entity_create:${JSON.stringify([target.tenantId, target.organizationId, target.entityType, target.entityId.toLowerCase()])}`
|
|
448
|
+
await this.withCustomEntityStorageLock(lockKey, async (txEm) => {
|
|
449
|
+
const db = txEm.getKysely<CustomEntityStorageDatabase>()
|
|
450
|
+
const doc = sql<CustomEntityValues>`${JSON.stringify(target.doc)}::jsonb`
|
|
451
|
+
let update = db
|
|
452
|
+
.updateTable('custom_entities_storage')
|
|
453
|
+
.set({ doc, updated_at: sql<Date>`now()`, deleted_at: null })
|
|
454
|
+
.where('entity_type', '=', target.entityType)
|
|
455
|
+
.where('entity_id', '=', target.entityId)
|
|
456
|
+
.where('organization_id', target.organizationId === null ? 'is' : '=', target.organizationId)
|
|
457
|
+
.where('tenant_id', target.tenantId === null ? 'is' : '=', target.tenantId)
|
|
458
|
+
if (target.requireActive) update = update.where('deleted_at', 'is', null)
|
|
459
|
+
const updated = await update.executeTakeFirst()
|
|
460
|
+
if (Number(updated.numUpdatedRows) > 0) return
|
|
461
|
+
if (target.requireActive) throw notFound()
|
|
462
|
+
await db.insertInto('custom_entities_storage').values({
|
|
463
|
+
entity_type: target.entityType,
|
|
464
|
+
entity_id: target.entityId,
|
|
465
|
+
organization_id: target.organizationId,
|
|
466
|
+
tenant_id: target.tenantId,
|
|
467
|
+
doc,
|
|
468
|
+
created_at: sql<Date>`now()`,
|
|
469
|
+
updated_at: sql<Date>`now()`,
|
|
470
|
+
deleted_at: null,
|
|
471
|
+
}).execute()
|
|
472
|
+
})
|
|
473
|
+
}
|
|
474
|
+
|
|
418
475
|
async createCustomEntityRecord(opts: Parameters<DataEngine['createCustomEntityRecord']>[0]): Promise<{ id: string }> {
|
|
419
476
|
assertCustomEntityStorageEntityId(this.em, opts.entityId)
|
|
420
|
-
const db = this.getKysely()
|
|
421
477
|
await this.ensureStorageTableExists()
|
|
422
478
|
const sanitizedValues = await sanitizeCustomFieldHtmlRichTextValuesServer(this.em, {
|
|
423
479
|
entityId: opts.entityId,
|
|
@@ -473,7 +529,7 @@ export class DefaultDataEngine implements DataEngine {
|
|
|
473
529
|
.selectFrom('custom_entities_storage')
|
|
474
530
|
.select('entity_id')
|
|
475
531
|
.where('entity_type', '=', opts.entityId)
|
|
476
|
-
.where(sql<string>`lower(entity_id)`, '=', id.toLowerCase())
|
|
532
|
+
.where(sql<string>`lower(entity_id::text)`, '=', id.toLowerCase())
|
|
477
533
|
.where('organization_id', orgId === null ? 'is' : '=', orgId)
|
|
478
534
|
.where('tenant_id', tenantId === null ? 'is' : '=', tenantId)
|
|
479
535
|
.executeTakeFirst()
|
|
@@ -486,36 +542,7 @@ export class DefaultDataEngine implements DataEngine {
|
|
|
486
542
|
}
|
|
487
543
|
})
|
|
488
544
|
} else {
|
|
489
|
-
|
|
490
|
-
await db
|
|
491
|
-
.insertInto('custom_entities_storage' as any)
|
|
492
|
-
.values(payload as any)
|
|
493
|
-
.onConflict((oc) => oc
|
|
494
|
-
.columns(['entity_type', 'entity_id', 'organization_id'])
|
|
495
|
-
.doUpdateSet({
|
|
496
|
-
doc: sql`${JSON.stringify(doc)}::jsonb`,
|
|
497
|
-
updated_at: sql`now()`,
|
|
498
|
-
deleted_at: null,
|
|
499
|
-
} as any))
|
|
500
|
-
.execute()
|
|
501
|
-
} catch {
|
|
502
|
-
// Fallback for global scope uniqueness
|
|
503
|
-
const updated = await db
|
|
504
|
-
.updateTable('custom_entities_storage' as any)
|
|
505
|
-
.set({
|
|
506
|
-
doc: sql`${JSON.stringify(doc)}::jsonb`,
|
|
507
|
-
updated_at: sql`now()`,
|
|
508
|
-
deleted_at: null,
|
|
509
|
-
} as any)
|
|
510
|
-
.where('entity_type' as any, '=', opts.entityId)
|
|
511
|
-
.where('entity_id' as any, '=', id)
|
|
512
|
-
.where('organization_id' as any, orgId === null ? 'is' : '=', orgId as any)
|
|
513
|
-
.where('tenant_id' as any, tenantId === null ? 'is' : '=', tenantId as any)
|
|
514
|
-
.executeTakeFirst()
|
|
515
|
-
if (!updated || Number(updated.numUpdatedRows ?? 0) === 0) {
|
|
516
|
-
await db.insertInto('custom_entities_storage' as any).values(payload as any).execute()
|
|
517
|
-
}
|
|
518
|
-
}
|
|
545
|
+
await this.writeCustomEntityStorageDoc({ entityType: opts.entityId, entityId: id, organizationId: orgId, tenantId, doc })
|
|
519
546
|
}
|
|
520
547
|
|
|
521
548
|
// Optional EAV backward compatibility (disabled by default)
|
|
@@ -576,30 +603,7 @@ export class DefaultDataEngine implements DataEngine {
|
|
|
576
603
|
doc: { ...prevDoc, ...this.normalizeDocValues(sanitizedValues || {}), id },
|
|
577
604
|
encryptionService: this.resolveCustomEntityEncryptionService(),
|
|
578
605
|
})
|
|
579
|
-
|
|
580
|
-
const updated = await applyScope(
|
|
581
|
-
db.updateTable('custom_entities_storage' as any).set({
|
|
582
|
-
doc: sql`${JSON.stringify(nextDoc)}::jsonb`,
|
|
583
|
-
updated_at: sql`now()`,
|
|
584
|
-
deleted_at: null,
|
|
585
|
-
} as any) as any
|
|
586
|
-
).executeTakeFirst()
|
|
587
|
-
if (!updated || Number((updated as any).numUpdatedRows ?? 0) === 0) {
|
|
588
|
-
if (requireActive) throw notFound()
|
|
589
|
-
await db.insertInto('custom_entities_storage' as any).values({
|
|
590
|
-
entity_type: opts.entityId,
|
|
591
|
-
entity_id: id,
|
|
592
|
-
organization_id: orgId,
|
|
593
|
-
tenant_id: tenantId,
|
|
594
|
-
doc: sql`${JSON.stringify(nextDoc)}::jsonb`,
|
|
595
|
-
created_at: sql`now()`,
|
|
596
|
-
updated_at: sql`now()`,
|
|
597
|
-
deleted_at: null,
|
|
598
|
-
} as any).execute()
|
|
599
|
-
}
|
|
600
|
-
} catch (err) {
|
|
601
|
-
throw err
|
|
602
|
-
}
|
|
606
|
+
await this.writeCustomEntityStorageDoc({ entityType: opts.entityId, entityId: id, organizationId: orgId, tenantId, doc: nextDoc, requireActive })
|
|
603
607
|
|
|
604
608
|
// Optional EAV backward compatibility (disabled by default)
|
|
605
609
|
if (this.backcompatEavEnabled() && sanitizedValues && Object.keys(sanitizedValues).length > 0) {
|