@fabrika/db 0.9.0-dev.1 → 0.9.0-dev.11
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 +44 -12
- package/src/admissions.ts +78 -12
- package/src/application-rows.ts +108 -0
- package/src/capability.ts +5 -0
- package/src/codegen-sqlite.ts +191 -0
- package/src/codegen.ts +170 -28
- package/src/database.ts +227 -61
- package/src/description.ts +34 -8
- package/src/errors.ts +6 -4
- package/src/file-columns.ts +36 -9
- package/src/framework-tables.ts +28 -8
- package/src/framework.ts +14 -5
- package/src/guard.ts +8 -0
- package/src/index.ts +10 -1
- package/src/job-leases.ts +210 -54
- package/src/job-wakes.ts +450 -0
- package/src/json.ts +124 -0
- package/src/links.ts +8 -1
- package/src/migrations-sqlite.ts +163 -0
- package/src/migrations.ts +25 -0
- package/src/nodes.ts +64 -9
- package/src/orm-types.ts +5 -5
- package/src/orm.ts +94 -37
- package/src/policy.ts +17 -2
- package/src/postgres.ts +26 -0
- package/src/registry.ts +11 -2
- package/src/sql.ts +230 -477
- package/src/sqlite-bun.ts +149 -0
- package/src/sqlite-migrate.ts +242 -0
- package/src/sqlite.ts +224 -0
- package/src/store.ts +385 -103
- package/src/stored-files.ts +188 -52
- package/src/tables.ts +16 -0
- package/src/temporal.ts +122 -9
- package/src/testing-shared.ts +999 -0
- package/src/testing-sqlite.ts +576 -0
- package/src/testing.ts +261 -482
- package/types/src/admissions.d.ts +21 -0
- package/types/src/application-rows.d.ts +58 -0
- package/types/src/builtins.d.ts +42 -0
- package/types/src/capability.d.ts +119 -0
- package/types/src/codegen-sqlite.d.ts +13 -0
- package/types/src/codegen.d.ts +113 -0
- package/types/src/database.d.ts +185 -0
- package/types/src/description.d.ts +124 -0
- package/types/src/errors.d.ts +98 -0
- package/types/src/file-columns.d.ts +39 -0
- package/types/src/file-references.d.ts +27 -0
- package/types/src/framework-tables.d.ts +59 -0
- package/types/src/framework.d.ts +38 -0
- package/types/src/guard.d.ts +23 -0
- package/types/src/identifier.d.ts +4 -0
- package/types/src/index.d.ts +11 -0
- package/types/src/job-leases.d.ts +112 -0
- package/types/src/job-wakes.d.ts +101 -0
- package/types/src/json.d.ts +18 -0
- package/types/src/links.d.ts +13 -0
- package/types/src/migrations-sqlite.d.ts +4 -0
- package/types/src/migrations.d.ts +26 -0
- package/types/src/nodes.d.ts +73 -0
- package/types/src/orm-types.d.ts +638 -0
- package/types/src/orm.d.ts +82 -0
- package/types/src/policy.d.ts +358 -0
- package/types/src/postgres.d.ts +12 -0
- package/types/src/production.d.ts +5 -0
- package/types/src/registry.d.ts +43 -0
- package/types/src/sql.d.ts +67 -0
- package/types/src/sqlite-bun.d.ts +21 -0
- package/types/src/sqlite-migrate.d.ts +28 -0
- package/types/src/sqlite.d.ts +24 -0
- package/types/src/store.d.ts +351 -0
- package/types/src/stored-files.d.ts +102 -0
- package/types/src/tables.d.ts +21 -0
- package/types/src/temporal.d.ts +36 -0
- package/types/src/testing-shared.d.ts +269 -0
- package/types/src/testing-sqlite.d.ts +22 -0
- package/types/src/testing.d.ts +35 -0
- package/types/src/vocabulary-tables.d.ts +225 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// @fabrika/db/migrations-sqlite — the framework's own tables in an application's SQLite database, a Cloudflare
|
|
2
|
+
// Durable Object's (ADR-0065). The same tables and columns as the Postgres bundle in `migrations.ts`, without the
|
|
3
|
+
// history a Postgres schema carries: no form files and no adoption of collection tables, which no SQLite database
|
|
4
|
+
// ever had. Plain data, applied as the bundle `fabrika` before the application's `app` bundle.
|
|
5
|
+
//
|
|
6
|
+
// The SQLite forms of the column types, which every framework statement and the row reads rely on:
|
|
7
|
+
// - a timestamp is TEXT in UTC, `YYYY-MM-DDTHH:MM:SS.sssZ`: what `strftime(SQLITE_NOW_FORMAT, 'now')` and
|
|
8
|
+
// `Date#toISOString()` both write, so text order is time order;
|
|
9
|
+
// - a boolean is INTEGER 0 or 1; a UUID is TEXT; a BIGINT is INTEGER.
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
FILE_DELETIONS_TABLE,
|
|
13
|
+
FILES_TABLE,
|
|
14
|
+
FORM_ADMISSIONS_TABLE,
|
|
15
|
+
FORM_BYTES_TABLE,
|
|
16
|
+
FORM_CHALLENGES_TABLE,
|
|
17
|
+
type FrameworkMigration,
|
|
18
|
+
JOB_KICKS_TABLE,
|
|
19
|
+
JOB_LEASES_TABLE,
|
|
20
|
+
JOB_WAKES_TABLE,
|
|
21
|
+
WEBHOOK_BINDINGS_TABLE,
|
|
22
|
+
WEBHOOK_DELIVERIES_TABLE,
|
|
23
|
+
} from './migrations'
|
|
24
|
+
|
|
25
|
+
/** The `strftime` format of a stored timestamp; `strftime(SQLITE_NOW_FORMAT, 'now')` is the database's clock. */
|
|
26
|
+
export const SQLITE_NOW_FORMAT = '%Y-%m-%dT%H:%M:%fZ'
|
|
27
|
+
|
|
28
|
+
export const SQLITE_FRAMEWORK_MIGRATIONS: readonly FrameworkMigration[] = Object.freeze([
|
|
29
|
+
Object.freeze({
|
|
30
|
+
name: '0001_framework.sql',
|
|
31
|
+
sql: `-- A binding admits deliveries for one scope value from one provider account, signed with its own secret.
|
|
32
|
+
CREATE TABLE ${WEBHOOK_BINDINGS_TABLE} (
|
|
33
|
+
id TEXT PRIMARY KEY,
|
|
34
|
+
webhook TEXT NOT NULL,
|
|
35
|
+
scope_type TEXT NOT NULL,
|
|
36
|
+
scope_value TEXT NOT NULL,
|
|
37
|
+
account TEXT NOT NULL,
|
|
38
|
+
secret TEXT NOT NULL,
|
|
39
|
+
created_by TEXT NOT NULL,
|
|
40
|
+
created_at INTEGER NOT NULL
|
|
41
|
+
) STRICT;
|
|
42
|
+
|
|
43
|
+
-- GitHub signs no timestamp, so a recorded delivery id is the replay defence. A delivery is claimed with a lease and
|
|
44
|
+
-- completed in its handler's transaction, which clears the lease.
|
|
45
|
+
CREATE TABLE ${WEBHOOK_DELIVERIES_TABLE} (
|
|
46
|
+
binding_id TEXT NOT NULL REFERENCES ${WEBHOOK_BINDINGS_TABLE} (id) ON DELETE CASCADE,
|
|
47
|
+
delivery_id TEXT NOT NULL,
|
|
48
|
+
received_at INTEGER NOT NULL,
|
|
49
|
+
lease_until INTEGER,
|
|
50
|
+
PRIMARY KEY (binding_id, delivery_id)
|
|
51
|
+
) STRICT;
|
|
52
|
+
|
|
53
|
+
-- One row per job: the lease a run takes, fenced by its generation, and the outcome of the last run.
|
|
54
|
+
CREATE TABLE ${JOB_LEASES_TABLE} (
|
|
55
|
+
job TEXT PRIMARY KEY,
|
|
56
|
+
generation INTEGER NOT NULL,
|
|
57
|
+
holder TEXT,
|
|
58
|
+
expires_at TEXT NOT NULL,
|
|
59
|
+
attempt_deadline TEXT NOT NULL,
|
|
60
|
+
first_seen_at TEXT NOT NULL,
|
|
61
|
+
last_started_at TEXT,
|
|
62
|
+
last_finished_at TEXT,
|
|
63
|
+
last_status TEXT,
|
|
64
|
+
last_succeeded_at TEXT,
|
|
65
|
+
last_duration_ms INTEGER,
|
|
66
|
+
last_missed_reported_at TEXT
|
|
67
|
+
) STRICT;
|
|
68
|
+
|
|
69
|
+
-- Whether a kick arrived after the job's current run started: that run runs the job once more after it succeeded.
|
|
70
|
+
CREATE TABLE ${JOB_KICKS_TABLE} (
|
|
71
|
+
job TEXT PRIMARY KEY,
|
|
72
|
+
pending INTEGER NOT NULL CHECK (pending IN (0, 1))
|
|
73
|
+
) STRICT;
|
|
74
|
+
|
|
75
|
+
-- A form's admission counters, one row per form, bucket and minute; a row of a past minute is pruned by the next
|
|
76
|
+
-- admission.
|
|
77
|
+
CREATE TABLE ${FORM_ADMISSIONS_TABLE} (
|
|
78
|
+
form TEXT NOT NULL,
|
|
79
|
+
bucket TEXT NOT NULL,
|
|
80
|
+
window_start TEXT NOT NULL,
|
|
81
|
+
count INTEGER NOT NULL,
|
|
82
|
+
PRIMARY KEY (form, bucket, window_start)
|
|
83
|
+
) STRICT;
|
|
84
|
+
CREATE INDEX ${FORM_ADMISSIONS_TABLE}_window_start ON ${FORM_ADMISSIONS_TABLE} (window_start);
|
|
85
|
+
|
|
86
|
+
-- The digest of each challenge token a form accepted, until the token could no longer verify: a replay is refused.
|
|
87
|
+
CREATE TABLE ${FORM_CHALLENGES_TABLE} (
|
|
88
|
+
form TEXT NOT NULL,
|
|
89
|
+
token_digest TEXT NOT NULL,
|
|
90
|
+
expires_at TEXT NOT NULL,
|
|
91
|
+
PRIMARY KEY (form, token_digest)
|
|
92
|
+
) STRICT;
|
|
93
|
+
CREATE INDEX ${FORM_CHALLENGES_TABLE}_expires_at ON ${FORM_CHALLENGES_TABLE} (expires_at);
|
|
94
|
+
|
|
95
|
+
-- The bytes a form's files may write per hour: one row per form and hour, which the next reservation of a later hour
|
|
96
|
+
-- prunes.
|
|
97
|
+
CREATE TABLE ${FORM_BYTES_TABLE} (
|
|
98
|
+
form TEXT NOT NULL,
|
|
99
|
+
window_start TEXT NOT NULL,
|
|
100
|
+
bytes INTEGER NOT NULL,
|
|
101
|
+
PRIMARY KEY (form, window_start)
|
|
102
|
+
) STRICT;
|
|
103
|
+
|
|
104
|
+
-- Every stored file: a value a table's file column references by id. A row is 'pending' from before its object
|
|
105
|
+
-- (<kind>/<id>) is written until the first reference claims it, then 'quarantined' (a scanning kind) or 'live'.
|
|
106
|
+
CREATE TABLE ${FILES_TABLE} (
|
|
107
|
+
id TEXT PRIMARY KEY,
|
|
108
|
+
kind TEXT NOT NULL,
|
|
109
|
+
status TEXT NOT NULL CONSTRAINT ${FILES_TABLE}_status CHECK (status IN ('pending', 'quarantined', 'live', 'rejected')),
|
|
110
|
+
object_key TEXT NOT NULL UNIQUE,
|
|
111
|
+
size INTEGER,
|
|
112
|
+
sha256 TEXT,
|
|
113
|
+
content_type TEXT NOT NULL,
|
|
114
|
+
name TEXT,
|
|
115
|
+
scope TEXT,
|
|
116
|
+
request TEXT,
|
|
117
|
+
created_at TEXT NOT NULL,
|
|
118
|
+
object_version TEXT,
|
|
119
|
+
scan_attempts INTEGER NOT NULL DEFAULT 0,
|
|
120
|
+
scan_after TEXT,
|
|
121
|
+
legacy TEXT
|
|
122
|
+
) STRICT;
|
|
123
|
+
CREATE INDEX ${FILES_TABLE}_created_at ON ${FILES_TABLE} (created_at) WHERE legacy IS NULL;
|
|
124
|
+
CREATE INDEX ${FILES_TABLE}_scan ON ${FILES_TABLE} (scan_after) WHERE status = 'quarantined';
|
|
125
|
+
|
|
126
|
+
-- An object whose file row is deleted, until the object is: the sweep finishes a deletion a crash interrupted.
|
|
127
|
+
CREATE TABLE ${FILE_DELETIONS_TABLE} (
|
|
128
|
+
object_key TEXT PRIMARY KEY,
|
|
129
|
+
recorded_at TEXT NOT NULL
|
|
130
|
+
) STRICT;
|
|
131
|
+
`,
|
|
132
|
+
}),
|
|
133
|
+
Object.freeze({
|
|
134
|
+
name: '0002_job_wakes.sql',
|
|
135
|
+
sql: `-- A job's pending work, one row per due unit: its next scheduled run, a kick, a claim's re-arm, a durable retry. due_at
|
|
136
|
+
-- is epoch milliseconds. Only the fenced finish of a run that started at or after a row changes it: consumed when the run is
|
|
137
|
+
-- done with it, or moved to a retry, which counts in attempt.
|
|
138
|
+
CREATE TABLE ${JOB_WAKES_TABLE} (
|
|
139
|
+
id TEXT PRIMARY KEY,
|
|
140
|
+
job TEXT NOT NULL,
|
|
141
|
+
due_at INTEGER NOT NULL,
|
|
142
|
+
reason TEXT NOT NULL CONSTRAINT ${JOB_WAKES_TABLE}_reason CHECK (reason IN ('cron', 'kick', 'claim', 'retry')),
|
|
143
|
+
attempt INTEGER NOT NULL DEFAULT 0,
|
|
144
|
+
dedupe TEXT
|
|
145
|
+
) STRICT;
|
|
146
|
+
CREATE INDEX ${JOB_WAKES_TABLE}_due ON ${JOB_WAKES_TABLE} (due_at);
|
|
147
|
+
CREATE UNIQUE INDEX ${JOB_WAKES_TABLE}_dedupe ON ${JOB_WAKES_TABLE} (dedupe) WHERE dedupe IS NOT NULL;
|
|
148
|
+
`,
|
|
149
|
+
}),
|
|
150
|
+
Object.freeze({
|
|
151
|
+
name: '0003_file_deletions_recorded_at.sql',
|
|
152
|
+
sql: `-- A queued key is a tombstone kept for the retention window: the sweep reads the due ones by age, not the whole queue.
|
|
153
|
+
CREATE INDEX ${FILE_DELETIONS_TABLE}_recorded_at ON ${FILE_DELETIONS_TABLE} (recorded_at, object_key);
|
|
154
|
+
`,
|
|
155
|
+
}),
|
|
156
|
+
Object.freeze({
|
|
157
|
+
name: '0004_job_wakes_owned.sql',
|
|
158
|
+
sql: `-- The wakes the run holding the job's lease took when it took the lease: its finish consumes or retries exactly these.
|
|
159
|
+
-- A wake written or merged into afterwards is not taken, though due before the run started: the run may not have read its work.
|
|
160
|
+
ALTER TABLE ${JOB_WAKES_TABLE} ADD COLUMN owned INTEGER NOT NULL DEFAULT 0 CHECK (owned IN (0, 1));
|
|
161
|
+
`,
|
|
162
|
+
}),
|
|
163
|
+
])
|
package/src/migrations.ts
CHANGED
|
@@ -9,6 +9,7 @@ export const WEBHOOK_BINDINGS_TABLE = 'fabrika_webhook_bindings'
|
|
|
9
9
|
export const WEBHOOK_DELIVERIES_TABLE = 'fabrika_webhook_deliveries'
|
|
10
10
|
export const JOB_LEASES_TABLE = 'fabrika_job_leases'
|
|
11
11
|
export const JOB_KICKS_TABLE = 'fabrika_job_kicks'
|
|
12
|
+
export const JOB_WAKES_TABLE = 'fabrika_wakes'
|
|
12
13
|
export const FORM_ADMISSIONS_TABLE = 'fabrika_form_admissions'
|
|
13
14
|
export const FORM_CHALLENGES_TABLE = 'fabrika_form_challenges'
|
|
14
15
|
export const FORM_FILES_TABLE = 'fabrika_form_files'
|
|
@@ -254,6 +255,30 @@ BEGIN
|
|
|
254
255
|
DELETE FROM ${FORM_FILES_TABLE} f WHERE status = 'held' AND EXISTS (SELECT 1 FROM ${FILES_TABLE} c WHERE c.id = f.id);
|
|
255
256
|
END
|
|
256
257
|
$$;
|
|
258
|
+
`,
|
|
259
|
+
}),
|
|
260
|
+
Object.freeze({
|
|
261
|
+
name: '0009_job_wakes.sql',
|
|
262
|
+
sql: `-- A job's pending work, one row per due unit: its next scheduled run, a kick, a claim's re-arm, a durable retry. due_at
|
|
263
|
+
-- is epoch milliseconds. Only the fenced finish of a run that started at or after a row changes it: consumed when the run is
|
|
264
|
+
-- done with it, or moved to a retry, which counts in attempt.
|
|
265
|
+
CREATE TABLE ${JOB_WAKES_TABLE} (
|
|
266
|
+
id UUID PRIMARY KEY,
|
|
267
|
+
job TEXT NOT NULL,
|
|
268
|
+
due_at BIGINT NOT NULL,
|
|
269
|
+
reason TEXT NOT NULL CONSTRAINT ${JOB_WAKES_TABLE}_reason CHECK (reason IN ('cron', 'kick', 'claim', 'retry')),
|
|
270
|
+
attempt INTEGER NOT NULL DEFAULT 0,
|
|
271
|
+
dedupe TEXT
|
|
272
|
+
);
|
|
273
|
+
CREATE INDEX ${JOB_WAKES_TABLE}_due ON ${JOB_WAKES_TABLE} (due_at);
|
|
274
|
+
CREATE UNIQUE INDEX ${JOB_WAKES_TABLE}_dedupe ON ${JOB_WAKES_TABLE} (dedupe) WHERE dedupe IS NOT NULL;
|
|
275
|
+
`,
|
|
276
|
+
}),
|
|
277
|
+
Object.freeze({
|
|
278
|
+
name: '0010_job_wakes_owned.sql',
|
|
279
|
+
sql: `-- The wakes the run holding the job's lease took when it took the lease: its finish consumes or retries exactly these.
|
|
280
|
+
-- A wake written or merged into afterwards is not taken, though due before the run started: the run may not have read its work.
|
|
281
|
+
ALTER TABLE ${JOB_WAKES_TABLE} ADD COLUMN owned BOOLEAN NOT NULL DEFAULT false;
|
|
257
282
|
`,
|
|
258
283
|
}),
|
|
259
284
|
])
|
package/src/nodes.ts
CHANGED
|
@@ -38,6 +38,7 @@ import {
|
|
|
38
38
|
WhereNode,
|
|
39
39
|
} from 'kysely'
|
|
40
40
|
import { inSet, type NameSet, nameSet, namesOf } from './builtins'
|
|
41
|
+
import type { SqlDialect } from './database'
|
|
41
42
|
import type { Compare, Condition, Contains, JoinState, Primitive, SelectState } from './description'
|
|
42
43
|
import { QueryRefusedError } from './errors'
|
|
43
44
|
|
|
@@ -49,31 +50,55 @@ export function equals(table: string, name: string, value: Primitive): Operation
|
|
|
49
50
|
return BinaryOperationNode.create(column(table, name), OperatorNode.create('='), ValueNode.create(value))
|
|
50
51
|
}
|
|
51
52
|
|
|
53
|
+
// The scope predicates are built through these, so they read their nodes by index, never through an iterator or a method.
|
|
52
54
|
export function allOf(nodes: readonly OperationNode[]): OperationNode {
|
|
53
|
-
|
|
54
|
-
if (
|
|
55
|
+
let joined = nodes[0]
|
|
56
|
+
if (joined === undefined) {
|
|
55
57
|
throw new QueryRefusedError('statement_shape', 'an empty conjunction')
|
|
56
58
|
}
|
|
57
|
-
|
|
59
|
+
for (let index = 1; index < nodes.length; index++) {
|
|
60
|
+
const node = nodes[index]
|
|
61
|
+
if (node !== undefined) {
|
|
62
|
+
joined = AndNode.create(joined, node)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return joined
|
|
58
66
|
}
|
|
59
67
|
|
|
60
68
|
/** One node, or the OR of several in a parenthesis: inside an AND, `a and b or c` would read as `(a and b) or c`. */
|
|
61
69
|
export function anyOf(nodes: readonly OperationNode[]): OperationNode {
|
|
62
|
-
|
|
63
|
-
if (
|
|
70
|
+
let joined = nodes[0]
|
|
71
|
+
if (joined === undefined) {
|
|
64
72
|
throw new QueryRefusedError('statement_shape', 'an empty disjunction')
|
|
65
73
|
}
|
|
66
|
-
|
|
74
|
+
for (let index = 1; index < nodes.length; index++) {
|
|
75
|
+
const node = nodes[index]
|
|
76
|
+
if (node !== undefined) {
|
|
77
|
+
joined = OrNode.create(joined, node)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return nodes.length === 1 ? joined : ParensNode.create(joined)
|
|
67
81
|
}
|
|
68
82
|
|
|
69
83
|
export function inList(table: string, name: string, values: readonly string[]): OperationNode {
|
|
70
84
|
return BinaryOperationNode.create(
|
|
71
85
|
column(table, name),
|
|
72
86
|
OperatorNode.create('in'),
|
|
73
|
-
ValueListNode.create(values
|
|
87
|
+
ValueListNode.create(valueNodes(values)),
|
|
74
88
|
)
|
|
75
89
|
}
|
|
76
90
|
|
|
91
|
+
function valueNodes(values: readonly string[]): ValueNode[] {
|
|
92
|
+
const nodes: ValueNode[] = []
|
|
93
|
+
for (let index = 0; index < values.length; index++) {
|
|
94
|
+
const value = values[index]
|
|
95
|
+
if (value !== undefined) {
|
|
96
|
+
nodes[nodes.length] = ValueNode.create(value)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return nodes
|
|
100
|
+
}
|
|
101
|
+
|
|
77
102
|
/** The scope of a `Within` that is not limited to scope values. */
|
|
78
103
|
export function everything(): OperationNode {
|
|
79
104
|
return ValueNode.createImmediate(true)
|
|
@@ -167,6 +192,15 @@ function joinedSelections(join: JoinState): SelectionNode[] {
|
|
|
167
192
|
return columns.map((name) => SelectionNode.create(AliasNode.create(column(child, name), IdentifierNode.create(`${child}.${name}`))))
|
|
168
193
|
}
|
|
169
194
|
|
|
195
|
+
/**
|
|
196
|
+
* One order term. Postgres sorts nulls last ascending and first descending; SQLite the other way, so on SQLite the term
|
|
197
|
+
* states Postgres's order and a keyset page (`orm.ts`) reads the same rows after its cursor on both.
|
|
198
|
+
*/
|
|
199
|
+
function orderItem(table: string, order: SelectState['orderBy'][number], dialect: SqlDialect): OrderByItemNode {
|
|
200
|
+
const item = OrderByItemNode.create(column(table, order.column), RawNode.createWithSql(order.direction))
|
|
201
|
+
return dialect === 'postgres' ? item : Object.freeze({ ...item, nulls: order.direction === 'asc' ? 'last' : 'first' })
|
|
202
|
+
}
|
|
203
|
+
|
|
170
204
|
/** `after` is a keyset page's position, ANDed to the caller's conditions inside their parenthesis. */
|
|
171
205
|
export function selectNode(
|
|
172
206
|
state: SelectState,
|
|
@@ -174,12 +208,13 @@ export function selectNode(
|
|
|
174
208
|
scope: OperationNode,
|
|
175
209
|
after?: OperationNode,
|
|
176
210
|
days: NameSet = NO_DAYS,
|
|
211
|
+
dialect: SqlDialect = 'postgres',
|
|
177
212
|
): SelectQueryNode {
|
|
178
213
|
const table = state.table
|
|
179
214
|
const columns = state.columns ?? visible
|
|
180
215
|
const conditions = conditionsNode(table, state.conditions)
|
|
181
216
|
const filter = after === undefined ? conditions : conditions === undefined ? after : AndNode.create(conditions, ParensNode.create(after))
|
|
182
|
-
const orderBy = state.orderBy.map((order) =>
|
|
217
|
+
const orderBy = state.orderBy.map((order) => orderItem(table, order, dialect))
|
|
183
218
|
const node: SelectQueryNode = {
|
|
184
219
|
kind: 'SelectQueryNode',
|
|
185
220
|
from: FromNode.create([TableNode.create(table)]),
|
|
@@ -352,6 +387,7 @@ export function claimNode(
|
|
|
352
387
|
due: { readonly due: string; readonly attempts: string; readonly now: Date; readonly maxAttempts: number },
|
|
353
388
|
scope: OperationNode,
|
|
354
389
|
days: NameSet,
|
|
390
|
+
dialect: SqlDialect = 'postgres',
|
|
355
391
|
): SelectQueryNode {
|
|
356
392
|
const table = state.table
|
|
357
393
|
const waiting = allOf([
|
|
@@ -359,10 +395,29 @@ export function claimNode(
|
|
|
359
395
|
BinaryOperationNode.create(column(table, due.due), OperatorNode.create('<='), ValueNode.create(due.now)),
|
|
360
396
|
BinaryOperationNode.create(column(table, due.attempts), OperatorNode.create('<'), ValueNode.create(due.maxAttempts)),
|
|
361
397
|
])
|
|
362
|
-
const node = selectNode(state, columns, scope, waiting, days)
|
|
398
|
+
const node = selectNode(state, columns, scope, waiting, days, dialect)
|
|
363
399
|
return Object.freeze({ ...node, endModifiers: [SelectModifierNode.create('ForUpdate'), SelectModifierNode.create('SkipLocked')] })
|
|
364
400
|
}
|
|
365
401
|
|
|
402
|
+
/**
|
|
403
|
+
* When the same claim next finds a row: `select <due> from table where (<conditions> and (<due> > $now and <attempts> <
|
|
404
|
+
* $max)) and scope order by <due> limit 1`, with `state` the claim's conditions ordered by `<due>`, limited to one.
|
|
405
|
+
*/
|
|
406
|
+
export function nextClaimNode(
|
|
407
|
+
state: SelectState,
|
|
408
|
+
due: { readonly due: string; readonly attempts: string; readonly now: Date; readonly maxAttempts: number },
|
|
409
|
+
scope: OperationNode,
|
|
410
|
+
days: NameSet,
|
|
411
|
+
dialect: SqlDialect = 'postgres',
|
|
412
|
+
): SelectQueryNode {
|
|
413
|
+
const table = state.table
|
|
414
|
+
const later = allOf([
|
|
415
|
+
BinaryOperationNode.create(column(table, due.due), OperatorNode.create('>'), ValueNode.create(due.now)),
|
|
416
|
+
BinaryOperationNode.create(column(table, due.attempts), OperatorNode.create('<'), ValueNode.create(due.maxAttempts)),
|
|
417
|
+
])
|
|
418
|
+
return selectNode(state, [due.due], scope, later, days, dialect)
|
|
419
|
+
}
|
|
420
|
+
|
|
366
421
|
/** `select <columns> from table where scope for update`: a mutation's precondition read of its own target. */
|
|
367
422
|
export function lockedSelectNode(table: string, columns: readonly string[], scope: OperationNode, days: NameSet = NO_DAYS): SelectQueryNode {
|
|
368
423
|
const node: SelectQueryNode = {
|
package/src/orm-types.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import type { Selectable, Updateable } from 'kysely'
|
|
6
6
|
import type { RowRef } from './capability'
|
|
7
|
-
import type { ConditionTuple } from './description'
|
|
7
|
+
import type { ComparableColumn, ConditionTuple } from './description'
|
|
8
8
|
import type { FileReference } from './file-references'
|
|
9
9
|
import type {
|
|
10
10
|
ActionOf,
|
|
@@ -240,7 +240,7 @@ type InsertValuesOf<E extends IndexedTable> = { [C in keyof E['insert']]: E['ins
|
|
|
240
240
|
|
|
241
241
|
/** `onConflict` of an insert: the columns of a unique index the migrations create, and what a conflict does. */
|
|
242
242
|
export interface InsertConflict<E extends IndexedTable, M extends 'nothing' | 'update'> {
|
|
243
|
-
readonly onConflict: { readonly columns: readonly
|
|
243
|
+
readonly onConflict: { readonly columns: readonly ComparableColumn<E['row']>[]; readonly do: M }
|
|
244
244
|
}
|
|
245
245
|
|
|
246
246
|
/**
|
|
@@ -336,7 +336,7 @@ export interface ListPage<Row> {
|
|
|
336
336
|
readonly next: string | null
|
|
337
337
|
}
|
|
338
338
|
|
|
339
|
-
export type OrderTerm<Row> = { [C in
|
|
339
|
+
export type OrderTerm<Row> = { [C in ComparableColumn<Row>]: readonly [C, 'asc' | 'desc'] }[ComparableColumn<Row>]
|
|
340
340
|
|
|
341
341
|
export interface ListOptions<Row> {
|
|
342
342
|
readonly where?: readonly ConditionTuple<Row>[]
|
|
@@ -686,8 +686,8 @@ type RefFragment<Ix extends AnyDbIndex, D extends string, A extends string> = {
|
|
|
686
686
|
}[TableName<Ix>]
|
|
687
687
|
|
|
688
688
|
/**
|
|
689
|
-
* What a `Within` writes on a `'write'` site: the
|
|
690
|
-
* write action included (the statement's
|
|
689
|
+
* What a `Within` writes on a `'write'` site: the table of a fragment of `use: 'update'` or `'delete'` whose action it
|
|
690
|
+
* holds, a column's own write action included (the statement's columns are the approver's to review, not checked).
|
|
691
691
|
*/
|
|
692
692
|
type WithinWrites<E extends IndexedTable, M extends 'read' | 'write'> = M extends 'write'
|
|
693
693
|
? E['actions']['update'] | E['actions']['delete'] | (keyof E['writes'] & string)
|