@nocentraldb/sdk 0.2.0-alpha.5 → 0.2.0-alpha.6
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/connect.js +8 -6
- package/dist/dates.d.ts +15 -0
- package/dist/dates.js +67 -0
- package/dist/nc.d.ts +7 -2
- package/dist/nc.js +9 -2
- package/dist/texts.d.ts +4 -0
- package/dist/texts.js +4 -0
- package/dist/wasm/nocentraldb_wasm.js +3 -3
- package/dist/wasm/nocentraldb_wasm_bg.wasm +0 -0
- package/package.json +1 -1
package/dist/connect.js
CHANGED
|
@@ -9,6 +9,7 @@ import { accountOf } from "./accountCalls.js";
|
|
|
9
9
|
import { createAdHoc, lookupsOf, readObjects } from "./adHoc.js";
|
|
10
10
|
import { decode, encode } from "./cbor.js";
|
|
11
11
|
import { flatten, randomId, } from "./connectParts.js";
|
|
12
|
+
import { equalToCore, fromCore, toCore } from "./dates.js";
|
|
12
13
|
import { reportsOf } from "./durabilityReport.js";
|
|
13
14
|
import { SetElements } from "./elements.js";
|
|
14
15
|
import { erasureCallsOf, signOffOf, takeBackOf } from "./eraseCalls.js";
|
|
@@ -276,7 +277,7 @@ export const connect = async (core, options) => {
|
|
|
276
277
|
index: state.index,
|
|
277
278
|
};
|
|
278
279
|
if (state.equal !== undefined) {
|
|
279
|
-
asked.equal = state.equal.map((v) => typeof v === "string" ? v.normalize("NFC") : v);
|
|
280
|
+
asked.equal = state.equal.map((v) => typeof v === "string" ? v.normalize("NFC") : equalToCore(v));
|
|
280
281
|
}
|
|
281
282
|
if (state.take !== undefined)
|
|
282
283
|
asked.take = state.take;
|
|
@@ -294,7 +295,7 @@ export const connect = async (core, options) => {
|
|
|
294
295
|
args.space = spaceId;
|
|
295
296
|
const answer = (await ask("find", args));
|
|
296
297
|
return {
|
|
297
|
-
items: answer.items.map((item) => read.remember(flatten(item), asked.collection)),
|
|
298
|
+
items: answer.items.map((item) => read.remember(dated(asked.collection, flatten(item)), asked.collection)),
|
|
298
299
|
cursor: answer.cursor === null
|
|
299
300
|
? undefined
|
|
300
301
|
: new Cursor(identity, answer.cursor),
|
|
@@ -332,10 +333,11 @@ export const connect = async (core, options) => {
|
|
|
332
333
|
!(typeof written === "number" && Number.isSafeInteger(written))) {
|
|
333
334
|
throw new NcError("invalidValue", texts.notASafeInteger(collection, field));
|
|
334
335
|
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
return written;
|
|
336
|
+
const sent = toCore(kind, collection, field, written);
|
|
337
|
+
return typeof sent === "string" ? sent.normalize("NFC") : sent;
|
|
338
338
|
};
|
|
339
|
+
/** `object`, read from `collection`, with each point in time as a Date. */
|
|
340
|
+
const dated = (collection, object) => fromCore(described[collection]?.fields, object);
|
|
339
341
|
const values = (collection, written) => {
|
|
340
342
|
const out = {};
|
|
341
343
|
for (const [field, v] of Object.entries(written)) {
|
|
@@ -438,7 +440,7 @@ export const connect = async (core, options) => {
|
|
|
438
440
|
}));
|
|
439
441
|
return answer === null
|
|
440
442
|
? undefined
|
|
441
|
-
: read.remember(flatten(answer), nameOf(collection));
|
|
443
|
+
: read.remember(dated(nameOf(collection), flatten(answer)), nameOf(collection));
|
|
442
444
|
};
|
|
443
445
|
return {
|
|
444
446
|
spaces: {
|
package/dist/dates.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The form the bridge takes for `written` in a field of `type`: a day as
|
|
3
|
+
* its text, a point in time as its milliseconds; `invalidValue` for
|
|
4
|
+
* anything else. A value of another type passes as it is.
|
|
5
|
+
*/
|
|
6
|
+
export declare const toCore: (type: string | undefined, collection: string, field: string, written: unknown) => unknown;
|
|
7
|
+
/** A query's equality value as the bridge takes it: a Date as its milliseconds. */
|
|
8
|
+
export declare const equalToCore: (value: unknown) => unknown;
|
|
9
|
+
/**
|
|
10
|
+
* `object`, read from `collection`, with each point in time as a Date. The
|
|
11
|
+
* bridge gives a day as its text already.
|
|
12
|
+
*/
|
|
13
|
+
export declare const fromCore: <T extends Record<string, unknown>>(fields: Record<string, {
|
|
14
|
+
type: string;
|
|
15
|
+
}> | undefined, object: T) => T;
|
package/dist/dates.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Days and points in time between the app and the core (RFC 0005, "Days
|
|
2
|
+
// and points in time"; Carsten, 2026-11-21, NOC-434). The app writes and
|
|
3
|
+
// reads a day as its "YYYY-MM-DD" string and a point in time as a Date; the
|
|
4
|
+
// bridge takes a day as that text and a point in time as its milliseconds
|
|
5
|
+
// since 1970-01-01T00:00:00.000Z (crates/nocentraldb/src/bridge/values.rs).
|
|
6
|
+
// Both range over the years 1 to 9999. Internal: the package's entry does
|
|
7
|
+
// not reach it.
|
|
8
|
+
import { NcError } from "./error.js";
|
|
9
|
+
import * as texts from "./texts.js";
|
|
10
|
+
const DATE = "ncdb/date";
|
|
11
|
+
const TIMESTAMP = "ncdb/timestamp";
|
|
12
|
+
/** The first and the last millisecond of the years 1 to 9999. */
|
|
13
|
+
const FIRST = -62_135_596_800_000;
|
|
14
|
+
const LAST = 253_402_300_799_999;
|
|
15
|
+
/** Whether `text` is exactly "YYYY-MM-DD" and names a day of the years 1 to 9999. */
|
|
16
|
+
const isDay = (text) => {
|
|
17
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text);
|
|
18
|
+
if (match === null)
|
|
19
|
+
return false;
|
|
20
|
+
const [year, month, day] = match.slice(1).map(Number);
|
|
21
|
+
if (year < 1)
|
|
22
|
+
return false;
|
|
23
|
+
// Date.UTC maps years 0 to 99 onto 1900 to 1999; setUTCFullYear does not.
|
|
24
|
+
const at = new Date(0);
|
|
25
|
+
at.setUTCFullYear(year, month - 1, day);
|
|
26
|
+
return (at.getUTCFullYear() === year &&
|
|
27
|
+
at.getUTCMonth() === month - 1 &&
|
|
28
|
+
at.getUTCDate() === day);
|
|
29
|
+
};
|
|
30
|
+
/** Whether `value` is a valid Date of the years 1 to 9999. */
|
|
31
|
+
const isPoint = (value) => value instanceof Date &&
|
|
32
|
+
!Number.isNaN(value.getTime()) &&
|
|
33
|
+
value.getTime() >= FIRST &&
|
|
34
|
+
value.getTime() <= LAST;
|
|
35
|
+
/**
|
|
36
|
+
* The form the bridge takes for `written` in a field of `type`: a day as
|
|
37
|
+
* its text, a point in time as its milliseconds; `invalidValue` for
|
|
38
|
+
* anything else. A value of another type passes as it is.
|
|
39
|
+
*/
|
|
40
|
+
export const toCore = (type, collection, field, written) => {
|
|
41
|
+
if (type === DATE) {
|
|
42
|
+
if (typeof written === "string" && isDay(written))
|
|
43
|
+
return written;
|
|
44
|
+
throw new NcError("invalidValue", texts.notADay(collection, field));
|
|
45
|
+
}
|
|
46
|
+
if (type === TIMESTAMP) {
|
|
47
|
+
if (isPoint(written))
|
|
48
|
+
return written.getTime();
|
|
49
|
+
throw new NcError("invalidValue", texts.notATimestamp(collection, field));
|
|
50
|
+
}
|
|
51
|
+
return written;
|
|
52
|
+
};
|
|
53
|
+
/** A query's equality value as the bridge takes it: a Date as its milliseconds. */
|
|
54
|
+
export const equalToCore = (value) => value instanceof Date ? value.getTime() : value;
|
|
55
|
+
/**
|
|
56
|
+
* `object`, read from `collection`, with each point in time as a Date. The
|
|
57
|
+
* bridge gives a day as its text already.
|
|
58
|
+
*/
|
|
59
|
+
export const fromCore = (fields, object) => {
|
|
60
|
+
for (const [name, field] of Object.entries(fields ?? {})) {
|
|
61
|
+
const value = object[name];
|
|
62
|
+
if (field.type === TIMESTAMP && typeof value === "number") {
|
|
63
|
+
object[name] = new Date(value);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return object;
|
|
67
|
+
};
|
package/dist/nc.d.ts
CHANGED
|
@@ -68,7 +68,8 @@ export type Unknown = {
|
|
|
68
68
|
/**
|
|
69
69
|
* An object of the collection `C` as the app reads it (RFC 0005, "Types for
|
|
70
70
|
* the app"): every field optional, without the deprecated ones. A choice
|
|
71
|
-
* reads as its options or `Unknown`, an `nc.int` as a `number`,
|
|
71
|
+
* reads as its options or `Unknown`, an `nc.int` as a `number`, an
|
|
72
|
+
* `nc.date` as a `"YYYY-MM-DD"` string, an `nc.timestamp` as a `Date`, a set as
|
|
72
73
|
* an array, a link as an object id, a reference as `{ space, id }`
|
|
73
74
|
* (RFC 0009, "Get by id").
|
|
74
75
|
*/
|
|
@@ -120,7 +121,11 @@ export declare const float: () => Field<number>;
|
|
|
120
121
|
export declare const boolean: () => Field<boolean>;
|
|
121
122
|
/** A choice from a fixed list of options, basic value. */
|
|
122
123
|
export declare const oneOf: <const O extends readonly string[]>(options: O) => Field<O[number] | Unknown>;
|
|
123
|
-
/** A
|
|
124
|
+
/** A calendar day, basic value, read and written as a `"YYYY-MM-DD"` string, never converted to a time zone (RFC 0005, "Days and points in time"). */
|
|
125
|
+
export declare const date: () => Field<string>;
|
|
126
|
+
/** A point in time to the millisecond, in UTC, basic value, read and written as a `Date` (RFC 0005, "Days and points in time"). */
|
|
127
|
+
export declare const timestamp: () => Field<Date>;
|
|
128
|
+
/** A set of basic values, other than days and points in time. */
|
|
124
129
|
export declare const set: <T>(element: Field<T>) => Field<T[]>;
|
|
125
130
|
/** A link to one object, in one collection or in one of several. */
|
|
126
131
|
export declare const link: (target: string | readonly string[]) => Field<ObjectId>;
|
package/dist/nc.js
CHANGED
|
@@ -82,7 +82,11 @@ export const float = () => basic("ncdb/float");
|
|
|
82
82
|
export const boolean = () => basic("ncdb/boolean");
|
|
83
83
|
/** A choice from a fixed list of options, basic value. */
|
|
84
84
|
export const oneOf = (options) => basic("ncdb/choice", options);
|
|
85
|
-
/** A
|
|
85
|
+
/** A calendar day, basic value, read and written as a `"YYYY-MM-DD"` string, never converted to a time zone (RFC 0005, "Days and points in time"). */
|
|
86
|
+
export const date = () => basic("ncdb/date");
|
|
87
|
+
/** A point in time to the millisecond, in UTC, basic value, read and written as a `Date` (RFC 0005, "Days and points in time"). */
|
|
88
|
+
export const timestamp = () => basic("ncdb/timestamp");
|
|
89
|
+
/** A set of basic values, other than days and points in time. */
|
|
86
90
|
export const set = (element) => field({ spec: { kind: "set", of: element } });
|
|
87
91
|
/** A link to one object, in one collection or in one of several. */
|
|
88
92
|
export const link = (target) => field({
|
|
@@ -190,7 +194,10 @@ const describeType = (path, spec) => {
|
|
|
190
194
|
throw new Error(texts.reservedFieldType(path, spec.name));
|
|
191
195
|
case "set": {
|
|
192
196
|
const element = fieldStates.get(spec.of);
|
|
193
|
-
if (element === undefined ||
|
|
197
|
+
if (element === undefined ||
|
|
198
|
+
element.spec.kind !== "basic" ||
|
|
199
|
+
element.spec.type === "ncdb/date" ||
|
|
200
|
+
element.spec.type === "ncdb/timestamp") {
|
|
194
201
|
throw new Error(texts.setElementNotBasic(path));
|
|
195
202
|
}
|
|
196
203
|
if (element.person !== undefined ||
|
package/dist/texts.d.ts
CHANGED
|
@@ -30,6 +30,8 @@ export declare const notACollection: (collection: unknown) => string;
|
|
|
30
30
|
export declare const notAComputedField: (collection: unknown, key: unknown) => string;
|
|
31
31
|
/** `notADatabaseName` (invalidValue): nc.open, given a name that breaks RFC 0006's rule for a browser database's name. */
|
|
32
32
|
export declare const NOT_ADATABASE_NAME = "a database name is 1 to 64 characters of a-z, 0-9, - and _";
|
|
33
|
+
/** `notADay` (invalidValue): A write of an nc.date() field given anything but such a string, a Date included. */
|
|
34
|
+
export declare const notADay: (collection: unknown, field: unknown) => string;
|
|
33
35
|
/** `notAField`: ncdb model build, on a model whose field is not declared with a field function. */
|
|
34
36
|
export declare const notAField: (path: unknown) => string;
|
|
35
37
|
/** `notAPairingAnswer` (invalidValue): nc.pairingAnswer, given a text that does not start with ncdb-pair-1: or is not unpadded base64url. */
|
|
@@ -38,6 +40,8 @@ export declare const NOT_APAIRING_ANSWER = "the text is not a pairing answer, wh
|
|
|
38
40
|
export declare const NOT_AQUERY_START_ONE = "not a query; start one from an index, such as model.tasks.indexes.byPriority";
|
|
39
41
|
/** `notASafeInteger` (invalidValue): A write of an nc.int() field given a number that is not a safe integer. */
|
|
40
42
|
export declare const notASafeInteger: (collection: unknown, field: unknown) => string;
|
|
43
|
+
/** `notATimestamp` (invalidValue): A write of an nc.timestamp() field given anything but such a Date, a string or a number included. */
|
|
44
|
+
export declare const notATimestamp: (collection: unknown, field: unknown) => string;
|
|
41
45
|
/** `notAView`: ncdb model build, on a model whose view is not declared with nc.view. */
|
|
42
46
|
export declare const notAView: (collection: unknown, key: unknown) => string;
|
|
43
47
|
/** `notAnErasureRequest` (invalidValue): nc.erasureRequest, given a text that does not start with ncdb-erase-1: or is not unpadded base64url. */
|
package/dist/texts.js
CHANGED
|
@@ -36,6 +36,8 @@ export const notACollection = (collection) => `${collection}: not a collection;
|
|
|
36
36
|
export const notAComputedField = (collection, key) => `${collection}.${key}: not a computed field; declare it with nc.reach(...).`;
|
|
37
37
|
/** `notADatabaseName` (invalidValue): nc.open, given a name that breaks RFC 0006's rule for a browser database's name. */
|
|
38
38
|
export const NOT_ADATABASE_NAME = "a database name is 1 to 64 characters of a-z, 0-9, - and _";
|
|
39
|
+
/** `notADay` (invalidValue): A write of an nc.date() field given anything but such a string, a Date included. */
|
|
40
|
+
export const notADay = (collection, field) => `${collection}.${field} takes a day as a "YYYY-MM-DD" string, from 0001-01-01 to 9999-12-31`;
|
|
39
41
|
/** `notAField`: ncdb model build, on a model whose field is not declared with a field function. */
|
|
40
42
|
export const notAField = (path) => `${path}: not a field; declare it with a field function such as nc.string().`;
|
|
41
43
|
/** `notAPairingAnswer` (invalidValue): nc.pairingAnswer, given a text that does not start with ncdb-pair-1: or is not unpadded base64url. */
|
|
@@ -44,6 +46,8 @@ export const NOT_APAIRING_ANSWER = "the text is not a pairing answer, which is n
|
|
|
44
46
|
export const NOT_AQUERY_START_ONE = "not a query; start one from an index, such as model.tasks.indexes.byPriority";
|
|
45
47
|
/** `notASafeInteger` (invalidValue): A write of an nc.int() field given a number that is not a safe integer. */
|
|
46
48
|
export const notASafeInteger = (collection, field) => `${collection}.${field} takes a safe integer, between -(2^53 - 1) and 2^53 - 1`;
|
|
49
|
+
/** `notATimestamp` (invalidValue): A write of an nc.timestamp() field given anything but such a Date, a string or a number included. */
|
|
50
|
+
export const notATimestamp = (collection, field) => `${collection}.${field} takes a valid Date from the year 1 to 9999`;
|
|
47
51
|
/** `notAView`: ncdb model build, on a model whose view is not declared with nc.view. */
|
|
48
52
|
export const notAView = (collection, key) => `${collection}.${key}: not a view; declare it with nc.view(...).`;
|
|
49
53
|
/** `notAnErasureRequest` (invalidValue): nc.erasureRequest, given a text that does not start with ncdb-erase-1: or is not unpadded base64url. */
|
|
@@ -667,17 +667,17 @@ function __wbg_get_imports() {
|
|
|
667
667
|
return ret;
|
|
668
668
|
}, arguments); },
|
|
669
669
|
__wbindgen_generic_0000000000000001: function(arg0, arg1) {
|
|
670
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx:
|
|
670
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 151, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
|
|
671
671
|
const ret = makeMutClosure(arg0, arg1, wasm_bindgen_70735ce8c264698d___convert__closures_____invoke___wasm_bindgen_70735ce8c264698d___JsValue______true_);
|
|
672
672
|
return ret;
|
|
673
673
|
},
|
|
674
674
|
__wbindgen_generic_0000000000000002: function(arg0, arg1) {
|
|
675
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx:
|
|
675
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 952, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
|
|
676
676
|
const ret = makeMutClosure(arg0, arg1, wasm_bindgen_70735ce8c264698d___convert__closures_____invoke___wasm_bindgen_70735ce8c264698d___JsValue__core_37ee0ce0c4cd57d5___result__Result_____wasm_bindgen_70735ce8c264698d___JsError___true_);
|
|
677
677
|
return ret;
|
|
678
678
|
},
|
|
679
679
|
__wbindgen_generic_0000000000000003: function(arg0, arg1) {
|
|
680
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("Event")], shim_idx:
|
|
680
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("Event")], shim_idx: 151, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
|
|
681
681
|
const ret = makeMutClosure(arg0, arg1, wasm_bindgen_70735ce8c264698d___convert__closures_____invoke___wasm_bindgen_70735ce8c264698d___JsValue______true__7);
|
|
682
682
|
return ret;
|
|
683
683
|
},
|
|
Binary file
|