@abloatai/ablo 0.9.2 → 0.9.3
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/AGENTS.md +1 -1
- package/CHANGELOG.md +6 -0
- package/README.md +40 -22
- package/dist/BaseSyncedStore.d.ts +2 -36
- package/dist/BaseSyncedStore.js +5 -53
- package/dist/NetworkMonitor.js +4 -1
- package/dist/SyncClient.d.ts +10 -5
- package/dist/SyncClient.js +63 -1
- package/dist/SyncEngineContext.js +5 -1
- package/dist/auth/index.js +3 -1
- package/dist/cli.cjs +302645 -0
- package/dist/client/Ablo.d.ts +12 -3
- package/dist/client/Ablo.js +28 -2
- package/dist/client/ApiClient.js +39 -6
- package/dist/client/createInternalComponents.js +1 -1
- package/dist/client/createModelProxy.d.ts +9 -0
- package/dist/client/createModelProxy.js +34 -10
- package/dist/client/persistence.d.ts +6 -1
- package/dist/client/persistence.js +1 -1
- package/dist/client/registerDataSource.d.ts +4 -4
- package/dist/client/registerDataSource.js +39 -31
- package/dist/client/writeOptionsSchema.d.ts +50 -0
- package/dist/client/writeOptionsSchema.js +57 -0
- package/dist/core/index.d.ts +18 -26
- package/dist/core/index.js +22 -46
- package/dist/errorCodes.d.ts +13 -0
- package/dist/errorCodes.js +16 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +7 -0
- package/dist/interfaces/index.d.ts +10 -0
- package/dist/mutators/UndoManager.d.ts +31 -5
- package/dist/mutators/UndoManager.js +113 -1
- package/dist/schema/ddl.js +2 -1
- package/dist/schema/field.js +2 -1
- package/dist/schema/serialize.js +2 -1
- package/dist/server/storage-mode.d.ts +7 -0
- package/dist/server/storage-mode.js +6 -0
- package/dist/source/adapters/drizzle.js +3 -2
- package/dist/source/adapters/kysely.d.ts +68 -0
- package/dist/source/adapters/kysely.js +210 -0
- package/dist/source/adapters/memory.js +2 -1
- package/dist/source/adapters/prisma.js +3 -2
- package/dist/source/index.js +2 -1
- package/dist/transactions/TransactionQueue.d.ts +6 -7
- package/dist/transactions/TransactionQueue.js +33 -9
- package/dist/utils/duration.js +3 -2
- package/docs/client-behavior.md +1 -1
- package/docs/data-sources.md +61 -42
- package/docs/guarantees.md +2 -2
- package/docs/index.md +2 -2
- package/docs/integration-guide.md +4 -7
- package/docs/mcp.md +1 -1
- package/docs/quickstart.md +84 -37
- package/docs/schema-contract.md +2 -4
- package/llms-full.txt +360 -0
- package/llms.txt +14 -9
- package/package.json +22 -2
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Kysely Data Source adapter. Same adapter interface + conformance shape as
|
|
3
|
+
* `prismaDataSource` / `drizzleDataSource`, built against Kysely's REAL
|
|
4
|
+
* query-builder API:
|
|
5
|
+
* - `db.transaction().execute(async (trx) => …)` — interactive transaction.
|
|
6
|
+
* - `insertInto/updateTable/deleteFrom/selectFrom` + `returningAll()` —
|
|
7
|
+
* the fluent builder; table/column names are plain strings, so no raw
|
|
8
|
+
* SQL tag is needed and this module imports NOTHING from `kysely`
|
|
9
|
+
* (structural `KyselyLike`, mirroring the Prisma adapter's zero-dep
|
|
10
|
+
* `PrismaLike`).
|
|
11
|
+
*
|
|
12
|
+
* SCHEMA-DRIVEN COLUMNS. Kysely is SQL-near: it passes the column names you
|
|
13
|
+
* give it through verbatim (no Prisma-style `@map`). Like the Drizzle
|
|
14
|
+
* adapter, every table + column name is derived from the SAME rule the
|
|
15
|
+
* provisioner uses (`generateProvisionPlan`):
|
|
16
|
+
* table = `model.tableName ?? key`
|
|
17
|
+
* column = `fieldMeta.column ?? camelToSnake(field)` (+ the tenancy column)
|
|
18
|
+
* so `ablo migrate` (which emits `operator_id`) and this adapter COMPOSE.
|
|
19
|
+
* The adapter is the translation boundary: rows in/out are field-keyed (the
|
|
20
|
+
* SDK shape); the physical columns it reads/writes are snake_case.
|
|
21
|
+
*
|
|
22
|
+
* JSONB note: the outbox `data` / idempotency `response` values are passed
|
|
23
|
+
* as JSON strings — Postgres infers the parameter type from the target
|
|
24
|
+
* `jsonb` column, so the coercion is server-side and driver-agnostic (no
|
|
25
|
+
* `::jsonb` cast available without raw SQL).
|
|
26
|
+
*/
|
|
27
|
+
import { AbloValidationError } from '../../errors.js';
|
|
28
|
+
import { outboxEventSchema } from '../contract.js';
|
|
29
|
+
import { adapterTableMigrations } from '../migrations.js';
|
|
30
|
+
import { toSchemaJSON } from '../../schema/serialize.js';
|
|
31
|
+
import { camelToSnake, snakeToCamel } from '../../schema/ddl.js';
|
|
32
|
+
import { tenancyColumn } from '../../schema/tenancy.js';
|
|
33
|
+
function rowId(op) {
|
|
34
|
+
const id = op.id ?? op.input?.id;
|
|
35
|
+
if (typeof id !== 'string' || id.length === 0) {
|
|
36
|
+
throw new AbloValidationError(`operation on "${op.model}" requires an id`, {
|
|
37
|
+
code: 'source_operation_id_required',
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return id;
|
|
41
|
+
}
|
|
42
|
+
function buildColumnMaps(schema) {
|
|
43
|
+
const json = toSchemaJSON(schema);
|
|
44
|
+
const out = new Map();
|
|
45
|
+
for (const [key, model] of Object.entries(json.models)) {
|
|
46
|
+
const fieldToColumn = new Map();
|
|
47
|
+
const columnToField = new Map();
|
|
48
|
+
const register = (field, column) => {
|
|
49
|
+
if (column === camelToSnake(field))
|
|
50
|
+
return;
|
|
51
|
+
fieldToColumn.set(field, column);
|
|
52
|
+
columnToField.set(column, field);
|
|
53
|
+
};
|
|
54
|
+
for (const [field, meta] of Object.entries(model.fields)) {
|
|
55
|
+
if (meta.column)
|
|
56
|
+
register(field, meta.column);
|
|
57
|
+
}
|
|
58
|
+
const orgColumn = tenancyColumn(model.tenancy);
|
|
59
|
+
if (orgColumn)
|
|
60
|
+
register('organizationId', orgColumn);
|
|
61
|
+
out.set(key, { table: model.tableName ?? key, fieldToColumn, columnToField });
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
export function kyselyDataSource(db, schema) {
|
|
66
|
+
const maps = buildColumnMaps(schema);
|
|
67
|
+
const modelColumns = (model) => {
|
|
68
|
+
const mc = maps.get(model);
|
|
69
|
+
if (!mc) {
|
|
70
|
+
throw new AbloValidationError(`kyselyDataSource: no model "${model}" in schema`, {
|
|
71
|
+
code: 'source_adapter_misconfigured',
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return mc;
|
|
75
|
+
};
|
|
76
|
+
const columnFor = (mc, field) => mc.fieldToColumn.get(field) ?? camelToSnake(field);
|
|
77
|
+
const fieldFor = (mc, column) => mc.columnToField.get(column) ?? snakeToCamel(column);
|
|
78
|
+
/** Field-keyed (SDK shape) → column-keyed (physical), for INSERT/UPDATE. */
|
|
79
|
+
const toColumns = (mc, row) => {
|
|
80
|
+
const out = {};
|
|
81
|
+
for (const k of Object.keys(row))
|
|
82
|
+
out[columnFor(mc, k)] = row[k];
|
|
83
|
+
return out;
|
|
84
|
+
};
|
|
85
|
+
/** Column-keyed (RETURNING * / SELECT *) → field-keyed (SDK shape). */
|
|
86
|
+
const toFields = (mc, row) => {
|
|
87
|
+
const out = {};
|
|
88
|
+
for (const k of Object.keys(row))
|
|
89
|
+
out[fieldFor(mc, k)] = row[k];
|
|
90
|
+
return out;
|
|
91
|
+
};
|
|
92
|
+
const applyOperation = async (trx, op) => {
|
|
93
|
+
const mc = modelColumns(op.model);
|
|
94
|
+
const id = rowId(op);
|
|
95
|
+
const input = op.input ?? {};
|
|
96
|
+
if (op.type === 'DELETE') {
|
|
97
|
+
const deleted = await trx
|
|
98
|
+
.deleteFrom(mc.table)
|
|
99
|
+
.where('id', '=', id)
|
|
100
|
+
.returningAll()
|
|
101
|
+
.execute();
|
|
102
|
+
return deleted[0] ? toFields(mc, deleted[0]) : { id };
|
|
103
|
+
}
|
|
104
|
+
if (op.type === 'CREATE') {
|
|
105
|
+
const inserted = await trx
|
|
106
|
+
.insertInto(mc.table)
|
|
107
|
+
.values(toColumns(mc, { id, ...input }))
|
|
108
|
+
.returningAll()
|
|
109
|
+
.execute();
|
|
110
|
+
return inserted[0] ? toFields(mc, inserted[0]) : { id, ...input };
|
|
111
|
+
}
|
|
112
|
+
// UPDATE / ARCHIVE / UNARCHIVE — the lifecycle field is `archivedAt`
|
|
113
|
+
// (camelCase) and goes through `toColumns` like any other, so it lands in
|
|
114
|
+
// `archived_at` — the same column the provisioner emits.
|
|
115
|
+
const patch = toColumns(mc, {
|
|
116
|
+
...input,
|
|
117
|
+
...(op.type === 'ARCHIVE' ? { archivedAt: new Date() } : {}),
|
|
118
|
+
...(op.type === 'UNARCHIVE' ? { archivedAt: null } : {}),
|
|
119
|
+
});
|
|
120
|
+
const updated = await trx
|
|
121
|
+
.updateTable(mc.table)
|
|
122
|
+
.set(patch)
|
|
123
|
+
.where('id', '=', id)
|
|
124
|
+
.returningAll()
|
|
125
|
+
.execute();
|
|
126
|
+
return updated[0] ? toFields(mc, updated[0]) : { id, ...input };
|
|
127
|
+
};
|
|
128
|
+
return {
|
|
129
|
+
capabilities: { transactions: true, propose: false, schemaIntrospection: true },
|
|
130
|
+
migrations() {
|
|
131
|
+
return adapterTableMigrations();
|
|
132
|
+
},
|
|
133
|
+
async read(req) {
|
|
134
|
+
const mc = modelColumns(req.model);
|
|
135
|
+
if (req.kind === 'load') {
|
|
136
|
+
const rows = await db
|
|
137
|
+
.selectFrom(mc.table)
|
|
138
|
+
.selectAll()
|
|
139
|
+
.where('id', '=', req.id)
|
|
140
|
+
.limit(1)
|
|
141
|
+
.execute();
|
|
142
|
+
return rows.map((r) => toFields(mc, r));
|
|
143
|
+
}
|
|
144
|
+
const limit = req.query?.limit ?? 1000;
|
|
145
|
+
const rows = await db.selectFrom(mc.table).selectAll().limit(limit).execute();
|
|
146
|
+
return rows.map((r) => toFields(mc, r));
|
|
147
|
+
},
|
|
148
|
+
async commit(change) {
|
|
149
|
+
return db.transaction().execute(async (trx) => {
|
|
150
|
+
const cached = await trx
|
|
151
|
+
.selectFrom('ablo_idempotency')
|
|
152
|
+
.selectAll()
|
|
153
|
+
.where('client_tx_id', '=', change.clientTxId)
|
|
154
|
+
.limit(1)
|
|
155
|
+
.execute();
|
|
156
|
+
if (cached.length > 0) {
|
|
157
|
+
const response = cached[0].response;
|
|
158
|
+
return {
|
|
159
|
+
rows: (typeof response === 'string' ? JSON.parse(response) : response),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
const rows = [];
|
|
163
|
+
for (const [index, op] of change.operations.entries()) {
|
|
164
|
+
const row = await applyOperation(trx, op);
|
|
165
|
+
rows.push(row);
|
|
166
|
+
const entityId = String(row.id ?? rowId(op));
|
|
167
|
+
await trx
|
|
168
|
+
.insertInto('ablo_outbox')
|
|
169
|
+
.values({
|
|
170
|
+
id: `${change.clientTxId}:${index}`,
|
|
171
|
+
model: op.model,
|
|
172
|
+
entity_id: entityId,
|
|
173
|
+
type: op.type,
|
|
174
|
+
data: op.type === 'DELETE' ? null : JSON.stringify(row),
|
|
175
|
+
client_tx_id: change.clientTxId,
|
|
176
|
+
occurred_at: Date.now(),
|
|
177
|
+
})
|
|
178
|
+
.execute();
|
|
179
|
+
}
|
|
180
|
+
await trx
|
|
181
|
+
.insertInto('ablo_idempotency')
|
|
182
|
+
.values({ client_tx_id: change.clientTxId, response: JSON.stringify(rows) })
|
|
183
|
+
.execute();
|
|
184
|
+
return { rows };
|
|
185
|
+
});
|
|
186
|
+
},
|
|
187
|
+
async events(cursor, limit) {
|
|
188
|
+
const after = cursor ?? '0';
|
|
189
|
+
const rows = await db
|
|
190
|
+
.selectFrom('ablo_outbox')
|
|
191
|
+
.selectAll()
|
|
192
|
+
.where('cursor', '>', after)
|
|
193
|
+
.orderBy('cursor', 'asc')
|
|
194
|
+
.limit(limit)
|
|
195
|
+
.execute();
|
|
196
|
+
const events = rows.map((r) => outboxEventSchema.parse({
|
|
197
|
+
id: r.id,
|
|
198
|
+
model: r.model,
|
|
199
|
+
entityId: r.entity_id,
|
|
200
|
+
type: r.type,
|
|
201
|
+
data: typeof r.data === 'string' ? JSON.parse(r.data) : r.data ?? null,
|
|
202
|
+
organizationId: r.organization_id ?? null,
|
|
203
|
+
clientTxId: r.client_tx_id ?? null,
|
|
204
|
+
occurredAt: r.occurred_at != null ? Number(r.occurred_at) : null,
|
|
205
|
+
cursor: String(r.cursor),
|
|
206
|
+
}));
|
|
207
|
+
return { events, nextCursor: events.length > 0 ? events[events.length - 1].cursor : null };
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
}
|
|
@@ -8,10 +8,11 @@
|
|
|
8
8
|
* It models the real semantics minimally but faithfully: one canonical row store
|
|
9
9
|
* per model, an idempotency ledger keyed by `clientTxId`, and a monotonic outbox.
|
|
10
10
|
*/
|
|
11
|
+
import { AbloValidationError } from '../../errors.js';
|
|
11
12
|
function rowId(op) {
|
|
12
13
|
const id = op.id ?? op.input?.id;
|
|
13
14
|
if (typeof id !== 'string' || id.length === 0) {
|
|
14
|
-
throw new
|
|
15
|
+
throw new AbloValidationError(`operation on "${op.model}" requires an id`, { code: 'source_operation_id_required' });
|
|
15
16
|
}
|
|
16
17
|
return id;
|
|
17
18
|
}
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
* (`PrismaLike`), so this compiles in the SDK package and is unit-testable with
|
|
13
13
|
* a fake, while a real `PrismaClient` satisfies it at the call site.
|
|
14
14
|
*/
|
|
15
|
+
import { AbloValidationError } from '../../errors.js';
|
|
15
16
|
import { outboxEventSchema } from '../contract.js';
|
|
16
17
|
import { adapterTableMigrations } from '../migrations.js';
|
|
17
18
|
const lowerFirst = (s) => (s ? s[0].toLowerCase() + s.slice(1) : s);
|
|
@@ -32,7 +33,7 @@ const lowerFirst = (s) => (s ? s[0].toLowerCase() + s.slice(1) : s);
|
|
|
32
33
|
function delegateFor(client, name) {
|
|
33
34
|
const delegate = client[name];
|
|
34
35
|
if (!delegate || typeof delegate.findMany !== 'function') {
|
|
35
|
-
throw new
|
|
36
|
+
throw new AbloValidationError(`prismaDataSource: no Prisma delegate "${name}" on the client`, { code: 'source_adapter_misconfigured' });
|
|
36
37
|
}
|
|
37
38
|
return delegate;
|
|
38
39
|
}
|
|
@@ -97,7 +98,7 @@ function findManyArgs(query) {
|
|
|
97
98
|
function rowId(op) {
|
|
98
99
|
const id = op.id ?? op.input?.id;
|
|
99
100
|
if (typeof id !== 'string' || id.length === 0) {
|
|
100
|
-
throw new
|
|
101
|
+
throw new AbloValidationError(`operation on "${op.model}" requires an id`, { code: 'source_operation_id_required' });
|
|
101
102
|
}
|
|
102
103
|
return id;
|
|
103
104
|
}
|
package/dist/source/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AbloValidationError } from '../errors.js';
|
|
1
2
|
import { changeSetSchema } from './contract.js';
|
|
2
3
|
/**
|
|
3
4
|
* Build the source-event marker customers should write to their outbox table in
|
|
@@ -10,7 +11,7 @@ import { changeSetSchema } from './contract.js';
|
|
|
10
11
|
export function sourceEventForOperation(options) {
|
|
11
12
|
const entityId = options.entityId ?? options.operation.id;
|
|
12
13
|
if (typeof entityId !== 'string' || entityId.length === 0) {
|
|
13
|
-
throw new
|
|
14
|
+
throw new AbloValidationError('sourceEventForOperation requires operation.id or an explicit entityId', { code: 'source_event_invalid' });
|
|
14
15
|
}
|
|
15
16
|
const occurredAt = normalizeEventOccurredAt(options.occurredAt);
|
|
16
17
|
return {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
import { EventEmitter } from 'events';
|
|
11
11
|
import type { Database } from '../Database.js';
|
|
12
12
|
import { Model } from '../Model.js';
|
|
13
|
-
import type {
|
|
13
|
+
import type { WriteOptions } from '../interfaces/index.js';
|
|
14
14
|
export interface UserContext {
|
|
15
15
|
userId: string;
|
|
16
16
|
organizationId: string;
|
|
@@ -19,7 +19,6 @@ export interface UserContext {
|
|
|
19
19
|
}
|
|
20
20
|
/** Wire-format mutation payload (post-projection). */
|
|
21
21
|
type MutationInput = Record<string, unknown>;
|
|
22
|
-
type TransactionWriteOptions = Pick<MutationOptions, 'readAt' | 'onStale'>;
|
|
23
22
|
export interface Transaction {
|
|
24
23
|
id: string;
|
|
25
24
|
type: 'create' | 'update' | 'delete' | 'archive' | 'unarchive';
|
|
@@ -34,7 +33,7 @@ export interface Transaction {
|
|
|
34
33
|
attempts: number;
|
|
35
34
|
priority: 'normal' | 'high';
|
|
36
35
|
priorityScore: number;
|
|
37
|
-
writeOptions?:
|
|
36
|
+
writeOptions?: WriteOptions;
|
|
38
37
|
batchId?: string;
|
|
39
38
|
/** LINEAR PATTERN: syncId threshold - transaction confirms when delta.id >= this value */
|
|
40
39
|
syncIdNeededForCompletion?: number;
|
|
@@ -237,16 +236,16 @@ export declare class TransactionQueue extends EventEmitter {
|
|
|
237
236
|
/**
|
|
238
237
|
* Create operation with optimistic update
|
|
239
238
|
*/
|
|
240
|
-
create(model: Model, context: UserContext, writeOptions?:
|
|
239
|
+
create(model: Model, context: UserContext, writeOptions?: WriteOptions): Promise<Transaction>;
|
|
241
240
|
/**
|
|
242
241
|
* Update operation with conflict detection
|
|
243
242
|
* @param precomputedChanges - Optional pre-captured changes (avoids re-reading from model)
|
|
244
243
|
*/
|
|
245
|
-
update(model: Model, context: UserContext, precomputedChanges?: Record<string, unknown>, writeOptions?:
|
|
244
|
+
update(model: Model, context: UserContext, precomputedChanges?: Record<string, unknown>, writeOptions?: WriteOptions): Promise<Transaction>;
|
|
246
245
|
/**
|
|
247
246
|
* Delete operation with cascade handling
|
|
248
247
|
*/
|
|
249
|
-
delete(model: Model, context: UserContext, writeOptions?:
|
|
248
|
+
delete(model: Model, context: UserContext, writeOptions?: WriteOptions): Promise<Transaction>;
|
|
250
249
|
/**
|
|
251
250
|
* Upload attachment — delegates to attachment-uploader.ts
|
|
252
251
|
*/
|
|
@@ -269,7 +268,7 @@ export declare class TransactionQueue extends EventEmitter {
|
|
|
269
268
|
/**
|
|
270
269
|
* Archive operation
|
|
271
270
|
*/
|
|
272
|
-
archive(model: Model, context: UserContext, writeOptions?:
|
|
271
|
+
archive(model: Model, context: UserContext, writeOptions?: WriteOptions): Promise<Transaction>;
|
|
273
272
|
/**
|
|
274
273
|
* Unarchive operation
|
|
275
274
|
*/
|
|
@@ -117,13 +117,31 @@ function hasStaleWriteOptions(options) {
|
|
|
117
117
|
return (options?.readAt !== undefined ||
|
|
118
118
|
options?.onStale !== undefined);
|
|
119
119
|
}
|
|
120
|
-
|
|
120
|
+
/**
|
|
121
|
+
* Project a transaction's `writeOptions` onto the wire operation. Stale
|
|
122
|
+
* guards (`readAt`/`onStale`) ride at the op root; `idempotencyKey`/`label`
|
|
123
|
+
* ride in the op's `options` slot (`MutationOperation.options` — the
|
|
124
|
+
* mutation_log cache key + audit tag). This is the single place the
|
|
125
|
+
* caller-supplied write vocabulary crosses onto the wire.
|
|
126
|
+
*/
|
|
127
|
+
function applyWriteOptions(op, transaction) {
|
|
121
128
|
const operation = op;
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (
|
|
126
|
-
operation.
|
|
129
|
+
const writeOptions = transaction.writeOptions;
|
|
130
|
+
if (!writeOptions)
|
|
131
|
+
return operation;
|
|
132
|
+
if (writeOptions.readAt !== undefined) {
|
|
133
|
+
operation.readAt = writeOptions.readAt;
|
|
134
|
+
}
|
|
135
|
+
if (writeOptions.onStale !== undefined) {
|
|
136
|
+
operation.onStale = writeOptions.onStale;
|
|
137
|
+
}
|
|
138
|
+
if (writeOptions.idempotencyKey != null || writeOptions.label !== undefined) {
|
|
139
|
+
operation.options = {
|
|
140
|
+
...(writeOptions.idempotencyKey != null
|
|
141
|
+
? { idempotencyKey: writeOptions.idempotencyKey }
|
|
142
|
+
: {}),
|
|
143
|
+
...(writeOptions.label !== undefined ? { label: writeOptions.label } : {}),
|
|
144
|
+
};
|
|
127
145
|
}
|
|
128
146
|
return operation;
|
|
129
147
|
}
|
|
@@ -552,7 +570,7 @@ export class TransactionQueue extends EventEmitter {
|
|
|
552
570
|
// Build operations list
|
|
553
571
|
const operations = pending.map((tx) => {
|
|
554
572
|
this.ensureDerivedFields(tx);
|
|
555
|
-
return
|
|
573
|
+
return applyWriteOptions({
|
|
556
574
|
type: TX_TYPE_TO_MUTATION_OP[tx.type],
|
|
557
575
|
model: tx.modelKey,
|
|
558
576
|
id: tx.modelId,
|
|
@@ -930,7 +948,7 @@ export class TransactionQueue extends EventEmitter {
|
|
|
930
948
|
// matches it via `OptimisticEchoTracker.consumeEcho` to suppress
|
|
931
949
|
// double-applying optimistic mutations. Distinct from the
|
|
932
950
|
// batch-level idempotency key in mutation_log.
|
|
933
|
-
const op =
|
|
951
|
+
const op = applyWriteOptions({
|
|
934
952
|
type: TX_TYPE_TO_MUTATION_OP[tx.type],
|
|
935
953
|
model: tx.modelKey,
|
|
936
954
|
id: tx.modelId,
|
|
@@ -1358,6 +1376,12 @@ export class TransactionQueue extends EventEmitter {
|
|
|
1358
1376
|
};
|
|
1359
1377
|
this.commitStore.set(clientTxId, tx);
|
|
1360
1378
|
this.commitLane.push(tx);
|
|
1379
|
+
// Surface the envelope on its OWN event so the undo stream can record
|
|
1380
|
+
// commit-lane writes too (`SyncClient.onLocalTransaction` enriches each
|
|
1381
|
+
// operation with pool-captured previous state). Deliberately NOT
|
|
1382
|
+
// `transaction:created` — that event also feeds the optimistic-echo
|
|
1383
|
+
// tracker, and commit-lane ops have no optimistic pool apply to echo.
|
|
1384
|
+
this.emit('commit:created', { clientTxId, operations: tx.operations });
|
|
1361
1385
|
void this.processCommitLane();
|
|
1362
1386
|
}
|
|
1363
1387
|
/**
|
|
@@ -1674,7 +1698,7 @@ export class TransactionQueue extends EventEmitter {
|
|
|
1674
1698
|
const input = (type === 'create' || type === 'update') ? data : undefined;
|
|
1675
1699
|
try {
|
|
1676
1700
|
await this.mutationExecutor.commit([
|
|
1677
|
-
|
|
1701
|
+
applyWriteOptions({ type: mutationType, model, id: modelId, input }, transaction),
|
|
1678
1702
|
]);
|
|
1679
1703
|
}
|
|
1680
1704
|
catch (error) {
|
package/dist/utils/duration.js
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
* without breaking numeric callers — the wrapper below branches on
|
|
17
17
|
* the input type.
|
|
18
18
|
*/
|
|
19
|
+
import { AbloValidationError } from '../errors.js';
|
|
19
20
|
const PATTERN = /^(\d+(?:\.\d+)?)(ms|s|m|h)$/;
|
|
20
21
|
const UNIT_MS = {
|
|
21
22
|
ms: 1,
|
|
@@ -34,8 +35,8 @@ export function toMs(input) {
|
|
|
34
35
|
return input * 1_000;
|
|
35
36
|
const match = PATTERN.exec(input);
|
|
36
37
|
if (!match) {
|
|
37
|
-
throw new
|
|
38
|
-
`a string like "500ms" | "30s" | "3m" | "24h"
|
|
38
|
+
throw new AbloValidationError(`Invalid duration "${input}" — expected number (seconds) or ` +
|
|
39
|
+
`a string like "500ms" | "30s" | "3m" | "24h".`, { code: 'duration_invalid' });
|
|
39
40
|
}
|
|
40
41
|
const value = Number(match[1]);
|
|
41
42
|
const unit = match[2];
|
package/docs/client-behavior.md
CHANGED
|
@@ -30,7 +30,7 @@ Common options:
|
|
|
30
30
|
| `schema` | Required for typed model clients. |
|
|
31
31
|
| `apiKey` | Bearer credential for trusted server runtimes. Defaults to `ABLO_API_KEY` when available. |
|
|
32
32
|
| `baseURL` | Override the hosted sync endpoint for staging or private deployments. |
|
|
33
|
-
| `persistence` | `
|
|
33
|
+
| `persistence` | `memory` by default. Use `indexeddb` for a durable browser cache that survives reloads. |
|
|
34
34
|
| `fetch` | Custom fetch implementation for tests or non-standard runtimes. |
|
|
35
35
|
| `defaultHeaders` | Extra headers attached to every HTTP request. |
|
|
36
36
|
| `defaultQuery` | Extra query parameters attached to every HTTP request. |
|
package/docs/data-sources.md
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# Connect Your Database
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
storing the data itself.
|
|
3
|
+
**Your database is the system of record — Ablo never hosts your data.** Every
|
|
4
|
+
synced model is backed by your own Postgres; Ablo is the transaction layer on
|
|
5
|
+
top of it. There are two ways to connect, and they are the same product with the
|
|
6
|
+
same writes — the only difference is where your database credential lives:
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
| | How Ablo reaches your Postgres | Use when |
|
|
9
|
+
|---|---|---|
|
|
10
|
+
| **Connection string** (default) | You pass `databaseUrl` to `Ablo(...)`; Ablo registers the connection and commits each write directly, behind row-level security. | You can hand over a scoped connection string. |
|
|
11
|
+
| **Signed endpoint** | Your app exposes one route built from an ORM adapter; Ablo sends signed commit requests and your app writes its own database. | Database credentials must never leave your infrastructure. |
|
|
12
12
|
|
|
13
13
|
Either way, you define an Ablo schema with `defineSchema`, `model`, and Zod. The
|
|
14
14
|
Ablo schema describes **only your synced, collaborative models** — the rows Ablo
|
|
@@ -16,17 +16,18 @@ coordinates and fans out in realtime. It is *not* your whole-database schema and
|
|
|
16
16
|
does *not* replace your `schema.prisma` (or your Drizzle schema). Your auth,
|
|
17
17
|
billing, and any other non-synced tables stay in your own ORM schema, owned by
|
|
18
18
|
your own migrations. One database, two schemas, side by side: Ablo owns the
|
|
19
|
-
synced models
|
|
20
|
-
tables
|
|
21
|
-
this — it reports your other tables as "ignored / owned by you," which is exactly
|
|
22
|
-
right.
|
|
19
|
+
synced models; you keep owning everything else. `ablo check` reflects this — it
|
|
20
|
+
reports your other tables as "ignored / owned by you," which is exactly right.
|
|
23
21
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
What Ablo stores, in both shapes: your schema *definition* (model names, fields,
|
|
23
|
+
types — pushed with `ablo push`), your hashed API keys, a safe projection of the
|
|
24
|
+
connection registration (host, database, schema — the connection string itself
|
|
25
|
+
is sealed and never echoed back), and the commit log that drives sync. Never
|
|
26
|
+
your rows.
|
|
28
27
|
|
|
29
|
-
|
|
28
|
+
## Connection String (default)
|
|
29
|
+
|
|
30
|
+
The canonical client carries all three values:
|
|
30
31
|
|
|
31
32
|
```ts
|
|
32
33
|
import Ablo from '@abloatai/ablo';
|
|
@@ -35,29 +36,50 @@ import { schema } from './ablo/schema';
|
|
|
35
36
|
export const ablo = Ablo({
|
|
36
37
|
schema,
|
|
37
38
|
apiKey: process.env.ABLO_API_KEY,
|
|
39
|
+
databaseUrl: process.env.DATABASE_URL, // your Postgres — rows live here, never with Ablo
|
|
38
40
|
});
|
|
39
41
|
```
|
|
40
42
|
|
|
41
|
-
Do not pass a database URL to `Ablo(...)`.
|
|
42
|
-
|
|
43
|
-
For the first production integration, prefer this shape:
|
|
44
|
-
|
|
45
43
|
```bash
|
|
46
|
-
#
|
|
47
|
-
DATABASE_URL=postgres
|
|
48
|
-
|
|
49
|
-
# The only Ablo credential in the customer app
|
|
44
|
+
# .env — server runtime only, never the browser
|
|
45
|
+
DATABASE_URL=postgres://ablo_app:...@host:5432/db
|
|
50
46
|
ABLO_API_KEY=sk_live_...
|
|
51
47
|
```
|
|
52
48
|
|
|
53
|
-
|
|
49
|
+
On first connect the SDK registers the connection — sent once over TLS, stored
|
|
50
|
+
sealed, never returned by any API. From then on Ablo commits every confirmed
|
|
51
|
+
write directly to your database and reads canonical rows from it.
|
|
52
|
+
|
|
53
|
+
Safety requirements, enforced server-side before the first write:
|
|
54
|
+
|
|
55
|
+
- **Non-superuser role.** The connection must not be a superuser or hold
|
|
56
|
+
`BYPASSRLS` — Ablo's tenant isolation is row-level security, and a role that
|
|
57
|
+
can bypass it is rejected outright.
|
|
58
|
+
- **Row-level security on synced tables.** `npx ablo migrate` provisions your
|
|
59
|
+
synced-model tables with `FORCE ROW LEVEL SECURITY` already applied; tables
|
|
60
|
+
you create yourself must do the same.
|
|
61
|
+
- **Public hosts only.** Connection strings resolving to loopback or private
|
|
62
|
+
address ranges are rejected.
|
|
63
|
+
|
|
64
|
+
`databaseUrl` is server-only: the SDK throws if it sees one in a browser-like
|
|
65
|
+
environment, and `dangerouslyAllowBrowser` does not override that.
|
|
66
|
+
|
|
67
|
+
## Signed Endpoint
|
|
54
68
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
69
|
+
When a connection string must not leave your infrastructure, keep
|
|
70
|
+
`DATABASE_URL` in your app and expose one HTTPS endpoint instead. Ablo signs a
|
|
71
|
+
commit request; an ORM adapter in your route runs it in one transaction against
|
|
72
|
+
your Postgres and returns the canonical rows. Omit `databaseUrl` from
|
|
73
|
+
`Ablo(...)` in this setup — the client takes only the schema and the API key:
|
|
59
74
|
|
|
60
|
-
|
|
75
|
+
```ts
|
|
76
|
+
export const ablo = Ablo({
|
|
77
|
+
schema,
|
|
78
|
+
apiKey: process.env.ABLO_API_KEY,
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
The SDK call is identical in both shapes:
|
|
61
83
|
|
|
62
84
|
```ts
|
|
63
85
|
await ablo.weatherReports.create({ data: { location: 'Stockholm', status: 'pending' } });
|
|
@@ -65,9 +87,7 @@ await ablo.weatherReports.update({ id: 'report_stockholm', data: { status: 'read
|
|
|
65
87
|
const report = ablo.weatherReports.get('report_stockholm');
|
|
66
88
|
```
|
|
67
89
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
Multiplayer behavior is the same in both modes. Writes made through
|
|
90
|
+
Multiplayer behavior is built in. Writes made through
|
|
71
91
|
`ablo.<model>.create/update/delete` are coordinated by Ablo, then confirmed rows
|
|
72
92
|
fan out to subscribers. If something writes to your database without going
|
|
73
93
|
through Ablo (a cron job, an admin tool), Ablo can't know about it
|
|
@@ -75,10 +95,10 @@ automatically. To keep everyone's screen up to date, your app reports those
|
|
|
75
95
|
outside changes back through the outbox feed — shown below in
|
|
76
96
|
[Outbox Events](#outbox-events).
|
|
77
97
|
|
|
78
|
-
##
|
|
98
|
+
## Your Database Stays Canonical
|
|
79
99
|
|
|
80
|
-
|
|
81
|
-
|
|
100
|
+
Your application database remains the source of truth and Ablo coordinates writes
|
|
101
|
+
against it.
|
|
82
102
|
|
|
83
103
|
If you are migrating an app where every button already calls a backend endpoint,
|
|
84
104
|
read [Integration Guide](./integration-guide.md) first, then
|
|
@@ -221,9 +241,9 @@ cron job or admin tool that never went through Ablo. Append an `ablo_outbox` row
|
|
|
221
241
|
(with no `clientTxId`) for those in the same transaction as the change, and the
|
|
222
242
|
adapter's feed carries them to every connected screen.
|
|
223
243
|
|
|
224
|
-
## Production Checklist
|
|
244
|
+
## Production Checklist (signed endpoint)
|
|
225
245
|
|
|
226
|
-
Before using
|
|
246
|
+
Before using the signed-endpoint shape in production:
|
|
227
247
|
|
|
228
248
|
- Keep `DATABASE_URL` in the customer app or backend environment.
|
|
229
249
|
- Use only the Data Source endpoint and `ABLO_API_KEY` as the customer-facing integration boundary.
|
|
@@ -239,9 +259,8 @@ transaction, `clientTxId` idempotency, returning canonical rows, the outbox
|
|
|
239
259
|
append per operation, and deduping the feed by event `id`. You don't write any of
|
|
240
260
|
that by hand.
|
|
241
261
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
its own security model.)
|
|
262
|
+
In this shape, leave `databaseUrl` out of `Ablo(...)` — the endpoint *is* the
|
|
263
|
+
connection, and registering both would point Ablo at your database twice.
|
|
245
264
|
|
|
246
265
|
## Security
|
|
247
266
|
|
package/docs/guarantees.md
CHANGED
|
@@ -109,7 +109,7 @@ authorized it, which run did it, and what state was it based on?"
|
|
|
109
109
|
|
|
110
110
|
## Persistence
|
|
111
111
|
|
|
112
|
-
Ablo defaults to
|
|
112
|
+
Ablo defaults to in-memory persistence ('memory'), so nothing is written to disk
|
|
113
113
|
unless you ask for it.
|
|
114
114
|
|
|
115
115
|
Opt into a durable browser cache that survives reloads when you need it:
|
|
@@ -122,7 +122,7 @@ const ablo = Ablo({
|
|
|
122
122
|
});
|
|
123
123
|
```
|
|
124
124
|
|
|
125
|
-
Node, SSR, tests, and agents use
|
|
125
|
+
Node, SSR, tests, and agents use in-memory persistence ('memory') automatically.
|
|
126
126
|
|
|
127
127
|
## Storage Boundary
|
|
128
128
|
|
package/docs/index.md
CHANGED
|
@@ -43,7 +43,7 @@ Three things stay true no matter how you use Ablo:
|
|
|
43
43
|
- [Schema Contract](./schema-contract.md) — One schema becomes typed model clients, React reads, agent writes, Data Source shape, and schema push.
|
|
44
44
|
- [CLI & Migrations](./cli.md) — `init` / `migrate` / `push` / `generate`, the shared Zod→Postgres type map, and structured migration errors.
|
|
45
45
|
- [Identity & Sync Groups](./identity.md) — Use your own authentication; tell Ablo who's connecting and how org / team / user map to sync-group scope.
|
|
46
|
-
- [Integration Guide](./integration-guide.md) —
|
|
46
|
+
- [Integration Guide](./integration-guide.md) — Connect your database via Data Source, plus React, multiplayer, and agent patterns.
|
|
47
47
|
- [Guarantees](./guarantees.md) — What confirmed writes, stale checks, and claims guarantee.
|
|
48
48
|
- [Interaction Model](./interaction-model.md) — The schema, claim, update, confirmation loop.
|
|
49
49
|
- [API Reference](./api.md) — Model-by-model method shape.
|
|
@@ -57,7 +57,7 @@ Three things stay true no matter how you use Ablo:
|
|
|
57
57
|
| Plane | Primitives | Purpose |
|
|
58
58
|
|---|---|---|
|
|
59
59
|
| State | `Schema`, `Model`, `Claim`, `Receipt` | The product path. Load, coordinate, write, confirm. |
|
|
60
|
-
| Storage | `
|
|
60
|
+
| Storage | `Data Source` | Your rows live in your own database behind a signed Data Source endpoint. |
|
|
61
61
|
|
|
62
62
|
## Use cases
|
|
63
63
|
|
|
@@ -42,14 +42,11 @@ schema -> ablo.<model>.list(...) -> ablo.<model>.update(...)
|
|
|
42
42
|
Commits and receipts exist under the hood. Most apps do not create protocol
|
|
43
43
|
objects by hand.
|
|
44
44
|
|
|
45
|
-
##
|
|
45
|
+
## Your Database
|
|
46
46
|
|
|
47
|
-
Every schema model
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
| ------------ | ----------------- | -------------------------------------------------------------------------------- |
|
|
51
|
-
| Ablo-managed | Ablo | New collaborative or agent-written state can live in Ablo. |
|
|
52
|
-
| Data Source | Your app database | You already have tables, service logic, and API endpoints that remain canonical. |
|
|
47
|
+
Every schema model is backed by **your own database**. You expose a signed Data
|
|
48
|
+
Source endpoint; Ablo coordinates each write and your app commits it to your
|
|
49
|
+
Postgres. The SDK call shape is the same everywhere.
|
|
53
50
|
|
|
54
51
|
Do not pass a database URL to `Ablo(...)`. Application and agent code use
|
|
55
52
|
`ABLO_API_KEY`. If your database stays canonical, expose a signed Data Source
|