@based/schema 5.0.0-alpha.1 → 5.0.0-alpha.11

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.
Files changed (42) hide show
  1. package/dist/def/addEdges.d.ts +4 -0
  2. package/dist/def/addEdges.js +76 -0
  3. package/dist/def/createEmptyDef.d.ts +38 -0
  4. package/dist/def/createEmptyDef.js +41 -0
  5. package/dist/def/defaultMap.d.ts +3 -0
  6. package/dist/def/defaultMap.js +27 -0
  7. package/dist/def/fillEmptyMain.d.ts +5 -0
  8. package/dist/def/fillEmptyMain.js +61 -0
  9. package/dist/def/getPropLen.d.ts +3 -0
  10. package/dist/def/getPropLen.js +23 -0
  11. package/dist/def/index.d.ts +9 -0
  12. package/dist/def/index.js +10 -0
  13. package/dist/def/makePacked.d.ts +3 -0
  14. package/dist/def/makePacked.js +50 -0
  15. package/dist/def/makeSeparateSort.d.ts +3 -0
  16. package/dist/def/makeSeparateSort.js +27 -0
  17. package/dist/def/makeSeparateTextSort.d.ts +3 -0
  18. package/dist/def/makeSeparateTextSort.js +38 -0
  19. package/dist/def/readFromPacked.d.ts +3 -0
  20. package/dist/def/readFromPacked.js +140 -0
  21. package/dist/def/selvaBuffer.d.ts +5 -0
  22. package/dist/def/selvaBuffer.js +115 -0
  23. package/dist/def/typeDef.d.ts +7 -0
  24. package/dist/def/typeDef.js +215 -0
  25. package/dist/def/types.d.ts +168 -0
  26. package/dist/def/types.js +119 -0
  27. package/dist/def/utils.d.ts +8 -0
  28. package/dist/def/utils.js +59 -0
  29. package/dist/def/validation.d.ts +7 -0
  30. package/dist/def/validation.js +258 -0
  31. package/dist/index.d.ts +2 -0
  32. package/dist/index.js +2 -0
  33. package/dist/lang.d.ts +1 -1
  34. package/dist/parse/index.d.ts +1 -1
  35. package/dist/parse/index.js +5 -5
  36. package/dist/parse/props.d.ts +1 -0
  37. package/dist/parse/props.js +107 -51
  38. package/dist/serialize.d.ts +5 -0
  39. package/dist/serialize.js +139 -0
  40. package/dist/types.d.ts +36 -13
  41. package/dist/types.js +2 -0
  42. package/package.json +9 -4
@@ -0,0 +1,115 @@
1
+ import { ALIAS, ALIASES, BINARY, EMPTY_MICRO_BUFFER, CARDINALITY, MICRO_BUFFER, REFERENCE, REFERENCES, STRING, TEXT, VECTOR, WEAK_REFERENCE, WEAK_REFERENCES, JSON, } from './types.js';
2
+ const selvaTypeMap = new Uint8Array(32); // 1.2x faster than JS array
3
+ selvaTypeMap[MICRO_BUFFER] = 1;
4
+ selvaTypeMap[VECTOR] = 1;
5
+ selvaTypeMap[BINARY] = 2;
6
+ selvaTypeMap[CARDINALITY] = 2;
7
+ selvaTypeMap[JSON] = 2;
8
+ selvaTypeMap[STRING] = 2;
9
+ selvaTypeMap[TEXT] = 3;
10
+ selvaTypeMap[REFERENCE] = 4;
11
+ selvaTypeMap[REFERENCES] = 5;
12
+ selvaTypeMap[WEAK_REFERENCE] = 6;
13
+ selvaTypeMap[WEAK_REFERENCES] = 7;
14
+ selvaTypeMap[ALIAS] = 8;
15
+ selvaTypeMap[ALIASES] = 9;
16
+ const EDGE_FIELD_CONSTRAINT_FLAG_DEPENDENT = 0x01;
17
+ function blockCapacity(blockCapacity) {
18
+ const buf = new Uint8Array(Uint32Array.BYTES_PER_ELEMENT);
19
+ const view = new DataView(buf.buffer);
20
+ view.setUint32(0, blockCapacity, true);
21
+ return buf;
22
+ }
23
+ function sepPropCount(props) {
24
+ return props.filter((prop) => prop.separate).length;
25
+ }
26
+ function makeEdgeConstraintFlags(prop) {
27
+ return prop.dependent ? EDGE_FIELD_CONSTRAINT_FLAG_DEPENDENT : 0x00;
28
+ }
29
+ const propDefBuffer = (schema, prop, isEdge) => {
30
+ const type = prop.typeIndex;
31
+ const selvaType = selvaTypeMap[type];
32
+ if (prop.len && (type === MICRO_BUFFER || type === VECTOR)) {
33
+ const buf = new Uint8Array(3);
34
+ const view = new DataView(buf.buffer);
35
+ buf[0] = selvaType;
36
+ view.setUint16(1, prop.len, true);
37
+ return [...buf];
38
+ }
39
+ else if (type === REFERENCE || type === REFERENCES) {
40
+ const buf = new Uint8Array(9);
41
+ const view = new DataView(buf.buffer);
42
+ const dstType = schema[prop.inverseTypeName];
43
+ let eschema = [];
44
+ // @ts-ignore
45
+ buf[0] = selvaType + 2 * !!isEdge; // field type
46
+ buf[1] = makeEdgeConstraintFlags(prop); // flags
47
+ view.setUint16(2, dstType.id, true); // dst_node_type
48
+ view.setUint32(5, 0, true); // schema_len
49
+ if (!isEdge) {
50
+ prop.inverseTypeId = dstType.id;
51
+ prop.inversePropNumber = dstType.props[prop.inversePropName].prop;
52
+ buf[4] = prop.inversePropNumber;
53
+ if (prop.edges) {
54
+ const edgesS = Object.values(prop.edges);
55
+ if (edgesS.length) {
56
+ const props = edgesS
57
+ .filter((v) => v.separate === true)
58
+ .sort((a, b) => (a.prop > b.prop ? 1 : -1));
59
+ const p = [
60
+ {
61
+ ...EMPTY_MICRO_BUFFER,
62
+ len: prop.edgeMainLen || 1, // allow zero here... else useless padding
63
+ __isEdgeDef: true,
64
+ },
65
+ // or handle this here...
66
+ ...props,
67
+ ];
68
+ eschema = p
69
+ .map((prop) => propDefBuffer(schema, prop, true))
70
+ .flat(1);
71
+ eschema.unshift(0, 0, 0, 0, sepPropCount(p), 0);
72
+ view.setUint32(5, eschema.length, true);
73
+ }
74
+ }
75
+ }
76
+ return [...buf, ...eschema];
77
+ }
78
+ else if (type === STRING ||
79
+ type === BINARY ||
80
+ type === CARDINALITY ||
81
+ type === JSON) {
82
+ return [selvaType, prop.len < 50 ? prop.len : 0];
83
+ }
84
+ {
85
+ return [selvaType];
86
+ }
87
+ };
88
+ // TODO rewrite
89
+ export function schemaToSelvaBuffer(schema) {
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 Uint8Array.from([
104
+ ...blockCapacity(t.blockCapacity),
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
+ ]).buffer;
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,215 @@
1
+ import { isPropType, getPropType, } from '../index.js';
2
+ import { setByPath } from '@saulx/utils';
3
+ import { TYPE_INDEX_MAP, REFERENCES, REFERENCE, NUMBER, } from './types.js';
4
+ import { DEFAULT_MAP } from './defaultMap.js';
5
+ import { makePacked } from './makePacked.js';
6
+ import { makeSeparateTextSort } from './makeSeparateTextSort.js';
7
+ import { makeSeparateSort } from './makeSeparateSort.js';
8
+ import { getPropLen } from './getPropLen.js';
9
+ import { isSeparate, parseMinMaxStep } from './utils.js';
10
+ import { addEdges } from './addEdges.js';
11
+ import { createEmptyDef } from './createEmptyDef.js';
12
+ import { hashObjectIgnoreKeyOrder } from '@saulx/hash';
13
+ import { fillEmptyMain, isZeroes } from './fillEmptyMain.js';
14
+ import { defaultValidation, VALIDATION_MAP } from './validation.js';
15
+ export const DEFAULT_BLOCK_CAPACITY = 100_000;
16
+ export const updateTypeDefs = (schema, schemaTypesParsed, schemaTypesParsedById) => {
17
+ for (const field in schemaTypesParsed) {
18
+ if (field in schema.types) {
19
+ continue;
20
+ }
21
+ const id = schemaTypesParsed[field].id;
22
+ delete schemaTypesParsed[field];
23
+ delete schemaTypesParsedById[id];
24
+ }
25
+ for (const field in schema.types) {
26
+ const type = schema.types[field];
27
+ if (schemaTypesParsed[field] &&
28
+ schemaTypesParsed[field].checksum === hashObjectIgnoreKeyOrder(type) // bit weird..
29
+ ) {
30
+ continue;
31
+ }
32
+ else {
33
+ if (!type.id) {
34
+ throw new Error('NEED ID ON TYPE');
35
+ }
36
+ const def = createSchemaTypeDef(field, type, schemaTypesParsed, schema.locales ?? {
37
+ en: {},
38
+ });
39
+ // TODO this should come from somewhere else
40
+ def.blockCapacity =
41
+ field === '_root' ? 2147483647 : DEFAULT_BLOCK_CAPACITY;
42
+ schemaTypesParsed[field] = def;
43
+ schemaTypesParsedById[type.id] = def;
44
+ }
45
+ }
46
+ };
47
+ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = createEmptyDef(typeName, type, locales), path = [], top = true) => {
48
+ if (result.id == 0 && top) {
49
+ if ('id' in type) {
50
+ result.id = type.id;
51
+ }
52
+ else {
53
+ throw new Error(`Invalid schema type id ${result.type}`);
54
+ }
55
+ }
56
+ result.locales = locales;
57
+ result.localeSize = Object.keys(locales).length;
58
+ result.idUint8[0] = result.id & 255;
59
+ result.idUint8[1] = result.id >> 8;
60
+ const target = type.props;
61
+ let separateSortProps = 0;
62
+ let separateSortText = 0;
63
+ for (const key in target) {
64
+ // Create prop def
65
+ const schemaProp = target[key];
66
+ const propPath = [...path, key];
67
+ const propType = getPropType(schemaProp);
68
+ if (propType === 'object') {
69
+ createSchemaTypeDef(typeName, schemaProp, parsed, locales, result, propPath, false);
70
+ }
71
+ else {
72
+ const len = getPropLen(schemaProp);
73
+ if (isPropType('string', schemaProp) ||
74
+ isPropType('alias', schemaProp) ||
75
+ isPropType('cardinality', schemaProp)) {
76
+ if (typeof schemaProp === 'object') {
77
+ if (!(schemaProp.maxBytes < 61) ||
78
+ !('max' in schemaProp && schemaProp.max < 31)) {
79
+ separateSortProps++;
80
+ }
81
+ }
82
+ else {
83
+ separateSortProps++;
84
+ }
85
+ }
86
+ else if (isPropType('text', schemaProp)) {
87
+ separateSortText++;
88
+ }
89
+ const isseparate = isSeparate(schemaProp, len);
90
+ const typeIndex = TYPE_INDEX_MAP[propType];
91
+ const prop = {
92
+ typeIndex,
93
+ __isPropDef: true,
94
+ separate: isseparate,
95
+ path: propPath,
96
+ start: 0,
97
+ validation: schemaProp.validation ??
98
+ VALIDATION_MAP[typeIndex] ??
99
+ defaultValidation,
100
+ len,
101
+ default: schemaProp.default ?? DEFAULT_MAP[typeIndex],
102
+ prop: isseparate ? ++result.cnt : 0,
103
+ };
104
+ if (schemaProp.max !== undefined) {
105
+ prop.max = parseMinMaxStep(schemaProp.max);
106
+ }
107
+ if (schemaProp.min !== undefined) {
108
+ prop.min = parseMinMaxStep(schemaProp.min);
109
+ }
110
+ if (schemaProp.step !== undefined) {
111
+ prop.step = parseMinMaxStep(schemaProp.step);
112
+ }
113
+ if (prop.typeIndex !== NUMBER && prop.step === undefined) {
114
+ prop.step = 1;
115
+ }
116
+ if (isPropType('enum', schemaProp)) {
117
+ prop.enum = Array.isArray(schemaProp) ? schemaProp : schemaProp.enum;
118
+ prop.reverseEnum = {};
119
+ for (let i = 0; i < prop.enum.length; i++) {
120
+ prop.reverseEnum[prop.enum[i]] = i;
121
+ }
122
+ }
123
+ else if (isPropType('references', schemaProp)) {
124
+ prop.inversePropName = schemaProp.items.prop;
125
+ prop.inverseTypeName = schemaProp.items.ref;
126
+ prop.dependent = schemaProp.items.dependent;
127
+ addEdges(prop, schemaProp.items);
128
+ }
129
+ else if (isPropType('reference', schemaProp)) {
130
+ prop.inversePropName = schemaProp.prop;
131
+ prop.inverseTypeName = schemaProp.ref;
132
+ prop.dependent = schemaProp.dependent;
133
+ addEdges(prop, schemaProp);
134
+ }
135
+ else if (typeof schemaProp === 'object') {
136
+ if (isPropType('string', schemaProp) ||
137
+ isPropType('text', schemaProp)) {
138
+ prop.compression =
139
+ 'compression' in schemaProp && schemaProp.compression === 'none'
140
+ ? 0
141
+ : 1;
142
+ }
143
+ else if (isPropType('timestamp', schemaProp) && 'on' in schemaProp) {
144
+ if (schemaProp.on[0] === 'c') {
145
+ result.createTs ??= [];
146
+ result.createTs.push(prop);
147
+ }
148
+ else if (schemaProp.on[0] === 'u') {
149
+ result.createTs ??= [];
150
+ result.createTs.push(prop);
151
+ result.updateTs ??= [];
152
+ result.updateTs.push(prop);
153
+ }
154
+ }
155
+ }
156
+ result.props[propPath.join('.')] = prop;
157
+ if (isseparate) {
158
+ result.separate.push(prop);
159
+ }
160
+ }
161
+ }
162
+ if (top) {
163
+ // Put top level together
164
+ const vals = Object.values(result.props);
165
+ vals.sort((a, b) => {
166
+ if (b.separate &&
167
+ (a.typeIndex === REFERENCES || a.typeIndex === REFERENCE)) {
168
+ return -1;
169
+ }
170
+ return a.prop - b.prop;
171
+ });
172
+ let lastProp = 0;
173
+ for (const p of vals) {
174
+ if (p.separate) {
175
+ p.prop = ++lastProp;
176
+ }
177
+ }
178
+ let len = 2;
179
+ for (const f of vals) {
180
+ if (f.separate) {
181
+ len += 2;
182
+ setByPath(result.tree, f.path, f);
183
+ }
184
+ else {
185
+ if (!result.mainLen) {
186
+ len += 2;
187
+ }
188
+ len += 1;
189
+ f.start = result.mainLen;
190
+ result.mainLen += f.len;
191
+ setByPath(result.tree, f.path, f);
192
+ }
193
+ }
194
+ result.mainEmpty = fillEmptyMain(vals, result.mainLen);
195
+ result.mainEmptyAllZeroes = isZeroes(result.mainEmpty);
196
+ makePacked(result, typeName, vals, len);
197
+ if (separateSortText > 0) {
198
+ makeSeparateTextSort(result);
199
+ }
200
+ if (separateSortProps > 0) {
201
+ makeSeparateSort(result);
202
+ }
203
+ for (const p in result.props) {
204
+ const x = result.props[p];
205
+ if (!x.separate) {
206
+ result.main[x.start] = x;
207
+ }
208
+ else {
209
+ result.reverseProps[x.prop] = x;
210
+ }
211
+ }
212
+ }
213
+ return result;
214
+ };
215
+ //# sourceMappingURL=typeDef.js.map
@@ -0,0 +1,168 @@
1
+ import type { LangCode, SchemaLocales } from '../index.js';
2
+ import { Validation } from './validation.js';
3
+ export declare const NULL = 0;
4
+ export declare const TIMESTAMP = 1;
5
+ export declare const NUMBER = 4;
6
+ export declare const CARDINALITY = 5;
7
+ export declare const INT8 = 20;
8
+ export declare const UINT8 = 6;
9
+ export declare const INT16 = 21;
10
+ export declare const UINT16 = 22;
11
+ export declare const INT32 = 23;
12
+ export declare const UINT32 = 7;
13
+ export declare const BOOLEAN = 9;
14
+ export declare const ENUM = 10;
15
+ export declare const STRING = 11;
16
+ export declare const TEXT = 12;
17
+ export declare const REFERENCE = 13;
18
+ export declare const REFERENCES = 14;
19
+ export declare const WEAK_REFERENCE = 15;
20
+ export declare const WEAK_REFERENCES = 16;
21
+ export declare const MICRO_BUFFER = 17;
22
+ export declare const ALIAS = 18;
23
+ export declare const ALIASES = 19;
24
+ export declare const BINARY = 25;
25
+ export declare const ID = 26;
26
+ export declare const VECTOR = 27;
27
+ export declare const JSON = 28;
28
+ export declare const OBJECT = 29;
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
+ number: number;
38
+ string: number;
39
+ text: number;
40
+ uint16: number;
41
+ uint32: number;
42
+ int16: number;
43
+ int32: number;
44
+ uint8: number;
45
+ enum: number;
46
+ int8: number;
47
+ id: number;
48
+ binary: number;
49
+ vector: number;
50
+ cardinality: number;
51
+ json: number;
52
+ object: number;
53
+ };
54
+ export type InternalSchemaProp = keyof typeof TYPE_INDEX_MAP;
55
+ export type TypeIndex = (typeof TYPE_INDEX_MAP)[InternalSchemaProp];
56
+ export type PropDef = {
57
+ __isPropDef: true;
58
+ prop: number;
59
+ typeIndex: TypeIndex;
60
+ separate: boolean;
61
+ path: string[];
62
+ start: number;
63
+ len: number;
64
+ inverseTypeName?: string;
65
+ inversePropName?: string;
66
+ compression?: 0 | 1;
67
+ inverseTypeId?: number;
68
+ inversePropNumber?: number;
69
+ enum?: any[];
70
+ dependent?: boolean;
71
+ validation: Validation;
72
+ default: any;
73
+ edgeMainLen?: 0;
74
+ reverseEnum?: {
75
+ [key: string]: number;
76
+ };
77
+ edgesSeperateCnt?: number;
78
+ edges?: {
79
+ [key: string]: PropDefEdge;
80
+ };
81
+ reverseSeperateEdges?: {
82
+ [prop: string]: PropDefEdge;
83
+ };
84
+ reverseMainEdges?: {
85
+ [start: string]: PropDefEdge;
86
+ };
87
+ edgeMainEmpty?: Uint8Array;
88
+ __isEdge?: boolean;
89
+ max?: any;
90
+ min?: any;
91
+ step?: any;
92
+ };
93
+ export type PropDefEdge = Partial<PropDef> & {
94
+ __isPropDef: true;
95
+ typeIndex: TypeIndex;
96
+ len: number;
97
+ prop: number;
98
+ name: string;
99
+ edgesTotalLen?: number;
100
+ __isEdge: true;
101
+ };
102
+ export type PropDefAggregate = Partial<PropDef> & {
103
+ __isPropDef: true;
104
+ typeIndex: TypeIndex;
105
+ len: number;
106
+ prop: number;
107
+ name: string;
108
+ };
109
+ export type SchemaPropTree = {
110
+ [key: string]: SchemaPropTree | PropDef;
111
+ };
112
+ export type SchemaSortUndefinedHandler = {
113
+ size: number;
114
+ buffer: Uint8Array;
115
+ bufferTmp: Uint8Array;
116
+ props: PropDef[];
117
+ };
118
+ export type SchemaTypeDef = {
119
+ cnt: number;
120
+ checksum: number;
121
+ total: number;
122
+ type: string;
123
+ lastId: number;
124
+ blockCapacity: number;
125
+ mainLen: number;
126
+ buf: Uint8Array;
127
+ propNames: Uint8Array;
128
+ packed: Uint8Array;
129
+ props: {
130
+ [path: string]: PropDef;
131
+ };
132
+ reverseProps: {
133
+ [field: string]: PropDef;
134
+ };
135
+ id: number;
136
+ idUint8: Uint8Array;
137
+ separate: PropDef[];
138
+ main: {
139
+ [start: string]: PropDef;
140
+ };
141
+ mainEmpty: Uint8Array;
142
+ mainEmptyAllZeroes: boolean;
143
+ tree: SchemaPropTree;
144
+ hasSeperateSort: boolean;
145
+ seperateSort: SchemaSortUndefinedHandler;
146
+ hasSeperateTextSort: boolean;
147
+ seperateTextSort: SchemaSortUndefinedHandler & {
148
+ noUndefined: Uint8Array;
149
+ localeStringToIndex: Map<string, Uint8Array>;
150
+ localeToIndex: Map<LangCode, number>;
151
+ };
152
+ createTs?: PropDef[];
153
+ updateTs?: PropDef[];
154
+ locales: Partial<SchemaLocales>;
155
+ localeSize: number;
156
+ };
157
+ export declare const SIZE_MAP: Record<InternalSchemaProp, number>;
158
+ export declare const REVERSE_SIZE_MAP: Record<TypeIndex, number>;
159
+ export declare const REVERSE_TYPE_INDEX_MAP: Record<TypeIndex, InternalSchemaProp>;
160
+ export declare const ID_FIELD_DEF: PropDef;
161
+ export declare const EMPTY_MICRO_BUFFER: PropDef;
162
+ export declare const getPropTypeName: (propType: TypeIndex) => InternalSchemaProp;
163
+ export declare const isPropDef: (prop: any) => prop is PropDef;
164
+ export type SchemaTypesParsed = {
165
+ [key: string]: SchemaTypeDef;
166
+ };
167
+ export type SchemaTypesParsedById = Record<number, SchemaTypeDef>;
168
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,119 @@
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 NUMBER = 4;
5
+ export const CARDINALITY = 5;
6
+ export const INT8 = 20;
7
+ export const UINT8 = 6;
8
+ export const INT16 = 21;
9
+ export const UINT16 = 22;
10
+ export const INT32 = 23;
11
+ export const UINT32 = 7;
12
+ export const BOOLEAN = 9;
13
+ export const ENUM = 10;
14
+ export const STRING = 11;
15
+ export const TEXT = 12;
16
+ export const REFERENCE = 13;
17
+ export const REFERENCES = 14;
18
+ export const WEAK_REFERENCE = 15;
19
+ export const WEAK_REFERENCES = 16;
20
+ export const MICRO_BUFFER = 17;
21
+ export const ALIAS = 18;
22
+ export const ALIASES = 19;
23
+ export const BINARY = 25;
24
+ export const ID = 26;
25
+ export const VECTOR = 27;
26
+ export const JSON = 28;
27
+ export const OBJECT = 29;
28
+ export const TYPE_INDEX_MAP = {
29
+ alias: ALIAS,
30
+ aliases: ALIASES,
31
+ microbuffer: MICRO_BUFFER,
32
+ references: REFERENCES,
33
+ reference: REFERENCE,
34
+ timestamp: TIMESTAMP,
35
+ boolean: BOOLEAN,
36
+ number: NUMBER,
37
+ string: STRING,
38
+ text: TEXT,
39
+ uint16: UINT16,
40
+ uint32: UINT32,
41
+ int16: INT16,
42
+ int32: INT32,
43
+ uint8: UINT8,
44
+ enum: ENUM,
45
+ int8: INT8,
46
+ id: NULL,
47
+ binary: BINARY,
48
+ vector: VECTOR,
49
+ cardinality: CARDINALITY,
50
+ json: JSON,
51
+ object: OBJECT,
52
+ };
53
+ export const SIZE_MAP = {
54
+ timestamp: 8, // 64bit
55
+ // double-precision 64-bit binary format IEEE 754 value
56
+ number: 8, // 64bit
57
+ int8: 1,
58
+ uint8: 1,
59
+ int16: 2,
60
+ uint16: 2,
61
+ int32: 4,
62
+ uint32: 4,
63
+ boolean: 1,
64
+ reference: 0, // separate
65
+ enum: 1, // enum
66
+ string: 0, // separate
67
+ text: 0, // separate
68
+ cardinality: 0, // separate
69
+ references: 0, // separate
70
+ microbuffer: 0, // separate
71
+ alias: 0,
72
+ aliases: 0,
73
+ id: 4,
74
+ binary: 0,
75
+ vector: 0, // separate
76
+ json: 0,
77
+ object: 0,
78
+ };
79
+ const reverseMap = {};
80
+ for (const k in TYPE_INDEX_MAP) {
81
+ reverseMap[TYPE_INDEX_MAP[k]] = k;
82
+ }
83
+ export const REVERSE_SIZE_MAP = {};
84
+ for (const k in SIZE_MAP) {
85
+ REVERSE_SIZE_MAP[TYPE_INDEX_MAP[k]] = SIZE_MAP[k];
86
+ }
87
+ export const REVERSE_TYPE_INDEX_MAP = reverseMap;
88
+ export const ID_FIELD_DEF = {
89
+ typeIndex: TYPE_INDEX_MAP['id'],
90
+ separate: true,
91
+ path: ['id'],
92
+ start: 0,
93
+ prop: 255,
94
+ default: 0,
95
+ len: 4,
96
+ validation: () => true,
97
+ __isPropDef: true,
98
+ };
99
+ export const EMPTY_MICRO_BUFFER = {
100
+ typeIndex: TYPE_INDEX_MAP['microbuffer'],
101
+ separate: true,
102
+ path: [''],
103
+ start: 0,
104
+ default: undefined,
105
+ prop: 0,
106
+ len: 1,
107
+ validation: () => true,
108
+ __isPropDef: true,
109
+ };
110
+ export const getPropTypeName = (propType) => {
111
+ return REVERSE_TYPE_INDEX_MAP[propType];
112
+ };
113
+ export const isPropDef = (prop) => {
114
+ if ('__isPropDef' in prop && prop.__isPropDef === true) {
115
+ return true;
116
+ }
117
+ return false;
118
+ };
119
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1,8 @@
1
+ import { PropDef, PropDefEdge } from './types.js';
2
+ import { SchemaProp } from '../types.js';
3
+ export declare function isSeparate(schemaProp: SchemaProp, len: number): boolean;
4
+ export declare const propIsSigned: (prop: PropDef | PropDefEdge) => boolean;
5
+ export declare const propIsNumerical: (prop: PropDef | PropDefEdge) => boolean;
6
+ export declare function getPropLen(schemaProp: SchemaProp): any;
7
+ export declare const parseMinMaxStep: (val: any) => string | number;
8
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1,59 @@
1
+ import { INT16, INT32, INT8, UINT16, UINT32, UINT8, NUMBER, TIMESTAMP, SIZE_MAP, } from './types.js';
2
+ import { isPropType } from '../types.js';
3
+ import { getPropType } from '../parse/utils.js';
4
+ import { convertToTimestamp } from '@saulx/utils';
5
+ export function isSeparate(schemaProp, len) {
6
+ return len === 0 || isPropType('vector', schemaProp);
7
+ }
8
+ export const propIsSigned = (prop) => {
9
+ const t = prop.typeIndex;
10
+ if (t === INT16 || t === INT32 || t === INT8) {
11
+ return true;
12
+ }
13
+ return false;
14
+ };
15
+ export const propIsNumerical = (prop) => {
16
+ const t = prop.typeIndex;
17
+ if (t === INT16 ||
18
+ t === INT32 ||
19
+ t === INT8 ||
20
+ t === UINT8 ||
21
+ t === UINT16 ||
22
+ t === UINT32 ||
23
+ t === NUMBER ||
24
+ t === TIMESTAMP) {
25
+ return true;
26
+ }
27
+ return false;
28
+ };
29
+ export function getPropLen(schemaProp) {
30
+ let len = SIZE_MAP[getPropType(schemaProp)];
31
+ if (isPropType('string', schemaProp) ||
32
+ isPropType('alias', schemaProp) ||
33
+ isPropType('cardinality', schemaProp)) {
34
+ if (typeof schemaProp === 'object') {
35
+ if (schemaProp.maxBytes < 61) {
36
+ len = schemaProp.maxBytes + 1;
37
+ }
38
+ else if ('max' in schemaProp && schemaProp.max < 31) {
39
+ len = schemaProp.max * 2 + 1;
40
+ }
41
+ }
42
+ }
43
+ else if (isPropType('vector', schemaProp)) {
44
+ len = 4 * schemaProp.size;
45
+ }
46
+ return len;
47
+ }
48
+ export const parseMinMaxStep = (val) => {
49
+ if (typeof val === 'number') {
50
+ return val;
51
+ }
52
+ if (typeof val === 'string') {
53
+ if (!val.includes('now')) {
54
+ return convertToTimestamp(val);
55
+ }
56
+ return val;
57
+ }
58
+ };
59
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1,7 @@
1
+ import { TypeIndex, PropDef, PropDefEdge } from './types.js';
2
+ export type Validation = (payload: any, prop: PropDef | PropDefEdge) => boolean;
3
+ export declare const VALIDATION_MAP: Record<TypeIndex, Validation>;
4
+ export declare const defaultValidation: () => boolean;
5
+ export declare const isValidId: (id: number) => boolean;
6
+ export declare const isValidString: (v: any) => boolean;
7
+ //# sourceMappingURL=validation.d.ts.map