@abloatai/ablo 0.29.3 → 0.30.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 +21 -0
- package/README.md +3 -2
- package/dist/BaseSyncedStore.js +3 -1
- package/dist/Database.js +29 -2
- package/dist/SyncClient.d.ts +5 -3
- package/dist/SyncClient.js +8 -4
- package/dist/ai-sdk/coordinatedTool.d.ts +0 -1
- package/dist/ai-sdk/coordinatedTool.js +0 -1
- package/dist/ai-sdk/coordinationContext.js +1 -5
- package/dist/cli.cjs +377 -84
- package/dist/client/Ablo.d.ts +1 -1
- package/dist/client/Ablo.js +24 -36
- package/dist/client/auth.d.ts +8 -18
- package/dist/client/auth.js +16 -96
- package/dist/client/createModelProxy.d.ts +16 -11
- package/dist/client/createModelProxy.js +162 -20
- package/dist/client/httpClient.d.ts +1 -1
- package/dist/client/httpTransport.js +263 -89
- package/dist/client/options.d.ts +0 -15
- package/dist/client/resourceTypes.d.ts +13 -26
- package/dist/client/schemaConfig.js +5 -0
- package/dist/client/writeOptionsSchema.d.ts +1 -0
- package/dist/client/writeOptionsSchema.js +11 -2
- package/dist/client/wsMutationExecutor.d.ts +3 -5
- package/dist/coordination/schema.d.ts +17 -13
- package/dist/coordination/schema.js +36 -10
- package/dist/errorCodes.d.ts +9 -4
- package/dist/errorCodes.js +10 -5
- package/dist/errors.js +4 -9
- package/dist/index.d.ts +4 -0
- package/dist/interfaces/index.d.ts +31 -19
- package/dist/schema/index.d.ts +1 -1
- package/dist/schema/index.js +4 -1
- package/dist/schema/schema.d.ts +13 -0
- package/dist/schema/select.js +7 -0
- package/dist/schema/syncDeltaRow.d.ts +31 -13
- package/dist/schema/syncDeltaRow.js +33 -15
- package/dist/schema/tenancy.d.ts +52 -16
- package/dist/schema/tenancy.js +86 -25
- package/dist/server/commit.d.ts +8 -24
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +1 -0
- package/dist/server/readConfig.d.ts +10 -0
- package/dist/source/adapter.d.ts +26 -15
- package/dist/source/adapter.js +5 -3
- package/dist/source/adapters/drizzle.js +35 -9
- package/dist/source/adapters/kysely.d.ts +33 -61
- package/dist/source/adapters/kysely.js +170 -177
- package/dist/source/adapters/kyselyMutationCore.d.ts +76 -0
- package/dist/source/adapters/kyselyMutationCore.js +125 -0
- package/dist/source/adapters/memory.d.ts +1 -1
- package/dist/source/adapters/memory.js +24 -10
- package/dist/source/adapters/prisma.js +35 -8
- package/dist/source/conformance.d.ts +26 -28
- package/dist/source/conformance.js +142 -69
- package/dist/source/contract.d.ts +46 -4
- package/dist/source/contract.js +66 -5
- package/dist/source/factory.d.ts +3 -3
- package/dist/source/factory.js +24 -3
- package/dist/source/idempotency.d.ts +46 -0
- package/dist/source/idempotency.js +129 -0
- package/dist/source/index.d.ts +6 -4
- package/dist/source/index.js +5 -3
- package/dist/source/migrations.d.ts +9 -5
- package/dist/source/migrations.js +39 -6
- package/dist/source/types.d.ts +88 -14
- package/dist/source/types.js +16 -0
- package/dist/surface.d.ts +1 -1
- package/dist/surface.js +0 -1
- package/dist/sync/BootstrapFetcher.js +7 -0
- package/dist/sync/SyncWebSocket.d.ts +5 -27
- package/dist/sync/awaitClaimGrant.d.ts +6 -0
- package/dist/sync/awaitClaimGrant.js +12 -2
- package/dist/sync/commitFrames.d.ts +2 -4
- package/dist/sync/commitFrames.js +1 -0
- package/dist/sync/createClaimStream.d.ts +1 -1
- package/dist/sync/createClaimStream.js +13 -15
- package/dist/sync/deltaPipeline.d.ts +1 -1
- package/dist/sync/deltaPipeline.js +1 -1
- package/dist/sync/participants.d.ts +2 -3
- package/dist/sync/participants.js +2 -2
- package/dist/sync/wsFrameHandlers.js +35 -15
- package/dist/testing/mocks/MockMutationExecutor.d.ts +21 -0
- package/dist/testing/mocks/MockMutationExecutor.js +53 -1
- package/dist/transactions/TransactionQueue.d.ts +61 -9
- package/dist/transactions/TransactionQueue.js +502 -114
- package/dist/transactions/commitEnvelope.d.ts +4 -0
- package/dist/transactions/commitEnvelope.js +25 -0
- package/dist/transactions/commitPayload.d.ts +23 -5
- package/dist/transactions/commitPayload.js +3 -0
- package/dist/transactions/deltaConfirmation.d.ts +2 -1
- package/dist/transactions/deltaConfirmation.js +22 -4
- package/dist/transactions/durableWriteStore.d.ts +8 -1
- package/dist/transactions/httpCommitEnvelope.d.ts +3 -1
- package/dist/transactions/httpCommitEnvelope.js +30 -0
- package/dist/transactions/replayValidation.d.ts +4 -0
- package/dist/transactions/replayValidation.js +6 -0
- package/dist/types/streams.d.ts +29 -17
- package/dist/wire/commit.d.ts +671 -0
- package/dist/wire/commit.js +300 -0
- package/dist/wire/delta.d.ts +4 -0
- package/dist/wire/delta.js +9 -0
- package/dist/wire/frames.d.ts +17 -45
- package/dist/wire/index.d.ts +2 -0
- package/dist/wire/index.js +4 -0
- package/docs/agent-messaging.md +4 -5
- package/docs/agents.md +1 -2
- package/docs/api-keys.md +1 -2
- package/docs/client-behavior.md +4 -6
- package/docs/coordination.md +64 -9
- package/docs/data-sources.md +4 -7
- package/docs/integration-guide.md +6 -8
- package/docs/migration.md +0 -3
- package/docs/quickstart.md +7 -8
- package/docs/schema-contract.md +7 -8
- package/package.json +4 -4
- package/dist/client/registerDataSource.d.ts +0 -19
- package/dist/client/registerDataSource.js +0 -66
|
@@ -1,212 +1,205 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* {@link DataSourceAdapter} contract as {@link prismaDataSource} and
|
|
4
|
-
* {@link drizzleDataSource} and passes the same conformance suite, built against
|
|
5
|
-
* Kysely's query builder:
|
|
6
|
-
* - `db.transaction().execute(async (trx) => …)` runs an interactive transaction.
|
|
7
|
-
* - `insertInto` / `updateTable` / `deleteFrom` / `selectFrom` with
|
|
8
|
-
* `returningAll()` form the fluent query. Table and column names are plain
|
|
9
|
-
* strings, so the adapter needs no raw-SQL tag and imports nothing from
|
|
10
|
-
* `kysely`; it depends only on the structural {@link KyselyLike} shape, the
|
|
11
|
-
* same approach the Prisma adapter takes with {@link PrismaLike}.
|
|
2
|
+
* Kysely wrappers for Ablo's two customer-database write transports.
|
|
12
3
|
*
|
|
13
|
-
*
|
|
14
|
-
* give it straight through to SQL, so, like the Drizzle adapter, this one derives
|
|
15
|
-
* every name from the same rule the table provisioner uses:
|
|
16
|
-
* table = `model.tableName ?? key`
|
|
17
|
-
* column = `fieldMeta.column ?? camelToSnake(field)` (plus the tenancy column)
|
|
18
|
-
* This keeps the tables `ablo migrate` creates (for example `operator_id`) and the
|
|
19
|
-
* columns this adapter uses in agreement. The adapter is the translation boundary:
|
|
20
|
-
* the rows it accepts and returns are keyed by field name, while the physical
|
|
21
|
-
* columns are snake_case.
|
|
4
|
+
* Both wrappers compose the same exported {@link createKyselyMutationCore}:
|
|
22
5
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
6
|
+
* - {@link kyselyDataSource} is the endpoint adapter. Its transaction contains
|
|
7
|
+
* row DML, the permanent idempotency ledger, and correlated outbox events. A
|
|
8
|
+
* Postgres logical marker remains optional for compatibility.
|
|
9
|
+
* - {@link kyselyDirectMutation} is the engine-side direct wrapper. Its
|
|
10
|
+
* transaction contains row DML, the same ledger, and a required logical
|
|
11
|
+
* marker. It never creates, writes, or serves `ablo_outbox`.
|
|
12
|
+
*
|
|
13
|
+
* The ledger reservation happens before DML via `INSERT ... ON CONFLICT DO
|
|
14
|
+
* NOTHING`. Postgres waits on a concurrent uncommitted owner of the same key;
|
|
15
|
+
* after that owner commits, the loser reads and hash-checks its durable response.
|
|
16
|
+
* If the owner rolls back, the waiter acquires the reservation and performs the
|
|
17
|
+
* mutation. This gives same-key concurrency one database arbiter and one effect.
|
|
27
18
|
*/
|
|
28
19
|
import { AbloValidationError } from '../../errors.js';
|
|
29
|
-
import { outboxEventSchema } from '../contract.js';
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
32
|
-
import {
|
|
33
|
-
import {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
20
|
+
import { changeSetSchema, outboxEventSchema, sourceCommitEchoMarkerSchema, } from '../contract.js';
|
|
21
|
+
import { assertSourceIdempotencyIntent, assertSourceIdempotencyRetention, sourceChangeIntentHash, } from '../idempotency.js';
|
|
22
|
+
import { adapterTableMigrations, idempotencyLedgerMigrations, } from '../migrations.js';
|
|
23
|
+
import { ABLO_POSTGRES_COMMIT_ECHO_PREFIX } from '../types.js';
|
|
24
|
+
import { createKyselyMutationCore, kyselyOperationRowId, } from './kyselyMutationCore.js';
|
|
25
|
+
export { createKyselyMutationCore, kyselyOperationRowId, } from './kyselyMutationCore.js';
|
|
26
|
+
function rawQuery(queryId, sql, parameters) {
|
|
27
|
+
return {
|
|
28
|
+
query: { kind: 'RawNode', sqlFragments: [sql], parameters: [] },
|
|
29
|
+
queryId: { queryId },
|
|
30
|
+
sql,
|
|
31
|
+
parameters,
|
|
32
|
+
};
|
|
42
33
|
}
|
|
43
|
-
function
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
register('organizationId', orgColumn);
|
|
62
|
-
out.set(key, { table: model.tableName ?? key, fieldToColumn, columnToField });
|
|
34
|
+
function reserveLedgerQuery(correlationId, requestHash) {
|
|
35
|
+
return rawQuery('ablo-idempotency-reserve', `INSERT INTO ablo_idempotency (client_tx_id, response, request_hash)
|
|
36
|
+
VALUES ($1, $2::jsonb, $3)
|
|
37
|
+
ON CONFLICT (client_tx_id) DO NOTHING
|
|
38
|
+
RETURNING client_tx_id`, [correlationId, '[]', requestHash]);
|
|
39
|
+
}
|
|
40
|
+
function completeLedgerQuery(correlationId, rows) {
|
|
41
|
+
return rawQuery('ablo-idempotency-complete', `UPDATE ablo_idempotency
|
|
42
|
+
SET response = $2::jsonb
|
|
43
|
+
WHERE client_tx_id = $1`, [correlationId, JSON.stringify(rows)]);
|
|
44
|
+
}
|
|
45
|
+
function postgresLogicalMarkerQuery(payload) {
|
|
46
|
+
return rawQuery('ablo-postgres-logical-marker', 'SELECT pg_logical_emit_message(true, $1::text, $2::text)', [ABLO_POSTGRES_COMMIT_ECHO_PREFIX, payload]);
|
|
47
|
+
}
|
|
48
|
+
function parseCachedRows(response) {
|
|
49
|
+
const parsed = typeof response === 'string' ? JSON.parse(response) : response;
|
|
50
|
+
if (!Array.isArray(parsed)) {
|
|
51
|
+
throw new AbloValidationError('The source idempotency response is corrupt and cannot be replayed safely', { code: 'idempotency_conflict' });
|
|
63
52
|
}
|
|
64
|
-
return
|
|
53
|
+
return parsed;
|
|
65
54
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
55
|
+
function markerAction(type) {
|
|
56
|
+
if (type === 'CREATE')
|
|
57
|
+
return 'I';
|
|
58
|
+
if (type === 'DELETE')
|
|
59
|
+
return 'D';
|
|
60
|
+
return 'U';
|
|
61
|
+
}
|
|
62
|
+
function assertDirectLogicalMarker(change, markerModelFor) {
|
|
63
|
+
const payload = change.echo?.payload;
|
|
64
|
+
if (!payload) {
|
|
65
|
+
throw new AbloValidationError('A direct Kysely mutation requires a transactional Postgres logical marker', { code: 'source_adapter_misconfigured' });
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
const marker = sourceCommitEchoMarkerSchema.parse(JSON.parse(payload));
|
|
69
|
+
if (marker.correlationId !== change.correlationId) {
|
|
70
|
+
throw new Error('marker correlation does not match the ledger key');
|
|
74
71
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const columnFor = (mc, field) => mc.fieldToColumn.get(field) ?? camelToSnake(field);
|
|
78
|
-
const fieldFor = (mc, column) => mc.columnToField.get(column) ?? snakeToCamel(column);
|
|
79
|
-
/** Field-keyed row to column-keyed row, for INSERT and UPDATE values. */
|
|
80
|
-
const toColumns = (mc, row) => {
|
|
81
|
-
const out = {};
|
|
82
|
-
for (const k of Object.keys(row))
|
|
83
|
-
out[columnFor(mc, k)] = row[k];
|
|
84
|
-
return out;
|
|
85
|
-
};
|
|
86
|
-
/** Column-keyed row (from `RETURNING *` or `SELECT *`) back to a field-keyed row. */
|
|
87
|
-
const toFields = (mc, row) => {
|
|
88
|
-
const out = {};
|
|
89
|
-
for (const k of Object.keys(row))
|
|
90
|
-
out[fieldFor(mc, k)] = row[k];
|
|
91
|
-
return out;
|
|
92
|
-
};
|
|
93
|
-
const applyOperation = async (trx, op) => {
|
|
94
|
-
const mc = modelColumns(op.model);
|
|
95
|
-
const id = rowId(op);
|
|
96
|
-
const input = op.input ?? {};
|
|
97
|
-
if (op.type === 'DELETE') {
|
|
98
|
-
const deleted = await trx
|
|
99
|
-
.deleteFrom(mc.table)
|
|
100
|
-
.where('id', '=', id)
|
|
101
|
-
.returningAll()
|
|
102
|
-
.execute();
|
|
103
|
-
return deleted[0] ? toFields(mc, deleted[0]) : { id };
|
|
72
|
+
if (marker.operations.length !== change.operations.length) {
|
|
73
|
+
throw new Error('marker operation count does not match the mutation');
|
|
104
74
|
}
|
|
105
|
-
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
75
|
+
for (const [index, operation] of change.operations.entries()) {
|
|
76
|
+
const markerOperation = marker.operations[index];
|
|
77
|
+
// The marker speaks the canonical schema TYPENAME (the vocabulary the
|
|
78
|
+
// WAL consumer validates deltas against), while the mutation operation
|
|
79
|
+
// carries the authoring wire key. `markerModelFor` translates the wire
|
|
80
|
+
// key through the schema so the two vocabularies compare correctly.
|
|
81
|
+
if (!markerOperation ||
|
|
82
|
+
markerOperation.model !== markerModelFor(operation.model) ||
|
|
83
|
+
markerOperation.id !== kyselyOperationRowId(operation) ||
|
|
84
|
+
markerOperation.action !== markerAction(operation.type) ||
|
|
85
|
+
markerOperation.transactionId !== operation.transactionId) {
|
|
86
|
+
throw new Error(`marker operation ${index} does not match the mutation`);
|
|
87
|
+
}
|
|
112
88
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
.updateTable(mc.table)
|
|
123
|
-
.set(patch)
|
|
124
|
-
.where('id', '=', id)
|
|
125
|
-
.returningAll()
|
|
126
|
-
.execute();
|
|
127
|
-
return updated[0] ? toFields(mc, updated[0]) : { id, ...input };
|
|
128
|
-
};
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
if (error instanceof AbloValidationError)
|
|
92
|
+
throw error;
|
|
93
|
+
throw new AbloValidationError(`The direct Postgres logical marker is invalid: ${error instanceof Error ? error.message : 'unknown marker error'}`, { code: 'source_adapter_misconfigured' });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export function createKyselyMutationAdapter(db, core, mode, options = {}) {
|
|
97
|
+
const markerModelFor = options.markerModelFor ?? ((model) => model);
|
|
129
98
|
return {
|
|
130
|
-
capabilities: {
|
|
99
|
+
capabilities: {
|
|
100
|
+
transactions: true,
|
|
101
|
+
propose: false,
|
|
102
|
+
schemaIntrospection: true,
|
|
103
|
+
postgresWalEcho: true,
|
|
104
|
+
outboxEvents: mode === 'endpoint',
|
|
105
|
+
},
|
|
131
106
|
migrations() {
|
|
132
|
-
return
|
|
107
|
+
return mode === 'endpoint'
|
|
108
|
+
? adapterTableMigrations()
|
|
109
|
+
: idempotencyLedgerMigrations();
|
|
133
110
|
},
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (req.kind === 'load') {
|
|
137
|
-
const rows = await db
|
|
138
|
-
.selectFrom(mc.table)
|
|
139
|
-
.selectAll()
|
|
140
|
-
.where('id', '=', req.id)
|
|
141
|
-
.limit(1)
|
|
142
|
-
.execute();
|
|
143
|
-
return rows.map((r) => toFields(mc, r));
|
|
144
|
-
}
|
|
145
|
-
const limit = req.query?.limit ?? 1000;
|
|
146
|
-
const rows = await db.selectFrom(mc.table).selectAll().limit(limit).execute();
|
|
147
|
-
return rows.map((r) => toFields(mc, r));
|
|
111
|
+
read(request) {
|
|
112
|
+
return core.read(request);
|
|
148
113
|
},
|
|
149
114
|
async commit(change) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
115
|
+
const request = changeSetSchema.parse(change);
|
|
116
|
+
if (mode === 'direct')
|
|
117
|
+
assertDirectLogicalMarker(request, markerModelFor);
|
|
118
|
+
const requestHash = sourceChangeIntentHash(request);
|
|
119
|
+
return db.transaction().execute(async (transaction) => {
|
|
120
|
+
const reservation = await transaction.executeQuery(reserveLedgerQuery(request.correlationId, requestHash));
|
|
121
|
+
if (reservation.rows.length === 0) {
|
|
122
|
+
const cached = await transaction
|
|
123
|
+
.selectFrom('ablo_idempotency')
|
|
124
|
+
.selectAll()
|
|
125
|
+
.where('client_tx_id', '=', request.correlationId)
|
|
126
|
+
.limit(1)
|
|
127
|
+
.execute();
|
|
128
|
+
const cachedRow = cached[0];
|
|
129
|
+
if (!cachedRow) {
|
|
130
|
+
throw new AbloValidationError('The source idempotency reservation disappeared during replay', { code: 'idempotency_conflict' });
|
|
131
|
+
}
|
|
132
|
+
assertSourceIdempotencyIntent(cachedRow.request_hash, requestHash);
|
|
133
|
+
assertSourceIdempotencyRetention(cachedRow.expires_at);
|
|
134
|
+
return { rows: parseCachedRows(cachedRow.response) };
|
|
163
135
|
}
|
|
164
136
|
const rows = [];
|
|
165
|
-
for (const [index,
|
|
166
|
-
const row = await applyOperation(
|
|
137
|
+
for (const [index, operation] of request.operations.entries()) {
|
|
138
|
+
const row = await core.applyOperation(transaction, operation);
|
|
167
139
|
rows.push(row);
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
140
|
+
if (mode === 'endpoint') {
|
|
141
|
+
const entityId = String(row.id ?? kyselyOperationRowId(operation));
|
|
142
|
+
await transaction
|
|
143
|
+
.insertInto('ablo_outbox')
|
|
144
|
+
.values({
|
|
145
|
+
id: `${request.correlationId}:${index}`,
|
|
146
|
+
model: operation.model,
|
|
147
|
+
entity_id: entityId,
|
|
148
|
+
type: operation.type,
|
|
149
|
+
data: operation.type === 'DELETE' ? null : JSON.stringify(row),
|
|
150
|
+
correlation_id: request.correlationId,
|
|
151
|
+
transaction_id: operation.transactionId ?? null,
|
|
152
|
+
occurred_at: Date.now(),
|
|
153
|
+
})
|
|
154
|
+
.execute();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
await transaction.executeQuery(completeLedgerQuery(request.correlationId, rows));
|
|
158
|
+
if (request.echo?.kind === 'postgres-wal') {
|
|
159
|
+
await transaction.executeQuery(postgresLogicalMarkerQuery(request.echo.payload));
|
|
181
160
|
}
|
|
182
|
-
await trx
|
|
183
|
-
.insertInto('ablo_idempotency')
|
|
184
|
-
.values({ client_tx_id: change.clientTxId, response: JSON.stringify(rows) })
|
|
185
|
-
.execute();
|
|
186
161
|
return { rows };
|
|
187
162
|
});
|
|
188
163
|
},
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/** Endpoint wrapper: mutation + ledger + outbox, with an optional marker. */
|
|
167
|
+
export function kyselyDataSource(db, schema) {
|
|
168
|
+
const mutation = createKyselyMutationAdapter(db, createKyselyMutationCore(db, schema), 'endpoint');
|
|
169
|
+
return {
|
|
170
|
+
...mutation,
|
|
171
|
+
capabilities: { ...mutation.capabilities, outboxEvents: true },
|
|
189
172
|
async events(cursor, limit) {
|
|
190
|
-
const after = cursor ?? '0';
|
|
191
173
|
const rows = await db
|
|
192
174
|
.selectFrom('ablo_outbox')
|
|
193
175
|
.selectAll()
|
|
194
|
-
.where('cursor', '>',
|
|
176
|
+
.where('cursor', '>', cursor ?? '0')
|
|
195
177
|
.orderBy('cursor', 'asc')
|
|
196
178
|
.limit(limit)
|
|
197
179
|
.execute();
|
|
198
|
-
const events = rows.map((
|
|
199
|
-
id:
|
|
200
|
-
model:
|
|
201
|
-
entityId:
|
|
202
|
-
type:
|
|
203
|
-
data: typeof
|
|
204
|
-
organizationId:
|
|
205
|
-
clientTxId:
|
|
206
|
-
|
|
207
|
-
|
|
180
|
+
const events = rows.map((row) => outboxEventSchema.parse({
|
|
181
|
+
id: row.id,
|
|
182
|
+
model: row.model,
|
|
183
|
+
entityId: row.entity_id,
|
|
184
|
+
type: row.type,
|
|
185
|
+
data: typeof row.data === 'string' ? JSON.parse(row.data) : row.data ?? null,
|
|
186
|
+
organizationId: row.organization_id ?? null,
|
|
187
|
+
clientTxId: row.client_tx_id ?? null,
|
|
188
|
+
correlationId: row.correlation_id ?? null,
|
|
189
|
+
transactionId: row.transaction_id ?? null,
|
|
190
|
+
occurredAt: row.occurred_at != null ? Number(row.occurred_at) : null,
|
|
191
|
+
cursor: String(row.cursor),
|
|
208
192
|
}));
|
|
209
193
|
return { events, nextCursor: events.at(-1)?.cursor ?? null };
|
|
210
194
|
},
|
|
211
195
|
};
|
|
212
196
|
}
|
|
197
|
+
/** Direct wrapper: mutation + ledger + required logical marker, never outbox. */
|
|
198
|
+
export function kyselyDirectMutation(db, schema) {
|
|
199
|
+
return createKyselyMutationAdapter(db, createKyselyMutationCore(db, schema), 'direct', {
|
|
200
|
+
markerModelFor: (operationModel) => {
|
|
201
|
+
const typename = schema.models[operationModel]?.typename;
|
|
202
|
+
return typename && typename.length > 0 ? typename : operationModel;
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reusable Kysely row-mutation core.
|
|
3
|
+
*
|
|
4
|
+
* This is the single schema-field ↔ physical-column translation used by both
|
|
5
|
+
* Kysely transports. It deliberately knows nothing about outboxes, logical
|
|
6
|
+
* markers, or idempotency: wrappers compose those transaction policies around
|
|
7
|
+
* the same `applyOperation` implementation, so direct and endpoint DML cannot
|
|
8
|
+
* drift into separate hand-written mappers.
|
|
9
|
+
*/
|
|
10
|
+
import type { Schema, SchemaRecord } from '../../schema/schema.js';
|
|
11
|
+
import type { AdapterReadRequest, Row } from '../adapter.js';
|
|
12
|
+
import type { Operation } from '../contract.js';
|
|
13
|
+
/** The subset of a Kysely instance, or transaction handle, used by the core. */
|
|
14
|
+
export interface KyselyLike {
|
|
15
|
+
selectFrom(table: string): KyselySelectBuilder;
|
|
16
|
+
insertInto(table: string): KyselyInsertBuilder;
|
|
17
|
+
updateTable(table: string): KyselyUpdateBuilder;
|
|
18
|
+
deleteFrom(table: string): KyselyDeleteBuilder;
|
|
19
|
+
/** Execute a precompiled raw query on this exact transaction handle. */
|
|
20
|
+
executeQuery(query: KyselyCompiledQuery): Promise<{
|
|
21
|
+
readonly rows: readonly Row[];
|
|
22
|
+
}>;
|
|
23
|
+
transaction(): KyselyTransactionBuilder;
|
|
24
|
+
}
|
|
25
|
+
/** Structural slice of Kysely's public `CompiledQuery` shape. */
|
|
26
|
+
export interface KyselyCompiledQuery {
|
|
27
|
+
readonly query: {
|
|
28
|
+
readonly kind: 'RawNode';
|
|
29
|
+
readonly sqlFragments: readonly string[];
|
|
30
|
+
readonly parameters: readonly [];
|
|
31
|
+
};
|
|
32
|
+
readonly queryId: {
|
|
33
|
+
readonly queryId: string;
|
|
34
|
+
};
|
|
35
|
+
readonly sql: string;
|
|
36
|
+
readonly parameters: readonly unknown[];
|
|
37
|
+
}
|
|
38
|
+
export interface KyselyTransactionBuilder {
|
|
39
|
+
execute<T>(fn: (trx: KyselyLike) => Promise<T>): Promise<T>;
|
|
40
|
+
}
|
|
41
|
+
export interface KyselySelectBuilder {
|
|
42
|
+
selectAll(): KyselySelectBuilder;
|
|
43
|
+
where(column: string, operator: string, value: unknown): KyselySelectBuilder;
|
|
44
|
+
orderBy(column: string, direction: 'asc' | 'desc'): KyselySelectBuilder;
|
|
45
|
+
limit(limit: number): KyselySelectBuilder;
|
|
46
|
+
execute(): Promise<readonly Row[]>;
|
|
47
|
+
}
|
|
48
|
+
export interface KyselyReturningExecutable {
|
|
49
|
+
execute(): Promise<readonly Row[]>;
|
|
50
|
+
}
|
|
51
|
+
export interface KyselyInsertBuilder {
|
|
52
|
+
values(row: Row): KyselyInsertValuesBuilder;
|
|
53
|
+
}
|
|
54
|
+
export interface KyselyInsertValuesBuilder {
|
|
55
|
+
returningAll(): KyselyReturningExecutable;
|
|
56
|
+
execute(): Promise<unknown>;
|
|
57
|
+
}
|
|
58
|
+
export interface KyselyUpdateBuilder {
|
|
59
|
+
set(patch: Row): KyselyUpdateSetBuilder;
|
|
60
|
+
}
|
|
61
|
+
export interface KyselyUpdateSetBuilder {
|
|
62
|
+
where(column: string, operator: string, value: unknown): KyselyUpdateSetBuilder;
|
|
63
|
+
returningAll(): KyselyReturningExecutable;
|
|
64
|
+
}
|
|
65
|
+
export interface KyselyDeleteBuilder {
|
|
66
|
+
where(column: string, operator: string, value: unknown): KyselyDeleteBuilder;
|
|
67
|
+
returningAll(): KyselyReturningExecutable;
|
|
68
|
+
}
|
|
69
|
+
/** Resolve the stable row id required by every source operation. */
|
|
70
|
+
export declare function kyselyOperationRowId(operation: Operation): string;
|
|
71
|
+
/** The transport-independent Kysely field/column mutation boundary. */
|
|
72
|
+
export interface KyselyMutationCore {
|
|
73
|
+
read(request: AdapterReadRequest): Promise<readonly Row[]>;
|
|
74
|
+
applyOperation(transaction: KyselyLike, operation: Operation): Promise<Row>;
|
|
75
|
+
}
|
|
76
|
+
export declare function createKyselyMutationCore<S extends SchemaRecord>(db: KyselyLike, schema: Schema<S>): KyselyMutationCore;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reusable Kysely row-mutation core.
|
|
3
|
+
*
|
|
4
|
+
* This is the single schema-field ↔ physical-column translation used by both
|
|
5
|
+
* Kysely transports. It deliberately knows nothing about outboxes, logical
|
|
6
|
+
* markers, or idempotency: wrappers compose those transaction policies around
|
|
7
|
+
* the same `applyOperation` implementation, so direct and endpoint DML cannot
|
|
8
|
+
* drift into separate hand-written mappers.
|
|
9
|
+
*/
|
|
10
|
+
import { AbloValidationError } from '../../errors.js';
|
|
11
|
+
import { toSchemaJSON } from '../../schema/serialize.js';
|
|
12
|
+
import { camelToSnake, snakeToCamel } from '../../schema/ddl.js';
|
|
13
|
+
import { tenancyColumn } from '../../schema/tenancy.js';
|
|
14
|
+
function buildColumnMaps(schema) {
|
|
15
|
+
const json = toSchemaJSON(schema);
|
|
16
|
+
const out = new Map();
|
|
17
|
+
for (const [key, model] of Object.entries(json.models)) {
|
|
18
|
+
const fieldToColumn = new Map();
|
|
19
|
+
const columnToField = new Map();
|
|
20
|
+
const register = (field, column) => {
|
|
21
|
+
if (column === camelToSnake(field))
|
|
22
|
+
return;
|
|
23
|
+
fieldToColumn.set(field, column);
|
|
24
|
+
columnToField.set(column, field);
|
|
25
|
+
};
|
|
26
|
+
for (const [field, meta] of Object.entries(model.fields)) {
|
|
27
|
+
if (meta.column)
|
|
28
|
+
register(field, meta.column);
|
|
29
|
+
}
|
|
30
|
+
const orgColumn = tenancyColumn(model.tenancy);
|
|
31
|
+
if (orgColumn)
|
|
32
|
+
register('organizationId', orgColumn);
|
|
33
|
+
out.set(key, {
|
|
34
|
+
table: model.tableName ?? key,
|
|
35
|
+
fieldToColumn,
|
|
36
|
+
columnToField,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
/** Resolve the stable row id required by every source operation. */
|
|
42
|
+
export function kyselyOperationRowId(operation) {
|
|
43
|
+
const id = operation.id ?? operation.input?.id;
|
|
44
|
+
if (typeof id !== 'string' || id.length === 0) {
|
|
45
|
+
throw new AbloValidationError(`operation on "${operation.model}" requires an id`, { code: 'source_operation_id_required' });
|
|
46
|
+
}
|
|
47
|
+
return id;
|
|
48
|
+
}
|
|
49
|
+
export function createKyselyMutationCore(db, schema) {
|
|
50
|
+
const maps = buildColumnMaps(schema);
|
|
51
|
+
const modelColumns = (model) => {
|
|
52
|
+
const columns = maps.get(model);
|
|
53
|
+
if (!columns) {
|
|
54
|
+
throw new AbloValidationError(`kysely mutation core: no model "${model}" in schema`, { code: 'source_adapter_misconfigured' });
|
|
55
|
+
}
|
|
56
|
+
return columns;
|
|
57
|
+
};
|
|
58
|
+
const columnFor = (columns, field) => columns.fieldToColumn.get(field) ?? camelToSnake(field);
|
|
59
|
+
const fieldFor = (columns, column) => columns.columnToField.get(column) ?? snakeToCamel(column);
|
|
60
|
+
const toColumns = (columns, row) => {
|
|
61
|
+
const out = {};
|
|
62
|
+
for (const key of Object.keys(row))
|
|
63
|
+
out[columnFor(columns, key)] = row[key];
|
|
64
|
+
return out;
|
|
65
|
+
};
|
|
66
|
+
const toFields = (columns, row) => {
|
|
67
|
+
const out = {};
|
|
68
|
+
for (const key of Object.keys(row))
|
|
69
|
+
out[fieldFor(columns, key)] = row[key];
|
|
70
|
+
return out;
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
async read(request) {
|
|
74
|
+
const columns = modelColumns(request.model);
|
|
75
|
+
if (request.kind === 'load') {
|
|
76
|
+
const rows = await db
|
|
77
|
+
.selectFrom(columns.table)
|
|
78
|
+
.selectAll()
|
|
79
|
+
.where('id', '=', request.id)
|
|
80
|
+
.limit(1)
|
|
81
|
+
.execute();
|
|
82
|
+
return rows.map((row) => toFields(columns, row));
|
|
83
|
+
}
|
|
84
|
+
const rows = await db
|
|
85
|
+
.selectFrom(columns.table)
|
|
86
|
+
.selectAll()
|
|
87
|
+
.limit(request.query?.limit ?? 1000)
|
|
88
|
+
.execute();
|
|
89
|
+
return rows.map((row) => toFields(columns, row));
|
|
90
|
+
},
|
|
91
|
+
async applyOperation(transaction, operation) {
|
|
92
|
+
const columns = modelColumns(operation.model);
|
|
93
|
+
const id = kyselyOperationRowId(operation);
|
|
94
|
+
const input = operation.input ?? {};
|
|
95
|
+
if (operation.type === 'DELETE') {
|
|
96
|
+
const deleted = await transaction
|
|
97
|
+
.deleteFrom(columns.table)
|
|
98
|
+
.where('id', '=', id)
|
|
99
|
+
.returningAll()
|
|
100
|
+
.execute();
|
|
101
|
+
return deleted[0] ? toFields(columns, deleted[0]) : { id };
|
|
102
|
+
}
|
|
103
|
+
if (operation.type === 'CREATE') {
|
|
104
|
+
const inserted = await transaction
|
|
105
|
+
.insertInto(columns.table)
|
|
106
|
+
.values(toColumns(columns, { id, ...input }))
|
|
107
|
+
.returningAll()
|
|
108
|
+
.execute();
|
|
109
|
+
return inserted[0] ? toFields(columns, inserted[0]) : { id, ...input };
|
|
110
|
+
}
|
|
111
|
+
const patch = toColumns(columns, {
|
|
112
|
+
...input,
|
|
113
|
+
...(operation.type === 'ARCHIVE' ? { archivedAt: new Date() } : {}),
|
|
114
|
+
...(operation.type === 'UNARCHIVE' ? { archivedAt: null } : {}),
|
|
115
|
+
});
|
|
116
|
+
const updated = await transaction
|
|
117
|
+
.updateTable(columns.table)
|
|
118
|
+
.set(patch)
|
|
119
|
+
.where('id', '=', id)
|
|
120
|
+
.returningAll()
|
|
121
|
+
.execute();
|
|
122
|
+
return updated[0] ? toFields(columns, updated[0]) : { id, ...input };
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* mapper is complete when it passes the same suite this one passes.
|
|
7
7
|
*
|
|
8
8
|
* It models the semantics minimally but faithfully: one row store per model, an
|
|
9
|
-
* idempotency ledger keyed by
|
|
9
|
+
* idempotency ledger keyed by scoped correlation, and an append-only outbox with a
|
|
10
10
|
* monotonic cursor.
|
|
11
11
|
*/
|
|
12
12
|
import type { DataSourceAdapter } from '../adapter.js';
|