@koolbase/core 12.31.0 → 12.32.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/dist/cjs/auth.js +13 -1
- package/dist/cjs/collection-controller.d.ts +8 -0
- package/dist/cjs/collection-controller.js +6 -0
- package/dist/cjs/database-errors.js +15 -0
- package/dist/cjs/database.js +19 -3
- package/dist/cjs/errors.d.ts +14 -0
- package/dist/cjs/errors.js +32 -1
- package/dist/cjs/fiscal.js +7 -1
- package/dist/cjs/function-errors.d.ts +1 -1
- package/dist/cjs/function-errors.js +6 -1
- package/dist/cjs/functions.js +2 -2
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/network.d.ts +2 -1
- package/dist/cjs/network.js +60 -7
- package/dist/cjs/query-builder.d.ts +119 -0
- package/dist/cjs/query-builder.js +136 -0
- package/dist/cjs/storage-errors.js +4 -0
- package/dist/cjs/sync-engine.js +5 -2
- package/dist/cjs/types.d.ts +10 -0
- package/dist/esm/auth.js +13 -1
- package/dist/esm/collection-controller.d.ts +8 -0
- package/dist/esm/collection-controller.js +6 -0
- package/dist/esm/database-errors.js +15 -0
- package/dist/esm/database.js +19 -3
- package/dist/esm/errors.d.ts +14 -0
- package/dist/esm/errors.js +29 -0
- package/dist/esm/fiscal.js +7 -1
- package/dist/esm/function-errors.d.ts +1 -1
- package/dist/esm/function-errors.js +6 -1
- package/dist/esm/functions.js +2 -2
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/network.d.ts +2 -1
- package/dist/esm/network.js +60 -7
- package/dist/esm/query-builder.d.ts +119 -0
- package/dist/esm/query-builder.js +130 -0
- package/dist/esm/storage-errors.js +4 -0
- package/dist/esm/sync-engine.js +5 -2
- package/dist/esm/types.d.ts +10 -0
- package/package.json +1 -1
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The typed query (#165): which records to read, written as a tree the
|
|
3
|
+
* server validates against the collection's declared fields and runs after
|
|
4
|
+
* its read rule, so a filter can only narrow what the caller may read.
|
|
5
|
+
* Same names and the same JSON as the Flutter SDK's KbWhere.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* await Koolbase.db.query('events', {
|
|
9
|
+
* where: KbWhere.and([
|
|
10
|
+
* KbField('starts_at').after(KbValue.now()),
|
|
11
|
+
* KbWhere.or([KbField('category').isIn(['music', 'food']), KbField('price').eq(0)]),
|
|
12
|
+
* ]),
|
|
13
|
+
* sort: [KbSort.desc('featured'), KbSort.asc('starts_at')],
|
|
14
|
+
* });
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Operators follow the field's declared type; the server refuses one the
|
|
18
|
+
* type does not take (`query_operator_invalid`), or a range or date operator
|
|
19
|
+
* on an undeclared field (`query_field_untyped`). Limits, the server's:
|
|
20
|
+
* groups 3 deep (the outer group is 1), 20 conditions, 100 values in a list.
|
|
21
|
+
*/
|
|
22
|
+
export type KbValueJson = {
|
|
23
|
+
kind: 'literal';
|
|
24
|
+
value: unknown;
|
|
25
|
+
} | {
|
|
26
|
+
kind: 'relative_time';
|
|
27
|
+
preset: string;
|
|
28
|
+
offset?: {
|
|
29
|
+
amount: number;
|
|
30
|
+
unit: 'minutes' | 'hours' | 'days';
|
|
31
|
+
};
|
|
32
|
+
} | {
|
|
33
|
+
kind: 'caller';
|
|
34
|
+
path: string;
|
|
35
|
+
} | {
|
|
36
|
+
kind: 'range';
|
|
37
|
+
from: KbValueJson;
|
|
38
|
+
to: KbValueJson;
|
|
39
|
+
};
|
|
40
|
+
export type KbWhereNode = {
|
|
41
|
+
op: 'and' | 'or';
|
|
42
|
+
conditions: KbWhereNode[];
|
|
43
|
+
} | {
|
|
44
|
+
field: string;
|
|
45
|
+
operator: string;
|
|
46
|
+
value?: KbValueJson;
|
|
47
|
+
};
|
|
48
|
+
export interface KbSortField {
|
|
49
|
+
field: string;
|
|
50
|
+
dir: 'asc' | 'desc';
|
|
51
|
+
}
|
|
52
|
+
export declare const KbWhere: {
|
|
53
|
+
/** Every condition must hold. */
|
|
54
|
+
and: (all: KbWhereNode[]) => KbWhereNode;
|
|
55
|
+
/** At least one condition must hold. */
|
|
56
|
+
or: (any: KbWhereNode[]) => KbWhereNode;
|
|
57
|
+
};
|
|
58
|
+
export declare const KbValue: {
|
|
59
|
+
/** v as an operand: a KbValueJson as is; a Date as an instant (UTC, ISO 8601); a list item by item. */
|
|
60
|
+
of(v: unknown): KbValueJson;
|
|
61
|
+
/** The server's clock now, optionally moved. */
|
|
62
|
+
now(offset?: {
|
|
63
|
+
minutes?: number;
|
|
64
|
+
hours?: number;
|
|
65
|
+
}): KbValueJson;
|
|
66
|
+
/** The start of today in the query's zone, optionally `days` later (calendar days). */
|
|
67
|
+
startOfToday(opts?: {
|
|
68
|
+
days?: number;
|
|
69
|
+
}): KbValueJson;
|
|
70
|
+
startOfTomorrow: () => KbValueJson;
|
|
71
|
+
/** Weeks start on Monday. */
|
|
72
|
+
startOfWeek: () => KbValueJson;
|
|
73
|
+
startOfNextWeek: () => KbValueJson;
|
|
74
|
+
startOfMonth: () => KbValueJson;
|
|
75
|
+
/** The signed-in user's id, from the session. Refused (caller_required) signed out. */
|
|
76
|
+
callerUserId: () => KbValueJson;
|
|
77
|
+
/** One of the signed-in user's claims, as the server set it. Refused (caller_claim_missing) without it. */
|
|
78
|
+
callerClaim: (name: string) => KbValueJson;
|
|
79
|
+
};
|
|
80
|
+
/** A record field to filter on: a declared field, or id, created_at, updated_at, created_by. */
|
|
81
|
+
export declare function KbField(name: string): {
|
|
82
|
+
eq: (v: unknown) => KbWhereNode;
|
|
83
|
+
neq: (v: unknown) => KbWhereNode;
|
|
84
|
+
gt: (v: unknown) => KbWhereNode;
|
|
85
|
+
gte: (v: unknown) => KbWhereNode;
|
|
86
|
+
lt: (v: unknown) => KbWhereNode;
|
|
87
|
+
lte: (v: unknown) => KbWhereNode;
|
|
88
|
+
contains: (text: string) => KbWhereNode;
|
|
89
|
+
startsWith: (text: string) => KbWhereNode;
|
|
90
|
+
/** One of the values: a list, or the caller's claim that holds one. */
|
|
91
|
+
isIn: (values: unknown) => KbWhereNode;
|
|
92
|
+
notIn: (values: unknown) => KbWhereNode;
|
|
93
|
+
/** Inclusive at both ends. */
|
|
94
|
+
between: (from: unknown, to: unknown) => KbWhereNode;
|
|
95
|
+
before: (when: unknown) => KbWhereNode;
|
|
96
|
+
after: (when: unknown) => KbWhereNode;
|
|
97
|
+
onOrBefore: (when: unknown) => KbWhereNode;
|
|
98
|
+
onOrAfter: (when: unknown) => KbWhereNode;
|
|
99
|
+
/** The calendar day of `when` in the query's zone. */
|
|
100
|
+
on: (when: unknown) => KbWhereNode;
|
|
101
|
+
/** No value: missing, null or an empty list. 0, false and an empty text are values. */
|
|
102
|
+
isEmpty: () => KbWhereNode;
|
|
103
|
+
isNotEmpty: () => KbWhereNode;
|
|
104
|
+
isTrue: () => KbWhereNode;
|
|
105
|
+
isFalse: () => KbWhereNode;
|
|
106
|
+
};
|
|
107
|
+
/** One sort field. Typed by the declaration; empty values last both ways; the id breaks ties. */
|
|
108
|
+
export declare const KbSort: {
|
|
109
|
+
asc: (field: string) => KbSortField;
|
|
110
|
+
desc: (field: string) => KbSortField;
|
|
111
|
+
};
|
|
112
|
+
/** Whether options speak #165's language. */
|
|
113
|
+
export declare function isTypedQuery(o: {
|
|
114
|
+
where?: unknown;
|
|
115
|
+
sort?: unknown[];
|
|
116
|
+
timezone?: string;
|
|
117
|
+
}): boolean;
|
|
118
|
+
/** The device's IANA zone ("Africa/Accra"), or undefined when the runtime cannot say -- the query then omits it and the server uses UTC. */
|
|
119
|
+
export declare function deviceZone(): string | undefined;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The typed query (#165): which records to read, written as a tree the
|
|
3
|
+
* server validates against the collection's declared fields and runs after
|
|
4
|
+
* its read rule, so a filter can only narrow what the caller may read.
|
|
5
|
+
* Same names and the same JSON as the Flutter SDK's KbWhere.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* await Koolbase.db.query('events', {
|
|
9
|
+
* where: KbWhere.and([
|
|
10
|
+
* KbField('starts_at').after(KbValue.now()),
|
|
11
|
+
* KbWhere.or([KbField('category').isIn(['music', 'food']), KbField('price').eq(0)]),
|
|
12
|
+
* ]),
|
|
13
|
+
* sort: [KbSort.desc('featured'), KbSort.asc('starts_at')],
|
|
14
|
+
* });
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Operators follow the field's declared type; the server refuses one the
|
|
18
|
+
* type does not take (`query_operator_invalid`), or a range or date operator
|
|
19
|
+
* on an undeclared field (`query_field_untyped`). Limits, the server's:
|
|
20
|
+
* groups 3 deep (the outer group is 1), 20 conditions, 100 values in a list.
|
|
21
|
+
*/
|
|
22
|
+
function group(op, conditions) {
|
|
23
|
+
if (conditions.length === 0)
|
|
24
|
+
throw new TypeError('a group needs at least one condition');
|
|
25
|
+
return { op, conditions: [...conditions] };
|
|
26
|
+
}
|
|
27
|
+
export const KbWhere = {
|
|
28
|
+
/** Every condition must hold. */
|
|
29
|
+
and: (all) => group('and', all),
|
|
30
|
+
/** At least one condition must hold. */
|
|
31
|
+
or: (any) => group('or', any),
|
|
32
|
+
};
|
|
33
|
+
function plain(v) {
|
|
34
|
+
return v instanceof Date ? v.toISOString() : v;
|
|
35
|
+
}
|
|
36
|
+
export const KbValue = {
|
|
37
|
+
/** v as an operand: a KbValueJson as is; a Date as an instant (UTC, ISO 8601); a list item by item. */
|
|
38
|
+
of(v) {
|
|
39
|
+
if (v !== null && typeof v === 'object' && !Array.isArray(v) && !(v instanceof Date) && 'kind' in v) {
|
|
40
|
+
return v;
|
|
41
|
+
}
|
|
42
|
+
if (Array.isArray(v))
|
|
43
|
+
return { kind: 'literal', value: v.map(plain) };
|
|
44
|
+
return { kind: 'literal', value: plain(v) };
|
|
45
|
+
},
|
|
46
|
+
/** The server's clock now, optionally moved. */
|
|
47
|
+
now(offset = {}) {
|
|
48
|
+
const m = (offset.hours ?? 0) * 60 + (offset.minutes ?? 0);
|
|
49
|
+
if (m === 0)
|
|
50
|
+
return { kind: 'relative_time', preset: 'now' };
|
|
51
|
+
return {
|
|
52
|
+
kind: 'relative_time',
|
|
53
|
+
preset: 'now',
|
|
54
|
+
offset: m % 60 === 0 ? { amount: m / 60, unit: 'hours' } : { amount: m, unit: 'minutes' },
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
/** The start of today in the query's zone, optionally `days` later (calendar days). */
|
|
58
|
+
startOfToday(opts = {}) {
|
|
59
|
+
return opts.days
|
|
60
|
+
? { kind: 'relative_time', preset: 'start_of_today', offset: { amount: opts.days, unit: 'days' } }
|
|
61
|
+
: { kind: 'relative_time', preset: 'start_of_today' };
|
|
62
|
+
},
|
|
63
|
+
startOfTomorrow: () => ({ kind: 'relative_time', preset: 'start_of_tomorrow' }),
|
|
64
|
+
/** Weeks start on Monday. */
|
|
65
|
+
startOfWeek: () => ({ kind: 'relative_time', preset: 'start_of_week' }),
|
|
66
|
+
startOfNextWeek: () => ({ kind: 'relative_time', preset: 'start_of_next_week' }),
|
|
67
|
+
startOfMonth: () => ({ kind: 'relative_time', preset: 'start_of_month' }),
|
|
68
|
+
/** The signed-in user's id, from the session. Refused (caller_required) signed out. */
|
|
69
|
+
callerUserId: () => ({ kind: 'caller', path: 'user_id' }),
|
|
70
|
+
/** One of the signed-in user's claims, as the server set it. Refused (caller_claim_missing) without it. */
|
|
71
|
+
callerClaim: (name) => ({ kind: 'caller', path: `claims.${name}` }),
|
|
72
|
+
};
|
|
73
|
+
/** A record field to filter on: a declared field, or id, created_at, updated_at, created_by. */
|
|
74
|
+
export function KbField(name) {
|
|
75
|
+
const withValue = (operator, v) => {
|
|
76
|
+
if (v === null || v === undefined)
|
|
77
|
+
throw new TypeError(`${name}: no value; use isEmpty() for "has none"`);
|
|
78
|
+
return { field: name, operator, value: KbValue.of(v) };
|
|
79
|
+
};
|
|
80
|
+
const bare = (operator) => ({ field: name, operator });
|
|
81
|
+
return {
|
|
82
|
+
eq: (v) => withValue('eq', v),
|
|
83
|
+
neq: (v) => withValue('neq', v),
|
|
84
|
+
gt: (v) => withValue('gt', v),
|
|
85
|
+
gte: (v) => withValue('gte', v),
|
|
86
|
+
lt: (v) => withValue('lt', v),
|
|
87
|
+
lte: (v) => withValue('lte', v),
|
|
88
|
+
contains: (text) => withValue('contains', text),
|
|
89
|
+
startsWith: (text) => withValue('starts_with', text),
|
|
90
|
+
/** One of the values: a list, or the caller's claim that holds one. */
|
|
91
|
+
isIn: (values) => withValue('in', values),
|
|
92
|
+
notIn: (values) => withValue('not_in', values),
|
|
93
|
+
/** Inclusive at both ends. */
|
|
94
|
+
between: (from, to) => ({
|
|
95
|
+
field: name,
|
|
96
|
+
operator: 'between',
|
|
97
|
+
value: { kind: 'range', from: KbValue.of(from), to: KbValue.of(to) },
|
|
98
|
+
}),
|
|
99
|
+
before: (when) => withValue('before', when),
|
|
100
|
+
after: (when) => withValue('after', when),
|
|
101
|
+
onOrBefore: (when) => withValue('on_or_before', when),
|
|
102
|
+
onOrAfter: (when) => withValue('on_or_after', when),
|
|
103
|
+
/** The calendar day of `when` in the query's zone. */
|
|
104
|
+
on: (when) => withValue('on', when),
|
|
105
|
+
/** No value: missing, null or an empty list. 0, false and an empty text are values. */
|
|
106
|
+
isEmpty: () => bare('is_empty'),
|
|
107
|
+
isNotEmpty: () => bare('is_not_empty'),
|
|
108
|
+
isTrue: () => bare('is_true'),
|
|
109
|
+
isFalse: () => bare('is_false'),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/** One sort field. Typed by the declaration; empty values last both ways; the id breaks ties. */
|
|
113
|
+
export const KbSort = {
|
|
114
|
+
asc: (field) => ({ field, dir: 'asc' }),
|
|
115
|
+
desc: (field) => ({ field, dir: 'desc' }),
|
|
116
|
+
};
|
|
117
|
+
/** Whether options speak #165's language. */
|
|
118
|
+
export function isTypedQuery(o) {
|
|
119
|
+
return o.where !== undefined || (o.sort !== undefined && o.sort.length > 0) || (o.timezone ?? '') !== '';
|
|
120
|
+
}
|
|
121
|
+
/** The device's IANA zone ("Africa/Accra"), or undefined when the runtime cannot say -- the query then omits it and the server uses UTC. */
|
|
122
|
+
export function deviceZone() {
|
|
123
|
+
try {
|
|
124
|
+
const zone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
125
|
+
return typeof zone === 'string' && zone !== '' && zone !== 'Etc/Unknown' ? zone : undefined;
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { KoolbaseInvalidPublicKeyError } from './errors';
|
|
1
2
|
import { KoolbaseError, KoolbasePlanLimitError, KoolbaseServiceUnavailableError, KoolbaseUnauthenticatedError } from './errors.js';
|
|
2
3
|
/**
|
|
3
4
|
* Base error type for all Koolbase storage errors. Catchable via
|
|
@@ -442,6 +443,9 @@ export function koolbaseStorageError(status, body, fallbackMessage = 'Storage re
|
|
|
442
443
|
case 404:
|
|
443
444
|
return new KoolbaseStorageNotFoundError(message);
|
|
444
445
|
case 401:
|
|
446
|
+
// A refused app key is setup, not a session (#169): never signs out.
|
|
447
|
+
if (code === 'invalid_public_key')
|
|
448
|
+
return new KoolbaseInvalidPublicKeyError(message);
|
|
445
449
|
// A rejected credential is not a storage problem: a session stops
|
|
446
450
|
// working for the whole SDK at once, so it raises the shared type.
|
|
447
451
|
return new KoolbaseUnauthenticatedError(message);
|
package/dist/esm/sync-engine.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { invalidPublicKeyIn } from './errors';
|
|
1
2
|
import { sessionFetch } from './network.js';
|
|
2
3
|
import { readOfflineState, mutateOfflineState, migrateLegacyQueue, flushLockName, } from './offline-state.js';
|
|
3
4
|
import { KoolbaseUnauthenticatedError } from './errors.js';
|
|
@@ -291,8 +292,10 @@ export class SyncEngine {
|
|
|
291
292
|
: '';
|
|
292
293
|
res = await sessionFetch(`${url}${q}`, { method: 'DELETE', headers });
|
|
293
294
|
}
|
|
294
|
-
if (res.status === 401)
|
|
295
|
-
|
|
295
|
+
if (res.status === 401) {
|
|
296
|
+
// A refused app key is setup, not a session (#169): never signs out.
|
|
297
|
+
throw invalidPublicKeyIn(await res.clone().text()) ?? new KoolbaseUnauthenticatedError('unauthorized');
|
|
298
|
+
}
|
|
296
299
|
if (res.status === 409) {
|
|
297
300
|
const body = await res.json().catch(() => ({}));
|
|
298
301
|
if (body?.code === 'revision_mismatch') {
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { KbSortField, KbWhereNode } from './query-builder.js';
|
|
1
2
|
export interface KoolbaseConfig {
|
|
2
3
|
/**
|
|
3
4
|
* Host platform adapter. Defaults to React Native. A web package or a test
|
|
@@ -295,6 +296,15 @@ export interface QueryOptions {
|
|
|
295
296
|
populate?: string[];
|
|
296
297
|
/** A search over the fields it names, on the server (#160). Empty text: none. */
|
|
297
298
|
fieldSearch?: FieldSearch;
|
|
299
|
+
/**
|
|
300
|
+
* The typed query (#165): a where tree (KbWhere), a sort (KbSort, up to
|
|
301
|
+
* 3 fields) and the IANA zone its relative times resolve in -- the
|
|
302
|
+
* device's own unless named. Not combined with filters, orderBy or
|
|
303
|
+
* orderDesc: use one or the other.
|
|
304
|
+
*/
|
|
305
|
+
where?: KbWhereNode;
|
|
306
|
+
sort?: KbSortField[];
|
|
307
|
+
timezone?: string;
|
|
298
308
|
/**
|
|
299
309
|
* How to read: 'default' is cache-first, refreshed in the background;
|
|
300
310
|
* 'network-only' skips the cache and waits for the server (the result still
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koolbase/core",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.32.0",
|
|
4
4
|
"description": "Koolbase SDK core \u2014 shared behaviour behind @koolbase/react-native and @koolbase/js. Install one of those, not this.",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"types": "./dist/esm/index.d.ts",
|