@crombolo/data 0.51.1 → 0.53.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/data",
3
- "version": "0.51.1",
3
+ "version": "0.53.2",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. SQLite and Postgres storage for crombolo: schema, migrations and repositories.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -28,7 +28,7 @@
28
28
  "access": "public"
29
29
  },
30
30
  "dependencies": {
31
- "@crombolo/core": "0.51.1",
31
+ "@crombolo/core": "0.53.2",
32
32
  "kysely": "0.29.6"
33
33
  }
34
34
  }
package/src/cluster.ts CHANGED
@@ -276,42 +276,34 @@ export async function touchServer(db: Db, identity: ServerIdentity): Promise<voi
276
276
  const now = DbDate.toDb(new Date())
277
277
  const role = identity.role ?? null
278
278
  const crombolo = identity.cromboloVersion ?? null
279
- const existing = await db.q
280
- .selectFrom('server')
281
- .select('node_id')
282
- .where('node_id', '=', identity.nodeId)
283
- .executeTakeFirst()
284
279
  const served = identity.served?.checkpoint ?? null
285
280
  const servedAt = identity.served ? DbDate.toDb(identity.served.at) : null
286
- if (existing) {
287
- await db.q
288
- .updateTable('server')
289
- .set({
281
+ // One statement, so a boot and a poll writing at once can't both find no row and
282
+ // both insert one.
283
+ await db.q
284
+ .insertInto('server')
285
+ .values({
286
+ node_id: identity.nodeId,
287
+ version: identity.version,
288
+ revision: identity.revision,
289
+ role,
290
+ crombolo_version: crombolo,
291
+ last_seen: now,
292
+ served_checkpoint: served,
293
+ served_at: servedAt,
294
+ })
295
+ .onConflict((conflict) =>
296
+ conflict.column('node_id').doUpdateSet({
290
297
  version: identity.version,
291
298
  revision: identity.revision,
292
299
  role,
293
300
  crombolo_version: crombolo,
294
301
  last_seen: now,
295
- served_checkpoint: sql<number>`coalesce(${served}, ${sql.ref('served_checkpoint')})`,
296
- served_at: sql<string>`coalesce(${servedAt}, ${sql.ref('served_at')})`,
297
- })
298
- .where('node_id', '=', identity.nodeId)
299
- .execute()
300
- } else {
301
- await db.q
302
- .insertInto('server')
303
- .values({
304
- node_id: identity.nodeId,
305
- version: identity.version,
306
- revision: identity.revision,
307
- role,
308
- crombolo_version: crombolo,
309
- last_seen: now,
310
- served_checkpoint: served,
311
- served_at: servedAt,
312
- })
313
- .execute()
314
- }
302
+ served_checkpoint: sql<number>`coalesce(${served}, ${sql.ref('server.served_checkpoint')})`,
303
+ served_at: sql<string>`coalesce(${servedAt}, ${sql.ref('server.served_at')})`,
304
+ }),
305
+ )
306
+ .execute()
315
307
  }
316
308
 
317
309
  export interface LiveNode {
@@ -7,6 +7,7 @@
7
7
  * properties".
8
8
  */
9
9
  import {
10
+ dateFromText,
10
11
  ObjectTypes,
11
12
  STORAGE_COLUMN,
12
13
  type StartNodes,
@@ -130,8 +131,8 @@ export function parseContentQuery(body: unknown): ParsedQuery {
130
131
  const out: { from?: Date; to?: Date } = {}
131
132
  for (const end of ['from', 'to'] as const) {
132
133
  if (value[end] === undefined) continue
133
- const date = typeof value[end] === 'string' ? new Date(value[end] as string) : undefined
134
- if (!date || Number.isNaN(date.getTime())) problems.push(`"${field}.${end}" must be a date.`)
134
+ const date = typeof value[end] === 'string' ? dateFromText(value[end] as string) : undefined
135
+ if (!date) problems.push(`"${field}.${end}" must be a date.`)
135
136
  else out[end] = date
136
137
  }
137
138
  return out
@@ -633,8 +634,8 @@ function comparable(value: unknown, storage: ValueStorageTypeName, alias: string
633
634
  return n
634
635
  }
635
636
  if (storage === 'Date') {
636
- const date = new Date(String(value))
637
- if (Number.isNaN(date.getTime())) throw new ContentQueryRefused(`"${alias}" compares dates.`)
637
+ const date = dateFromText(String(value))
638
+ if (!date) throw new ContentQueryRefused(`"${alias}" compares dates.`)
638
639
  return DbDate.toDb(date)
639
640
  }
640
641
  if (typeof value === 'object') throw new ContentQueryRefused(`"${alias}" compares text.`)
@@ -14,6 +14,7 @@ import {
14
14
  type DocumentValue,
15
15
  type DocumentVariant,
16
16
  type DocumentVersionSummary,
17
+ dateFromText,
17
18
  variesByCulture as flagVariesByCulture,
18
19
  type MediaItemAccess,
19
20
  normaliseUuid,
@@ -2420,12 +2421,8 @@ export class DocumentRepository {
2420
2421
  if (value instanceof Date) return DbDate.toDb(value)
2421
2422
  // The date pickers send wall-clock time with no zone ("2026-01-02 03:04:05");
2422
2423
  // it is kept as that wall-clock time, not shifted by the server's zone.
2423
- const text = String(value).trim()
2424
- const naive = /^\d{4}-\d{2}-\d{2}([ T]\d{2}:\d{2}(:\d{2}(\.\d+)?)?)?$/.test(text)
2425
- const date = new Date(
2426
- naive ? `${text.replace(' ', 'T')}${text.length === 10 ? 'T00:00:00' : ''}Z` : text,
2427
- )
2428
- return Number.isNaN(date.getTime()) ? null : DbDate.toDb(date)
2424
+ const date = dateFromText(String(value))
2425
+ return date ? DbDate.toDb(date) : null
2429
2426
  }
2430
2427
  default:
2431
2428
  return typeof value === 'string' ? value : JSON.stringify(value)
@@ -34,7 +34,8 @@ export function normaliseDomainName(name: string): string {
34
34
  .toLowerCase()
35
35
  }
36
36
 
37
- const VALID_DOMAIN = /^[a-z0-9.-]+(:\d+)?(\/[a-z0-9._~%-]+)*$/i
37
+ /** A hostname with an optional path, or a path alone, which answers on any hostname. */
38
+ const VALID_DOMAIN = /^(?:[a-z0-9.-]+(:\d+)?(\/[a-z0-9._~%-]+)*|(\/[a-z0-9._~%-]+)+)$/i
38
39
 
39
40
  export class DomainRepository {
40
41
  #db: Db
@@ -65,6 +65,8 @@ export interface FormEntryQuery {
65
65
  /** Only the entries that came through this Library form; `null` for those that came through none. */
66
66
  libraryKey?: string | null
67
67
  state?: FormEntryState
68
+ /** Only the entries filled in in this language. */
69
+ culture?: string
68
70
  /** Inclusive; the entries view defaults to the last month. */
69
71
  from?: Date
70
72
  to?: Date
@@ -185,6 +187,19 @@ export class FormEntryRepository {
185
187
  return hydrate(row, (await this.#valuesFor([id])).get(id) ?? [])
186
188
  }
187
189
 
190
+ /** The languages a form's entries were filled in. */
191
+ async languages(formKey: string): Promise<string[]> {
192
+ const rows = await this.#db.q
193
+ .selectFrom('form_entry')
194
+ .select('culture')
195
+ .distinct()
196
+ .where('form_key', '=', formKey)
197
+ .where('culture', 'is not', null)
198
+ .orderBy('culture')
199
+ .execute()
200
+ return rows.flatMap((row) => (row.culture ? [row.culture] : []))
201
+ }
202
+
188
203
  async list(query: FormEntryQuery): Promise<{ total: number; items: FormEntry[] }> {
189
204
  const search = query.search ? `%${query.search.toLowerCase()}%` : undefined
190
205
  const matching = this.#db.q
@@ -196,6 +211,7 @@ export class FormEntryRepository {
196
211
  : q.where('library_key', '=', query.libraryKey as string),
197
212
  )
198
213
  .$if(Boolean(query.state), (q) => q.where('state', '=', query.state as FormEntryState))
214
+ .$if(Boolean(query.culture), (q) => q.where('culture', '=', query.culture as string))
199
215
  .$if(Boolean(query.from), (q) =>
200
216
  q.where('create_date', '>=', DbDate.toDb(query.from as Date)),
201
217
  )