@crombolo/data 0.36.2 → 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.
- package/package.json +3 -2
- package/src/audit.ts +163 -133
- package/src/change-report.ts +66 -43
- package/src/cluster.ts +119 -59
- package/src/content-query.ts +205 -200
- package/src/{bundle-tables → custom-tables}/builder.ts +104 -12
- package/src/custom-tables/chain.ts +150 -0
- package/src/custom-tables/ddl.ts +159 -0
- package/src/custom-tables/handle.ts +141 -0
- package/src/{bundle-tables → custom-tables}/index.ts +3 -2
- package/src/custom-tables/names.ts +94 -0
- package/src/custom-tables/owners.ts +299 -0
- package/src/{bundle-tables → custom-tables}/schema.ts +263 -90
- package/src/custom-tables/values.ts +181 -0
- package/src/database.ts +40 -5
- package/src/dialect.ts +43 -16
- package/src/index.ts +8 -1
- package/src/migrations.ts +130 -88
- package/src/query.ts +158 -0
- package/src/relations.ts +49 -28
- package/src/repositories/approvals.ts +130 -89
- package/src/repositories/bundle-grants.ts +74 -0
- package/src/repositories/bundles.ts +85 -76
- package/src/repositories/content-applied.ts +47 -28
- package/src/repositories/content-events.ts +114 -67
- package/src/repositories/content-notifications.ts +102 -74
- package/src/repositories/content-types.ts +449 -320
- package/src/repositories/data-types.ts +105 -56
- package/src/repositories/delivery-member-tokens.ts +37 -28
- package/src/repositories/dictionary.ts +77 -58
- package/src/repositories/documents.ts +956 -580
- package/src/repositories/domains.ts +28 -16
- package/src/repositories/dynamic-roots.ts +32 -17
- package/src/repositories/external-logins.ts +38 -24
- package/src/repositories/folders.ts +7 -4
- package/src/repositories/form-entries.ts +120 -103
- package/src/repositories/form-workflows.ts +87 -62
- package/src/repositories/jobs.ts +296 -0
- package/src/repositories/languages.ts +51 -38
- package/src/repositories/log-searches.ts +7 -5
- package/src/repositories/media-access.ts +173 -123
- package/src/repositories/media-listing.ts +155 -92
- package/src/repositories/members.ts +189 -162
- package/src/repositories/nodes.ts +227 -181
- package/src/repositories/notifications.ts +22 -17
- package/src/repositories/public-access.ts +77 -61
- package/src/repositories/redirects.ts +113 -70
- package/src/repositories/references.ts +55 -44
- package/src/repositories/remote-jobs.ts +45 -35
- package/src/repositories/tags.ts +11 -10
- package/src/repositories/templates.ts +24 -14
- package/src/repositories/transfer-answers.ts +58 -50
- package/src/repositories/transfer-runs.ts +93 -75
- package/src/repositories/two-factor.ts +74 -55
- package/src/repositories/user-data.ts +54 -36
- package/src/repositories/users.ts +345 -300
- package/src/repositories/webhooks.ts +196 -137
- package/src/schema/bundle-grants.ts +32 -0
- package/src/schema/jobs.ts +56 -0
- package/src/schema/plan.ts +4 -0
- package/src/schema-ops.ts +15 -12
- package/src/schema-state.ts +53 -38
- package/src/search.ts +175 -129
- package/src/setup.ts +17 -13
- package/src/site-search.ts +134 -83
- package/src/table-types.ts +152 -0
- package/src/tables.gen.ts +859 -0
- package/src/tree-invariants.ts +21 -12
- package/src/url-options.ts +17 -13
- package/src/workflow-store.ts +17 -13
- package/src/bundle-tables/capability.ts +0 -377
- package/src/bundle-tables/chain.ts +0 -105
- package/src/bundle-tables/ddl.ts +0 -124
- package/src/bundle-tables/guard.ts +0 -116
- package/src/bundle-tables/names.ts +0 -58
package/src/cluster.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { Db } from './database.ts'
|
|
7
7
|
import { DbDate } from './dialect.ts'
|
|
8
|
+
import { sql } from './query.ts'
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* `views` is a code change, not a data one: a template was written, so every
|
|
@@ -76,17 +77,31 @@ export async function instructionsBetween(
|
|
|
76
77
|
after: number,
|
|
77
78
|
upTo: number,
|
|
78
79
|
): Promise<CacheInstruction[]> {
|
|
79
|
-
const rows = await db.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
80
|
+
const rows = await db.q
|
|
81
|
+
.selectFrom('cache_instruction')
|
|
82
|
+
.select(INSTRUCTION_COLUMNS)
|
|
83
|
+
.where('checkpoint', '>', after)
|
|
84
|
+
.where('checkpoint', '<=', upTo)
|
|
85
|
+
.orderBy('checkpoint')
|
|
86
|
+
.execute()
|
|
84
87
|
return rows.map(instructionOf)
|
|
85
88
|
}
|
|
86
89
|
|
|
87
90
|
/** Where Postgres says an instruction was written, so nodes poll now rather than on their timer. */
|
|
88
91
|
export const CACHE_INSTRUCTION_CHANNEL = 'crombolo_cache_instruction'
|
|
89
92
|
|
|
93
|
+
/** Where a node that leaves its jobs to a worker says it left work (story 223). */
|
|
94
|
+
export const JOBS_CHANNEL = 'crombolo_jobs'
|
|
95
|
+
|
|
96
|
+
/** What a write left for the jobs: the event outbox, the search queue, or a queued job. */
|
|
97
|
+
export type JobWake = 'events' | 'search' | 'jobs'
|
|
98
|
+
|
|
99
|
+
/** Tells a listening worker there is work; Postgres only, which a worker needs. */
|
|
100
|
+
export async function notifyJobs(db: Db, work: JobWake): Promise<void> {
|
|
101
|
+
if (db.dialect.name !== 'postgres') return
|
|
102
|
+
await db.q.selectNoFrom(sql`pg_notify(${JOBS_CHANNEL}, ${work})`.as('n')).execute()
|
|
103
|
+
}
|
|
104
|
+
|
|
90
105
|
/**
|
|
91
106
|
* Tells the other nodes what changed. A `content` or `schema` instruction moves
|
|
92
107
|
* the content checkpoint (story 174): called on the change's own transaction,
|
|
@@ -101,19 +116,21 @@ export async function appendCacheInstruction(
|
|
|
101
116
|
): Promise<number | null> {
|
|
102
117
|
return db.transaction(async (tx) => {
|
|
103
118
|
const checkpoint = instruction.kind === 'components' ? null : await advanceCheckpoint(tx)
|
|
104
|
-
await tx.
|
|
105
|
-
'
|
|
106
|
-
|
|
107
|
-
instruction.kind,
|
|
108
|
-
JSON.stringify(instruction.payload ?? {}),
|
|
109
|
-
DbDate.toDb(new Date()),
|
|
110
|
-
instruction.by ?? null,
|
|
119
|
+
await tx.q
|
|
120
|
+
.insertInto('cache_instruction')
|
|
121
|
+
.values({
|
|
122
|
+
kind: instruction.kind,
|
|
123
|
+
payload: JSON.stringify(instruction.payload ?? {}),
|
|
124
|
+
created_at: DbDate.toDb(new Date()),
|
|
125
|
+
created_by: instruction.by ?? null,
|
|
111
126
|
checkpoint,
|
|
112
|
-
|
|
113
|
-
|
|
127
|
+
})
|
|
128
|
+
.execute()
|
|
114
129
|
// Delivered at commit, so a write that rolls back wakes nobody.
|
|
115
130
|
if (tx.dialect.name === 'postgres')
|
|
116
|
-
await tx.
|
|
131
|
+
await tx.q
|
|
132
|
+
.selectNoFrom(sql`pg_notify(${CACHE_INSTRUCTION_CHANNEL}, ${instruction.kind})`.as('n'))
|
|
133
|
+
.execute()
|
|
117
134
|
return checkpoint
|
|
118
135
|
})
|
|
119
136
|
}
|
|
@@ -123,11 +140,15 @@ export async function appendCacheInstruction(
|
|
|
123
140
|
* so a row lost to a truncate is started again rather than silently not counted.
|
|
124
141
|
*/
|
|
125
142
|
async function advanceCheckpoint(tx: Db): Promise<number> {
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
143
|
+
const row = await tx.q
|
|
144
|
+
.insertInto('content_checkpoint')
|
|
145
|
+
.values({ id: 1, n: 1 })
|
|
146
|
+
.onConflict((oc) =>
|
|
147
|
+
oc.column('id').doUpdateSet((eb) => ({ n: eb('content_checkpoint.n', '+', 1) })),
|
|
148
|
+
)
|
|
149
|
+
.returning('n')
|
|
150
|
+
.executeTakeFirst()
|
|
151
|
+
return Number(row?.n ?? 0)
|
|
131
152
|
}
|
|
132
153
|
|
|
133
154
|
/**
|
|
@@ -152,17 +173,22 @@ export async function waitForCheckpoint(
|
|
|
152
173
|
|
|
153
174
|
/** When the first change after `checkpoint` was recorded: how long a node serving it has been behind. */
|
|
154
175
|
export async function changedSince(db: Db, checkpoint: number): Promise<Date | undefined> {
|
|
155
|
-
const
|
|
156
|
-
'
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
176
|
+
const row = await db.q
|
|
177
|
+
.selectFrom('cache_instruction')
|
|
178
|
+
.select((eb) => eb.fn.min('created_at').as('at'))
|
|
179
|
+
.where('checkpoint', '>', checkpoint)
|
|
180
|
+
.executeTakeFirst()
|
|
181
|
+
return DbDate.fromDb(row?.at) ?? undefined
|
|
160
182
|
}
|
|
161
183
|
|
|
162
184
|
/** The latest committed checkpoint: every change numbered at or below it has committed. */
|
|
163
185
|
export async function latestCheckpoint(db: Db): Promise<number> {
|
|
164
|
-
const
|
|
165
|
-
|
|
186
|
+
const row = await db.q
|
|
187
|
+
.selectFrom('content_checkpoint')
|
|
188
|
+
.select('n')
|
|
189
|
+
.where('id', '=', 1)
|
|
190
|
+
.executeTakeFirst()
|
|
191
|
+
return Number(row?.n ?? 0)
|
|
166
192
|
}
|
|
167
193
|
|
|
168
194
|
/**
|
|
@@ -171,28 +197,36 @@ export async function latestCheckpoint(db: Db): Promise<number> {
|
|
|
171
197
|
* nothing older is ever read again.
|
|
172
198
|
*/
|
|
173
199
|
export async function pruneCacheInstructions(db: Db, before: Date): Promise<number> {
|
|
174
|
-
const rows = await db.
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
200
|
+
const rows = await db.q
|
|
201
|
+
.deleteFrom('cache_instruction')
|
|
202
|
+
.where('created_at', '<', DbDate.toDb(before))
|
|
203
|
+
.where('id', '<', (eb) => eb.selectFrom('cache_instruction').select(eb.fn.max('id').as('max')))
|
|
204
|
+
.returning('id')
|
|
205
|
+
.execute()
|
|
179
206
|
return rows.length
|
|
180
207
|
}
|
|
181
208
|
|
|
182
209
|
/** The highest instruction id, so a fresh node ignores history. */
|
|
183
210
|
export async function latestCacheInstructionId(db: Db): Promise<number> {
|
|
184
|
-
const
|
|
185
|
-
|
|
211
|
+
const row = await db.q
|
|
212
|
+
.selectFrom('cache_instruction')
|
|
213
|
+
.select((eb) => eb.fn.max('id').as('n'))
|
|
214
|
+
.executeTakeFirst()
|
|
215
|
+
return Number(row?.n ?? 0)
|
|
186
216
|
}
|
|
187
217
|
|
|
188
218
|
export async function cacheInstructionsAfter(db: Db, afterId: number): Promise<CacheInstruction[]> {
|
|
189
|
-
const rows = await db.
|
|
190
|
-
'
|
|
191
|
-
|
|
192
|
-
|
|
219
|
+
const rows = await db.q
|
|
220
|
+
.selectFrom('cache_instruction')
|
|
221
|
+
.select(INSTRUCTION_COLUMNS)
|
|
222
|
+
.where('id', '>', afterId)
|
|
223
|
+
.orderBy('id')
|
|
224
|
+
.execute()
|
|
193
225
|
return rows.map(instructionOf)
|
|
194
226
|
}
|
|
195
227
|
|
|
228
|
+
const INSTRUCTION_COLUMNS = ['id', 'kind', 'payload', 'created_by', 'checkpoint'] as const
|
|
229
|
+
|
|
196
230
|
const instructionOf = (row: Record<string, unknown>): CacheInstruction => ({
|
|
197
231
|
id: Number(row.id),
|
|
198
232
|
kind: String(row.kind) as CacheInstructionKind,
|
|
@@ -219,25 +253,41 @@ export async function touchServer(db: Db, identity: ServerIdentity): Promise<voi
|
|
|
219
253
|
const now = DbDate.toDb(new Date())
|
|
220
254
|
const role = identity.role ?? null
|
|
221
255
|
const crombolo = identity.cromboloVersion ?? null
|
|
222
|
-
const existing = await db.
|
|
223
|
-
'
|
|
224
|
-
|
|
225
|
-
|
|
256
|
+
const existing = await db.q
|
|
257
|
+
.selectFrom('server')
|
|
258
|
+
.select('node_id')
|
|
259
|
+
.where('node_id', '=', identity.nodeId)
|
|
260
|
+
.executeTakeFirst()
|
|
226
261
|
const served = identity.served?.checkpoint ?? null
|
|
227
262
|
const servedAt = identity.served ? DbDate.toDb(identity.served.at) : null
|
|
228
|
-
if (existing
|
|
229
|
-
await db.
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
263
|
+
if (existing) {
|
|
264
|
+
await db.q
|
|
265
|
+
.updateTable('server')
|
|
266
|
+
.set({
|
|
267
|
+
version: identity.version,
|
|
268
|
+
revision: identity.revision,
|
|
269
|
+
role,
|
|
270
|
+
crombolo_version: crombolo,
|
|
271
|
+
last_seen: now,
|
|
272
|
+
served_checkpoint: sql<number>`coalesce(${served}, ${sql.ref('served_checkpoint')})`,
|
|
273
|
+
served_at: sql<string>`coalesce(${servedAt}, ${sql.ref('served_at')})`,
|
|
274
|
+
})
|
|
275
|
+
.where('node_id', '=', identity.nodeId)
|
|
276
|
+
.execute()
|
|
235
277
|
} else {
|
|
236
|
-
await db.
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
278
|
+
await db.q
|
|
279
|
+
.insertInto('server')
|
|
280
|
+
.values({
|
|
281
|
+
node_id: identity.nodeId,
|
|
282
|
+
version: identity.version,
|
|
283
|
+
revision: identity.revision,
|
|
284
|
+
role,
|
|
285
|
+
crombolo_version: crombolo,
|
|
286
|
+
last_seen: now,
|
|
287
|
+
served_checkpoint: served,
|
|
288
|
+
served_at: servedAt,
|
|
289
|
+
})
|
|
290
|
+
.execute()
|
|
241
291
|
}
|
|
242
292
|
}
|
|
243
293
|
|
|
@@ -257,11 +307,21 @@ export interface LiveNode {
|
|
|
257
307
|
|
|
258
308
|
/** The nodes that have polled within `seenWithinMs`: the cluster as it stands. */
|
|
259
309
|
export async function liveNodes(db: Db, options: { seenWithinMs: number }): Promise<LiveNode[]> {
|
|
260
|
-
const rows = await db.
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
310
|
+
const rows = await db.q
|
|
311
|
+
.selectFrom('server')
|
|
312
|
+
.select([
|
|
313
|
+
'node_id',
|
|
314
|
+
'role',
|
|
315
|
+
'version',
|
|
316
|
+
'revision',
|
|
317
|
+
'crombolo_version',
|
|
318
|
+
'last_seen',
|
|
319
|
+
'served_checkpoint',
|
|
320
|
+
'served_at',
|
|
321
|
+
])
|
|
322
|
+
.where('last_seen', '>=', DbDate.toDb(new Date(Date.now() - options.seenWithinMs)))
|
|
323
|
+
.orderBy('node_id')
|
|
324
|
+
.execute()
|
|
265
325
|
return rows.map((row) => ({
|
|
266
326
|
nodeId: String(row.node_id),
|
|
267
327
|
role: (row.role as string | null) ?? null,
|