@swirls/looms 0.19.1 → 0.20.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 +8 -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/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/dist/schedule/effects.d.ts +18 -18
- package/package.json +1 -1
|
@@ -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;
|
|
@@ -332,15 +332,6 @@ export declare const planEffect: NoInfer<import("@swirls/looms/core").LocalEffec
|
|
|
332
332
|
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
333
333
|
readonly type: "schedule.paused";
|
|
334
334
|
readonly payload: {};
|
|
335
|
-
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
336
|
-
readonly type: "schedule.resumed";
|
|
337
|
-
readonly payload: {};
|
|
338
|
-
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
339
|
-
readonly type: "schedule.retired";
|
|
340
|
-
readonly payload: {};
|
|
341
|
-
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
342
|
-
readonly type: "schedule.triggered";
|
|
343
|
-
readonly payload: {};
|
|
344
335
|
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
345
336
|
readonly type: "schedule.planned";
|
|
346
337
|
readonly payload: {
|
|
@@ -364,6 +355,12 @@ export declare const planEffect: NoInfer<import("@swirls/looms/core").LocalEffec
|
|
|
364
355
|
readonly payload: {
|
|
365
356
|
readonly reason: string;
|
|
366
357
|
};
|
|
358
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
359
|
+
readonly type: "schedule.resumed";
|
|
360
|
+
readonly payload: {};
|
|
361
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
362
|
+
readonly type: "schedule.retired";
|
|
363
|
+
readonly payload: {};
|
|
367
364
|
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
368
365
|
readonly type: "schedule.skipped";
|
|
369
366
|
readonly payload: {
|
|
@@ -374,6 +371,9 @@ export declare const planEffect: NoInfer<import("@swirls/looms/core").LocalEffec
|
|
|
374
371
|
readonly manual: boolean;
|
|
375
372
|
readonly triggerId?: string | undefined;
|
|
376
373
|
};
|
|
374
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
375
|
+
readonly type: "schedule.triggered";
|
|
376
|
+
readonly payload: {};
|
|
377
377
|
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
378
378
|
readonly type: "schedule.updated";
|
|
379
379
|
readonly payload: {
|
|
@@ -683,15 +683,6 @@ export declare const fireEffect: NoInfer<import("@swirls/looms/core").LocalEffec
|
|
|
683
683
|
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
684
684
|
readonly type: "schedule.paused";
|
|
685
685
|
readonly payload: {};
|
|
686
|
-
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
687
|
-
readonly type: "schedule.resumed";
|
|
688
|
-
readonly payload: {};
|
|
689
|
-
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
690
|
-
readonly type: "schedule.retired";
|
|
691
|
-
readonly payload: {};
|
|
692
|
-
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
693
|
-
readonly type: "schedule.triggered";
|
|
694
|
-
readonly payload: {};
|
|
695
686
|
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
696
687
|
readonly type: "schedule.planned";
|
|
697
688
|
readonly payload: {
|
|
@@ -715,6 +706,12 @@ export declare const fireEffect: NoInfer<import("@swirls/looms/core").LocalEffec
|
|
|
715
706
|
readonly payload: {
|
|
716
707
|
readonly reason: string;
|
|
717
708
|
};
|
|
709
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
710
|
+
readonly type: "schedule.resumed";
|
|
711
|
+
readonly payload: {};
|
|
712
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
713
|
+
readonly type: "schedule.retired";
|
|
714
|
+
readonly payload: {};
|
|
718
715
|
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
719
716
|
readonly type: "schedule.skipped";
|
|
720
717
|
readonly payload: {
|
|
@@ -725,6 +722,9 @@ export declare const fireEffect: NoInfer<import("@swirls/looms/core").LocalEffec
|
|
|
725
722
|
readonly manual: boolean;
|
|
726
723
|
readonly triggerId?: string | undefined;
|
|
727
724
|
};
|
|
725
|
+
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
726
|
+
readonly type: "schedule.triggered";
|
|
727
|
+
readonly payload: {};
|
|
728
728
|
}) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
|
|
729
729
|
readonly type: "schedule.updated";
|
|
730
730
|
readonly payload: {
|