@based/schema 5.0.0-alpha.3 → 5.0.0-alpha.5

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.
@@ -0,0 +1,6 @@
1
+ export * from './types.js';
2
+ export * from './typeDef.js';
3
+ export * from './utils.js';
4
+ export * from './selvaBuffer.js';
5
+ export * from './readFromPacked.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,7 @@
1
+ // flap
2
+ export * from './types.js';
3
+ export * from './typeDef.js';
4
+ export * from './utils.js';
5
+ export * from './selvaBuffer.js';
6
+ export * from './readFromPacked.js';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ import { SchemaTypeDef } from './types.js';
2
+ export declare const readFromPacked: (packed: Uint8Array) => SchemaTypeDef;
3
+ //# sourceMappingURL=readFromPacked.d.ts.map
@@ -0,0 +1,126 @@
1
+ import { REVERSE_SIZE_MAP } from './types.js';
2
+ export const readFromPacked = (packed) => {
3
+ const size = (packed[0] | (packed[1] << 8)) >>> 0;
4
+ const props = [];
5
+ const b = packed.subarray(2, 2 + size);
6
+ let collectMain = false;
7
+ const mainProps = [];
8
+ const typeId = b.subarray(0, 2);
9
+ const typeIdNr = (typeId[0] | (typeId[1] << 8)) >>> 0;
10
+ for (let i = 2; i < b.length; i++) {
11
+ const prop = b[i];
12
+ if (collectMain) {
13
+ if (prop === 0) {
14
+ collectMain = false;
15
+ }
16
+ else {
17
+ mainProps.push({
18
+ prop: 0,
19
+ typeIndex: b[i],
20
+ });
21
+ }
22
+ }
23
+ else {
24
+ if (prop == 0) {
25
+ collectMain = true;
26
+ }
27
+ else {
28
+ props.push({ prop, typeIndex: b[i + 1] });
29
+ i++;
30
+ }
31
+ }
32
+ }
33
+ const decoder = new TextDecoder();
34
+ const fields = [];
35
+ const f = packed.subarray(2 + size, packed.length);
36
+ for (let i = 0; i < f.length; i++) {
37
+ const size = f[i];
38
+ fields.push(decoder.decode(f.subarray(i + 1, i + 1 + size)));
39
+ i += size;
40
+ }
41
+ for (let i = 0; i < mainProps.length; i++) {
42
+ mainProps[i].path = fields[i + 1];
43
+ }
44
+ for (let i = 0; i < props.length; i++) {
45
+ props[i].path = fields[i + 1 + mainProps.length];
46
+ }
47
+ // Fixed len strings not supported
48
+ // Refs also not supported
49
+ // Text not supported yet
50
+ // Ref not supported yet
51
+ // compression: 1 (0)
52
+ // YET
53
+ const result = {
54
+ cnt: 0,
55
+ checksum: 0,
56
+ total: 0,
57
+ type: fields[0],
58
+ lastId: 0,
59
+ blockCapacity: 0,
60
+ mainLen: mainProps.length,
61
+ buf: b,
62
+ propNames: f,
63
+ packed,
64
+ props: {},
65
+ reverseProps: {}, // in a bit
66
+ id: typeIdNr,
67
+ idUint8: typeId,
68
+ separate: [],
69
+ main: {},
70
+ tree: {},
71
+ // not nessecary...
72
+ hasSeperateSort: false,
73
+ seperateSort: {
74
+ size: 0,
75
+ buffer: new Uint8Array([]),
76
+ bufferTmp: new Uint8Array([]),
77
+ props: [],
78
+ },
79
+ hasSeperateTextSort: false,
80
+ seperateTextSort: {
81
+ size: 0,
82
+ buffer: new Uint8Array([]),
83
+ bufferTmp: new Uint8Array([]),
84
+ props: [],
85
+ },
86
+ // need this...
87
+ locales: {},
88
+ localeSize: 0,
89
+ };
90
+ let s = 0;
91
+ for (const p of mainProps) {
92
+ const len = REVERSE_SIZE_MAP[p.typeIndex];
93
+ result.props[p.path] = {
94
+ prop: p.prop,
95
+ separate: false,
96
+ __isPropDef: true,
97
+ start: s,
98
+ typeIndex: p.typeIndex,
99
+ path: p.path.split('.'),
100
+ len,
101
+ };
102
+ s += len;
103
+ }
104
+ for (const p of props) {
105
+ result.props[p.path] = {
106
+ prop: p.prop,
107
+ separate: true,
108
+ __isPropDef: true,
109
+ start: 0,
110
+ typeIndex: p.typeIndex,
111
+ path: p.path.split('.'),
112
+ len: 0,
113
+ compression: 1,
114
+ };
115
+ }
116
+ // make this into a typeDef
117
+ // return {
118
+ // type: fields[0],
119
+ // fields,
120
+ // typeId,
121
+ // props,
122
+ // mainProps,
123
+ // }
124
+ return result;
125
+ };
126
+ //# sourceMappingURL=readFromPacked.js.map
@@ -0,0 +1,5 @@
1
+ import { SchemaTypeDef } from './types.js';
2
+ export declare function schemaToSelvaBuffer(schema: {
3
+ [key: string]: SchemaTypeDef;
4
+ }): Buffer[];
5
+ //# sourceMappingURL=selvaBuffer.d.ts.map
@@ -0,0 +1,115 @@
1
+ import { ALIAS, ALIASES, BINARY, BOOLEAN, CREATED, EMPTY_MICRO_BUFFER, ENUM, CARDINALITY, INT16, INT32, INT8, MICRO_BUFFER, NULL, NUMBER, REFERENCE, REFERENCES, STRING, TEXT, TIMESTAMP, UINT16, UINT32, UINT8, UPDATED, VECTOR, WEAK_REFERENCE, WEAK_REFERENCES, JSON, } from './types.js';
2
+ const selvaTypeMap = [];
3
+ selvaTypeMap[NULL] = 0;
4
+ selvaTypeMap[TIMESTAMP] = 4;
5
+ selvaTypeMap[CREATED] = 1;
6
+ selvaTypeMap[UPDATED] = 1;
7
+ selvaTypeMap[NUMBER] = 4;
8
+ selvaTypeMap[CARDINALITY] = 11;
9
+ selvaTypeMap[INT8] = 20;
10
+ selvaTypeMap[UINT8] = 6;
11
+ selvaTypeMap[INT16] = 21;
12
+ selvaTypeMap[UINT16] = 22;
13
+ selvaTypeMap[INT32] = 23;
14
+ selvaTypeMap[UINT32] = 7;
15
+ selvaTypeMap[BOOLEAN] = 9;
16
+ selvaTypeMap[ENUM] = 10;
17
+ selvaTypeMap[STRING] = 11;
18
+ selvaTypeMap[TEXT] = 12;
19
+ selvaTypeMap[REFERENCE] = 13;
20
+ selvaTypeMap[REFERENCES] = 14;
21
+ selvaTypeMap[WEAK_REFERENCE] = 15;
22
+ selvaTypeMap[WEAK_REFERENCES] = 16;
23
+ selvaTypeMap[MICRO_BUFFER] = 17;
24
+ selvaTypeMap[ALIAS] = 18;
25
+ selvaTypeMap[ALIASES] = 19;
26
+ selvaTypeMap[BINARY] = 11;
27
+ selvaTypeMap[VECTOR] = 17;
28
+ selvaTypeMap[JSON] = 11;
29
+ const EDGE_FIELD_CONSTRAINT_FLAG_DEPENDENT = 0x01;
30
+ function blockCapacity(blockCapacity) {
31
+ const buf = Buffer.allocUnsafe(4);
32
+ buf.writeInt32LE(blockCapacity);
33
+ return buf;
34
+ }
35
+ function sepPropCount(props) {
36
+ return props.filter((prop) => prop.separate).length;
37
+ }
38
+ function makeEdgeConstraintFlags(prop) {
39
+ return prop.dependent ? EDGE_FIELD_CONSTRAINT_FLAG_DEPENDENT : 0x00;
40
+ }
41
+ const propDefBuffer = (schema, prop, isEdge) => {
42
+ const type = prop.typeIndex;
43
+ const selvaType = selvaTypeMap[type];
44
+ if (prop.len && (type === MICRO_BUFFER || type === VECTOR)) {
45
+ const buf = Buffer.allocUnsafe(3);
46
+ buf[0] = selvaType;
47
+ buf.writeUint16LE(prop.len, 1);
48
+ return [...buf.values()];
49
+ }
50
+ else if (type === REFERENCE || type === REFERENCES) {
51
+ const buf = Buffer.allocUnsafe(9);
52
+ const dstType = schema[prop.inverseTypeName];
53
+ let eschema = [];
54
+ // @ts-ignore
55
+ buf[0] = selvaType + 2 * !!isEdge; // field type
56
+ buf[1] = makeEdgeConstraintFlags(prop); // flags
57
+ buf.writeUInt16LE(dstType.id, 2); // dst_node_type
58
+ buf.writeUint32LE(0, 5); // schema_len
59
+ if (!isEdge) {
60
+ prop.inverseTypeId = dstType.id;
61
+ prop.inversePropNumber = dstType.props[prop.inversePropName].prop;
62
+ buf[4] = prop.inversePropNumber;
63
+ if (prop.edges) {
64
+ const props = Object.values(prop.edges);
65
+ eschema = props
66
+ .map((prop) => propDefBuffer(schema, prop, true))
67
+ .flat(1);
68
+ eschema.unshift(0, 0, 0, 0, sepPropCount(props), 0);
69
+ buf.writeUint32LE(eschema.length, 5);
70
+ }
71
+ }
72
+ return [...buf.values(), ...eschema];
73
+ }
74
+ else if (type === STRING ||
75
+ type === BINARY ||
76
+ type === CARDINALITY ||
77
+ type === JSON) {
78
+ return [selvaType, prop.len < 50 ? prop.len : 0];
79
+ }
80
+ {
81
+ return [selvaType];
82
+ }
83
+ };
84
+ // todo rewrite
85
+ export function schemaToSelvaBuffer(schema) {
86
+ if (typeof Buffer === 'undefined') {
87
+ // TMP
88
+ return [];
89
+ }
90
+ return Object.values(schema).map((t, i) => {
91
+ const props = Object.values(t.props);
92
+ const rest = [];
93
+ let refFields = 0;
94
+ for (const f of props) {
95
+ if (f.separate) {
96
+ if (f.typeIndex === REFERENCE || f.typeIndex === REFERENCES) {
97
+ refFields++;
98
+ }
99
+ rest.push(f);
100
+ }
101
+ }
102
+ rest.sort((a, b) => a.prop - b.prop);
103
+ return Buffer.from([
104
+ ...blockCapacity(t.blockCapacity).values(),
105
+ 1 + sepPropCount(props),
106
+ 1 + refFields,
107
+ ...propDefBuffer(schema, {
108
+ ...EMPTY_MICRO_BUFFER,
109
+ len: t.mainLen === 0 ? 1 : t.mainLen,
110
+ }),
111
+ ...rest.map((f) => propDefBuffer(schema, f)).flat(1),
112
+ ]);
113
+ });
114
+ }
115
+ //# sourceMappingURL=selvaBuffer.js.map
@@ -0,0 +1,7 @@
1
+ import { SchemaObject, StrictSchemaType, SchemaLocales } from '../index.js';
2
+ import { SchemaTypeDef, SchemaTypesParsedById, SchemaTypesParsed } from './types.js';
3
+ import { StrictSchema } from '../types.js';
4
+ export declare const DEFAULT_BLOCK_CAPACITY = 100000;
5
+ export declare const updateTypeDefs: (schema: StrictSchema, schemaTypesParsed: SchemaTypesParsed, schemaTypesParsedById: SchemaTypesParsedById) => void;
6
+ export declare const createSchemaTypeDef: (typeName: string, type: StrictSchemaType | SchemaObject, parsed: SchemaTypesParsed, locales: Partial<SchemaLocales>, result?: Partial<SchemaTypeDef>, path?: string[], top?: boolean) => SchemaTypeDef;
7
+ //# sourceMappingURL=typeDef.d.ts.map
@@ -0,0 +1,376 @@
1
+ import { isPropType, getPropType, } from '../index.js';
2
+ import { setByPath } from '@saulx/utils';
3
+ import { hashObjectIgnoreKeyOrder } from '@saulx/hash';
4
+ import { SIZE_MAP, TYPE_INDEX_MAP, STRING, ALIAS, CARDINALITY, REFERENCES, REFERENCE, TEXT, ENUM, } from './types.js';
5
+ // TMP
6
+ export const DEFAULT_BLOCK_CAPACITY = 100_000;
7
+ function getPropLen(schemaProp) {
8
+ let len = SIZE_MAP[getPropType(schemaProp)];
9
+ if (isPropType('string', schemaProp) ||
10
+ isPropType('alias', schemaProp) ||
11
+ isPropType('cardinality', schemaProp)) {
12
+ if (typeof schemaProp === 'object') {
13
+ if (schemaProp.maxBytes < 61) {
14
+ len = schemaProp.maxBytes + 1;
15
+ }
16
+ else if ('max' in schemaProp && schemaProp.max < 31) {
17
+ len = schemaProp.max * 2 + 1;
18
+ }
19
+ }
20
+ }
21
+ else if (isPropType('vector', schemaProp)) {
22
+ len = 4 * schemaProp.size;
23
+ }
24
+ return len;
25
+ }
26
+ function isSeparate(schemaProp, len) {
27
+ return len === 0 || isPropType('vector', schemaProp);
28
+ }
29
+ const addEdges = (prop, refProp) => {
30
+ let edgesCnt = 0;
31
+ for (const key in refProp) {
32
+ if (key[0] === '$') {
33
+ if (!prop.edges) {
34
+ prop.edges = {};
35
+ prop.reverseEdges = {};
36
+ prop.edgesTotalLen = 0;
37
+ }
38
+ edgesCnt++;
39
+ const edgeType = getPropType(refProp[key]);
40
+ const edge = {
41
+ __isPropDef: true,
42
+ __isEdge: true,
43
+ prop: edgesCnt,
44
+ name: key,
45
+ typeIndex: TYPE_INDEX_MAP[edgeType],
46
+ len: SIZE_MAP[edgeType],
47
+ separate: true,
48
+ path: [...prop.path, key],
49
+ };
50
+ if (edge.len == 0) {
51
+ prop.edgesTotalLen = 0;
52
+ }
53
+ else {
54
+ // [field] [size] [data]
55
+ prop.edgesTotalLen += 1 + 2 + edge.len; // field len
56
+ }
57
+ if (edge.typeIndex === ENUM) {
58
+ edge.enum = Array.isArray(refProp[key])
59
+ ? refProp[key]
60
+ : refProp[key].enum;
61
+ edge.reverseEnum = {};
62
+ for (let i = 0; i < edge.enum.length; i++) {
63
+ edge.reverseEnum[edge.enum[i]] = i;
64
+ }
65
+ }
66
+ else if (edge.typeIndex === REFERENCES) {
67
+ edge.inverseTypeName = refProp[key].items.ref;
68
+ }
69
+ else if (edge.typeIndex === REFERENCE) {
70
+ edge.inverseTypeName = refProp[key].ref;
71
+ }
72
+ prop.edges[key] = edge;
73
+ prop.reverseEdges[edge.prop] = edge;
74
+ }
75
+ }
76
+ };
77
+ function makePacked(result, typeName, vals, len) {
78
+ const encoder = new TextEncoder();
79
+ result.buf = new Uint8Array(len);
80
+ result.buf[0] = result.idUint8[0];
81
+ result.buf[1] = result.idUint8[1];
82
+ const fieldNames = [];
83
+ const tNameBuf = encoder.encode(typeName);
84
+ fieldNames.push(tNameBuf);
85
+ let fieldNameLen = tNameBuf.byteLength + 1;
86
+ let i = 2;
87
+ if (result.mainLen) {
88
+ result.buf[i] = 0;
89
+ for (const f of vals) {
90
+ if (!f.separate) {
91
+ i++;
92
+ result.buf[i] = f.typeIndex;
93
+ const name = encoder.encode(f.path.join('.'));
94
+ fieldNames.push(name);
95
+ fieldNameLen += name.byteLength + 1;
96
+ }
97
+ }
98
+ i++;
99
+ result.buf[i] = 0;
100
+ }
101
+ for (const f of vals) {
102
+ if (f.separate) {
103
+ i++;
104
+ result.buf[i] = f.prop;
105
+ i++;
106
+ result.buf[i] = f.typeIndex;
107
+ const name = encoder.encode(f.path.join('.'));
108
+ fieldNames.push(name);
109
+ fieldNameLen += name.byteLength + 1;
110
+ }
111
+ }
112
+ result.propNames = new Uint8Array(fieldNameLen);
113
+ let lastWritten = 0;
114
+ for (const f of fieldNames) {
115
+ result.propNames[lastWritten] = f.byteLength;
116
+ result.propNames.set(f, lastWritten + 1);
117
+ lastWritten += f.byteLength + 1;
118
+ }
119
+ let bufLen = result.buf.length;
120
+ result.packed = new Uint8Array(2 + bufLen + result.propNames.length);
121
+ result.packed[0] = bufLen;
122
+ result.packed[1] = bufLen >>>= 8;
123
+ result.packed.set(result.buf, 2);
124
+ result.packed.set(result.propNames, result.buf.length + 2);
125
+ }
126
+ function makeSeparateTextSort(result) {
127
+ result.hasSeperateTextSort = true;
128
+ let max = 0;
129
+ for (const f of result.separate) {
130
+ if (f.typeIndex === TEXT) {
131
+ if (f.prop > max) {
132
+ max = f.prop;
133
+ }
134
+ }
135
+ }
136
+ result.seperateTextSort.buffer = new Uint8Array(max * result.localeSize + 1);
137
+ for (const f of result.separate) {
138
+ if (f.typeIndex === TEXT) {
139
+ result.seperateTextSort.buffer[f.prop] = 1;
140
+ result.seperateTextSort.props.push(f);
141
+ result.seperateTextSort.size += result.localeSize;
142
+ }
143
+ }
144
+ result.seperateTextSort.bufferTmp = new Uint8Array(max * result.localeSize + 1);
145
+ result.seperateTextSort.buffer.set(result.seperateTextSort.bufferTmp);
146
+ }
147
+ function makeSeparateSort(result) {
148
+ result.hasSeperateSort = true;
149
+ let max = 0;
150
+ for (const f of result.separate) {
151
+ if (f.typeIndex === STRING ||
152
+ f.typeIndex === ALIAS ||
153
+ f.typeIndex === CARDINALITY) {
154
+ if (f.prop > max) {
155
+ max = f.prop;
156
+ }
157
+ }
158
+ }
159
+ result.seperateSort.buffer = new Uint8Array(max + 1);
160
+ for (const f of result.separate) {
161
+ if (f.typeIndex === STRING ||
162
+ f.typeIndex === ALIAS ||
163
+ f.typeIndex === CARDINALITY) {
164
+ result.seperateSort.buffer[f.prop] = 1;
165
+ result.seperateSort.props.push(f);
166
+ result.seperateSort.size++;
167
+ }
168
+ }
169
+ result.seperateSort.bufferTmp = new Uint8Array(max + 1);
170
+ result.seperateSort.buffer.set(result.seperateSort.bufferTmp);
171
+ }
172
+ export const updateTypeDefs = (schema, schemaTypesParsed, schemaTypesParsedById) => {
173
+ for (const field in schemaTypesParsed) {
174
+ if (field in schema.types) {
175
+ continue;
176
+ }
177
+ const id = schemaTypesParsed[field].id;
178
+ delete schemaTypesParsed[field];
179
+ delete schemaTypesParsedById[id];
180
+ }
181
+ for (const field in schema.types) {
182
+ const type = schema.types[field];
183
+ if (schemaTypesParsed[field] &&
184
+ schemaTypesParsed[field].checksum === hashObjectIgnoreKeyOrder(type) // bit weird..
185
+ ) {
186
+ continue;
187
+ }
188
+ else {
189
+ if (!type.id) {
190
+ throw new Error('NEED ID ON TYPE');
191
+ }
192
+ const def = createSchemaTypeDef(field, type, schemaTypesParsed, schema.locales ?? {
193
+ en: {},
194
+ });
195
+ def.blockCapacity =
196
+ field === '_root' ? 2147483647 : DEFAULT_BLOCK_CAPACITY; // TODO this should come from somewhere else
197
+ schemaTypesParsed[field] = def;
198
+ schemaTypesParsedById[type.id] = def;
199
+ }
200
+ }
201
+ };
202
+ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
203
+ cnt: 0,
204
+ checksum: hashObjectIgnoreKeyOrder(type),
205
+ type: typeName,
206
+ props: {},
207
+ reverseProps: {},
208
+ idUint8: new Uint8Array([0, 0]),
209
+ id: 0,
210
+ mainLen: 0,
211
+ separate: [],
212
+ tree: {},
213
+ total: 0,
214
+ lastId: 0,
215
+ main: {},
216
+ hasSeperateSort: false,
217
+ seperateSort: {
218
+ size: 0,
219
+ props: [],
220
+ buffer: new Uint8Array([]),
221
+ bufferTmp: new Uint8Array([]),
222
+ },
223
+ hasSeperateTextSort: false,
224
+ seperateTextSort: {
225
+ size: 0, // prop len
226
+ props: [],
227
+ buffer: new Uint8Array([]),
228
+ bufferTmp: new Uint8Array([]),
229
+ },
230
+ }, path = [], top = true) => {
231
+ if (result.id == 0 && top) {
232
+ if ('id' in type) {
233
+ result.id = type.id;
234
+ }
235
+ else {
236
+ throw new Error(`Invalid schema type id ${result.type}`);
237
+ }
238
+ }
239
+ result.locales = locales;
240
+ result.localeSize = Object.keys(locales).length;
241
+ result.idUint8[0] = result.id & 255;
242
+ result.idUint8[1] = result.id >> 8;
243
+ const target = type.props;
244
+ let separateSortProps = 0;
245
+ let separateSortText = 0;
246
+ for (const key in target) {
247
+ const schemaProp = target[key];
248
+ const propPath = [...path, key];
249
+ const propType = getPropType(schemaProp);
250
+ if (propType === 'object') {
251
+ createSchemaTypeDef(typeName, schemaProp, parsed, locales, result, propPath, false);
252
+ }
253
+ else {
254
+ const len = getPropLen(schemaProp);
255
+ if (isPropType('string', schemaProp) ||
256
+ isPropType('alias', schemaProp) ||
257
+ isPropType('cardinality', schemaProp)) {
258
+ if (typeof schemaProp === 'object') {
259
+ if (!(schemaProp.maxBytes < 61) || !('max' in schemaProp && schemaProp.max < 31)) {
260
+ separateSortProps++;
261
+ }
262
+ }
263
+ else {
264
+ separateSortProps++;
265
+ }
266
+ }
267
+ else if (isPropType('text', schemaProp)) {
268
+ separateSortText++;
269
+ }
270
+ const isseparate = isSeparate(schemaProp, len);
271
+ const prop = {
272
+ typeIndex: TYPE_INDEX_MAP[propType],
273
+ __isPropDef: true,
274
+ separate: isseparate,
275
+ path: propPath,
276
+ start: 0,
277
+ len,
278
+ prop: isseparate ? ++result.cnt : 0,
279
+ };
280
+ if (isPropType('enum', schemaProp)) {
281
+ prop.enum = Array.isArray(schemaProp) ? schemaProp : schemaProp.enum;
282
+ prop.reverseEnum = {};
283
+ for (let i = 0; i < prop.enum.length; i++) {
284
+ prop.reverseEnum[prop.enum[i]] = i;
285
+ }
286
+ }
287
+ else if (isPropType('references', schemaProp)) {
288
+ prop.inversePropName = schemaProp.items.prop;
289
+ prop.inverseTypeName = schemaProp.items.ref;
290
+ prop.dependent = schemaProp.items.dependent;
291
+ addEdges(prop, schemaProp.items);
292
+ }
293
+ else if (isPropType('reference', schemaProp)) {
294
+ prop.inversePropName = schemaProp.prop;
295
+ prop.inverseTypeName = schemaProp.ref;
296
+ prop.dependent = schemaProp.dependent;
297
+ addEdges(prop, schemaProp);
298
+ }
299
+ else if (typeof schemaProp === 'object') {
300
+ if (isPropType('string', schemaProp) ||
301
+ isPropType('text', schemaProp)) {
302
+ prop.compression =
303
+ 'compression' in schemaProp && schemaProp.compression === 'none'
304
+ ? 0
305
+ : 1;
306
+ }
307
+ else if (isPropType('timestamp', schemaProp) && 'on' in schemaProp) {
308
+ if (schemaProp.on[0] === 'c') {
309
+ result.createTs ??= [];
310
+ result.createTs.push(prop);
311
+ }
312
+ else if (schemaProp.on[0] === 'u') {
313
+ result.createTs ??= [];
314
+ result.createTs.push(prop);
315
+ result.updateTs ??= [];
316
+ result.updateTs.push(prop);
317
+ }
318
+ }
319
+ }
320
+ result.props[propPath.join('.')] = prop;
321
+ if (isseparate) {
322
+ result.separate.push(prop);
323
+ }
324
+ }
325
+ }
326
+ if (top) {
327
+ const vals = Object.values(result.props);
328
+ vals.sort((a, b) => {
329
+ if (b.separate &&
330
+ (a.typeIndex === REFERENCES || a.typeIndex === REFERENCE)) {
331
+ return -1;
332
+ }
333
+ return a.prop - b.prop;
334
+ });
335
+ let lastProp = 0;
336
+ for (const p of vals) {
337
+ if (p.separate) {
338
+ p.prop = ++lastProp;
339
+ }
340
+ }
341
+ let len = 2;
342
+ for (const f of vals) {
343
+ if (f.separate) {
344
+ len += 2;
345
+ setByPath(result.tree, f.path, f);
346
+ }
347
+ else {
348
+ if (!result.mainLen) {
349
+ len += 2;
350
+ }
351
+ len += 1;
352
+ f.start = result.mainLen;
353
+ result.mainLen += f.len;
354
+ setByPath(result.tree, f.path, f);
355
+ }
356
+ }
357
+ makePacked(result, typeName, vals, len);
358
+ if (separateSortText > 0) {
359
+ makeSeparateTextSort(result);
360
+ }
361
+ if (separateSortProps > 0) {
362
+ makeSeparateSort(result);
363
+ }
364
+ for (const p in result.props) {
365
+ const x = result.props[p];
366
+ if (!x.separate) {
367
+ result.main[x.start] = x;
368
+ }
369
+ else {
370
+ result.reverseProps[x.prop] = x;
371
+ }
372
+ }
373
+ }
374
+ return result;
375
+ };
376
+ //# sourceMappingURL=typeDef.js.map
@@ -0,0 +1,146 @@
1
+ import type { SchemaLocales } from '../index.js';
2
+ export declare const NULL = 0;
3
+ export declare const TIMESTAMP = 1;
4
+ export declare const CREATED = 2;
5
+ export declare const UPDATED = 3;
6
+ export declare const NUMBER = 4;
7
+ export declare const CARDINALITY = 5;
8
+ export declare const INT8 = 20;
9
+ export declare const UINT8 = 6;
10
+ export declare const INT16 = 21;
11
+ export declare const UINT16 = 22;
12
+ export declare const INT32 = 23;
13
+ export declare const UINT32 = 7;
14
+ export declare const BOOLEAN = 9;
15
+ export declare const ENUM = 10;
16
+ export declare const STRING = 11;
17
+ export declare const TEXT = 12;
18
+ export declare const REFERENCE = 13;
19
+ export declare const REFERENCES = 14;
20
+ export declare const WEAK_REFERENCE = 15;
21
+ export declare const WEAK_REFERENCES = 16;
22
+ export declare const MICRO_BUFFER = 17;
23
+ export declare const ALIAS = 18;
24
+ export declare const ALIASES = 19;
25
+ export declare const BINARY = 25;
26
+ export declare const ID = 26;
27
+ export declare const VECTOR = 27;
28
+ export declare const JSON = 28;
29
+ export declare const TYPE_INDEX_MAP: {
30
+ alias: number;
31
+ aliases: number;
32
+ microbuffer: number;
33
+ references: number;
34
+ reference: number;
35
+ timestamp: number;
36
+ boolean: number;
37
+ created: number;
38
+ updated: number;
39
+ number: number;
40
+ string: number;
41
+ text: number;
42
+ uint16: number;
43
+ uint32: number;
44
+ int16: number;
45
+ int32: number;
46
+ uint8: number;
47
+ enum: number;
48
+ int8: number;
49
+ id: number;
50
+ binary: number;
51
+ vector: number;
52
+ cardinality: number;
53
+ json: number;
54
+ };
55
+ export type InternalSchemaProp = keyof typeof TYPE_INDEX_MAP;
56
+ export type TypeIndex = (typeof TYPE_INDEX_MAP)[InternalSchemaProp];
57
+ export type PropDef = {
58
+ __isPropDef: true;
59
+ prop: number;
60
+ typeIndex: TypeIndex;
61
+ separate: boolean;
62
+ path: string[];
63
+ start: number;
64
+ len: number;
65
+ inverseTypeName?: string;
66
+ inversePropName?: string;
67
+ compression?: 0 | 1;
68
+ inverseTypeId?: number;
69
+ inversePropNumber?: number;
70
+ enum?: any[];
71
+ reverseEnum?: {
72
+ [key: string]: number;
73
+ };
74
+ edgesTotalLen?: number;
75
+ edges?: {
76
+ [key: string]: PropDefEdge;
77
+ };
78
+ reverseEdges?: {
79
+ [prop: string]: PropDefEdge;
80
+ };
81
+ __isEdge?: boolean;
82
+ dependent?: boolean;
83
+ };
84
+ export type PropDefEdge = Partial<PropDef> & {
85
+ __isPropDef: true;
86
+ typeIndex: TypeIndex;
87
+ len: number;
88
+ prop: number;
89
+ name: string;
90
+ edgesTotalLen?: number;
91
+ __isEdge: true;
92
+ };
93
+ export type SchemaPropTree = {
94
+ [key: string]: SchemaPropTree | PropDef;
95
+ };
96
+ export type SchemaSortUndefinedHandler = {
97
+ size: number;
98
+ buffer: Uint8Array;
99
+ bufferTmp: Uint8Array;
100
+ props: PropDef[];
101
+ };
102
+ export type SchemaTypeDef = {
103
+ cnt: number;
104
+ checksum: number;
105
+ total: number;
106
+ type: string;
107
+ lastId: number;
108
+ blockCapacity: number;
109
+ mainLen: number;
110
+ buf: Uint8Array;
111
+ propNames: Uint8Array;
112
+ packed: Uint8Array;
113
+ props: {
114
+ [path: string]: PropDef;
115
+ };
116
+ reverseProps: {
117
+ [field: string]: PropDef;
118
+ };
119
+ id: number;
120
+ idUint8: Uint8Array;
121
+ separate: PropDef[];
122
+ main: {
123
+ [start: string]: PropDef;
124
+ };
125
+ tree: SchemaPropTree;
126
+ hasSeperateSort: boolean;
127
+ seperateSort: SchemaSortUndefinedHandler;
128
+ hasSeperateTextSort: boolean;
129
+ seperateTextSort: SchemaSortUndefinedHandler;
130
+ createTs?: PropDef[];
131
+ updateTs?: PropDef[];
132
+ locales: Partial<SchemaLocales>;
133
+ localeSize: number;
134
+ };
135
+ export declare const SIZE_MAP: Record<InternalSchemaProp, number>;
136
+ export declare let REVERSE_SIZE_MAP: Record<TypeIndex, number>;
137
+ export declare const REVERSE_TYPE_INDEX_MAP: Record<TypeIndex, InternalSchemaProp>;
138
+ export declare const ID_FIELD_DEF: PropDef;
139
+ export declare const EMPTY_MICRO_BUFFER: PropDef;
140
+ export declare const getPropTypeName: (propType: TypeIndex) => InternalSchemaProp;
141
+ export declare const isPropDef: (prop: any) => prop is PropDef;
142
+ export type SchemaTypesParsed = {
143
+ [key: string]: SchemaTypeDef;
144
+ };
145
+ export type SchemaTypesParsedById = Record<number, SchemaTypeDef>;
146
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,120 @@
1
+ // WARN: The following type codes are used in js and zig but selva has its own typing.
2
+ export const NULL = 0;
3
+ export const TIMESTAMP = 1;
4
+ export const CREATED = 2;
5
+ export const UPDATED = 3;
6
+ export const NUMBER = 4;
7
+ export const CARDINALITY = 5;
8
+ export const INT8 = 20;
9
+ export const UINT8 = 6;
10
+ export const INT16 = 21;
11
+ export const UINT16 = 22;
12
+ export const INT32 = 23;
13
+ export const UINT32 = 7;
14
+ export const BOOLEAN = 9;
15
+ export const ENUM = 10;
16
+ export const STRING = 11;
17
+ export const TEXT = 12;
18
+ export const REFERENCE = 13;
19
+ export const REFERENCES = 14;
20
+ export const WEAK_REFERENCE = 15;
21
+ export const WEAK_REFERENCES = 16;
22
+ export const MICRO_BUFFER = 17;
23
+ export const ALIAS = 18;
24
+ export const ALIASES = 19;
25
+ export const BINARY = 25;
26
+ export const ID = 26;
27
+ export const VECTOR = 27;
28
+ export const JSON = 28;
29
+ export const TYPE_INDEX_MAP = {
30
+ alias: ALIAS,
31
+ aliases: ALIASES,
32
+ microbuffer: MICRO_BUFFER,
33
+ references: REFERENCES,
34
+ reference: REFERENCE,
35
+ timestamp: TIMESTAMP,
36
+ boolean: BOOLEAN,
37
+ created: CREATED,
38
+ updated: UPDATED,
39
+ number: NUMBER,
40
+ string: STRING,
41
+ text: TEXT,
42
+ uint16: UINT16,
43
+ uint32: UINT32,
44
+ int16: INT16,
45
+ int32: INT32,
46
+ uint8: UINT8,
47
+ enum: ENUM,
48
+ int8: INT8,
49
+ id: NULL,
50
+ binary: BINARY,
51
+ vector: VECTOR,
52
+ cardinality: CARDINALITY,
53
+ json: JSON,
54
+ };
55
+ export const SIZE_MAP = {
56
+ timestamp: 8, // 64bit
57
+ created: 8,
58
+ updated: 8,
59
+ // double-precision 64-bit binary format IEEE 754 value
60
+ number: 8, // 64bit
61
+ int8: 1,
62
+ uint8: 1,
63
+ int16: 2,
64
+ uint16: 2,
65
+ int32: 4,
66
+ uint32: 4,
67
+ boolean: 1,
68
+ reference: 0, // separate
69
+ enum: 1, // enum
70
+ string: 0, // separate
71
+ text: 0, // separate
72
+ cardinality: 0, // separate
73
+ references: 0, // separate
74
+ microbuffer: 0, // separate
75
+ alias: 0,
76
+ aliases: 0,
77
+ id: 4,
78
+ binary: 0,
79
+ vector: 0, // separate
80
+ json: 0,
81
+ };
82
+ const reverseMap = {};
83
+ for (const k in TYPE_INDEX_MAP) {
84
+ reverseMap[TYPE_INDEX_MAP[k]] = k;
85
+ }
86
+ export let REVERSE_SIZE_MAP;
87
+ // @ts-ignore
88
+ REVERSE_SIZE_MAP = {};
89
+ for (const k in SIZE_MAP) {
90
+ REVERSE_SIZE_MAP[TYPE_INDEX_MAP[k]] = SIZE_MAP[k];
91
+ }
92
+ export const REVERSE_TYPE_INDEX_MAP = reverseMap;
93
+ export const ID_FIELD_DEF = {
94
+ typeIndex: TYPE_INDEX_MAP['id'],
95
+ separate: true,
96
+ path: ['id'],
97
+ start: 0,
98
+ prop: 255,
99
+ len: 4,
100
+ __isPropDef: true,
101
+ };
102
+ export const EMPTY_MICRO_BUFFER = {
103
+ typeIndex: TYPE_INDEX_MAP['microbuffer'],
104
+ separate: true,
105
+ path: [''],
106
+ start: 0,
107
+ prop: 0,
108
+ len: 1,
109
+ __isPropDef: true,
110
+ };
111
+ export const getPropTypeName = (propType) => {
112
+ return REVERSE_TYPE_INDEX_MAP[propType];
113
+ };
114
+ export const isPropDef = (prop) => {
115
+ if ('__isPropDef' in prop && prop.__isPropDef === true) {
116
+ return true;
117
+ }
118
+ return false;
119
+ };
120
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1,4 @@
1
+ import { PropDef, PropDefEdge } from './types.js';
2
+ export declare const propIsSigned: (prop: PropDef | PropDefEdge) => boolean;
3
+ export declare const propIsNumerical: (prop: PropDef | PropDefEdge) => boolean;
4
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1,23 @@
1
+ import { INT16, INT32, INT8, UINT16, UINT32, UINT8, NUMBER, TIMESTAMP, } from './types.js';
2
+ export const propIsSigned = (prop) => {
3
+ const t = prop.typeIndex;
4
+ if (t === INT16 || t === INT32 || t === INT8) {
5
+ return true;
6
+ }
7
+ return false;
8
+ };
9
+ export const propIsNumerical = (prop) => {
10
+ const t = prop.typeIndex;
11
+ if (t === INT16 ||
12
+ t === INT32 ||
13
+ t === INT8 ||
14
+ t === UINT8 ||
15
+ t === UINT16 ||
16
+ t === UINT32 ||
17
+ t === NUMBER ||
18
+ t === TIMESTAMP) {
19
+ return true;
20
+ }
21
+ return false;
22
+ };
23
+ //# sourceMappingURL=utils.js.map
package/dist/lang.d.ts CHANGED
@@ -145,7 +145,7 @@ declare const langCodes: {
145
145
  readonly yo: 143;
146
146
  readonly zu: 144;
147
147
  };
148
- export declare const langCodesMap: Map<string, 0 | 2 | 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144>;
148
+ export declare const langCodesMap: Map<string, 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144>;
149
149
  export declare const inverseLangMap: Map<any, any>;
150
150
  export type LangName = keyof typeof langCodes;
151
151
  export type LangCode = (typeof langCodes)[LangName];
package/package.json CHANGED
@@ -1,19 +1,21 @@
1
1
  {
2
2
  "name": "@based/schema",
3
- "version": "5.0.0-alpha.3",
3
+ "version": "5.0.0-alpha.5",
4
4
  "license": "MIT",
5
- "main": "dist/index.js",
6
5
  "files": [
7
6
  "dist",
8
7
  "README.md",
9
8
  "package.json",
10
9
  "!dist/**/*.map"
11
10
  ],
11
+ "exports": {
12
+ "./def": "./dist/def/index.js",
13
+ ".": "./dist/index.js"
14
+ },
12
15
  "scripts": {
13
16
  "build": "tsc",
14
17
  "watch": "tsc --watch",
15
- "test": "tsc && tsc test/*.ts --noEmit && tsx --test test/*.ts",
16
- "test1": "tsc && tsc $npm_config --noEmit && tsx --test $npm_config"
18
+ "test": "tsc && tsc $npm_config --noEmit && tsx --test $npm_config"
17
19
  },
18
20
  "prettier": "@saulx/prettier-config",
19
21
  "sideEffects": false,
@@ -26,6 +28,7 @@
26
28
  "typescript": "^5.6.3"
27
29
  },
28
30
  "dependencies": {
31
+ "@saulx/utils": "^5.0.0",
29
32
  "picocolors": "^1.1.0"
30
33
  }
31
34
  }