@aria-framework/db-worker 0.29.0 → 0.31.0

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/dialect.js CHANGED
@@ -71,6 +71,26 @@ const sqlite = {
71
71
  * SQL for the same answer, which is precisely why it belongs here once rather than in each app.
72
72
  */
73
73
  foreignKeys: (table) => `PRAGMA foreign_key_list(${ident(table)})`,
74
+ // 0.31.0 — THE REST OF A TABLE'S SHAPE, for the table-definitions schema check (tableDefinitionsCheck.js), in one
75
+ // row shape on both engines like the three above. columns() and foreignKeys() stay as they are: columns() answers
76
+ // names only, and foreignKeys() pairs no columns by position (see the postgres twin), so a composite key needs
77
+ // keyColumns().
78
+ /**
79
+ * Each column: `{ name, type (upper case), notnull (0/1), pk (its position in the primary key, 0 if none), dflt (its
80
+ * default as the engine keeps it, or null) }`.
81
+ */
82
+ columnDetails: (table) => `SELECT name, upper(type) AS type, "notnull" AS "notnull", pk, dflt_value AS dflt FROM pragma_table_info(${strLit(table)}) ORDER BY cid`,
83
+ /**
84
+ * Each index, one row per column in order: `{ name, unique (0/1), origin ('pk' | 'u' (backs a UNIQUE / PRIMARY KEY
85
+ * constraint) | 'c' (CREATE INDEX)), partial (0/1), seq, col }`. A rowid primary key has no index here.
86
+ */
87
+ indexes: (table) => `SELECT il.name AS name, il."unique" AS "unique", il.origin AS origin, il.partial AS partial, ii.seqno AS seq, ii.name AS col`
88
+ + ` FROM pragma_index_list(${strLit(table)}) AS il JOIN pragma_index_info(il.name) AS ii ORDER BY il.name, ii.seqno`,
89
+ /** Each foreign key, one row per column pair in order: `{ id, seq, parent, col, parent_col, on_delete, on_update }`. */
90
+ keyColumns: (table) => `SELECT id, seq, "table" AS parent, "from" AS col, "to" AS parent_col, on_delete, on_update`
91
+ + ` FROM pragma_foreign_key_list(${strLit(table)}) ORDER BY id, seq`,
92
+ /** Row-level security: SQLite has none, so there is no query (null). */
93
+ policies: () => null,
74
94
  /**
75
95
  * N minutes AFTER now — an expiry window.
76
96
  *
@@ -187,6 +207,52 @@ const postgres = {
187
207
  + ` ON ccu.constraint_name = tc.constraint_name AND ccu.table_schema = tc.table_schema`
188
208
  + ` WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_schema = 'public'`
189
209
  + ` AND tc.table_name = ${strLit(table)}`,
210
+ // 0.31.0 — the sqlite twin's four, from the system catalogues. (foreignKeys() above joins key_column_usage to
211
+ // constraint_column_usage by the constraint's NAME only, so a key of n columns answers n × n rows with no pairing,
212
+ // and constraint_column_usage lists only tables the current role OWNS, so for an app role that owns nothing it
213
+ // answers no rows at all — both measured on PGlite in test/tableDefinitions.pglite.js. It is left as it is;
214
+ // keyColumns() pairs by position and reads pg_constraint, which every role can.)
215
+ columnDetails: (table) => 'SELECT c.column_name AS name, upper(c.data_type) AS type,'
216
+ + " CASE WHEN c.is_nullable = 'NO' THEN 1 ELSE 0 END AS \"notnull\","
217
+ + ' COALESCE((SELECT k.ord::int FROM pg_constraint pk'
218
+ + ' JOIN pg_class pt ON pt.oid = pk.conrelid JOIN pg_namespace pn ON pn.oid = pt.relnamespace'
219
+ + ' CROSS JOIN LATERAL unnest(pk.conkey) WITH ORDINALITY AS k(att, ord)'
220
+ + ' JOIN pg_attribute a ON a.attrelid = pk.conrelid AND a.attnum = k.att'
221
+ + " WHERE pk.contype = 'p' AND pn.nspname = 'public' AND pt.relname = c.table_name AND a.attname = c.column_name), 0) AS pk,"
222
+ + ' c.column_default AS dflt'
223
+ + ' FROM information_schema.columns c'
224
+ + ` WHERE c.table_schema = 'public' AND c.table_name = ${strLit(table)} ORDER BY c.ordinal_position`,
225
+ indexes: (table) => 'SELECT ic.relname AS name, CASE WHEN x.indisunique THEN 1 ELSE 0 END AS "unique",'
226
+ + " CASE WHEN x.indisprimary THEN 'pk' WHEN EXISTS (SELECT 1 FROM pg_constraint con WHERE con.conindid = x.indexrelid"
227
+ + " AND con.conrelid = x.indrelid AND con.contype IN ('u', 'p')) THEN 'u' ELSE 'c' END AS origin,"
228
+ + ' CASE WHEN x.indpred IS NULL THEN 0 ELSE 1 END AS partial, (k.ord - 1)::int AS seq, a.attname AS col'
229
+ + ' FROM pg_index x JOIN pg_class t ON t.oid = x.indrelid JOIN pg_namespace n ON n.oid = t.relnamespace'
230
+ + ' JOIN pg_class ic ON ic.oid = x.indexrelid'
231
+ + ' CROSS JOIN LATERAL unnest(x.indkey::int2[]) WITH ORDINALITY AS k(attnum, ord)'
232
+ + ' LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum'
233
+ + ` WHERE n.nspname = 'public' AND t.relname = ${strLit(table)} ORDER BY ic.relname, k.ord`,
234
+ keyColumns: (table) => {
235
+ const act = (c) => `CASE ${c} WHEN 'a' THEN 'NO ACTION' WHEN 'r' THEN 'RESTRICT' WHEN 'c' THEN 'CASCADE'`
236
+ + ` WHEN 'n' THEN 'SET NULL' WHEN 'd' THEN 'SET DEFAULT' END`;
237
+ return `SELECT c.conname AS id, (k.ord - 1)::int AS seq, p.relname AS parent, a.attname AS col, pa.attname AS parent_col,`
238
+ + ` ${act('c.confdeltype')} AS on_delete, ${act('c.confupdtype')} AS on_update`
239
+ + ' FROM pg_constraint c JOIN pg_class t ON t.oid = c.conrelid JOIN pg_namespace n ON n.oid = t.relnamespace'
240
+ + ' JOIN pg_class p ON p.oid = c.confrelid'
241
+ + ' CROSS JOIN LATERAL unnest(c.conkey, c.confkey) WITH ORDINALITY AS k(att, patt, ord)'
242
+ + ' JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = k.att'
243
+ + ' JOIN pg_attribute pa ON pa.attrelid = c.confrelid AND pa.attnum = k.patt'
244
+ + ` WHERE c.contype = 'f' AND n.nspname = 'public' AND t.relname = ${strLit(table)} ORDER BY c.conname, k.ord`;
245
+ },
246
+ /**
247
+ * A table's row-level security and its policies, one row per policy (one row with name NULL when it has none):
248
+ * `{ rls, force, name, permissive, roles (comma-joined), cmd, qual, with_check }` — qual and with_check as the
249
+ * server deparses them, not as the migration typed them.
250
+ */
251
+ policies: (table) => 'SELECT c.relrowsecurity AS rls, c.relforcerowsecurity AS "force", p.policyname AS name,'
252
+ + " p.permissive AS permissive, array_to_string(p.roles, ',') AS roles, p.cmd AS cmd, p.qual AS qual, p.with_check AS with_check"
253
+ + ' FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace'
254
+ + ' LEFT JOIN pg_policies p ON p.schemaname = n.nspname AND p.tablename = c.relname'
255
+ + ` WHERE n.nspname = 'public' AND c.relname = ${strLit(table)} AND c.relkind IN ('r', 'p') ORDER BY p.policyname`,
190
256
  nowPlusMinutes: (n) => `to_char((NOW() + INTERVAL '${Number(n)} minutes') AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI:SS')`,
191
257
  // date_trunc on the UTC value, not on NOW(): truncating a timestamptz uses the SESSION time
192
258
  // zone, so 'start of month' would land on a different instant depending on where the server
package/index.js CHANGED
@@ -54,5 +54,18 @@ module.exports = {
54
54
  sql: require('./sql'),
55
55
  ...require('./tenancyManifest'),
56
56
  ...require('./tenantScope'),
57
- ...require('./tenantSettings')
57
+ ...require('./tenantSettings'),
58
+ // 0.30.0 — the customer pin: the third level under the tenant context's two (customerPin.js), the worker's
59
+ // customer-safe lock (createCustomerScope), and — on tenancyManifest above — the frozen CUSTOMER_POLICY_EXPRESSION.
60
+ ...require('./customerPin'),
61
+ createCustomerScope: require('./customerScope').createCustomerScope,
62
+ // 0.31.0 — tables a package needs, as DATA (tableDefinitions.js): the app's own generator writes them into the app's
63
+ // own migration (aria-tenancy new --definitions), and the package checks the database against them at boot.
64
+ defineTables: require('./tableDefinitions').defineTables,
65
+ tenancyFragment: require('./tableDefinitions').tenancyFragment,
66
+ classificationOf: require('./tableDefinitions').classificationOf,
67
+ isTableDefinitions: require('./tableDefinitions').isTableDefinitions,
68
+ TableDefinitionError: require('./tableDefinitions').TableDefinitionError,
69
+ checkDefinedSchema: require('./tableDefinitionsCheck').checkDefinedSchema,
70
+ assertDefinedSchema: require('./tableDefinitionsCheck').assertDefinedSchema
58
71
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aria-framework/db-worker",
3
- "version": "0.29.0",
3
+ "version": "0.31.0",
4
4
  "description": "Aria App Framework — db-worker module. Async RPC bridge over synchronous better-sqlite3: a worker-thread dispatcher (runWorker), a main-thread client with per-call timeouts and crash handling (DbClient), and a SQLite layer with WAL pragmas + a numbered-migration runner (database).",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,
@@ -13,6 +13,8 @@
13
13
  },
14
14
  "files": [
15
15
  "client.js",
16
+ "customerPin.js",
17
+ "customerScope.js",
16
18
  "bin/",
17
19
  "database.js",
18
20
  "dialect.js",
@@ -22,6 +24,8 @@
22
24
  "settingsStore.js",
23
25
  "sql.js",
24
26
  "tableOrder.js",
27
+ "tableDefinitions.js",
28
+ "tableDefinitionsCheck.js",
25
29
  "tenancyManifest.js",
26
30
  "tenancyGenerator.js",
27
31
  "tenancyMigration.js",
@@ -49,7 +53,7 @@
49
53
  "pg": "^8.23.0"
50
54
  },
51
55
  "scripts": {
52
- "test": "node test/driverContract.js && node test/migrations.js && node test/migrations-notx.js && node test/migrationFailureSurfaces.js && node test/migrationDialect.js && node test/regressions.js && node test/transactions.js && node test/tenantContext.js && node test/tenancyMigration.js && node test/tenancyAttribution.pglite.js && node test/tenancyNullable.pg.js && node test/tenancyPartnerFk.pg.js && node test/tenancyOnUpdate.pg.js && node test/dialectIntrospection.js && node test/tenantRls.pg.js && node test/nestedTransaction.pg.js && node test/clientError.pg.js && node test/migrationRole.pg.js && node test/smoke.js && node test/tier2.js && node test/timezone.pg.js && node test/pinPoolUtc.js && node test/pinAwaited.pg.js && node test/inTransaction.js && node test/sql.js && node test/tenancyManifest.js && node test/tenantScope.js && node test/tenantSettings.js && node test/dbClientRefusal.js && node test/migrationsSquash.js && node test/tenancyGenerator.js",
56
+ "test": "node test/driverContract.js && node test/migrations.js && node test/migrations-notx.js && node test/migrationFailureSurfaces.js && node test/migrationDialect.js && node test/regressions.js && node test/transactions.js && node test/tenantContext.js && node test/tenancyMigration.js && node test/tenancyAttribution.pglite.js && node test/tenancyNullable.pg.js && node test/tenancyPartnerFk.pg.js && node test/tenancyOnUpdate.pg.js && node test/dialectIntrospection.js && node test/tenantRls.pg.js && node test/nestedTransaction.pg.js && node test/clientError.pg.js && node test/migrationRole.pg.js && node test/smoke.js && node test/tier2.js && node test/timezone.pg.js && node test/pinPoolUtc.js && node test/pinAwaited.pg.js && node test/inTransaction.js && node test/sql.js && node test/tenancyManifest.js && node test/tenantScope.js && node test/tenantSettings.js && node test/dbClientRefusal.js && node test/migrationsSquash.js && node test/tenancyGenerator.js && node test/customerPin.js && node test/customerPin.pglite.js && node test/tableDefinitions.js && node test/tableDefinitions.pglite.js",
53
57
  "prepublishOnly": "node ../../test/packaging.js db-worker"
54
58
  }
55
59
  }