@koolbase/core 10.0.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/README.md +19 -0
- package/dist/cjs/analytics.d.ts +24 -0
- package/dist/cjs/analytics.js +110 -0
- package/dist/cjs/auth-errors.d.ts +117 -0
- package/dist/cjs/auth-errors.js +250 -0
- package/dist/cjs/auth.d.ts +213 -0
- package/dist/cjs/auth.js +813 -0
- package/dist/cjs/cache-store.d.ts +50 -0
- package/dist/cjs/cache-store.js +194 -0
- package/dist/cjs/conflict.d.ts +80 -0
- package/dist/cjs/conflict.js +84 -0
- package/dist/cjs/database-errors.d.ts +101 -0
- package/dist/cjs/database-errors.js +200 -0
- package/dist/cjs/database.d.ts +298 -0
- package/dist/cjs/database.js +852 -0
- package/dist/cjs/device-id.d.ts +1 -0
- package/dist/cjs/device-id.js +57 -0
- package/dist/cjs/device-metadata.d.ts +36 -0
- package/dist/cjs/device-metadata.js +94 -0
- package/dist/cjs/errors.d.ts +64 -0
- package/dist/cjs/errors.js +85 -0
- package/dist/cjs/flags.d.ts +15 -0
- package/dist/cjs/flags.js +76 -0
- package/dist/cjs/function-errors.d.ts +51 -0
- package/dist/cjs/function-errors.js +103 -0
- package/dist/cjs/functions.d.ts +15 -0
- package/dist/cjs/functions.js +83 -0
- package/dist/cjs/index.d.ts +22 -0
- package/dist/cjs/index.js +56 -0
- package/dist/cjs/messaging.d.ts +13 -0
- package/dist/cjs/messaging.js +36 -0
- package/dist/cjs/offline-state.d.ts +97 -0
- package/dist/cjs/offline-state.js +198 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/pending-write.d.ts +47 -0
- package/dist/cjs/pending-write.js +22 -0
- package/dist/cjs/platform.d.ts +51 -0
- package/dist/cjs/platform.js +41 -0
- package/dist/cjs/realtime.d.ts +44 -0
- package/dist/cjs/realtime.js +195 -0
- package/dist/cjs/record.d.ts +2 -0
- package/dist/cjs/record.js +23 -0
- package/dist/cjs/shared.d.ts +9 -0
- package/dist/cjs/shared.js +43 -0
- package/dist/cjs/storage-errors.d.ts +163 -0
- package/dist/cjs/storage-errors.js +253 -0
- package/dist/cjs/storage.d.ts +198 -0
- package/dist/cjs/storage.js +451 -0
- package/dist/cjs/sync-engine.d.ts +30 -0
- package/dist/cjs/sync-engine.js +286 -0
- package/dist/cjs/types.d.ts +487 -0
- package/dist/cjs/types.js +40 -0
- package/dist/esm/analytics.d.ts +24 -0
- package/dist/esm/analytics.js +106 -0
- package/dist/esm/auth-errors.d.ts +117 -0
- package/dist/esm/auth-errors.js +222 -0
- package/dist/esm/auth.d.ts +213 -0
- package/dist/esm/auth.js +809 -0
- package/dist/esm/cache-store.d.ts +50 -0
- package/dist/esm/cache-store.js +179 -0
- package/dist/esm/conflict.d.ts +80 -0
- package/dist/esm/conflict.js +80 -0
- package/dist/esm/database-errors.d.ts +101 -0
- package/dist/esm/database-errors.js +189 -0
- package/dist/esm/database.d.ts +298 -0
- package/dist/esm/database.js +848 -0
- package/dist/esm/device-id.d.ts +1 -0
- package/dist/esm/device-id.js +54 -0
- package/dist/esm/device-metadata.d.ts +36 -0
- package/dist/esm/device-metadata.js +90 -0
- package/dist/esm/errors.d.ts +64 -0
- package/dist/esm/errors.js +79 -0
- package/dist/esm/flags.d.ts +15 -0
- package/dist/esm/flags.js +72 -0
- package/dist/esm/function-errors.d.ts +51 -0
- package/dist/esm/function-errors.js +93 -0
- package/dist/esm/functions.d.ts +15 -0
- package/dist/esm/functions.js +79 -0
- package/dist/esm/index.d.ts +22 -0
- package/dist/esm/index.js +25 -0
- package/dist/esm/messaging.d.ts +13 -0
- package/dist/esm/messaging.js +32 -0
- package/dist/esm/offline-state.d.ts +97 -0
- package/dist/esm/offline-state.js +189 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/pending-write.d.ts +47 -0
- package/dist/esm/pending-write.js +19 -0
- package/dist/esm/platform.d.ts +51 -0
- package/dist/esm/platform.js +36 -0
- package/dist/esm/realtime.d.ts +44 -0
- package/dist/esm/realtime.js +191 -0
- package/dist/esm/record.d.ts +2 -0
- package/dist/esm/record.js +20 -0
- package/dist/esm/shared.d.ts +9 -0
- package/dist/esm/shared.js +39 -0
- package/dist/esm/storage-errors.d.ts +163 -0
- package/dist/esm/storage-errors.js +239 -0
- package/dist/esm/storage.d.ts +198 -0
- package/dist/esm/storage.js +447 -0
- package/dist/esm/sync-engine.d.ts +30 -0
- package/dist/esm/sync-engine.js +282 -0
- package/dist/esm/types.d.ts +487 -0
- package/dist/esm/types.js +37 -0
- package/package.json +45 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { KoolbaseRecord, LegacyPendingWrite, QueryResult } from './types.js';
|
|
2
|
+
export declare function hashQuery(collection: string, options: Record<string, unknown>): string;
|
|
3
|
+
export declare function getCached(userId: string, collection: string, queryHash: string): Promise<QueryResult | null>;
|
|
4
|
+
export declare function setCached(userId: string, collection: string, queryHash: string, result: QueryResult): Promise<void>;
|
|
5
|
+
export declare function invalidateCache(userId: string, collection: string): Promise<void>;
|
|
6
|
+
/**
|
|
7
|
+
* Drops everything cached for a user.
|
|
8
|
+
*
|
|
9
|
+
* Deliberately spares the write queue. The prefix covers every key for this
|
|
10
|
+
* user, and the queue lives under one of them — so clearing the cache used to
|
|
11
|
+
* delete offline writes the user believes are saved, silently and
|
|
12
|
+
* irrecoverably. A cache is what can be refetched; queued writes are not that.
|
|
13
|
+
*/
|
|
14
|
+
export declare function clearUserCache(userId: string): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* A record as the SDK last saw it, with the revision it was read at.
|
|
17
|
+
*
|
|
18
|
+
* Separate from the query cache, which answers "what did this query return".
|
|
19
|
+
* This answers "what is the latest copy of this record" — and an offline
|
|
20
|
+
* mutation composes against the second. Scanning query blobs for one would mean
|
|
21
|
+
* the same record appearing in several snapshots at different revisions, with no
|
|
22
|
+
* principled way to choose.
|
|
23
|
+
*
|
|
24
|
+
* Keyed with `record` where a collection name would sit, so invalidateCache —
|
|
25
|
+
* which scopes to a collection and runs after every write — cannot reach these.
|
|
26
|
+
* A user's own edit must not remove the baseline they need for the next one.
|
|
27
|
+
*/
|
|
28
|
+
export interface CachedRecord {
|
|
29
|
+
collection: string;
|
|
30
|
+
data: Record<string, unknown>;
|
|
31
|
+
revision?: number;
|
|
32
|
+
cachedAt: string;
|
|
33
|
+
}
|
|
34
|
+
export declare function getCachedRecord(userId: string, recordId: string): Promise<CachedRecord | null>;
|
|
35
|
+
/**
|
|
36
|
+
* Stores a record, refusing to move it backwards.
|
|
37
|
+
*
|
|
38
|
+
* Query responses can arrive out of order — a slow request from an earlier
|
|
39
|
+
* screen resolving after a fresh one — and an older copy overwriting a newer
|
|
40
|
+
* would compose the next mutation against a stale revision, producing a conflict
|
|
41
|
+
* the user never caused. False conflicts teach people to force-overwrite, which
|
|
42
|
+
* is worse than none.
|
|
43
|
+
*/
|
|
44
|
+
export declare function cacheRecord(userId: string, collection: string, recordId: string, data: Record<string, unknown>, revision?: number): Promise<void>;
|
|
45
|
+
export declare function removeCachedRecord(userId: string, recordId: string): Promise<void>;
|
|
46
|
+
export declare function getWriteQueue(userId: string): Promise<LegacyPendingWrite[]>;
|
|
47
|
+
export declare function addToWriteQueue(userId: string, write: Omit<LegacyPendingWrite, 'retries' | 'createdAt'>): Promise<void>;
|
|
48
|
+
export declare function removeFromWriteQueue(userId: string, writeId: string): Promise<void>;
|
|
49
|
+
export declare function incrementWriteRetry(userId: string, writeId: string): Promise<void>;
|
|
50
|
+
export declare function optimisticallyInsert(userId: string, collection: string, record: KoolbaseRecord): Promise<void>;
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hashQuery = hashQuery;
|
|
4
|
+
exports.getCached = getCached;
|
|
5
|
+
exports.setCached = setCached;
|
|
6
|
+
exports.invalidateCache = invalidateCache;
|
|
7
|
+
exports.clearUserCache = clearUserCache;
|
|
8
|
+
exports.getCachedRecord = getCachedRecord;
|
|
9
|
+
exports.cacheRecord = cacheRecord;
|
|
10
|
+
exports.removeCachedRecord = removeCachedRecord;
|
|
11
|
+
exports.getWriteQueue = getWriteQueue;
|
|
12
|
+
exports.addToWriteQueue = addToWriteQueue;
|
|
13
|
+
exports.removeFromWriteQueue = removeFromWriteQueue;
|
|
14
|
+
exports.incrementWriteRetry = incrementWriteRetry;
|
|
15
|
+
exports.optimisticallyInsert = optimisticallyInsert;
|
|
16
|
+
const platform_js_1 = require("./platform.js");
|
|
17
|
+
const CACHE_VERSION = 'v1';
|
|
18
|
+
function cacheKey(userId, collection, queryHash) {
|
|
19
|
+
return `koolbase:${CACHE_VERSION}:${userId}:${collection}:${queryHash}`;
|
|
20
|
+
}
|
|
21
|
+
function writeQueueKey(userId) {
|
|
22
|
+
return `koolbase:${CACHE_VERSION}:${userId}:write_queue`;
|
|
23
|
+
}
|
|
24
|
+
function hashQuery(collection, options) {
|
|
25
|
+
return `${collection}:${JSON.stringify(options)}`;
|
|
26
|
+
}
|
|
27
|
+
// ─── Cache ──────────────────────────────────────────────────────────────────
|
|
28
|
+
async function getCached(userId, collection, queryHash) {
|
|
29
|
+
try {
|
|
30
|
+
const raw = await (0, platform_js_1.getPlatform)().storage.getItem(cacheKey(userId, collection, queryHash));
|
|
31
|
+
if (!raw)
|
|
32
|
+
return null;
|
|
33
|
+
return JSON.parse(raw);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async function setCached(userId, collection, queryHash, result) {
|
|
40
|
+
try {
|
|
41
|
+
await (0, platform_js_1.getPlatform)().storage.setItem(cacheKey(userId, collection, queryHash), JSON.stringify(result));
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// ignore storage errors
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async function invalidateCache(userId, collection) {
|
|
48
|
+
try {
|
|
49
|
+
const keys = await (0, platform_js_1.getPlatform)().storage.getAllKeys();
|
|
50
|
+
const prefix = `koolbase:${CACHE_VERSION}:${userId}:${collection}:`;
|
|
51
|
+
const toDelete = keys.filter(k => k.startsWith(prefix));
|
|
52
|
+
for (const key of toDelete) {
|
|
53
|
+
await (0, platform_js_1.getPlatform)().storage.removeItem(key);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// ignore
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Drops everything cached for a user.
|
|
62
|
+
*
|
|
63
|
+
* Deliberately spares the write queue. The prefix covers every key for this
|
|
64
|
+
* user, and the queue lives under one of them — so clearing the cache used to
|
|
65
|
+
* delete offline writes the user believes are saved, silently and
|
|
66
|
+
* irrecoverably. A cache is what can be refetched; queued writes are not that.
|
|
67
|
+
*/
|
|
68
|
+
async function clearUserCache(userId) {
|
|
69
|
+
try {
|
|
70
|
+
const keys = await (0, platform_js_1.getPlatform)().storage.getAllKeys();
|
|
71
|
+
const prefix = `koolbase:${CACHE_VERSION}:${userId}:`;
|
|
72
|
+
const queueKey = writeQueueKey(userId);
|
|
73
|
+
const toDelete = keys.filter(k => k.startsWith(prefix) && k !== queueKey);
|
|
74
|
+
for (const key of toDelete) {
|
|
75
|
+
await (0, platform_js_1.getPlatform)().storage.removeItem(key);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// ignore
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function recordCacheKey(userId, recordId) {
|
|
83
|
+
return `koolbase:${CACHE_VERSION}:${userId}:record:${recordId}`;
|
|
84
|
+
}
|
|
85
|
+
async function getCachedRecord(userId, recordId) {
|
|
86
|
+
try {
|
|
87
|
+
const raw = await (0, platform_js_1.getPlatform)().storage.getItem(recordCacheKey(userId, recordId));
|
|
88
|
+
return raw ? JSON.parse(raw) : null;
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Stores a record, refusing to move it backwards.
|
|
96
|
+
*
|
|
97
|
+
* Query responses can arrive out of order — a slow request from an earlier
|
|
98
|
+
* screen resolving after a fresh one — and an older copy overwriting a newer
|
|
99
|
+
* would compose the next mutation against a stale revision, producing a conflict
|
|
100
|
+
* the user never caused. False conflicts teach people to force-overwrite, which
|
|
101
|
+
* is worse than none.
|
|
102
|
+
*/
|
|
103
|
+
async function cacheRecord(userId, collection, recordId, data, revision) {
|
|
104
|
+
try {
|
|
105
|
+
const existing = await getCachedRecord(userId, recordId);
|
|
106
|
+
if (existing?.revision !== undefined &&
|
|
107
|
+
revision !== undefined &&
|
|
108
|
+
revision < existing.revision) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const entry = {
|
|
112
|
+
collection,
|
|
113
|
+
data,
|
|
114
|
+
revision,
|
|
115
|
+
cachedAt: new Date().toISOString(),
|
|
116
|
+
};
|
|
117
|
+
await (0, platform_js_1.getPlatform)().storage.setItem(recordCacheKey(userId, recordId), JSON.stringify(entry));
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
// ignore
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
async function removeCachedRecord(userId, recordId) {
|
|
124
|
+
try {
|
|
125
|
+
await (0, platform_js_1.getPlatform)().storage.removeItem(recordCacheKey(userId, recordId));
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
// ignore
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// ─── Write Queue ────────────────────────────────────────────────────────────
|
|
132
|
+
async function getWriteQueue(userId) {
|
|
133
|
+
try {
|
|
134
|
+
const raw = await (0, platform_js_1.getPlatform)().storage.getItem(writeQueueKey(userId));
|
|
135
|
+
if (!raw)
|
|
136
|
+
return [];
|
|
137
|
+
return JSON.parse(raw);
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
return [];
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async function addToWriteQueue(userId, write) {
|
|
144
|
+
try {
|
|
145
|
+
const queue = await getWriteQueue(userId);
|
|
146
|
+
queue.push({ ...write, retries: 0, createdAt: new Date().toISOString() });
|
|
147
|
+
await (0, platform_js_1.getPlatform)().storage.setItem(writeQueueKey(userId), JSON.stringify(queue));
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
// ignore
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
async function removeFromWriteQueue(userId, writeId) {
|
|
154
|
+
try {
|
|
155
|
+
const queue = await getWriteQueue(userId);
|
|
156
|
+
const updated = queue.filter(w => w.id !== writeId);
|
|
157
|
+
await (0, platform_js_1.getPlatform)().storage.setItem(writeQueueKey(userId), JSON.stringify(updated));
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
// ignore
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async function incrementWriteRetry(userId, writeId) {
|
|
164
|
+
try {
|
|
165
|
+
const queue = await getWriteQueue(userId);
|
|
166
|
+
const updated = queue.map(w => w.id === writeId ? { ...w, retries: w.retries + 1 } : w);
|
|
167
|
+
// Drop writes that have exceeded 3 retries
|
|
168
|
+
const filtered = updated.filter(w => w.retries <= 3);
|
|
169
|
+
await (0, platform_js_1.getPlatform)().storage.setItem(writeQueueKey(userId), JSON.stringify(filtered));
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
// ignore
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// ─── Optimistic cache update ─────────────────────────────────────────────────
|
|
176
|
+
async function optimisticallyInsert(userId, collection, record) {
|
|
177
|
+
try {
|
|
178
|
+
const keys = await (0, platform_js_1.getPlatform)().storage.getAllKeys();
|
|
179
|
+
const prefix = `koolbase:${CACHE_VERSION}:${userId}:${collection}:`;
|
|
180
|
+
const collectionKeys = keys.filter(k => k.startsWith(prefix));
|
|
181
|
+
for (const key of collectionKeys) {
|
|
182
|
+
const raw = await (0, platform_js_1.getPlatform)().storage.getItem(key);
|
|
183
|
+
if (!raw)
|
|
184
|
+
continue;
|
|
185
|
+
const cached = JSON.parse(raw);
|
|
186
|
+
cached.records = [record, ...cached.records];
|
|
187
|
+
cached.total = cached.total + 1;
|
|
188
|
+
await (0, platform_js_1.getPlatform)().storage.setItem(key, JSON.stringify(cached));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
// ignore
|
|
193
|
+
}
|
|
194
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { ConflictReason } from './offline-state.js';
|
|
2
|
+
/** Resolves conflicts by id. */
|
|
3
|
+
export interface ConflictResolver {
|
|
4
|
+
resolveWithLocal(conflictId: string): Promise<void>;
|
|
5
|
+
resolveWithServer(conflictId: string): Promise<void>;
|
|
6
|
+
resolveWithMerge(conflictId: string, data: Record<string, unknown>): Promise<void>;
|
|
7
|
+
abandon(conflictId: string): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A queued offline write that could not be applied, waiting for a decision.
|
|
11
|
+
*
|
|
12
|
+
* Not an error to dismiss and not a write to retry: retrying cannot help, and
|
|
13
|
+
* discarding it would lose a change the user believes is saved. It waits, and
|
|
14
|
+
* keeps waiting across restarts, until the application decides.
|
|
15
|
+
*
|
|
16
|
+
* Only the application can decide. Whether a later edit should win depends on
|
|
17
|
+
* what the data means, and a platform that chooses for everyone is wrong for
|
|
18
|
+
* someone.
|
|
19
|
+
*/
|
|
20
|
+
export declare class KoolbaseConflict {
|
|
21
|
+
readonly id: string;
|
|
22
|
+
readonly reason: ConflictReason;
|
|
23
|
+
readonly operation: 'insert' | 'update' | 'delete';
|
|
24
|
+
readonly collection: string;
|
|
25
|
+
readonly recordId: string;
|
|
26
|
+
/** The change the user made, still unapplied. */
|
|
27
|
+
readonly local: Record<string, unknown> | undefined;
|
|
28
|
+
/** The record as it was when the change was composed, where that is known. */
|
|
29
|
+
readonly baseline: Record<string, unknown> | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* The record as the server held it when the write was refused, captured with
|
|
32
|
+
* the refusal so deciding needs no fetch and cannot race one.
|
|
33
|
+
*
|
|
34
|
+
* Undefined when the reason is `baseline_unavailable` — nothing was ever
|
|
35
|
+
* sent, so the server never answered.
|
|
36
|
+
*/
|
|
37
|
+
readonly server: Record<string, unknown> | undefined;
|
|
38
|
+
readonly baseRevision: number | undefined;
|
|
39
|
+
readonly serverRevision: number | undefined;
|
|
40
|
+
readonly createdAt: string;
|
|
41
|
+
private readonly resolver;
|
|
42
|
+
constructor(id: string, reason: ConflictReason, operation: 'insert' | 'update' | 'delete', collection: string, recordId: string,
|
|
43
|
+
/** The change the user made, still unapplied. */
|
|
44
|
+
local: Record<string, unknown> | undefined,
|
|
45
|
+
/** The record as it was when the change was composed, where that is known. */
|
|
46
|
+
baseline: Record<string, unknown> | undefined,
|
|
47
|
+
/**
|
|
48
|
+
* The record as the server held it when the write was refused, captured with
|
|
49
|
+
* the refusal so deciding needs no fetch and cannot race one.
|
|
50
|
+
*
|
|
51
|
+
* Undefined when the reason is `baseline_unavailable` — nothing was ever
|
|
52
|
+
* sent, so the server never answered.
|
|
53
|
+
*/
|
|
54
|
+
server: Record<string, unknown> | undefined, baseRevision: number | undefined, serverRevision: number | undefined, createdAt: string, resolver: ConflictResolver);
|
|
55
|
+
/**
|
|
56
|
+
* Fields where the user's change and the server's version disagree.
|
|
57
|
+
*
|
|
58
|
+
* Only the fields the change touches: a record accumulates values the write
|
|
59
|
+
* never asserted, and listing those would bury the real disagreement. Empty
|
|
60
|
+
* when there is no server version to compare against.
|
|
61
|
+
*/
|
|
62
|
+
get divergentFields(): string[];
|
|
63
|
+
/** How long this has been waiting. Metadata, not a deletion rule. */
|
|
64
|
+
get ageMs(): number;
|
|
65
|
+
/**
|
|
66
|
+
* Reapplies the user's change to the record as it stands now.
|
|
67
|
+
*
|
|
68
|
+
* An explicit decision to overwrite the server's version of the fields that
|
|
69
|
+
* disagree. Conditional where a revision is known, so a record that moved
|
|
70
|
+
* again while someone was deciding produces a new conflict rather than an
|
|
71
|
+
* unnoticed overwrite.
|
|
72
|
+
*/
|
|
73
|
+
resolveWithLocal(): Promise<void>;
|
|
74
|
+
/** Keeps the server's version and discards the user's change, as a decision. */
|
|
75
|
+
resolveWithServer(): Promise<void>;
|
|
76
|
+
/** Applies something the application composed from both versions. */
|
|
77
|
+
resolveWithMerge(data: Record<string, unknown>): Promise<void>;
|
|
78
|
+
/** Drops the change without claiming either version won. */
|
|
79
|
+
abandon(): Promise<void>;
|
|
80
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KoolbaseConflict = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* A queued offline write that could not be applied, waiting for a decision.
|
|
6
|
+
*
|
|
7
|
+
* Not an error to dismiss and not a write to retry: retrying cannot help, and
|
|
8
|
+
* discarding it would lose a change the user believes is saved. It waits, and
|
|
9
|
+
* keeps waiting across restarts, until the application decides.
|
|
10
|
+
*
|
|
11
|
+
* Only the application can decide. Whether a later edit should win depends on
|
|
12
|
+
* what the data means, and a platform that chooses for everyone is wrong for
|
|
13
|
+
* someone.
|
|
14
|
+
*/
|
|
15
|
+
class KoolbaseConflict {
|
|
16
|
+
constructor(id, reason, operation, collection, recordId,
|
|
17
|
+
/** The change the user made, still unapplied. */
|
|
18
|
+
local,
|
|
19
|
+
/** The record as it was when the change was composed, where that is known. */
|
|
20
|
+
baseline,
|
|
21
|
+
/**
|
|
22
|
+
* The record as the server held it when the write was refused, captured with
|
|
23
|
+
* the refusal so deciding needs no fetch and cannot race one.
|
|
24
|
+
*
|
|
25
|
+
* Undefined when the reason is `baseline_unavailable` — nothing was ever
|
|
26
|
+
* sent, so the server never answered.
|
|
27
|
+
*/
|
|
28
|
+
server, baseRevision, serverRevision, createdAt, resolver) {
|
|
29
|
+
this.id = id;
|
|
30
|
+
this.reason = reason;
|
|
31
|
+
this.operation = operation;
|
|
32
|
+
this.collection = collection;
|
|
33
|
+
this.recordId = recordId;
|
|
34
|
+
this.local = local;
|
|
35
|
+
this.baseline = baseline;
|
|
36
|
+
this.server = server;
|
|
37
|
+
this.baseRevision = baseRevision;
|
|
38
|
+
this.serverRevision = serverRevision;
|
|
39
|
+
this.createdAt = createdAt;
|
|
40
|
+
this.resolver = resolver;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Fields where the user's change and the server's version disagree.
|
|
44
|
+
*
|
|
45
|
+
* Only the fields the change touches: a record accumulates values the write
|
|
46
|
+
* never asserted, and listing those would bury the real disagreement. Empty
|
|
47
|
+
* when there is no server version to compare against.
|
|
48
|
+
*/
|
|
49
|
+
get divergentFields() {
|
|
50
|
+
if (!this.local)
|
|
51
|
+
return [];
|
|
52
|
+
if (!this.server)
|
|
53
|
+
return Object.keys(this.local);
|
|
54
|
+
return Object.keys(this.local).filter((k) => JSON.stringify(this.server[k]) !== JSON.stringify(this.local[k]));
|
|
55
|
+
}
|
|
56
|
+
/** How long this has been waiting. Metadata, not a deletion rule. */
|
|
57
|
+
get ageMs() {
|
|
58
|
+
return Date.now() - new Date(this.createdAt).getTime();
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Reapplies the user's change to the record as it stands now.
|
|
62
|
+
*
|
|
63
|
+
* An explicit decision to overwrite the server's version of the fields that
|
|
64
|
+
* disagree. Conditional where a revision is known, so a record that moved
|
|
65
|
+
* again while someone was deciding produces a new conflict rather than an
|
|
66
|
+
* unnoticed overwrite.
|
|
67
|
+
*/
|
|
68
|
+
resolveWithLocal() {
|
|
69
|
+
return this.resolver.resolveWithLocal(this.id);
|
|
70
|
+
}
|
|
71
|
+
/** Keeps the server's version and discards the user's change, as a decision. */
|
|
72
|
+
resolveWithServer() {
|
|
73
|
+
return this.resolver.resolveWithServer(this.id);
|
|
74
|
+
}
|
|
75
|
+
/** Applies something the application composed from both versions. */
|
|
76
|
+
resolveWithMerge(data) {
|
|
77
|
+
return this.resolver.resolveWithMerge(this.id, data);
|
|
78
|
+
}
|
|
79
|
+
/** Drops the change without claiming either version won. */
|
|
80
|
+
abandon() {
|
|
81
|
+
return this.resolver.abandon(this.id);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.KoolbaseConflict = KoolbaseConflict;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { KoolbaseError } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Base class for errors surfaced by the Koolbase data layer (database reads
|
|
4
|
+
* and writes). Every data error carries a `message` and, when the server
|
|
5
|
+
* provides one, its stable `code` (e.g. `not_found`, `validation_error`,
|
|
6
|
+
* `unique_violation`).
|
|
7
|
+
*
|
|
8
|
+
* Catch this to handle any data-layer failure generically, or catch a
|
|
9
|
+
* specific subclass to branch on the kind of failure.
|
|
10
|
+
*/
|
|
11
|
+
export declare class KoolbaseDataError extends KoolbaseError {
|
|
12
|
+
/**
|
|
13
|
+
* Structured payload from the server's error body, when it sent one — e.g. a
|
|
14
|
+
* revision_mismatch 409 carries {expected_revision, current_revision,
|
|
15
|
+
* record}. Attached by the factory; absent when the body had none.
|
|
16
|
+
*/
|
|
17
|
+
details?: Record<string, unknown>;
|
|
18
|
+
constructor(message: string, code?: string);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Thrown when a write is rejected because the value would violate a
|
|
22
|
+
* collection's unique constraint — the server responds with 409 Conflict.
|
|
23
|
+
* Catch it to handle duplicates, e.g. an email or username already in use.
|
|
24
|
+
*
|
|
25
|
+
* `field` names the field that collided, when the server reports it
|
|
26
|
+
* (`details.field`) — useful when a collection has more than one unique
|
|
27
|
+
* constraint and you need to know which value clashed.
|
|
28
|
+
*
|
|
29
|
+
* Surfaced by `insert`, `update`, and `upsert` whenever the server is
|
|
30
|
+
* reachable and rejects the write with a 409. These writes are online-first:
|
|
31
|
+
* a server-side conflict throws immediately. Only a genuine network failure
|
|
32
|
+
* falls back to the offline queue, where a conflict that surfaces at sync
|
|
33
|
+
* time is handled by the sync engine rather than thrown here.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* try {
|
|
37
|
+
* await koolbase.db.upsert('users', { email }, { name });
|
|
38
|
+
* } catch (e) {
|
|
39
|
+
* if (e instanceof KoolbaseConflictError) {
|
|
40
|
+
* showError(`That ${e.field ?? 'value'} is already registered.`);
|
|
41
|
+
* }
|
|
42
|
+
* }
|
|
43
|
+
*/
|
|
44
|
+
export declare class KoolbaseConflictError extends KoolbaseDataError {
|
|
45
|
+
field?: string;
|
|
46
|
+
constructor(message?: string, field?: string);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Thrown when the requested record or collection does not exist — the server
|
|
50
|
+
* responds with 404 and code `not_found` / `record_not_found` /
|
|
51
|
+
* `collection_not_found`.
|
|
52
|
+
*/
|
|
53
|
+
export declare class KoolbaseNotFoundError extends KoolbaseDataError {
|
|
54
|
+
constructor(message?: string);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Thrown when the request is rejected as invalid — the server responds with
|
|
58
|
+
* 400 and code `validation_error`.
|
|
59
|
+
*/
|
|
60
|
+
export declare class KoolbaseValidationError extends KoolbaseDataError {
|
|
61
|
+
constructor(message?: string);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Thrown when the caller is authenticated but not allowed to perform the
|
|
65
|
+
* operation — the server responds with 403 and code `permission_denied`
|
|
66
|
+
* (typically a collection access rule rejecting the read/write).
|
|
67
|
+
*/
|
|
68
|
+
export declare class KoolbasePermissionError extends KoolbaseDataError {
|
|
69
|
+
constructor(message?: string);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Thrown when the server is rate-limiting the caller — 429 with code
|
|
73
|
+
* `rate_limit`. Back off and retry after a short delay.
|
|
74
|
+
*/
|
|
75
|
+
export declare class KoolbaseRateLimitError extends KoolbaseDataError {
|
|
76
|
+
constructor(message?: string);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Thrown when the supplied vector's length does not match the dimension
|
|
80
|
+
* declared on the collection's vector field — the server responds with
|
|
81
|
+
* 400 and code `vector_dimension_mismatch`. The message includes both
|
|
82
|
+
* the expected and actual dimensions so you can surface a precise error.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* try {
|
|
86
|
+
* await koolbase.db.setVector(id, 'embedding', [0.1, 0.2]); // 2 dims
|
|
87
|
+
* } catch (e) {
|
|
88
|
+
* if (e instanceof KoolbaseVectorDimensionMismatchError) {
|
|
89
|
+
* showError(e.message); // "expected 1536, got 2"
|
|
90
|
+
* }
|
|
91
|
+
* }
|
|
92
|
+
*/
|
|
93
|
+
export declare class KoolbaseVectorDimensionMismatchError extends KoolbaseDataError {
|
|
94
|
+
constructor(message?: string);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Maps a non-2xx data-layer response to a typed {@link KoolbaseDataError},
|
|
98
|
+
* preferring the server's stable `code` and falling back to the HTTP status
|
|
99
|
+
* for older or uncoded responses. Always returns an error to throw.
|
|
100
|
+
*/
|
|
101
|
+
export declare function koolbaseDataError(status: number, body: any, fallbackMessage?: string): KoolbaseError;
|