@crombolo/data 0.37.0 → 0.38.1

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.
Files changed (75) hide show
  1. package/package.json +3 -2
  2. package/src/audit.ts +163 -133
  3. package/src/change-report.ts +66 -43
  4. package/src/cluster.ts +119 -59
  5. package/src/content-query.ts +205 -200
  6. package/src/{bundle-tables → custom-tables}/builder.ts +104 -12
  7. package/src/custom-tables/chain.ts +150 -0
  8. package/src/custom-tables/ddl.ts +159 -0
  9. package/src/custom-tables/handle.ts +141 -0
  10. package/src/{bundle-tables → custom-tables}/index.ts +3 -2
  11. package/src/custom-tables/names.ts +94 -0
  12. package/src/custom-tables/owners.ts +299 -0
  13. package/src/{bundle-tables → custom-tables}/schema.ts +263 -90
  14. package/src/custom-tables/values.ts +181 -0
  15. package/src/database.ts +40 -5
  16. package/src/dialect.ts +43 -16
  17. package/src/index.ts +8 -1
  18. package/src/migrations.ts +130 -88
  19. package/src/query.ts +158 -0
  20. package/src/relations.ts +49 -28
  21. package/src/repositories/approvals.ts +130 -89
  22. package/src/repositories/bundle-grants.ts +74 -0
  23. package/src/repositories/bundles.ts +85 -76
  24. package/src/repositories/content-applied.ts +47 -28
  25. package/src/repositories/content-events.ts +114 -67
  26. package/src/repositories/content-notifications.ts +102 -74
  27. package/src/repositories/content-types.ts +449 -320
  28. package/src/repositories/data-types.ts +105 -56
  29. package/src/repositories/delivery-member-tokens.ts +37 -28
  30. package/src/repositories/dictionary.ts +77 -58
  31. package/src/repositories/documents.ts +956 -580
  32. package/src/repositories/domains.ts +28 -16
  33. package/src/repositories/dynamic-roots.ts +32 -17
  34. package/src/repositories/external-logins.ts +38 -24
  35. package/src/repositories/folders.ts +7 -4
  36. package/src/repositories/form-entries.ts +120 -103
  37. package/src/repositories/form-workflows.ts +87 -62
  38. package/src/repositories/jobs.ts +296 -0
  39. package/src/repositories/languages.ts +51 -38
  40. package/src/repositories/log-searches.ts +7 -5
  41. package/src/repositories/media-access.ts +173 -123
  42. package/src/repositories/media-listing.ts +155 -92
  43. package/src/repositories/members.ts +189 -162
  44. package/src/repositories/nodes.ts +227 -181
  45. package/src/repositories/notifications.ts +22 -17
  46. package/src/repositories/public-access.ts +77 -61
  47. package/src/repositories/redirects.ts +113 -70
  48. package/src/repositories/references.ts +55 -44
  49. package/src/repositories/remote-jobs.ts +45 -35
  50. package/src/repositories/tags.ts +11 -10
  51. package/src/repositories/templates.ts +24 -14
  52. package/src/repositories/transfer-answers.ts +58 -50
  53. package/src/repositories/transfer-runs.ts +93 -75
  54. package/src/repositories/two-factor.ts +74 -55
  55. package/src/repositories/user-data.ts +54 -36
  56. package/src/repositories/users.ts +345 -300
  57. package/src/repositories/webhooks.ts +196 -137
  58. package/src/schema/bundle-grants.ts +32 -0
  59. package/src/schema/jobs.ts +56 -0
  60. package/src/schema/plan.ts +4 -0
  61. package/src/schema-ops.ts +15 -12
  62. package/src/schema-state.ts +53 -38
  63. package/src/search.ts +175 -129
  64. package/src/setup.ts +17 -13
  65. package/src/site-search.ts +134 -83
  66. package/src/table-types.ts +152 -0
  67. package/src/tables.gen.ts +859 -0
  68. package/src/tree-invariants.ts +21 -12
  69. package/src/url-options.ts +17 -13
  70. package/src/workflow-store.ts +17 -13
  71. package/src/bundle-tables/capability.ts +0 -377
  72. package/src/bundle-tables/chain.ts +0 -105
  73. package/src/bundle-tables/ddl.ts +0 -124
  74. package/src/bundle-tables/guard.ts +0 -116
  75. package/src/bundle-tables/names.ts +0 -58
@@ -94,7 +94,7 @@ const scheduleFrom = (json: string): ApprovalScheduleEntry[] =>
94
94
  }))
95
95
 
96
96
  /** States a request is still open in: it holds the page until it ends. */
97
- const OPEN = "state IN ('waiting', 'scheduled')"
97
+ const OPEN = ['waiting', 'scheduled']
98
98
 
99
99
  const requestFrom = (row: Record<string, unknown>): ApprovalRequest => ({
100
100
  id: Number(row.id),
@@ -157,96 +157,127 @@ export class ApprovalRepository {
157
157
  schedule?: ApprovalScheduleEntry[] | null
158
158
  }): Promise<ApprovalRequest> {
159
159
  const key = crypto.randomUUID()
160
- await this.#db.exec(
161
- `INSERT INTO approval_request (key, node_key, cultures, action, event_id, workflow_key,
162
- workflow_version, workflow, state, requested_by, requested_at, schedule)
163
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'waiting', ?, ?, ?)`,
164
- [
160
+ await this.#db.q
161
+ .insertInto('approval_request')
162
+ .values({
165
163
  key,
166
- normaliseUuid(input.nodeKey),
167
- input.cultures ? JSON.stringify(input.cultures) : null,
168
- input.action,
169
- input.eventId,
170
- workflowIdentity(input.workflow),
171
- input.workflowVersion,
172
- JSON.stringify(input.workflow),
173
- normaliseUuid(input.requestedBy),
174
- DbDate.toDb(new Date()),
175
- input.schedule?.length ? JSON.stringify(input.schedule) : null,
176
- ],
177
- )
164
+ node_key: normaliseUuid(input.nodeKey),
165
+ cultures: input.cultures ? JSON.stringify(input.cultures) : null,
166
+ action: input.action,
167
+ event_id: input.eventId,
168
+ workflow_key: workflowIdentity(input.workflow),
169
+ workflow_version: input.workflowVersion,
170
+ workflow: JSON.stringify(input.workflow),
171
+ state: 'waiting',
172
+ requested_by: normaliseUuid(input.requestedBy),
173
+ requested_at: DbDate.toDb(new Date()),
174
+ schedule: input.schedule?.length ? JSON.stringify(input.schedule) : null,
175
+ })
176
+ .execute()
178
177
  return (await this.byKey(key)) as ApprovalRequest
179
178
  }
180
179
 
181
180
  async byKey(key: string): Promise<ApprovalRequest | undefined> {
182
- const rows = await this.#db.query('SELECT * FROM approval_request WHERE key = ?', [
183
- normaliseUuid(key),
184
- ])
185
- return rows[0] ? requestFrom(rows[0]) : undefined
181
+ const row = await this.#db.q
182
+ .selectFrom('approval_request')
183
+ .selectAll()
184
+ .where('key', '=', normaliseUuid(key))
185
+ .executeTakeFirst()
186
+ return row ? requestFrom(row) : undefined
186
187
  }
187
188
 
188
189
  /** The request a page is waiting on or scheduled under, if any: at most one at a time. */
189
190
  async openFor(nodeKey: string): Promise<ApprovalRequest | undefined> {
190
- const rows = await this.#db.query(
191
- `SELECT * FROM approval_request WHERE node_key = ? AND ${OPEN} ORDER BY id DESC`,
192
- [normaliseUuid(nodeKey)],
193
- )
194
- return rows[0] ? requestFrom(rows[0]) : undefined
191
+ const row = await this.#db.q
192
+ .selectFrom('approval_request')
193
+ .selectAll()
194
+ .where('node_key', '=', normaliseUuid(nodeKey))
195
+ .where('state', 'in', OPEN)
196
+ .orderBy('id', 'desc')
197
+ .executeTakeFirst()
198
+ return row ? requestFrom(row) : undefined
195
199
  }
196
200
 
197
201
  /** A page's requests, newest first. */
198
202
  async forNode(nodeKey: string, limit = 20): Promise<ApprovalRequest[]> {
199
- const rows = await this.#db.query(
200
- 'SELECT * FROM approval_request WHERE node_key = ? ORDER BY id DESC LIMIT ?',
201
- [normaliseUuid(nodeKey), limit],
202
- )
203
+ const rows = await this.#db.q
204
+ .selectFrom('approval_request')
205
+ .selectAll()
206
+ .where('node_key', '=', normaliseUuid(nodeKey))
207
+ .orderBy('id', 'desc')
208
+ .limit(limit)
209
+ .execute()
203
210
  return rows.map(requestFrom)
204
211
  }
205
212
 
206
213
  /** Every request still open (waiting or scheduled), oldest first. */
207
214
  async open(): Promise<ApprovalRequest[]> {
208
- const rows = await this.#db.query(`SELECT * FROM approval_request WHERE ${OPEN} ORDER BY id`)
215
+ const rows = await this.#db.q
216
+ .selectFrom('approval_request')
217
+ .selectAll()
218
+ .where('state', 'in', OPEN)
219
+ .orderBy('id')
220
+ .execute()
209
221
  return rows.map(requestFrom)
210
222
  }
211
223
 
212
224
  /** Every request approved and waiting for its time, oldest first. */
213
225
  async scheduled(): Promise<ApprovalRequest[]> {
214
- const rows = await this.#db.query(
215
- "SELECT * FROM approval_request WHERE state = 'scheduled' ORDER BY id",
216
- )
226
+ const rows = await this.#db.q
227
+ .selectFrom('approval_request')
228
+ .selectAll()
229
+ .where('state', '=', 'scheduled')
230
+ .orderBy('id')
231
+ .execute()
217
232
  return rows.map(requestFrom)
218
233
  }
219
234
 
220
235
  /** Requests that have finished, newest first. */
221
236
  async finished(skip: number, take: number): Promise<{ total: number; items: ApprovalRequest[] }> {
222
- const counted = await this.#db.query<{ n: number }>(
223
- `SELECT COUNT(*) AS n FROM approval_request WHERE NOT ${OPEN}`,
224
- )
225
- const rows = await this.#db.query(
226
- `SELECT * FROM approval_request WHERE NOT ${OPEN} ORDER BY id DESC LIMIT ? OFFSET ?`,
227
- [take, skip],
228
- )
229
- return { total: Number(counted[0]?.n ?? 0), items: rows.map(requestFrom) }
237
+ const counted = await this.#db.q
238
+ .selectFrom('approval_request')
239
+ .select((eb) => eb.fn.countAll<number>().as('n'))
240
+ .where('state', 'not in', OPEN)
241
+ .executeTakeFirst()
242
+ const rows = await this.#db.q
243
+ .selectFrom('approval_request')
244
+ .selectAll()
245
+ .where('state', 'not in', OPEN)
246
+ .orderBy('id', 'desc')
247
+ .limit(take)
248
+ .offset(skip)
249
+ .execute()
250
+ return { total: Number(counted?.n ?? 0), items: rows.map(requestFrom) }
230
251
  }
231
252
 
232
253
  async waitingUnder(workflowKey: string): Promise<ApprovalRequest[]> {
233
- const rows = await this.#db.query(
234
- "SELECT * FROM approval_request WHERE workflow_key = ? AND state = 'waiting' ORDER BY id",
235
- [workflowKey],
236
- )
254
+ const rows = await this.#db.q
255
+ .selectFrom('approval_request')
256
+ .selectAll()
257
+ .where('workflow_key', '=', workflowKey)
258
+ .where('state', '=', 'waiting')
259
+ .orderBy('id')
260
+ .execute()
237
261
  return rows.map(requestFrom)
238
262
  }
239
263
 
240
264
  async setState(id: number, state: ApprovalState): Promise<void> {
241
- await this.#db.exec('UPDATE approval_request SET state = ?, finished_at = ? WHERE id = ?', [
242
- state,
243
- state === 'waiting' || state === 'scheduled' ? null : DbDate.toDb(new Date()),
244
- id,
245
- ])
265
+ await this.#db.q
266
+ .updateTable('approval_request')
267
+ .set({
268
+ state,
269
+ finished_at: state === 'waiting' || state === 'scheduled' ? null : DbDate.toDb(new Date()),
270
+ })
271
+ .where('id', '=', id)
272
+ .execute()
246
273
  }
247
274
 
248
275
  async setEvent(id: number, eventId: number | null): Promise<void> {
249
- await this.#db.exec('UPDATE approval_request SET event_id = ? WHERE id = ?', [eventId, id])
276
+ await this.#db.q
277
+ .updateTable('approval_request')
278
+ .set({ event_id: eventId })
279
+ .where('id', '=', id)
280
+ .execute()
250
281
  }
251
282
 
252
283
  /**
@@ -254,35 +285,44 @@ export class ApprovalRepository {
254
285
  * stage visits are cleared, and its history keeps what happened.
255
286
  */
256
287
  async rebase(id: number, workflow: SchemaWorkflow, version: string): Promise<void> {
257
- await this.#db.exec(
258
- 'UPDATE approval_request SET workflow = ?, workflow_version = ? WHERE id = ?',
259
- [JSON.stringify(workflow), version, id],
260
- )
261
- await this.#db.exec('DELETE FROM approval_step WHERE request_id = ?', [id])
288
+ await this.#db.q
289
+ .updateTable('approval_request')
290
+ .set({ workflow: JSON.stringify(workflow), workflow_version: version })
291
+ .where('id', '=', id)
292
+ .execute()
293
+ await this.#db.q.deleteFrom('approval_step').where('request_id', '=', id).execute()
262
294
  }
263
295
 
264
296
  async steps(requestId: number): Promise<ApprovalStep[]> {
265
- const rows = await this.#db.query(
266
- 'SELECT * FROM approval_step WHERE request_id = ? ORDER BY id',
267
- [requestId],
268
- )
297
+ const rows = await this.#db.q
298
+ .selectFrom('approval_step')
299
+ .selectAll()
300
+ .where('request_id', '=', requestId)
301
+ .orderBy('id')
302
+ .execute()
269
303
  return rows.map(stepFrom)
270
304
  }
271
305
 
272
306
  async addStep(requestId: number, stage: string): Promise<number> {
273
- const rows = await this.#db.query<{ id: number }>(
274
- "INSERT INTO approval_step (request_id, stage, state, reached_at) VALUES (?, ?, 'waiting', ?) RETURNING id",
275
- [requestId, stage, DbDate.toDb(new Date())],
276
- )
277
- return Number(rows[0]?.id)
307
+ const row = await this.#db.q
308
+ .insertInto('approval_step')
309
+ .values({
310
+ request_id: requestId,
311
+ stage,
312
+ state: 'waiting',
313
+ reached_at: DbDate.toDb(new Date()),
314
+ })
315
+ .returning('id')
316
+ .executeTakeFirst()
317
+ return Number(row?.id)
278
318
  }
279
319
 
280
320
  async setStepState(id: number, state: ApprovalStepState): Promise<void> {
281
- await this.#db.exec('UPDATE approval_step SET state = ?, done_at = ? WHERE id = ?', [
282
- state,
283
- state === 'waiting' ? null : DbDate.toDb(new Date()),
284
- id,
285
- ])
321
+ await this.#db.q
322
+ .updateTable('approval_step')
323
+ .set({ state, done_at: state === 'waiting' ? null : DbDate.toDb(new Date()) })
324
+ .where('id', '=', id)
325
+ .execute()
286
326
  }
287
327
 
288
328
  async record(
@@ -295,26 +335,27 @@ export class ApprovalRepository {
295
335
  comment?: string | null
296
336
  },
297
337
  ): Promise<void> {
298
- await this.#db.exec(
299
- `INSERT INTO approval_history (request_id, step_id, stage, kind, user_key, comment, create_date)
300
- VALUES (?, ?, ?, ?, ?, ?, ?)`,
301
- [
302
- requestId,
303
- entry.stepId ?? null,
304
- entry.stage ?? null,
305
- entry.kind,
306
- entry.userKey ? normaliseUuid(entry.userKey) : null,
307
- entry.comment ?? null,
308
- DbDate.toDb(new Date()),
309
- ],
310
- )
338
+ await this.#db.q
339
+ .insertInto('approval_history')
340
+ .values({
341
+ request_id: requestId,
342
+ step_id: entry.stepId ?? null,
343
+ stage: entry.stage ?? null,
344
+ kind: entry.kind,
345
+ user_key: entry.userKey ? normaliseUuid(entry.userKey) : null,
346
+ comment: entry.comment ?? null,
347
+ create_date: DbDate.toDb(new Date()),
348
+ })
349
+ .execute()
311
350
  }
312
351
 
313
352
  async history(requestId: number): Promise<ApprovalHistoryEntry[]> {
314
- const rows = await this.#db.query(
315
- 'SELECT * FROM approval_history WHERE request_id = ? ORDER BY id',
316
- [requestId],
317
- )
353
+ const rows = await this.#db.q
354
+ .selectFrom('approval_history')
355
+ .selectAll()
356
+ .where('request_id', '=', requestId)
357
+ .orderBy('id')
358
+ .execute()
318
359
  return rows.map(historyFrom)
319
360
  }
320
361
  }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * What an administrator granted each bundle (story 222): one row per bundle,
3
+ * replaced on each grant and deleted on withdrawal. docs/bundles.md,
4
+ * "Permissions".
5
+ */
6
+ import type { Db } from '../database.ts'
7
+ import { DbDate } from '../dialect.ts'
8
+
9
+ export interface BundleGrant {
10
+ bundleId: string
11
+ packageName: string
12
+ version: string
13
+ permissions: string[]
14
+ grantedBy: string | null
15
+ grantedAt: Date
16
+ }
17
+
18
+ const permissionsOf = (value: unknown): string[] => {
19
+ try {
20
+ const parsed = JSON.parse(String(value)) as unknown
21
+ return Array.isArray(parsed) ? parsed.filter((p): p is string => typeof p === 'string') : []
22
+ } catch {
23
+ return []
24
+ }
25
+ }
26
+
27
+ export class BundleGrantRepository {
28
+ constructor(private readonly db: Db) {}
29
+
30
+ async all(): Promise<BundleGrant[]> {
31
+ const rows = await this.db.q
32
+ .selectFrom('bundle_grant')
33
+ .selectAll()
34
+ .orderBy('bundle_id')
35
+ .execute()
36
+ return rows.map((row) => ({
37
+ bundleId: row.bundle_id,
38
+ packageName: row.package_name,
39
+ version: row.version,
40
+ permissions: permissionsOf(row.permissions),
41
+ grantedBy: row.granted_by ?? null,
42
+ grantedAt: DbDate.fromDb(row.granted_at) ?? new Date(0),
43
+ }))
44
+ }
45
+
46
+ async get(bundleId: string): Promise<BundleGrant | undefined> {
47
+ return (await this.all()).find((grant) => grant.bundleId === bundleId)
48
+ }
49
+
50
+ /** Grants this list, replacing whatever was granted before. */
51
+ async grant(grant: Omit<BundleGrant, 'grantedAt'>): Promise<void> {
52
+ await this.db.transaction(async (tx) => {
53
+ await tx.q.deleteFrom('bundle_grant').where('bundle_id', '=', grant.bundleId).execute()
54
+ await tx.q
55
+ .insertInto('bundle_grant')
56
+ .values({
57
+ bundle_id: grant.bundleId,
58
+ package_name: grant.packageName,
59
+ version: grant.version,
60
+ permissions: JSON.stringify(grant.permissions),
61
+ granted_by: grant.grantedBy,
62
+ granted_at: DbDate.toDb(new Date()),
63
+ })
64
+ .execute()
65
+ })
66
+ }
67
+
68
+ /** Withdraws a bundle's grant; true when there was one. */
69
+ async withdraw(bundleId: string): Promise<boolean> {
70
+ const before = await this.get(bundleId)
71
+ await this.db.q.deleteFrom('bundle_grant').where('bundle_id', '=', bundleId).execute()
72
+ return before !== undefined
73
+ }
74
+ }
@@ -9,6 +9,7 @@
9
9
  import { randomUUID } from 'node:crypto'
10
10
  import type { Db } from '../database.ts'
11
11
  import { DbDate, fromDbBool } from '../dialect.ts'
12
+ import { sql } from '../query.ts'
12
13
 
13
14
  export interface Bundle {
14
15
  id: string
@@ -64,12 +65,45 @@ interface Row {
64
65
  update_date: unknown
65
66
  }
66
67
 
67
- const SELECT = `SELECT id, name, content_node_id, content_load_child_nodes,
68
- media_load_child_nodes, media_ids, element_ids, content_types,
69
- media_types, data_types, components, stylesheets,
70
- scripts, languages, dictionary_items, package_name, package_version,
71
- create_date, update_date
72
- FROM bundle`
68
+ const COLUMNS = [
69
+ 'id',
70
+ 'name',
71
+ 'content_node_id',
72
+ 'content_load_child_nodes',
73
+ 'media_load_child_nodes',
74
+ 'media_ids',
75
+ 'element_ids',
76
+ 'content_types',
77
+ 'media_types',
78
+ 'data_types',
79
+ 'components',
80
+ 'stylesheets',
81
+ 'scripts',
82
+ 'languages',
83
+ 'dictionary_items',
84
+ 'package_name',
85
+ 'package_version',
86
+ 'create_date',
87
+ 'update_date',
88
+ ] as const
89
+
90
+ /** The selection lists as stored: JSON text, one column each. */
91
+ const selections = (db: Db, input: BundleInput) => ({
92
+ name: input.name,
93
+ content_node_id: input.contentNodeId,
94
+ content_load_child_nodes: db.dialect.boolValue(input.contentLoadChildNodes),
95
+ media_load_child_nodes: db.dialect.boolValue(input.mediaLoadChildNodes),
96
+ media_ids: JSON.stringify(input.mediaIds),
97
+ element_ids: input.elementIds === null ? null : JSON.stringify(input.elementIds),
98
+ content_types: JSON.stringify(input.contentTypes),
99
+ media_types: JSON.stringify(input.mediaTypes),
100
+ data_types: JSON.stringify(input.dataTypes),
101
+ components: JSON.stringify(input.components),
102
+ stylesheets: JSON.stringify(input.stylesheets),
103
+ scripts: JSON.stringify(input.scripts),
104
+ languages: JSON.stringify(input.languages),
105
+ dictionary_items: JSON.stringify(input.dictionaryItems),
106
+ })
73
107
 
74
108
  const list = (value: string | null): string[] => {
75
109
  if (!value) return []
@@ -115,57 +149,49 @@ export class BundleRepository {
115
149
  async list(
116
150
  options: { skip?: number; take?: number } = {},
117
151
  ): Promise<{ total: number; items: Bundle[] }> {
118
- const counted = await this.#db.query<{ total: number }>('SELECT COUNT(*) AS total FROM bundle')
119
- const rows = await this.#db.query<Row>(`${SELECT} ORDER BY name LIMIT ? OFFSET ?`, [
120
- options.take ?? 100,
121
- options.skip ?? 0,
122
- ])
123
- return { total: Number(counted[0]?.total ?? 0), items: rows.map(hydrate) }
152
+ const counted = await this.#db.q
153
+ .selectFrom('bundle')
154
+ .select(sql<number>`count(*)`.as('total'))
155
+ .executeTakeFirst()
156
+ const rows = await this.#db.q
157
+ .selectFrom('bundle')
158
+ .select(COLUMNS)
159
+ .orderBy('name')
160
+ .limit(options.take ?? 100)
161
+ .offset(options.skip ?? 0)
162
+ .execute()
163
+ return { total: Number(counted?.total ?? 0), items: rows.map(hydrate) }
124
164
  }
125
165
 
126
166
  async byId(id: string): Promise<Bundle | undefined> {
127
- const rows = await this.#db.query<Row>(`${SELECT} WHERE id = ?`, [id])
128
- const row = rows[0]
167
+ const row = await this.#db.q
168
+ .selectFrom('bundle')
169
+ .select(COLUMNS)
170
+ .where('id', '=', id)
171
+ .executeTakeFirst()
129
172
  return row ? hydrate(row) : undefined
130
173
  }
131
174
 
132
175
  async byName(name: string): Promise<Bundle | undefined> {
133
- const rows = await this.#db.query<Row>(`${SELECT} WHERE LOWER(name) = ?`, [
134
- name.trim().toLowerCase(),
135
- ])
136
- const row = rows[0]
176
+ const row = await this.#db.q
177
+ .selectFrom('bundle')
178
+ .select(COLUMNS)
179
+ .where((eb) => eb(eb.fn('lower', ['name']), '=', name.trim().toLowerCase()))
180
+ .executeTakeFirst()
137
181
  return row ? hydrate(row) : undefined
138
182
  }
139
183
 
140
184
  async create(input: BundleInput, id: string = randomUUID()): Promise<Bundle> {
141
185
  const now = new Date()
142
- await this.#db.exec(
143
- `INSERT INTO bundle
144
- (id, name, content_node_id, content_load_child_nodes, media_load_child_nodes,
145
- media_ids, element_ids, content_types, media_types, data_types, components,
146
- stylesheets, scripts, languages, dictionary_items,
147
- create_date, update_date)
148
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
149
- [
186
+ await this.#db.q
187
+ .insertInto('bundle')
188
+ .values({
150
189
  id,
151
- input.name,
152
- input.contentNodeId,
153
- this.#db.dialect.boolValue(input.contentLoadChildNodes),
154
- this.#db.dialect.boolValue(input.mediaLoadChildNodes),
155
- JSON.stringify(input.mediaIds),
156
- input.elementIds === null ? null : JSON.stringify(input.elementIds),
157
- JSON.stringify(input.contentTypes),
158
- JSON.stringify(input.mediaTypes),
159
- JSON.stringify(input.dataTypes),
160
- JSON.stringify(input.components),
161
- JSON.stringify(input.stylesheets),
162
- JSON.stringify(input.scripts),
163
- JSON.stringify(input.languages),
164
- JSON.stringify(input.dictionaryItems),
165
- DbDate.toDb(now),
166
- DbDate.toDb(now),
167
- ],
168
- )
190
+ ...selections(this.#db, input),
191
+ create_date: DbDate.toDb(now),
192
+ update_date: DbDate.toDb(now),
193
+ })
194
+ .execute()
169
195
  return {
170
196
  ...input,
171
197
  id,
@@ -177,33 +203,11 @@ export class BundleRepository {
177
203
  }
178
204
 
179
205
  async update(id: string, input: BundleInput): Promise<void> {
180
- await this.#db.exec(
181
- `UPDATE bundle
182
- SET name = ?, content_node_id = ?, content_load_child_nodes = ?,
183
- media_load_child_nodes = ?, media_ids = ?, element_ids = ?,
184
- content_types = ?, media_types = ?, data_types = ?, components = ?,
185
- stylesheets = ?, scripts = ?, languages = ?,
186
- dictionary_items = ?, update_date = ?
187
- WHERE id = ?`,
188
- [
189
- input.name,
190
- input.contentNodeId,
191
- this.#db.dialect.boolValue(input.contentLoadChildNodes),
192
- this.#db.dialect.boolValue(input.mediaLoadChildNodes),
193
- JSON.stringify(input.mediaIds),
194
- input.elementIds === null ? null : JSON.stringify(input.elementIds),
195
- JSON.stringify(input.contentTypes),
196
- JSON.stringify(input.mediaTypes),
197
- JSON.stringify(input.dataTypes),
198
- JSON.stringify(input.components),
199
- JSON.stringify(input.stylesheets),
200
- JSON.stringify(input.scripts),
201
- JSON.stringify(input.languages),
202
- JSON.stringify(input.dictionaryItems),
203
- DbDate.toDb(new Date()),
204
- id,
205
- ],
206
- )
206
+ await this.#db.q
207
+ .updateTable('bundle')
208
+ .set({ ...selections(this.#db, input), update_date: DbDate.toDb(new Date()) })
209
+ .where('id', '=', id)
210
+ .execute()
207
211
  }
208
212
 
209
213
  /** The npm package a definition is built as; null clears a value back to its default. */
@@ -211,13 +215,18 @@ export class BundleRepository {
211
215
  id: string,
212
216
  pkg: { name: string | null; version: string | null },
213
217
  ): Promise<void> {
214
- await this.#db.exec(
215
- 'UPDATE bundle SET package_name = ?, package_version = ?, update_date = ? WHERE id = ?',
216
- [pkg.name, pkg.version, DbDate.toDb(new Date()), id],
217
- )
218
+ await this.#db.q
219
+ .updateTable('bundle')
220
+ .set({
221
+ package_name: pkg.name,
222
+ package_version: pkg.version,
223
+ update_date: DbDate.toDb(new Date()),
224
+ })
225
+ .where('id', '=', id)
226
+ .execute()
218
227
  }
219
228
 
220
229
  async delete(id: string): Promise<void> {
221
- await this.#db.exec('DELETE FROM bundle WHERE id = ?', [id])
230
+ await this.#db.q.deleteFrom('bundle').where('id', '=', id).execute()
222
231
  }
223
232
  }