@crombolo/data 0.27.0 → 0.28.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/data",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.28.0",
|
|
4
4
|
"description": "SQLite and Postgres storage for crombolo: schema, migrations and repositories.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Crombolo",
|
|
@@ -28,6 +28,6 @@
|
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@crombolo/core": "0.
|
|
31
|
+
"@crombolo/core": "0.28.0"
|
|
32
32
|
}
|
|
33
33
|
}
|
package/src/index.ts
CHANGED
|
@@ -14,6 +14,7 @@ export * from './pending.ts'
|
|
|
14
14
|
export * from './relations.ts'
|
|
15
15
|
export * from './repositories/approvals.ts'
|
|
16
16
|
export * from './repositories/bundles.ts'
|
|
17
|
+
export * from './repositories/content-applied.ts'
|
|
17
18
|
export * from './repositories/content-events.ts'
|
|
18
19
|
export * from './repositories/content-notifications.ts'
|
|
19
20
|
export * from './repositories/content-types.ts'
|
|
@@ -56,6 +57,7 @@ export * from './schema/bundles-rename.ts'
|
|
|
56
57
|
export * from './schema/change-report.ts'
|
|
57
58
|
export * from './schema/client-id-prefix.ts'
|
|
58
59
|
export * from './schema/content.ts'
|
|
60
|
+
export * from './schema/content-applied.ts'
|
|
59
61
|
export * from './schema/content-checkpoint.ts'
|
|
60
62
|
export * from './schema/content-events.ts'
|
|
61
63
|
export * from './schema/content-notifications.ts'
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an import last applied to each node (story 163): the base a later import
|
|
3
|
+
* compares the bundle and the site against. Written inside the import's
|
|
4
|
+
* transaction, with the state it replaced, so a revert can put it back.
|
|
5
|
+
*/
|
|
6
|
+
import { normaliseUuid } from '@crombolo/core'
|
|
7
|
+
import type { Db } from '../database.ts'
|
|
8
|
+
import { DbDate } from '../dialect.ts'
|
|
9
|
+
|
|
10
|
+
/** One node as an import left it. Values are keyed `alias|culture|segment`, names by culture. */
|
|
11
|
+
export interface AppliedState {
|
|
12
|
+
values: Record<string, unknown>
|
|
13
|
+
names: Record<string, string>
|
|
14
|
+
parent: string | null
|
|
15
|
+
/** The order the bundle gave it among its siblings. */
|
|
16
|
+
bundleSortOrder: number
|
|
17
|
+
/** The order it had here once the import was done. */
|
|
18
|
+
sortOrder: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const appliedValueKey = (alias: string, culture: string | null, segment: string | null) =>
|
|
22
|
+
`${alias}|${culture ?? ''}|${segment ?? ''}`
|
|
23
|
+
|
|
24
|
+
const parse = (text: unknown): AppliedState | null =>
|
|
25
|
+
text === null || text === undefined ? null : (JSON.parse(String(text)) as AppliedState)
|
|
26
|
+
|
|
27
|
+
export class ContentAppliedRepository {
|
|
28
|
+
#db: Db
|
|
29
|
+
|
|
30
|
+
constructor(db: Db) {
|
|
31
|
+
this.#db = db
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async byKeys(keys: readonly string[]): Promise<Map<string, AppliedState>> {
|
|
35
|
+
const out = new Map<string, AppliedState>()
|
|
36
|
+
for (let i = 0; i < keys.length; i += 500) {
|
|
37
|
+
const chunk = keys.slice(i, i + 500).map(normaliseUuid)
|
|
38
|
+
if (chunk.length === 0) continue
|
|
39
|
+
const rows = await this.#db.query<{ node_key: string; state: string }>(
|
|
40
|
+
`SELECT node_key, state FROM content_applied WHERE node_key IN (${chunk.map(() => '?').join(', ')})`,
|
|
41
|
+
chunk,
|
|
42
|
+
)
|
|
43
|
+
for (const row of rows) {
|
|
44
|
+
const state = parse(row.state)
|
|
45
|
+
if (state) out.set(normaliseUuid(String(row.node_key)), state)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return out
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Records what a run applied to a node, keeping what it replaced. */
|
|
52
|
+
async record(key: string, runId: string, state: AppliedState): Promise<void> {
|
|
53
|
+
const nodeKey = normaliseUuid(key)
|
|
54
|
+
const rows = await this.#db.query<{ run_id: string; state: string }>(
|
|
55
|
+
'SELECT run_id, state FROM content_applied WHERE node_key = ?',
|
|
56
|
+
[nodeKey],
|
|
57
|
+
)
|
|
58
|
+
const was = rows[0]
|
|
59
|
+
const now = DbDate.toDb(new Date())
|
|
60
|
+
if (!was) {
|
|
61
|
+
await this.#db.exec(
|
|
62
|
+
`INSERT INTO content_applied (node_key, run_id, state, applied_at, previous_run_id, previous_state)
|
|
63
|
+
VALUES (?, ?, ?, ?, NULL, NULL)`,
|
|
64
|
+
[nodeKey, runId, JSON.stringify(state), now],
|
|
65
|
+
)
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
await this.#db.exec(
|
|
69
|
+
`UPDATE content_applied SET run_id = ?, state = ?, applied_at = ?, previous_run_id = ?, previous_state = ?
|
|
70
|
+
WHERE node_key = ?`,
|
|
71
|
+
[runId, JSON.stringify(state), now, String(was.run_id), String(was.state), nodeKey],
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Undoes what a run recorded: the state before it, or no record where it made the first. */
|
|
76
|
+
async revert(runId: string): Promise<void> {
|
|
77
|
+
await this.#db.exec('DELETE FROM content_applied WHERE run_id = ? AND previous_state IS NULL', [
|
|
78
|
+
runId,
|
|
79
|
+
])
|
|
80
|
+
await this.#db.exec(
|
|
81
|
+
`UPDATE content_applied SET run_id = previous_run_id, state = previous_state,
|
|
82
|
+
previous_run_id = NULL, previous_state = NULL
|
|
83
|
+
WHERE run_id = ?`,
|
|
84
|
+
[runId],
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration 049 — what an import last applied to each node (story 163).
|
|
3
|
+
*
|
|
4
|
+
* `content_applied` holds, per node, the values, names, order and parent the
|
|
5
|
+
* last import that wrote it left there. A later import compares three ways —
|
|
6
|
+
* the bundle, the site and this — so a change made only in the bundle is
|
|
7
|
+
* taken, one made only here is kept, and only a change made in both is a
|
|
8
|
+
* decision for a person. The state before the run is kept beside it, so a
|
|
9
|
+
* revert puts the record back as well as the content.
|
|
10
|
+
*/
|
|
11
|
+
import type { Db } from '../database.ts'
|
|
12
|
+
import type { Migration } from '../migrations.ts'
|
|
13
|
+
import { STATE_DROP_ASSISTANT } from './drop-assistant.ts'
|
|
14
|
+
|
|
15
|
+
export const STATE_CONTENT_APPLIED = '{5d1f8a3c-0049-4e72-b6a9-3c8e1f4d2b49}'
|
|
16
|
+
|
|
17
|
+
export const contentAppliedMigration: Migration = {
|
|
18
|
+
from: STATE_DROP_ASSISTANT,
|
|
19
|
+
to: STATE_CONTENT_APPLIED,
|
|
20
|
+
name: 'AddContentApplied',
|
|
21
|
+
kind: 'expand',
|
|
22
|
+
release: '0.28.0',
|
|
23
|
+
async up(db: Db) {
|
|
24
|
+
const t = db.dialect.types
|
|
25
|
+
await db.exec(
|
|
26
|
+
`CREATE TABLE content_applied (
|
|
27
|
+
node_key ${t.uuid} PRIMARY KEY,
|
|
28
|
+
run_id ${t.varchar(64)} NOT NULL,
|
|
29
|
+
state ${t.text} NOT NULL,
|
|
30
|
+
applied_at ${t.timestamp} NOT NULL,
|
|
31
|
+
previous_run_id ${t.varchar(64)},
|
|
32
|
+
previous_state ${t.text}
|
|
33
|
+
)`,
|
|
34
|
+
)
|
|
35
|
+
},
|
|
36
|
+
}
|
package/src/schema/plan.ts
CHANGED
|
@@ -23,6 +23,7 @@ import { bundlesRenameMigration } from './bundles-rename.ts'
|
|
|
23
23
|
import { changeReportMigration } from './change-report.ts'
|
|
24
24
|
import { clientIdPrefixMigration } from './client-id-prefix.ts'
|
|
25
25
|
import { contentMigration } from './content.ts'
|
|
26
|
+
import { contentAppliedMigration } from './content-applied.ts'
|
|
26
27
|
import { contentCheckpointMigration } from './content-checkpoint.ts'
|
|
27
28
|
import { contentEditingMigration } from './content-editing.ts'
|
|
28
29
|
import { contentEventsMigration } from './content-events.ts'
|
|
@@ -111,4 +112,5 @@ export const cromboloPlan = new MigrationPlan([
|
|
|
111
112
|
contentEventsMigration,
|
|
112
113
|
contentCheckpointMigration,
|
|
113
114
|
dropAssistantMigration,
|
|
115
|
+
contentAppliedMigration,
|
|
114
116
|
])
|