@fabrika/db 0.9.0-dev.2 → 0.9.0-dev.20
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 +35 -4
- package/src/admissions.ts +78 -12
- package/src/application-rows.ts +108 -0
- package/src/authority.ts +560 -0
- package/src/builtins.ts +21 -0
- package/src/capability.ts +56 -2
- package/src/codegen-sqlite.ts +194 -0
- package/src/codegen.ts +229 -40
- package/src/condition-nodes.ts +119 -0
- package/src/conditions.ts +683 -0
- package/src/database.ts +227 -61
- package/src/description.ts +37 -8
- package/src/errors.ts +6 -4
- package/src/file-columns.ts +56 -11
- package/src/framework-tables.ts +28 -8
- package/src/framework.ts +14 -5
- package/src/guard.ts +373 -15
- package/src/index.ts +13 -1
- package/src/job-leases.ts +212 -55
- 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 +84 -14
- package/src/orm-types.ts +124 -45
- package/src/orm.ts +283 -133
- package/src/policy.ts +1851 -119
- package/src/postgres.ts +26 -0
- package/src/registry.ts +52 -3
- package/src/sql.ts +247 -476
- package/src/sqlite-bun.ts +149 -0
- package/src/sqlite-migrate.ts +242 -0
- package/src/sqlite.ts +269 -0
- package/src/store.ts +2290 -239
- package/src/stored-files.ts +188 -52
- package/src/tables.ts +164 -3
- package/src/temporal.ts +122 -9
- package/src/testing-shared.ts +1348 -0
- package/src/testing-sqlite.ts +685 -0
- package/src/testing.ts +310 -494
- package/src/vocabulary-tables.ts +124 -18
- package/types/src/application-rows.d.ts +58 -0
- package/types/src/authority.d.ts +147 -0
- package/types/src/builtins.d.ts +2 -0
- package/types/src/capability.d.ts +55 -2
- package/types/src/codegen-sqlite.d.ts +13 -0
- package/types/src/codegen.d.ts +79 -5
- package/types/src/condition-nodes.d.ts +9 -0
- package/types/src/conditions.d.ts +151 -0
- package/types/src/database.d.ts +81 -20
- package/types/src/description.d.ts +26 -8
- package/types/src/errors.d.ts +5 -4
- package/types/src/file-columns.d.ts +6 -4
- package/types/src/framework.d.ts +11 -5
- package/types/src/guard.d.ts +24 -0
- package/types/src/index.d.ts +4 -3
- package/types/src/job-leases.d.ts +34 -5
- package/types/src/job-wakes.d.ts +101 -0
- package/types/src/json.d.ts +18 -0
- package/types/src/migrations-sqlite.d.ts +4 -0
- package/types/src/migrations.d.ts +1 -0
- package/types/src/nodes.d.ts +17 -2
- package/types/src/orm-types.d.ts +91 -42
- package/types/src/orm.d.ts +10 -0
- package/types/src/policy.d.ts +332 -6
- package/types/src/postgres.d.ts +12 -0
- package/types/src/registry.d.ts +37 -3
- package/types/src/sql.d.ts +29 -21
- 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 +143 -34
- package/types/src/stored-files.d.ts +6 -2
- package/types/src/tables.d.ts +17 -1
- package/types/src/temporal.d.ts +24 -0
- package/types/src/testing-shared.d.ts +290 -0
- package/types/src/testing-sqlite.d.ts +37 -0
- package/types/src/testing.d.ts +2 -93
- package/types/src/vocabulary-tables.d.ts +141 -17
|
@@ -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,29 +50,54 @@ 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
|
-
return BinaryOperationNode.create(
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
84
|
+
return BinaryOperationNode.create(column(table, name), OperatorNode.create('in'), valueList(values))
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** `($1, $2, …)`: bound values, as the right operand of an `in`. */
|
|
88
|
+
export function valueList(values: readonly string[]): OperationNode {
|
|
89
|
+
return ValueListNode.create(valueNodes(values))
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function valueNodes(values: readonly string[]): ValueNode[] {
|
|
93
|
+
const nodes: ValueNode[] = []
|
|
94
|
+
for (let index = 0; index < values.length; index++) {
|
|
95
|
+
const value = values[index]
|
|
96
|
+
if (value !== undefined) {
|
|
97
|
+
nodes[nodes.length] = ValueNode.create(value)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return nodes
|
|
75
101
|
}
|
|
76
102
|
|
|
77
103
|
/** The scope of a `Within` that is not limited to scope values. */
|
|
@@ -156,7 +182,9 @@ function joinNode(parent: string, join: JoinState): JoinNode {
|
|
|
156
182
|
BinaryOperationNode.create(column(child, pair.childColumn), OperatorNode.create('='), column(parent, pair.parentColumn))
|
|
157
183
|
),
|
|
158
184
|
)
|
|
159
|
-
const
|
|
185
|
+
const requested = conditionsNode(child, join.select.conditions)
|
|
186
|
+
const held = join.condition
|
|
187
|
+
const conditions = held === undefined ? requested : requested === undefined ? held : AndNode.create(requested, ParensNode.create(held))
|
|
160
188
|
const on = conditions === undefined ? foreignKey : AndNode.create(foreignKey, ParensNode.create(conditions))
|
|
161
189
|
return JoinNode.createWithOn('InnerJoin', TableNode.create(child), on)
|
|
162
190
|
}
|
|
@@ -167,6 +195,15 @@ function joinedSelections(join: JoinState): SelectionNode[] {
|
|
|
167
195
|
return columns.map((name) => SelectionNode.create(AliasNode.create(column(child, name), IdentifierNode.create(`${child}.${name}`))))
|
|
168
196
|
}
|
|
169
197
|
|
|
198
|
+
/**
|
|
199
|
+
* One order term. Postgres sorts nulls last ascending and first descending; SQLite the other way, so on SQLite the term
|
|
200
|
+
* states Postgres's order and a keyset page (`orm.ts`) reads the same rows after its cursor on both.
|
|
201
|
+
*/
|
|
202
|
+
function orderItem(table: string, order: SelectState['orderBy'][number], dialect: SqlDialect): OrderByItemNode {
|
|
203
|
+
const item = OrderByItemNode.create(column(table, order.column), RawNode.createWithSql(order.direction))
|
|
204
|
+
return dialect === 'postgres' ? item : Object.freeze({ ...item, nulls: order.direction === 'asc' ? 'last' : 'first' })
|
|
205
|
+
}
|
|
206
|
+
|
|
170
207
|
/** `after` is a keyset page's position, ANDed to the caller's conditions inside their parenthesis. */
|
|
171
208
|
export function selectNode(
|
|
172
209
|
state: SelectState,
|
|
@@ -174,12 +211,13 @@ export function selectNode(
|
|
|
174
211
|
scope: OperationNode,
|
|
175
212
|
after?: OperationNode,
|
|
176
213
|
days: NameSet = NO_DAYS,
|
|
214
|
+
dialect: SqlDialect = 'postgres',
|
|
177
215
|
): SelectQueryNode {
|
|
178
216
|
const table = state.table
|
|
179
217
|
const columns = state.columns ?? visible
|
|
180
218
|
const conditions = conditionsNode(table, state.conditions)
|
|
181
219
|
const filter = after === undefined ? conditions : conditions === undefined ? after : AndNode.create(conditions, ParensNode.create(after))
|
|
182
|
-
const orderBy = state.orderBy.map((order) =>
|
|
220
|
+
const orderBy = state.orderBy.map((order) => orderItem(table, order, dialect))
|
|
183
221
|
const node: SelectQueryNode = {
|
|
184
222
|
kind: 'SelectQueryNode',
|
|
185
223
|
from: FromNode.create([TableNode.create(table)]),
|
|
@@ -316,6 +354,18 @@ export function locateNode(table: string, columns: readonly string[], scope: Ope
|
|
|
316
354
|
return Object.freeze(node)
|
|
317
355
|
}
|
|
318
356
|
|
|
357
|
+
/** `select T.<name> from T where <scope> group by T.<name>`: each value of `name` the scoped rows hold, once. */
|
|
358
|
+
export function valuesNode(table: string, name: string, scope: OperationNode): SelectQueryNode {
|
|
359
|
+
const node: SelectQueryNode = {
|
|
360
|
+
kind: 'SelectQueryNode',
|
|
361
|
+
from: FromNode.create([TableNode.create(table)]),
|
|
362
|
+
selections: [selection(table, name)],
|
|
363
|
+
where: scopedWhere(undefined, scope),
|
|
364
|
+
groupBy: GroupByNode.create([GroupByItemNode.create(column(table, name))]),
|
|
365
|
+
}
|
|
366
|
+
return Object.freeze(node)
|
|
367
|
+
}
|
|
368
|
+
|
|
319
369
|
/**
|
|
320
370
|
* The same check, locked: `for share` keeps the row from moving until the transaction that checked it ends, `for
|
|
321
371
|
* update` also keeps every other writer and locker of it waiting.
|
|
@@ -352,6 +402,7 @@ export function claimNode(
|
|
|
352
402
|
due: { readonly due: string; readonly attempts: string; readonly now: Date; readonly maxAttempts: number },
|
|
353
403
|
scope: OperationNode,
|
|
354
404
|
days: NameSet,
|
|
405
|
+
dialect: SqlDialect = 'postgres',
|
|
355
406
|
): SelectQueryNode {
|
|
356
407
|
const table = state.table
|
|
357
408
|
const waiting = allOf([
|
|
@@ -359,10 +410,29 @@ export function claimNode(
|
|
|
359
410
|
BinaryOperationNode.create(column(table, due.due), OperatorNode.create('<='), ValueNode.create(due.now)),
|
|
360
411
|
BinaryOperationNode.create(column(table, due.attempts), OperatorNode.create('<'), ValueNode.create(due.maxAttempts)),
|
|
361
412
|
])
|
|
362
|
-
const node = selectNode(state, columns, scope, waiting, days)
|
|
413
|
+
const node = selectNode(state, columns, scope, waiting, days, dialect)
|
|
363
414
|
return Object.freeze({ ...node, endModifiers: [SelectModifierNode.create('ForUpdate'), SelectModifierNode.create('SkipLocked')] })
|
|
364
415
|
}
|
|
365
416
|
|
|
417
|
+
/**
|
|
418
|
+
* When the same claim next finds a row: `select <due> from table where (<conditions> and (<due> > $now and <attempts> <
|
|
419
|
+
* $max)) and scope order by <due> limit 1`, with `state` the claim's conditions ordered by `<due>`, limited to one.
|
|
420
|
+
*/
|
|
421
|
+
export function nextClaimNode(
|
|
422
|
+
state: SelectState,
|
|
423
|
+
due: { readonly due: string; readonly attempts: string; readonly now: Date; readonly maxAttempts: number },
|
|
424
|
+
scope: OperationNode,
|
|
425
|
+
days: NameSet,
|
|
426
|
+
dialect: SqlDialect = 'postgres',
|
|
427
|
+
): SelectQueryNode {
|
|
428
|
+
const table = state.table
|
|
429
|
+
const later = allOf([
|
|
430
|
+
BinaryOperationNode.create(column(table, due.due), OperatorNode.create('>'), ValueNode.create(due.now)),
|
|
431
|
+
BinaryOperationNode.create(column(table, due.attempts), OperatorNode.create('<'), ValueNode.create(due.maxAttempts)),
|
|
432
|
+
])
|
|
433
|
+
return selectNode(state, [due.due], scope, later, days, dialect)
|
|
434
|
+
}
|
|
435
|
+
|
|
366
436
|
/** `select <columns> from table where scope for update`: a mutation's precondition read of its own target. */
|
|
367
437
|
export function lockedSelectNode(table: string, columns: readonly string[], scope: OperationNode, days: NameSet = NO_DAYS): SelectQueryNode {
|
|
368
438
|
const node: SelectQueryNode = {
|