@swirls/looms 0.19.1 → 0.21.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/CHANGELOG.md +14 -0
- package/dist/agent/module.d.ts +727 -727
- package/dist/evaluator/effects.d.ts +33 -33
- package/dist/evaluator/module.d.ts +66 -66
- package/dist/nats/index.d.ts +2 -0
- package/dist/nats/index.js +2 -0
- package/dist/nats/projector.d.ts +69 -0
- package/dist/nats/projector.js +278 -0
- package/dist/nats/subjects.d.ts +14 -0
- package/dist/nats/subjects.js +44 -0
- package/dist/projectors/event-log.d.ts +7 -3
- package/dist/projectors/event-log.js +42 -9
- package/dist/projectors/replica/columns.d.ts +10 -3
- package/dist/projectors/replica/columns.js +22 -7
- package/dist/projectors/replica/drizzle.d.ts +9 -242
- package/dist/projectors/replica/drizzle.js +19 -6
- package/dist/projectors/replica/migrations.d.ts +8 -1
- package/dist/projectors/replica/migrations.js +26 -2
- package/dist/projectors/replica/postgres.d.ts +3 -2
- package/dist/projectors/replica/postgres.js +1 -1
- package/dist/projectors/replica/sqlite-migrations.js +19 -1
- package/dist/projectors/replica/sqlite.d.ts +3 -2
- package/dist/projectors/replica/sqlite.js +13 -4
- package/package.json +8 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DateTime, Effect } from 'effect';
|
|
2
2
|
import createPostgresClient, {} from 'postgres';
|
|
3
|
-
import {} from '@swirls/looms/core';
|
|
3
|
+
import { isJsonObject, readAttributes, } from '@swirls/looms/core';
|
|
4
4
|
import { eventLogOps } from './event-log-ops.js';
|
|
5
5
|
import { runProjectorPromise } from './projector.js';
|
|
6
6
|
import { columnValues, resolveReplicaColumns } from './replica/columns.js';
|
|
@@ -172,7 +172,10 @@ function postgresStore(sql, ownsClient, additionalFields, migrate) {
|
|
|
172
172
|
: Promise.resolve(),
|
|
173
173
|
};
|
|
174
174
|
}
|
|
175
|
-
/**
|
|
175
|
+
/** Postgres allows 65,535 bind parameters per statement. */
|
|
176
|
+
const MAX_BIND_PARAMETERS = 65_535;
|
|
177
|
+
/** Bind parameters per event row before declared attribute columns. */
|
|
178
|
+
const EVENT_ROW_PARAMETERS = 14;
|
|
176
179
|
const INSERT_CHUNK_ROWS = 1_000;
|
|
177
180
|
function runIndexPatches(ops) {
|
|
178
181
|
const patches = new Map();
|
|
@@ -201,7 +204,10 @@ function runIndexPatches(ops) {
|
|
|
201
204
|
}
|
|
202
205
|
export function applySqlOps(sql, ops, columns = []) {
|
|
203
206
|
const events = ops.flatMap((op) => (op.type === 'insert' ? [envelopeFromEntry(op.entry)] : []));
|
|
204
|
-
return Promise.all([
|
|
207
|
+
return Promise.all([
|
|
208
|
+
insertRunEvents(sql, events, columns),
|
|
209
|
+
applyRunIndexOps(sql, ops, columns),
|
|
210
|
+
]).then(() => undefined);
|
|
205
211
|
}
|
|
206
212
|
/** Fold run-summary ops. Event rows are written by {@link insertRunEvents}. */
|
|
207
213
|
export function applyRunIndexOps(sql, ops, columns = []) {
|
|
@@ -217,13 +223,39 @@ export function applyRunIndexOps(sql, ops, columns = []) {
|
|
|
217
223
|
}
|
|
218
224
|
return Promise.all(statements).then(() => undefined);
|
|
219
225
|
}
|
|
220
|
-
/**
|
|
221
|
-
|
|
226
|
+
/**
|
|
227
|
+
* Insert event envelopes, including ephemeral ones. Durable callers pass events that are already filtered.
|
|
228
|
+
* Each declared attribute column is filled from the run's attributes: the started event when it is in
|
|
229
|
+
* `events`, otherwise the run's `looms_run_index` row, which an earlier batch wrote.
|
|
230
|
+
*/
|
|
231
|
+
export async function insertRunEvents(sql, events, columns = []) {
|
|
232
|
+
const attributes = columns.length === 0 ? new Map() : await runAttributes(sql, events);
|
|
233
|
+
const chunkRows = Math.min(INSERT_CHUNK_ROWS, Math.floor(MAX_BIND_PARAMETERS / (EVENT_ROW_PARAMETERS + columns.length)));
|
|
222
234
|
const statements = [];
|
|
223
|
-
for (let index = 0; index < events.length; index +=
|
|
224
|
-
statements.push(insertEventChunk(sql, events.slice(index, index +
|
|
235
|
+
for (let index = 0; index < events.length; index += chunkRows) {
|
|
236
|
+
statements.push(insertEventChunk(sql, events.slice(index, index + chunkRows), columns, attributes));
|
|
225
237
|
}
|
|
226
|
-
|
|
238
|
+
await Promise.all(statements);
|
|
239
|
+
}
|
|
240
|
+
/** Attributes for each run in `events`. They are immutable, so any source for a run agrees. */
|
|
241
|
+
async function runAttributes(sql, events) {
|
|
242
|
+
const found = new Map();
|
|
243
|
+
for (const event of events) {
|
|
244
|
+
if (event.type === 'runtime.run.started' && !event.ephemeral && !found.has(event.runId)) {
|
|
245
|
+
const payload = isJsonObject(event.payload) ? event.payload : {};
|
|
246
|
+
found.set(event.runId, readAttributes(payload.attributes));
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const missing = [...new Set(events.map((event) => event.runId))].filter((id) => !found.has(id));
|
|
250
|
+
if (missing.length > 0) {
|
|
251
|
+
const rows = await sql `
|
|
252
|
+
SELECT run_id, attributes FROM looms_run_index WHERE run_id IN ${sql(missing)}
|
|
253
|
+
`;
|
|
254
|
+
for (const row of rows) {
|
|
255
|
+
found.set(row.run_id, row.attributes);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return found;
|
|
227
259
|
}
|
|
228
260
|
function envelopeFromEntry(entry) {
|
|
229
261
|
return {
|
|
@@ -242,8 +274,9 @@ function envelopeFromEntry(entry) {
|
|
|
242
274
|
idempotencyKey: entry.idempotencyKey,
|
|
243
275
|
};
|
|
244
276
|
}
|
|
245
|
-
function insertEventChunk(sql, events) {
|
|
277
|
+
function insertEventChunk(sql, events, columns, attributes) {
|
|
246
278
|
const rows = events.map((event) => ({
|
|
279
|
+
...hostColumnRow(columns, attributes.get(event.runId)),
|
|
247
280
|
run_id: event.runId,
|
|
248
281
|
seq: event.seq,
|
|
249
282
|
event_id: event.id,
|
|
@@ -11,7 +11,7 @@ export interface ReplicaPurgeOptions<Fields extends RunAttributeFields = RunAttr
|
|
|
11
11
|
readonly [Key in keyof Fields & string]?: string;
|
|
12
12
|
};
|
|
13
13
|
}
|
|
14
|
-
/** A declared attribute and the
|
|
14
|
+
/** A declared attribute and the column that stores it on `looms_run_index` and `looms_run_events`. */
|
|
15
15
|
export interface ResolvedReplicaColumn {
|
|
16
16
|
/** Attribute name, and the property name on the Drizzle table. */
|
|
17
17
|
readonly key: string;
|
|
@@ -34,8 +34,15 @@ export interface ReplicaMigration {
|
|
|
34
34
|
readonly id: string;
|
|
35
35
|
readonly sql: string;
|
|
36
36
|
}
|
|
37
|
+
export interface ColumnMigrationSql {
|
|
38
|
+
readonly runIndexColumn: (column: ResolvedReplicaColumn) => string;
|
|
39
|
+
readonly runIndexIndex: (column: ResolvedReplicaColumn) => string;
|
|
40
|
+
readonly eventColumn: (column: ResolvedReplicaColumn) => string;
|
|
41
|
+
readonly eventIndex: (column: ResolvedReplicaColumn) => string;
|
|
42
|
+
}
|
|
37
43
|
/**
|
|
38
44
|
* Migrations for declared attribute columns, after the Looms tables.
|
|
39
|
-
* Each column and each index is its own migration, so declaring one later only adds
|
|
45
|
+
* Each column and each index is its own migration, so declaring one later only adds those files.
|
|
46
|
+
* The `looms_run_index` column comes first; the `looms_run_events` column backfills from that table.
|
|
40
47
|
*/
|
|
41
|
-
export declare function columnMigrations(fields: RunAttributeFields | undefined, migration: (id: string, sql: string) => ReplicaMigration,
|
|
48
|
+
export declare function columnMigrations(fields: RunAttributeFields | undefined, migration: (id: string, sql: string) => ReplicaMigration, sql: ColumnMigrationSql): readonly ReplicaMigration[];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** Postgres caps identifiers at 63 bytes, and
|
|
1
|
+
/** Postgres caps identifiers at 63 bytes, and index names add `looms_run_index_` or `looms_run_event_`. */
|
|
2
2
|
const COLUMN_NAME = /^[a-z_][a-z0-9_]{0,46}$/;
|
|
3
3
|
const LOOMS_COLUMNS = new Set([
|
|
4
4
|
'run_id',
|
|
@@ -16,6 +16,18 @@ const LOOMS_COLUMNS = new Set([
|
|
|
16
16
|
'completed_at',
|
|
17
17
|
'replicated_seq',
|
|
18
18
|
'purged_at',
|
|
19
|
+
'seq',
|
|
20
|
+
'event_id',
|
|
21
|
+
'ts',
|
|
22
|
+
'type',
|
|
23
|
+
'payload',
|
|
24
|
+
'thread_id',
|
|
25
|
+
'parent_thread_id',
|
|
26
|
+
'causation_id',
|
|
27
|
+
'effect_id',
|
|
28
|
+
'origin',
|
|
29
|
+
'idempotency_key',
|
|
30
|
+
'ephemeral',
|
|
19
31
|
]);
|
|
20
32
|
function snakeCase(key) {
|
|
21
33
|
return key
|
|
@@ -67,11 +79,14 @@ export function sqlStringLiteral(value) {
|
|
|
67
79
|
}
|
|
68
80
|
/**
|
|
69
81
|
* Migrations for declared attribute columns, after the Looms tables.
|
|
70
|
-
* Each column and each index is its own migration, so declaring one later only adds
|
|
82
|
+
* Each column and each index is its own migration, so declaring one later only adds those files.
|
|
83
|
+
* The `looms_run_index` column comes first; the `looms_run_events` column backfills from that table.
|
|
71
84
|
*/
|
|
72
|
-
export function columnMigrations(fields, migration,
|
|
73
|
-
return resolveReplicaColumns(fields).flatMap((column) =>
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
85
|
+
export function columnMigrations(fields, migration, sql) {
|
|
86
|
+
return resolveReplicaColumns(fields).flatMap((column) => [
|
|
87
|
+
migration(`column_${column.name}`, sql.runIndexColumn(column)),
|
|
88
|
+
...(column.index ? [migration(`index_${column.name}`, sql.runIndexIndex(column))] : []),
|
|
89
|
+
migration(`event_column_${column.name}`, sql.eventColumn(column)),
|
|
90
|
+
...(column.index ? [migration(`event_index_${column.name}`, sql.eventIndex(column))] : []),
|
|
91
|
+
]);
|
|
77
92
|
}
|
|
@@ -104,6 +104,7 @@ type AttributeColumnBuilders<Fields extends RunAttributeFields> = {
|
|
|
104
104
|
[Key in keyof Fields & string]: PgTextBuilderInitial<'', [string, ...string[]]>;
|
|
105
105
|
};
|
|
106
106
|
type RunIndexColumns<Extra extends RunIndexExtras, Fields extends RunAttributeFields> = ReturnType<typeof loomsRunIndexColumns> & AttributeColumnBuilders<Fields> & Extra;
|
|
107
|
+
type RunEventsColumns<Fields extends RunAttributeFields> = ReturnType<typeof loomsRunEventsColumns> & AttributeColumnBuilders<Fields>;
|
|
107
108
|
export interface LoomsReplicaTablesOptions<Extra extends RunIndexExtras = Record<never, never>, Fields extends RunAttributeFields = NoAdditionalFields> {
|
|
108
109
|
/** Declared run attributes. Pass the same declaration the replica uses. */
|
|
109
110
|
readonly additionalFields?: Fields;
|
|
@@ -112,8 +113,9 @@ export interface LoomsReplicaTablesOptions<Extra extends RunIndexExtras = Record
|
|
|
112
113
|
}
|
|
113
114
|
/**
|
|
114
115
|
* Drizzle tables for the replica. `additionalFields` adds a column per declared attribute,
|
|
115
|
-
* keyed by attribute name, with its index
|
|
116
|
-
* (`input`, `error`, and so on); those are not in
|
|
116
|
+
* keyed by attribute name, with its index, to both `looms_run_index` and `looms_run_events`.
|
|
117
|
+
* `runIndex` adds columns the host owns (`input`, `error`, and so on); those are not in
|
|
118
|
+
* Looms migrations.
|
|
117
119
|
*/
|
|
118
120
|
export declare function loomsReplicaTables<Extra extends RunIndexExtras = Record<never, never>, const Fields extends RunAttributeFields = NoAdditionalFields>(options?: LoomsReplicaTablesOptions<Extra, Fields>): {
|
|
119
121
|
loomsRunIndex: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
@@ -129,246 +131,11 @@ export declare function loomsReplicaTables<Extra extends RunIndexExtras = Record
|
|
|
129
131
|
loomsRunEvents: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
130
132
|
name: "looms_run_events";
|
|
131
133
|
schema: undefined;
|
|
132
|
-
columns: {
|
|
133
|
-
|
|
134
|
-
name: "
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
columnType: "PgText";
|
|
138
|
-
data: string;
|
|
139
|
-
driverParam: string;
|
|
140
|
-
notNull: true;
|
|
141
|
-
hasDefault: false;
|
|
142
|
-
isPrimaryKey: false;
|
|
143
|
-
isAutoincrement: false;
|
|
144
|
-
hasRuntimeDefault: false;
|
|
145
|
-
enumValues: [string, ...string[]];
|
|
146
|
-
baseColumn: never;
|
|
147
|
-
identity: undefined;
|
|
148
|
-
generated: undefined;
|
|
149
|
-
}, {}, {}>;
|
|
150
|
-
seq: import("drizzle-orm/pg-core").PgColumn<{
|
|
151
|
-
name: "seq";
|
|
152
|
-
tableName: "looms_run_events";
|
|
153
|
-
dataType: "number";
|
|
154
|
-
columnType: "PgBigInt53";
|
|
155
|
-
data: number;
|
|
156
|
-
driverParam: string | number;
|
|
157
|
-
notNull: true;
|
|
158
|
-
hasDefault: false;
|
|
159
|
-
isPrimaryKey: false;
|
|
160
|
-
isAutoincrement: false;
|
|
161
|
-
hasRuntimeDefault: false;
|
|
162
|
-
enumValues: undefined;
|
|
163
|
-
baseColumn: never;
|
|
164
|
-
identity: undefined;
|
|
165
|
-
generated: undefined;
|
|
166
|
-
}, {}, {}>;
|
|
167
|
-
eventId: import("drizzle-orm/pg-core").PgColumn<{
|
|
168
|
-
name: "event_id";
|
|
169
|
-
tableName: "looms_run_events";
|
|
170
|
-
dataType: "string";
|
|
171
|
-
columnType: "PgText";
|
|
172
|
-
data: string;
|
|
173
|
-
driverParam: string;
|
|
174
|
-
notNull: true;
|
|
175
|
-
hasDefault: false;
|
|
176
|
-
isPrimaryKey: false;
|
|
177
|
-
isAutoincrement: false;
|
|
178
|
-
hasRuntimeDefault: false;
|
|
179
|
-
enumValues: [string, ...string[]];
|
|
180
|
-
baseColumn: never;
|
|
181
|
-
identity: undefined;
|
|
182
|
-
generated: undefined;
|
|
183
|
-
}, {}, {}>;
|
|
184
|
-
ts: import("drizzle-orm/pg-core").PgColumn<{
|
|
185
|
-
name: "ts";
|
|
186
|
-
tableName: "looms_run_events";
|
|
187
|
-
dataType: "date";
|
|
188
|
-
columnType: "PgTimestamp";
|
|
189
|
-
data: Date;
|
|
190
|
-
driverParam: string;
|
|
191
|
-
notNull: true;
|
|
192
|
-
hasDefault: false;
|
|
193
|
-
isPrimaryKey: false;
|
|
194
|
-
isAutoincrement: false;
|
|
195
|
-
hasRuntimeDefault: false;
|
|
196
|
-
enumValues: undefined;
|
|
197
|
-
baseColumn: never;
|
|
198
|
-
identity: undefined;
|
|
199
|
-
generated: undefined;
|
|
200
|
-
}, {}, {}>;
|
|
201
|
-
type: import("drizzle-orm/pg-core").PgColumn<{
|
|
202
|
-
name: "type";
|
|
203
|
-
tableName: "looms_run_events";
|
|
204
|
-
dataType: "string";
|
|
205
|
-
columnType: "PgText";
|
|
206
|
-
data: string;
|
|
207
|
-
driverParam: string;
|
|
208
|
-
notNull: true;
|
|
209
|
-
hasDefault: false;
|
|
210
|
-
isPrimaryKey: false;
|
|
211
|
-
isAutoincrement: false;
|
|
212
|
-
hasRuntimeDefault: false;
|
|
213
|
-
enumValues: [string, ...string[]];
|
|
214
|
-
baseColumn: never;
|
|
215
|
-
identity: undefined;
|
|
216
|
-
generated: undefined;
|
|
217
|
-
}, {}, {}>;
|
|
218
|
-
payload: import("drizzle-orm/pg-core").PgColumn<{
|
|
219
|
-
name: "payload";
|
|
220
|
-
tableName: "looms_run_events";
|
|
221
|
-
dataType: "json";
|
|
222
|
-
columnType: "PgJsonb";
|
|
223
|
-
data: unknown;
|
|
224
|
-
driverParam: unknown;
|
|
225
|
-
notNull: true;
|
|
226
|
-
hasDefault: false;
|
|
227
|
-
isPrimaryKey: false;
|
|
228
|
-
isAutoincrement: false;
|
|
229
|
-
hasRuntimeDefault: false;
|
|
230
|
-
enumValues: undefined;
|
|
231
|
-
baseColumn: never;
|
|
232
|
-
identity: undefined;
|
|
233
|
-
generated: undefined;
|
|
234
|
-
}, {}, {}>;
|
|
235
|
-
threadId: import("drizzle-orm/pg-core").PgColumn<{
|
|
236
|
-
name: "thread_id";
|
|
237
|
-
tableName: "looms_run_events";
|
|
238
|
-
dataType: "string";
|
|
239
|
-
columnType: "PgText";
|
|
240
|
-
data: string;
|
|
241
|
-
driverParam: string;
|
|
242
|
-
notNull: false;
|
|
243
|
-
hasDefault: false;
|
|
244
|
-
isPrimaryKey: false;
|
|
245
|
-
isAutoincrement: false;
|
|
246
|
-
hasRuntimeDefault: false;
|
|
247
|
-
enumValues: [string, ...string[]];
|
|
248
|
-
baseColumn: never;
|
|
249
|
-
identity: undefined;
|
|
250
|
-
generated: undefined;
|
|
251
|
-
}, {}, {}>;
|
|
252
|
-
parentThreadId: import("drizzle-orm/pg-core").PgColumn<{
|
|
253
|
-
name: "parent_thread_id";
|
|
254
|
-
tableName: "looms_run_events";
|
|
255
|
-
dataType: "string";
|
|
256
|
-
columnType: "PgText";
|
|
257
|
-
data: string;
|
|
258
|
-
driverParam: string;
|
|
259
|
-
notNull: false;
|
|
260
|
-
hasDefault: false;
|
|
261
|
-
isPrimaryKey: false;
|
|
262
|
-
isAutoincrement: false;
|
|
263
|
-
hasRuntimeDefault: false;
|
|
264
|
-
enumValues: [string, ...string[]];
|
|
265
|
-
baseColumn: never;
|
|
266
|
-
identity: undefined;
|
|
267
|
-
generated: undefined;
|
|
268
|
-
}, {}, {}>;
|
|
269
|
-
causationId: import("drizzle-orm/pg-core").PgColumn<{
|
|
270
|
-
name: "causation_id";
|
|
271
|
-
tableName: "looms_run_events";
|
|
272
|
-
dataType: "string";
|
|
273
|
-
columnType: "PgText";
|
|
274
|
-
data: string;
|
|
275
|
-
driverParam: string;
|
|
276
|
-
notNull: false;
|
|
277
|
-
hasDefault: false;
|
|
278
|
-
isPrimaryKey: false;
|
|
279
|
-
isAutoincrement: false;
|
|
280
|
-
hasRuntimeDefault: false;
|
|
281
|
-
enumValues: [string, ...string[]];
|
|
282
|
-
baseColumn: never;
|
|
283
|
-
identity: undefined;
|
|
284
|
-
generated: undefined;
|
|
285
|
-
}, {}, {}>;
|
|
286
|
-
correlationId: import("drizzle-orm/pg-core").PgColumn<{
|
|
287
|
-
name: "correlation_id";
|
|
288
|
-
tableName: "looms_run_events";
|
|
289
|
-
dataType: "string";
|
|
290
|
-
columnType: "PgText";
|
|
291
|
-
data: string;
|
|
292
|
-
driverParam: string;
|
|
293
|
-
notNull: false;
|
|
294
|
-
hasDefault: false;
|
|
295
|
-
isPrimaryKey: false;
|
|
296
|
-
isAutoincrement: false;
|
|
297
|
-
hasRuntimeDefault: false;
|
|
298
|
-
enumValues: [string, ...string[]];
|
|
299
|
-
baseColumn: never;
|
|
300
|
-
identity: undefined;
|
|
301
|
-
generated: undefined;
|
|
302
|
-
}, {}, {}>;
|
|
303
|
-
effectId: import("drizzle-orm/pg-core").PgColumn<{
|
|
304
|
-
name: "effect_id";
|
|
305
|
-
tableName: "looms_run_events";
|
|
306
|
-
dataType: "string";
|
|
307
|
-
columnType: "PgText";
|
|
308
|
-
data: string;
|
|
309
|
-
driverParam: string;
|
|
310
|
-
notNull: false;
|
|
311
|
-
hasDefault: false;
|
|
312
|
-
isPrimaryKey: false;
|
|
313
|
-
isAutoincrement: false;
|
|
314
|
-
hasRuntimeDefault: false;
|
|
315
|
-
enumValues: [string, ...string[]];
|
|
316
|
-
baseColumn: never;
|
|
317
|
-
identity: undefined;
|
|
318
|
-
generated: undefined;
|
|
319
|
-
}, {}, {}>;
|
|
320
|
-
origin: import("drizzle-orm/pg-core").PgColumn<{
|
|
321
|
-
name: "origin";
|
|
322
|
-
tableName: "looms_run_events";
|
|
323
|
-
dataType: "json";
|
|
324
|
-
columnType: "PgJsonb";
|
|
325
|
-
data: unknown;
|
|
326
|
-
driverParam: unknown;
|
|
327
|
-
notNull: true;
|
|
328
|
-
hasDefault: false;
|
|
329
|
-
isPrimaryKey: false;
|
|
330
|
-
isAutoincrement: false;
|
|
331
|
-
hasRuntimeDefault: false;
|
|
332
|
-
enumValues: undefined;
|
|
333
|
-
baseColumn: never;
|
|
334
|
-
identity: undefined;
|
|
335
|
-
generated: undefined;
|
|
336
|
-
}, {}, {}>;
|
|
337
|
-
idempotencyKey: import("drizzle-orm/pg-core").PgColumn<{
|
|
338
|
-
name: "idempotency_key";
|
|
339
|
-
tableName: "looms_run_events";
|
|
340
|
-
dataType: "string";
|
|
341
|
-
columnType: "PgText";
|
|
342
|
-
data: string;
|
|
343
|
-
driverParam: string;
|
|
344
|
-
notNull: false;
|
|
345
|
-
hasDefault: false;
|
|
346
|
-
isPrimaryKey: false;
|
|
347
|
-
isAutoincrement: false;
|
|
348
|
-
hasRuntimeDefault: false;
|
|
349
|
-
enumValues: [string, ...string[]];
|
|
350
|
-
baseColumn: never;
|
|
351
|
-
identity: undefined;
|
|
352
|
-
generated: undefined;
|
|
353
|
-
}, {}, {}>;
|
|
354
|
-
ephemeral: import("drizzle-orm/pg-core").PgColumn<{
|
|
355
|
-
name: "ephemeral";
|
|
356
|
-
tableName: "looms_run_events";
|
|
357
|
-
dataType: "boolean";
|
|
358
|
-
columnType: "PgBoolean";
|
|
359
|
-
data: boolean;
|
|
360
|
-
driverParam: boolean;
|
|
361
|
-
notNull: true;
|
|
362
|
-
hasDefault: true;
|
|
363
|
-
isPrimaryKey: false;
|
|
364
|
-
isAutoincrement: false;
|
|
365
|
-
hasRuntimeDefault: false;
|
|
366
|
-
enumValues: undefined;
|
|
367
|
-
baseColumn: never;
|
|
368
|
-
identity: undefined;
|
|
369
|
-
generated: undefined;
|
|
370
|
-
}, {}, {}>;
|
|
371
|
-
};
|
|
134
|
+
columns: RunEventsColumns<Fields> extends infer T_1 extends Record<string, import("drizzle-orm").ColumnBuilderBase<import("drizzle-orm").ColumnBuilderBaseConfig<import("drizzle-orm").ColumnDataType, string>, object>> ? { [Key in keyof T_1]: import("drizzle-orm").BuildColumn<TTableName, {
|
|
135
|
+
_: Omit<T_1[Key]["_"], "name"> & {
|
|
136
|
+
name: T_1[Key]["_"]["name"] extends "" ? import("drizzle-orm").Assume<Key, string> : T_1[Key]["_"]["name"];
|
|
137
|
+
};
|
|
138
|
+
}, TDialect>; } : never;
|
|
372
139
|
dialect: 'pg';
|
|
373
140
|
}>;
|
|
374
141
|
loomsRunActors: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
@@ -74,8 +74,9 @@ export const loomsSchemaMigrations = pgTable('looms_schema_migrations', {
|
|
|
74
74
|
});
|
|
75
75
|
/**
|
|
76
76
|
* Drizzle tables for the replica. `additionalFields` adds a column per declared attribute,
|
|
77
|
-
* keyed by attribute name, with its index
|
|
78
|
-
* (`input`, `error`, and so on); those are not in
|
|
77
|
+
* keyed by attribute name, with its index, to both `looms_run_index` and `looms_run_events`.
|
|
78
|
+
* `runIndex` adds columns the host owns (`input`, `error`, and so on); those are not in
|
|
79
|
+
* Looms migrations.
|
|
79
80
|
*/
|
|
80
81
|
export function loomsReplicaTables(options) {
|
|
81
82
|
const attributeColumns = resolveReplicaColumns(options?.additionalFields);
|
|
@@ -96,10 +97,22 @@ export function loomsReplicaTables(options) {
|
|
|
96
97
|
}),
|
|
97
98
|
];
|
|
98
99
|
});
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
// SAFETY: resolveReplicaColumns returns one column for each key of Fields.
|
|
101
|
+
const eventColumns = {
|
|
102
|
+
...loomsRunEventsColumns(),
|
|
103
|
+
...Object.fromEntries(attributeColumns.map((column) => [column.key, text(column.name)])),
|
|
104
|
+
};
|
|
105
|
+
const loomsRunEvents = pgTable('looms_run_events', eventColumns, (table) => {
|
|
106
|
+
const byKey = table;
|
|
107
|
+
return [
|
|
108
|
+
primaryKey({ columns: [table.runId, table.seq] }),
|
|
109
|
+
index('looms_run_events_type_ts').on(table.type, table.ts),
|
|
110
|
+
...attributeColumns.flatMap((column) => {
|
|
111
|
+
const built = byKey[column.key];
|
|
112
|
+
return column.index && built ? [index(`looms_run_event_${column.name}`).on(built)] : [];
|
|
113
|
+
}),
|
|
114
|
+
];
|
|
115
|
+
});
|
|
103
116
|
const loomsRunActors = pgTable('looms_run_actors', loomsRunActorsColumns(), (table) => [
|
|
104
117
|
primaryKey({ columns: [table.runId, table.actorId] }),
|
|
105
118
|
index('looms_run_actors_launched_by').on(table.launchedByRunId),
|
|
@@ -29,7 +29,14 @@ export declare const POSTGRES_SCHEMA_MIGRATIONS_SQL: string;
|
|
|
29
29
|
export declare function postgresColumnSql(column: ResolvedReplicaColumn): string;
|
|
30
30
|
export declare function postgresColumnIndexSql(column: ResolvedReplicaColumn): string;
|
|
31
31
|
/**
|
|
32
|
-
*
|
|
32
|
+
* Adds one attribute column to `looms_run_events` and copies it from the run's
|
|
33
|
+
* `looms_run_index` attributes for events already in the table. Safe to repeat:
|
|
34
|
+
* the backfill only touches rows whose column is still empty.
|
|
35
|
+
*/
|
|
36
|
+
export declare function postgresEventColumnSql(column: ResolvedReplicaColumn): string;
|
|
37
|
+
export declare function postgresEventColumnIndexSql(column: ResolvedReplicaColumn): string;
|
|
38
|
+
/**
|
|
39
|
+
* Attribute column and index migrations for Postgres, on `looms_run_index` and `looms_run_events`.
|
|
33
40
|
* Each runs once when recorded in `looms_schema_migrations`, including the backfill UPDATE.
|
|
34
41
|
*/
|
|
35
42
|
export declare function postgresAttributeMigrations(additionalFields?: RunAttributeFields): readonly ReplicaMigration[];
|
|
@@ -124,11 +124,35 @@ export function postgresColumnIndexSql(column) {
|
|
|
124
124
|
return `CREATE INDEX IF NOT EXISTS looms_run_index_${column.name} ON looms_run_index ("${column.name}");`;
|
|
125
125
|
}
|
|
126
126
|
/**
|
|
127
|
-
*
|
|
127
|
+
* Adds one attribute column to `looms_run_events` and copies it from the run's
|
|
128
|
+
* `looms_run_index` attributes for events already in the table. Safe to repeat:
|
|
129
|
+
* the backfill only touches rows whose column is still empty.
|
|
130
|
+
*/
|
|
131
|
+
export function postgresEventColumnSql(column) {
|
|
132
|
+
const name = `"${column.name}"`;
|
|
133
|
+
const attribute = sqlStringLiteral(column.key);
|
|
134
|
+
return `
|
|
135
|
+
ALTER TABLE looms_run_events ADD COLUMN IF NOT EXISTS ${name} TEXT;
|
|
136
|
+
|
|
137
|
+
UPDATE looms_run_events AS event SET ${name} = run.attributes ->> ${attribute}
|
|
138
|
+
FROM looms_run_index AS run
|
|
139
|
+
WHERE event.run_id = run.run_id AND event.${name} IS NULL AND run.attributes ? ${attribute};
|
|
140
|
+
`.trim();
|
|
141
|
+
}
|
|
142
|
+
export function postgresEventColumnIndexSql(column) {
|
|
143
|
+
return `CREATE INDEX IF NOT EXISTS looms_run_event_${column.name} ON looms_run_events ("${column.name}");`;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Attribute column and index migrations for Postgres, on `looms_run_index` and `looms_run_events`.
|
|
128
147
|
* Each runs once when recorded in `looms_schema_migrations`, including the backfill UPDATE.
|
|
129
148
|
*/
|
|
130
149
|
export function postgresAttributeMigrations(additionalFields) {
|
|
131
|
-
return columnMigrations(additionalFields, (id, sql) => replicaMigration(id, [sql]),
|
|
150
|
+
return columnMigrations(additionalFields, (id, sql) => replicaMigration(id, [sql]), {
|
|
151
|
+
runIndexColumn: postgresColumnSql,
|
|
152
|
+
runIndexIndex: postgresColumnIndexSql,
|
|
153
|
+
eventColumn: postgresEventColumnSql,
|
|
154
|
+
eventIndex: postgresEventColumnIndexSql,
|
|
155
|
+
});
|
|
132
156
|
}
|
|
133
157
|
/** Serializes Looms migrations across processes that share one database. */
|
|
134
158
|
const MIGRATION_LOCK_KEY = 814_764;
|
|
@@ -31,8 +31,9 @@ export type PostgresReplicaOptions<Fields extends RunAttributeFields = NoAdditio
|
|
|
31
31
|
*/
|
|
32
32
|
readonly migrate: PostgresReplicaMigrate;
|
|
33
33
|
/**
|
|
34
|
-
* Declared run attributes, from `defineRunAttributes`. Each one is a `looms_run_index`
|
|
35
|
-
*
|
|
34
|
+
* Declared run attributes, from `defineRunAttributes`. Each one is a column on `looms_run_index`
|
|
35
|
+
* and on `looms_run_events`, and each column and index is its own migration.
|
|
36
|
+
* `purge({ where })` filters on them.
|
|
36
37
|
*/
|
|
37
38
|
readonly additionalFields?: Fields;
|
|
38
39
|
};
|
|
@@ -151,7 +151,7 @@ export function postgresReplica(options) {
|
|
|
151
151
|
const next = contiguousSeq(runId, replicatedSeq, batch);
|
|
152
152
|
const durable = batch.filter((event) => !event.ephemeral);
|
|
153
153
|
const last = batch.at(-1);
|
|
154
|
-
await insertRunEvents(tx, batch);
|
|
154
|
+
await insertRunEvents(tx, batch, columns);
|
|
155
155
|
await applyRunIndexOps(tx, durable.flatMap((event) => eventLogOps(event)), columns);
|
|
156
156
|
await applyIndexOps(tx, durable.flatMap(indexOpsFor));
|
|
157
157
|
if (next === replicatedSeq || !last) {
|
|
@@ -100,6 +100,19 @@ WHERE ${name} IS NULL AND ${value} IS NOT NULL;
|
|
|
100
100
|
function sqliteColumnIndexSql(column) {
|
|
101
101
|
return `CREATE INDEX IF NOT EXISTS looms_run_index_${column.name} ON looms_run_index ("${column.name}");`;
|
|
102
102
|
}
|
|
103
|
+
function sqliteEventColumnSql(column) {
|
|
104
|
+
const name = `"${column.name}"`;
|
|
105
|
+
const value = `(SELECT value FROM looms_run_index AS run, json_each(run.attributes) WHERE run.run_id = looms_run_events.run_id AND key = ${sqlStringLiteral(column.key)})`;
|
|
106
|
+
return `
|
|
107
|
+
ALTER TABLE looms_run_events ADD COLUMN ${name} TEXT;
|
|
108
|
+
|
|
109
|
+
UPDATE looms_run_events SET ${name} = ${value}
|
|
110
|
+
WHERE ${name} IS NULL AND ${value} IS NOT NULL;
|
|
111
|
+
`.trim();
|
|
112
|
+
}
|
|
113
|
+
function sqliteEventColumnIndexSql(column) {
|
|
114
|
+
return `CREATE INDEX IF NOT EXISTS looms_run_event_${column.name} ON looms_run_events ("${column.name}");`;
|
|
115
|
+
}
|
|
103
116
|
/** Versioned DDL for the SQLite replica with no attribute columns, including D1. Looms only creates `looms_*` objects. */
|
|
104
117
|
export const SQLITE_REPLICA_MIGRATIONS = [
|
|
105
118
|
sqliteReplicaMigration('0001_replica', SQLITE_REPLICA_SCHEMA_SQL),
|
|
@@ -107,6 +120,11 @@ export const SQLITE_REPLICA_MIGRATIONS = [
|
|
|
107
120
|
export function sqliteReplicaMigrations(additionalFields) {
|
|
108
121
|
return [
|
|
109
122
|
...SQLITE_REPLICA_MIGRATIONS,
|
|
110
|
-
...columnMigrations(additionalFields, sqliteReplicaMigration,
|
|
123
|
+
...columnMigrations(additionalFields, sqliteReplicaMigration, {
|
|
124
|
+
runIndexColumn: sqliteColumnSql,
|
|
125
|
+
runIndexIndex: sqliteColumnIndexSql,
|
|
126
|
+
eventColumn: sqliteEventColumnSql,
|
|
127
|
+
eventIndex: sqliteEventColumnIndexSql,
|
|
128
|
+
}),
|
|
111
129
|
];
|
|
112
130
|
}
|
|
@@ -51,8 +51,9 @@ export type SqliteReplicaOptions<Fields extends RunAttributeFields = NoAdditiona
|
|
|
51
51
|
*/
|
|
52
52
|
readonly migrate: SqliteReplicaMigrate;
|
|
53
53
|
/**
|
|
54
|
-
* Declared run attributes, from `defineRunAttributes`. Each one is a `looms_run_index`
|
|
55
|
-
*
|
|
54
|
+
* Declared run attributes, from `defineRunAttributes`. Each one is a column on `looms_run_index`
|
|
55
|
+
* and on `looms_run_events`, and each column and index is its own migration.
|
|
56
|
+
* `purge({ where })` filters on them.
|
|
56
57
|
*/
|
|
57
58
|
readonly additionalFields?: Fields;
|
|
58
59
|
};
|
|
@@ -130,11 +130,19 @@ function contiguousSeq(runId, replicatedSeq, events) {
|
|
|
130
130
|
function sqlStatement(sql, params) {
|
|
131
131
|
return { sql, params };
|
|
132
132
|
}
|
|
133
|
-
|
|
133
|
+
/**
|
|
134
|
+
* Declared attribute columns are copied from the run's `looms_run_index` row, so the run index
|
|
135
|
+
* statements for the same batch, which fill those columns from the started event, must run first.
|
|
136
|
+
*/
|
|
137
|
+
function insertEvents(events, columns) {
|
|
138
|
+
const hostNames = columns.map((column) => `, "${column.name}"`).join('');
|
|
139
|
+
const hostValues = columns
|
|
140
|
+
.map((column) => `, (SELECT "${column.name}" FROM looms_run_index WHERE run_id = ?)`)
|
|
141
|
+
.join('');
|
|
134
142
|
return events.map((event) => sqlStatement(`INSERT INTO looms_run_events (
|
|
135
143
|
run_id, seq, event_id, ts, type, payload, thread_id, parent_thread_id,
|
|
136
|
-
causation_id, correlation_id, effect_id, origin, idempotency_key, ephemeral
|
|
137
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
|
|
144
|
+
causation_id, correlation_id, effect_id, origin, idempotency_key, ephemeral${hostNames}
|
|
145
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?${hostValues})
|
|
138
146
|
ON CONFLICT (run_id, seq) DO NOTHING`, [
|
|
139
147
|
event.runId,
|
|
140
148
|
event.seq,
|
|
@@ -150,6 +158,7 @@ function insertEvents(events) {
|
|
|
150
158
|
jsonText(event.origin),
|
|
151
159
|
event.idempotencyKey ?? null,
|
|
152
160
|
event.ephemeral === true ? 1 : 0,
|
|
161
|
+
...columns.map(() => event.runId),
|
|
153
162
|
]));
|
|
154
163
|
}
|
|
155
164
|
function runIndexPatches(ops) {
|
|
@@ -341,8 +350,8 @@ export function sqliteReplica(options) {
|
|
|
341
350
|
const next = contiguousSeq(runId, replicatedSeq, batch);
|
|
342
351
|
const durable = batch.filter((event) => !event.ephemeral);
|
|
343
352
|
const last = batch.at(-1);
|
|
344
|
-
statements.push(...insertEvents(batch));
|
|
345
353
|
statements.push(...runIndexStatements(durable.flatMap((event) => eventLogOps(event)), columns));
|
|
354
|
+
statements.push(...insertEvents(batch, columns));
|
|
346
355
|
statements.push(...indexStatements(durable.flatMap(indexOpsFor)));
|
|
347
356
|
if (next === replicatedSeq || !last) {
|
|
348
357
|
continue;
|