@open-mercato/shared 0.10.1-develop.8039.1.6ede0dd9ce → 0.10.1-develop.8042.1.c981a94bea

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.
@@ -1,4 +1,4 @@
1
- const APP_VERSION = "0.10.1-develop.8039.1.6ede0dd9ce";
1
+ const APP_VERSION = "0.10.1-develop.8042.1.c981a94bea";
2
2
  const appVersion = APP_VERSION;
3
3
  export {
4
4
  APP_VERSION,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/lib/version.ts"],
4
- "sourcesContent": ["// Build-time generated version\nexport const APP_VERSION = '0.10.1-develop.8039.1.6ede0dd9ce';\nexport const appVersion = APP_VERSION;\n"],
4
+ "sourcesContent": ["// Build-time generated version\nexport const APP_VERSION = '0.10.1-develop.8042.1.c981a94bea';\nexport const appVersion = APP_VERSION;\n"],
5
5
  "mappings": "AACO,MAAM,cAAc;AACpB,MAAM,aAAa;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-mercato/shared",
3
- "version": "0.10.1-develop.8039.1.6ede0dd9ce",
3
+ "version": "0.10.1-develop.8042.1.c981a94bea",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -8,7 +8,7 @@
8
8
  "build": "node build.mjs",
9
9
  "watch": "node watch.mjs",
10
10
  "test": "jest --config jest.config.cjs",
11
- "test:pg-integration": "cross-env OM_PG_INTEGRATION=1 jest --config jest.config.cjs --runInBand 'src/lib/db/__tests__/advisoryLock\\.integration\\.test\\.ts$'",
11
+ "test:pg-integration": "cross-env OM_PG_INTEGRATION=1 jest --config jest.config.cjs --runInBand 'src/lib/(db/__tests__/advisoryLock|data/__tests__/engine.custom-entity-storage-upsert)\\.integration\\.test\\.ts$'",
12
12
  "typecheck": "tsc --noEmit"
13
13
  },
14
14
  "exports": {
@@ -130,7 +130,7 @@
130
130
  "@mikro-orm/core": "^7.1.14",
131
131
  "@mikro-orm/decorators": "^7.1.14",
132
132
  "@mikro-orm/postgresql": "^7.1.14",
133
- "@open-mercato/cache": "0.10.1-develop.8039.1.6ede0dd9ce",
133
+ "@open-mercato/cache": "0.10.1-develop.8042.1.c981a94bea",
134
134
  "@types/html-to-text": "^9.0.4",
135
135
  "@types/sanitize-html": "^2.16.1",
136
136
  "dotenv": "^17.4.2",
@@ -0,0 +1,122 @@
1
+ import { randomUUID } from 'node:crypto'
2
+ import { Kysely, PostgresDialect, sql } from 'kysely'
3
+ import { Pool } from 'pg'
4
+ import type { StartedTestContainer } from 'testcontainers'
5
+ import { DefaultDataEngine, clearDataEngineTableExistsCache } from '../engine'
6
+
7
+ type StorageRow = {
8
+ entity_type: string
9
+ entity_id: string
10
+ organization_id: string | null
11
+ tenant_id: string | null
12
+ doc: Record<string, unknown>
13
+ created_at: Date
14
+ updated_at: Date
15
+ deleted_at: Date | null
16
+ }
17
+
18
+ const describePostgres = process.env.OM_PG_INTEGRATION === '1' ? describe : describe.skip
19
+
20
+ describePostgres('custom entity storage transactions with the nonunique production index', () => {
21
+ let postgres: StartedTestContainer | undefined
22
+ let db: Kysely<{ custom_entities_storage: StorageRow }> | undefined
23
+ const backcompatToggle = process.env.ENTITIES_BACKCOMPAT_EAV_FOR_CUSTOM
24
+
25
+ beforeAll(async () => {
26
+ const { GenericContainer, Wait } = await import('testcontainers')
27
+ postgres = await new GenericContainer('postgres:16')
28
+ .withEnvironment({ POSTGRES_PASSWORD: 'test', POSTGRES_DB: 'storage_test' })
29
+ .withExposedPorts(5432)
30
+ .withWaitStrategy(Wait.forLogMessage(/database system is ready to accept connections/, 2))
31
+ .start()
32
+ db = new Kysely({ dialect: new PostgresDialect({ pool: new Pool({
33
+ host: postgres.getHost(), port: postgres.getMappedPort(5432),
34
+ user: 'postgres', password: 'test', database: 'storage_test',
35
+ }) }) })
36
+ await sql`create table custom_entities_storage (
37
+ id uuid primary key default gen_random_uuid(),
38
+ entity_type text not null, entity_id uuid not null, organization_id uuid, tenant_id uuid,
39
+ doc jsonb not null, created_at timestamptz not null, updated_at timestamptz not null, deleted_at timestamptz
40
+ )`.execute(db)
41
+ await sql`create index custom_entities_storage_unique_idx
42
+ on custom_entities_storage (entity_type, entity_id, organization_id)`.execute(db)
43
+ process.env.ENTITIES_BACKCOMPAT_EAV_FOR_CUSTOM = 'no'
44
+ }, 120_000)
45
+
46
+ afterAll(async () => {
47
+ if (backcompatToggle === undefined) delete process.env.ENTITIES_BACKCOMPAT_EAV_FOR_CUSTOM
48
+ else process.env.ENTITIES_BACKCOMPAT_EAV_FOR_CUSTOM = backcompatToggle
49
+ clearDataEngineTableExistsCache()
50
+ try {
51
+ await db?.destroy()
52
+ } finally {
53
+ await postgres?.stop()
54
+ }
55
+ })
56
+
57
+ test.each(['scoped', 'global'] as const)('create, upsert, update and revival commit without aborting an outer transaction (%s)', async (scope) => {
58
+ if (!db) throw new Error('[internal] PostgreSQL was not initialized')
59
+ const storage = db
60
+ const recordId = randomUUID()
61
+ const entityId = `user:storage_${randomUUID().replaceAll('-', '')}`
62
+ const organizationId = scope === 'global' ? null : randomUUID()
63
+ const tenantId = scope === 'global' ? null : randomUUID()
64
+ const otherOrganizationId = randomUUID()
65
+ const otherTenantId = randomUUID()
66
+ const original = { id: recordId, title: 'Original', count: 1, tags: ['one', 'two'], enabled: true }
67
+ const replacement = { ...original, title: 'Replaced', count: 2 }
68
+ const updated = { ...replacement, title: 'Updated', tags: ['three'] }
69
+ const otherScope = { id: recordId, title: 'Other scope' }
70
+ try {
71
+ for (const neighbor of [
72
+ { organization_id: otherOrganizationId, tenant_id: tenantId },
73
+ { organization_id: organizationId, tenant_id: otherTenantId },
74
+ ]) {
75
+ await storage.insertInto('custom_entities_storage').values({
76
+ entity_type: entityId, entity_id: recordId, ...neighbor, doc: otherScope,
77
+ created_at: sql`now()`, updated_at: sql`now()`, deleted_at: null,
78
+ }).execute()
79
+ }
80
+ for (const phase of ['create', 'upsert', 'update', 'revive'] as const) {
81
+ await storage.transaction().execute(async (transaction) => {
82
+ const em = {
83
+ isInTransaction: () => true,
84
+ execute: async (query: string, parameters: readonly unknown[]) => {
85
+ expect(query).toBe('select pg_try_advisory_xact_lock(hashtextextended(?, 0)) as locked')
86
+ const lockKey = parameters[0]
87
+ if (typeof lockKey !== 'string') throw new Error('[internal] Missing advisory lock key')
88
+ const result = await sql<{ locked: boolean }>`select pg_try_advisory_xact_lock(hashtextextended(${lockKey}, 0)) as locked`.execute(transaction)
89
+ return result.rows
90
+ },
91
+ getKysely: () => transaction,
92
+ getMetadata: () => ({ find: () => undefined, getAll: () => [] }),
93
+ find: async () => [],
94
+ }
95
+ const engine = new DefaultDataEngine(em as never, {} as never)
96
+ const target = { entityId, recordId, organizationId, tenantId }
97
+ clearDataEngineTableExistsCache()
98
+ if (phase === 'create') await engine.createCustomEntityRecord({ ...target, values: original })
99
+ if (phase === 'upsert') await engine.createCustomEntityRecord({ ...target, values: replacement })
100
+ if (phase === 'update') await engine.updateCustomEntityRecord({ ...target, values: { title: 'Updated', tags: ['three'] } })
101
+ if (phase === 'revive') {
102
+ await engine.deleteCustomEntityRecord(target)
103
+ await engine.createCustomEntityRecord({ ...target, values: updated })
104
+ }
105
+ const probe = await sql<{ usable: number }>`select 1 as usable`.execute(transaction)
106
+ expect(probe.rows).toEqual([{ usable: 1 }])
107
+ })
108
+ const rows = await storage.selectFrom('custom_entities_storage').selectAll()
109
+ .where('entity_type', '=', entityId).execute()
110
+ expect(rows).toHaveLength(3)
111
+ const ownRow = rows.find((row) => row.organization_id === organizationId && row.tenant_id === tenantId)
112
+ expect(ownRow).toMatchObject({
113
+ doc: phase === 'create' ? original : phase === 'upsert' ? replacement : updated,
114
+ deleted_at: null, organization_id: organizationId, tenant_id: tenantId,
115
+ })
116
+ expect(rows.filter((row) => row !== ownRow).map((row) => row.doc)).toEqual([otherScope, otherScope])
117
+ }
118
+ } finally {
119
+ await storage.deleteFrom('custom_entities_storage').where('entity_type', '=', entityId).execute()
120
+ }
121
+ })
122
+ })