@based/schema 5.0.2 → 5.0.4-alpha.1

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,8 @@
1
+ import { StrictSchema } from "./types.js";
2
+ export type DbSchema = StrictSchema & {
3
+ lastId: number;
4
+ hash: number;
5
+ };
6
+ export type SchemaChecksum = number;
7
+ export declare const strictSchemaToDbSchema: (schema: StrictSchema) => DbSchema;
8
+ //# sourceMappingURL=dbSchema.d.ts.map
@@ -0,0 +1,59 @@
1
+ import { hash } from '@based/hash';
2
+ import { getPropType } from "./parse/utils.js";
3
+ import { deepCopy } from '@based/utils';
4
+ export const strictSchemaToDbSchema = (schema) => {
5
+ // @ts-ignore
6
+ let dbSchema = deepCopy(schema);
7
+ // reserve 1 for root (even if you dont have it)
8
+ dbSchema.lastId = 1;
9
+ // Make the _root type
10
+ if (dbSchema.props) {
11
+ for (const key in dbSchema.props) {
12
+ const prop = dbSchema.props[key];
13
+ const propType = getPropType(prop);
14
+ let refProp;
15
+ if (propType === 'reference') {
16
+ refProp = prop;
17
+ }
18
+ else if (propType === 'references') {
19
+ refProp = prop.items;
20
+ prop.items = refProp;
21
+ }
22
+ if (refProp) {
23
+ const type = dbSchema.types[refProp.ref];
24
+ const inverseKey = '_' + key;
25
+ dbSchema.types[refProp.ref] = {
26
+ ...type,
27
+ props: {
28
+ ...type.props,
29
+ [inverseKey]: {
30
+ items: {
31
+ ref: '_root',
32
+ prop: key,
33
+ },
34
+ },
35
+ },
36
+ };
37
+ refProp.prop = inverseKey;
38
+ }
39
+ }
40
+ dbSchema.types ??= {};
41
+ // @ts-ignore This creates an internal type to use for root props
42
+ dbSchema.types._root = {
43
+ id: 1,
44
+ props: dbSchema.props,
45
+ };
46
+ delete dbSchema.props;
47
+ }
48
+ // Assign typeIds
49
+ for (const typeName in dbSchema.types) {
50
+ if (!('id' in dbSchema.types[typeName])) {
51
+ dbSchema.lastId++;
52
+ dbSchema.types[typeName].id = dbSchema.lastId;
53
+ }
54
+ }
55
+ delete dbSchema.hash;
56
+ dbSchema.hash = hash(dbSchema);
57
+ return dbSchema;
58
+ };
59
+ //# sourceMappingURL=dbSchema.js.map
@@ -2,7 +2,7 @@ import { getPropType } from '../index.js';
2
2
  import { DEFAULT_MAP } from './defaultMap.js';
3
3
  import { fillEmptyMain } from './fillEmptyMain.js';
4
4
  import { TYPE_INDEX_MAP, REFERENCES, REFERENCE, ENUM, NUMBER, } from './types.js';
5
- import { getPropLen, isSeparate, parseMinMaxStep } from './utils.js';
5
+ import { getPropLen, isSeparate, parseMinMaxStep, sortMainProps, } from './utils.js';
6
6
  import { defaultValidation, VALIDATION_MAP } from './validation.js';
7
7
  export const addEdges = (prop, refProp) => {
8
8
  const mainEdges = [];
@@ -38,7 +38,7 @@ export const addEdges = (prop, refProp) => {
38
38
  separate,
39
39
  path: [...prop.path, key],
40
40
  default: edgeProp.default ?? DEFAULT_MAP[typeIndex],
41
- start: prop.edgeMainLen,
41
+ // start: prop.edgeMainLen,
42
42
  };
43
43
  if (!separate) {
44
44
  mainEdges.push(edge);
@@ -55,7 +55,6 @@ export const addEdges = (prop, refProp) => {
55
55
  if (edge.typeIndex !== NUMBER && edge.step === undefined) {
56
56
  edge.step = 1;
57
57
  }
58
- prop.edgeMainLen += edge.len;
59
58
  if (edge.typeIndex === ENUM) {
60
59
  edge.enum = Array.isArray(refProp[key])
61
60
  ? refProp[key]
@@ -75,11 +74,14 @@ export const addEdges = (prop, refProp) => {
75
74
  if (separate) {
76
75
  prop.reverseSeperateEdges[edge.prop] = edge;
77
76
  }
78
- else {
79
- prop.reverseMainEdges[edge.start] = edge;
80
- }
81
77
  }
82
78
  }
79
+ mainEdges.sort(sortMainProps);
80
+ for (const edge of mainEdges) {
81
+ edge.start = prop.edgeMainLen;
82
+ prop.edgeMainLen += edge.len;
83
+ prop.reverseMainEdges[edge.start] = edge;
84
+ }
83
85
  prop.edgeMainEmpty = fillEmptyMain(mainEdges, prop.edgeMainLen);
84
86
  };
85
87
  //# sourceMappingURL=addEdges.js.map
@@ -1,5 +1,4 @@
1
- import { SchemaObject, StrictSchemaType, SchemaLocales } from '../index.js';
2
- import { SchemaTypeDef, SchemaTypesParsed } from './types.js';
1
+ import { SchemaTypeDef } from './types.js';
3
2
  import { StrictSchema } from '../types.js';
4
3
  export declare const updateTypeDefs: (schema: StrictSchema) => {
5
4
  schemaTypesParsed: {
@@ -9,5 +8,4 @@ export declare const updateTypeDefs: (schema: StrictSchema) => {
9
8
  [id: number]: SchemaTypeDef;
10
9
  };
11
10
  };
12
- export declare const createSchemaTypeDef: (typeName: string, type: StrictSchemaType | SchemaObject, parsed: SchemaTypesParsed, locales: Partial<SchemaLocales>, result?: Partial<SchemaTypeDef>, path?: string[], top?: boolean) => SchemaTypeDef;
13
11
  //# sourceMappingURL=typeDef.d.ts.map
@@ -1,10 +1,10 @@
1
1
  import { isPropType, getPropType, } from '../index.js';
2
2
  import { setByPath } from '@based/utils';
3
- import { TYPE_INDEX_MAP, REFERENCES, REFERENCE, NUMBER, BLOCK_CAPACITY_MAX, BLOCK_CAPACITY_DEFAULT, BLOCK_CAPACITY_MIN, ALIAS, ALIASES, VECTOR, COLVEC, } from './types.js';
3
+ import { TYPE_INDEX_MAP, REFERENCES, REFERENCE, NUMBER, BLOCK_CAPACITY_MAX, BLOCK_CAPACITY_DEFAULT, BLOCK_CAPACITY_MIN, VECTOR, COLVEC, CARDINALITY, } from './types.js';
4
4
  import { DEFAULT_MAP } from './defaultMap.js';
5
5
  import { makeSeparateTextSort } from './makeSeparateTextSort.js';
6
6
  import { makeSeparateSort } from './makeSeparateSort.js';
7
- import { getPropLen, isSeparate, parseMinMaxStep, schemaVectorBaseTypeToEnum, } from './utils.js';
7
+ import { getPropLen, isSeparate, parseMinMaxStep, reorderProps, schemaVectorBaseTypeToEnum, sortMainProps, cardinalityModeToEnum, } from './utils.js';
8
8
  import { addEdges } from './addEdges.js';
9
9
  import { createEmptyDef } from './createEmptyDef.js';
10
10
  import { fillEmptyMain, isZeroes } from './fillEmptyMain.js';
@@ -12,14 +12,6 @@ import { defaultValidation, VALIDATION_MAP } from './validation.js';
12
12
  export const updateTypeDefs = (schema) => {
13
13
  const schemaTypesParsed = {};
14
14
  const schemaTypesParsedById = {};
15
- for (const typeName in schemaTypesParsed) {
16
- if (typeName in schema.types) {
17
- continue;
18
- }
19
- const id = schemaTypesParsed[typeName].id;
20
- delete schemaTypesParsed[typeName];
21
- delete schemaTypesParsedById[id];
22
- }
23
15
  for (const typeName in schema.types) {
24
16
  const type = schema.types[typeName];
25
17
  if (!type.id) {
@@ -45,33 +37,7 @@ export const updateTypeDefs = (schema) => {
45
37
  }
46
38
  return { schemaTypesParsed, schemaTypesParsedById };
47
39
  };
48
- function propIndexOffset(prop) {
49
- if (!prop.separate) {
50
- return 0;
51
- }
52
- switch (prop.typeIndex) {
53
- case REFERENCES:
54
- case REFERENCE:
55
- return -300;
56
- case ALIAS:
57
- case ALIASES:
58
- case COLVEC:
59
- return 300;
60
- default:
61
- return 0;
62
- }
63
- }
64
- function reorderProps(props) {
65
- props.sort((a, b) => a.prop + propIndexOffset(a) - (b.prop + propIndexOffset(b)));
66
- // Reassign prop indices
67
- let lastProp = 0;
68
- for (const p of props) {
69
- if (p.separate) {
70
- p.prop = ++lastProp;
71
- }
72
- }
73
- }
74
- export const createSchemaTypeDef = (typeName, type, parsed, locales, result = createEmptyDef(typeName, type, locales), path = [], top = true) => {
40
+ const createSchemaTypeDef = (typeName, type, parsed, locales, result = createEmptyDef(typeName, type, locales), path = [], top = true) => {
75
41
  if (top) {
76
42
  if (result.id == 0) {
77
43
  if ('id' in type) {
@@ -171,6 +137,11 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
171
137
  if (prop.typeIndex === VECTOR || prop.typeIndex === COLVEC) {
172
138
  prop.vectorBaseType = schemaVectorBaseTypeToEnum(schemaProp.baseType ?? 'number');
173
139
  }
140
+ if (prop.typeIndex === CARDINALITY) {
141
+ prop.cardinalityMode ??= cardinalityModeToEnum((schemaProp.mode ??= 'sparse'));
142
+ const prec = typeName == '_root' ? 14 : 8;
143
+ prop.cardinalityPrecision ??= schemaProp.precision ??= prec;
144
+ }
174
145
  if (isPropType('enum', schemaProp)) {
175
146
  prop.enum = Array.isArray(schemaProp) ? schemaProp : schemaProp.enum;
176
147
  prop.reverseEnum = {};
@@ -247,15 +218,16 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
247
218
  }
248
219
  }
249
220
  }
250
- else {
251
- if (!result.mainLen) {
252
- len += 2;
253
- }
254
- len += 1;
255
- f.start = result.mainLen;
256
- result.mainLen += f.len;
257
- setByPath(result.tree, f.path, f);
221
+ }
222
+ const mainProps = vals.filter((v) => !v.separate).sort(sortMainProps);
223
+ for (const f of mainProps) {
224
+ if (!result.mainLen) {
225
+ len += 2;
258
226
  }
227
+ len += 1;
228
+ f.start = result.mainLen;
229
+ result.mainLen += f.len;
230
+ setByPath(result.tree, f.path, f);
259
231
  }
260
232
  if (result.hasSeperateDefaults) {
261
233
  result.separateDefaults.bufferTmp = new Uint8Array(biggestSeperatePropDefault + 1);
@@ -33,6 +33,8 @@ export type PropDef = {
33
33
  default: any;
34
34
  vectorBaseType?: VectorBaseType;
35
35
  vectorSize?: number;
36
+ cardinalityMode?: number;
37
+ cardinalityPrecision?: number;
36
38
  edgeMainLen?: 0;
37
39
  hasDefaultEdges?: boolean;
38
40
  reverseEnum?: {
@@ -1,9 +1,13 @@
1
1
  import { PropDef, PropDefEdge, VectorBaseType } from './types.js';
2
- import { SchemaProp, SchemaVectorBaseType } from '../types.js';
2
+ import { SchemaProp, SchemaVectorBaseType, HLLRegisterRepresentation } from '../types.js';
3
3
  export declare function isSeparate(schemaProp: SchemaProp, len: number): boolean;
4
4
  export declare const propIsSigned: (prop: PropDef | PropDefEdge) => boolean;
5
5
  export declare const propIsNumerical: (prop: PropDef | PropDefEdge) => boolean;
6
6
  export declare const schemaVectorBaseTypeToEnum: (vector: SchemaVectorBaseType) => VectorBaseType;
7
+ export declare const cardinalityModeToEnum: (mode: HLLRegisterRepresentation) => number;
7
8
  export declare function getPropLen(schemaProp: SchemaProp): number;
8
9
  export declare const parseMinMaxStep: (val: any) => string | number;
10
+ export declare const sortMainProps: (a: PropDef | PropDefEdge, b: PropDef | PropDefEdge) => 0 | 1 | -1;
11
+ export declare const propIndexOffset: (prop: PropDef) => 0 | 300 | -300;
12
+ export declare const reorderProps: (props: PropDef[]) => void;
9
13
  //# sourceMappingURL=utils.d.ts.map
package/dist/def/utils.js CHANGED
@@ -1,5 +1,5 @@
1
- import { INT16, INT32, INT8, UINT16, UINT32, UINT8, NUMBER, TIMESTAMP, SIZE_MAP, VECTOR_BASE_TYPE_SIZE_MAP, VectorBaseType, } from './types.js';
2
- import { isPropType } from '../types.js';
1
+ import { INT16, INT32, INT8, UINT16, UINT32, UINT8, NUMBER, TIMESTAMP, SIZE_MAP, VECTOR_BASE_TYPE_SIZE_MAP, VectorBaseType, REVERSE_SIZE_MAP, REFERENCES, REFERENCE, ALIAS, ALIASES, COLVEC, } from './types.js';
2
+ import { isPropType, } from '../types.js';
3
3
  import { getPropType } from '../parse/utils.js';
4
4
  import { convertToTimestamp } from '@based/utils';
5
5
  export function isSeparate(schemaProp, len) {
@@ -50,6 +50,12 @@ export const schemaVectorBaseTypeToEnum = (vector) => {
50
50
  return VectorBaseType.Float64;
51
51
  }
52
52
  };
53
+ export const cardinalityModeToEnum = (mode) => {
54
+ if (mode === 'dense')
55
+ return 1;
56
+ else
57
+ 0;
58
+ };
53
59
  export function getPropLen(schemaProp) {
54
60
  let len = SIZE_MAP[getPropType(schemaProp)];
55
61
  if (isPropType('string', schemaProp) ||
@@ -86,4 +92,44 @@ export const parseMinMaxStep = (val) => {
86
92
  return val;
87
93
  }
88
94
  };
95
+ export const sortMainProps = (a, b) => {
96
+ const sizeA = REVERSE_SIZE_MAP[a.typeIndex];
97
+ const sizeB = REVERSE_SIZE_MAP[b.typeIndex];
98
+ if (sizeA === 8) {
99
+ return -1;
100
+ }
101
+ if (sizeA === 4 && sizeB !== 8) {
102
+ return -1;
103
+ }
104
+ if (sizeA === sizeB) {
105
+ return 0;
106
+ }
107
+ return 1;
108
+ };
109
+ export const propIndexOffset = (prop) => {
110
+ if (!prop.separate) {
111
+ return 0;
112
+ }
113
+ switch (prop.typeIndex) {
114
+ case REFERENCES:
115
+ case REFERENCE:
116
+ return -300;
117
+ case ALIAS:
118
+ case ALIASES:
119
+ case COLVEC:
120
+ return 300;
121
+ default:
122
+ return 0;
123
+ }
124
+ };
125
+ export const reorderProps = (props) => {
126
+ props.sort((a, b) => a.prop + propIndexOffset(a) - (b.prop + propIndexOffset(b)));
127
+ // Reassign prop indices
128
+ let lastProp = 0;
129
+ for (const p of props) {
130
+ if (p.separate) {
131
+ p.prop = ++lastProp;
132
+ }
133
+ }
134
+ };
89
135
  //# sourceMappingURL=utils.js.map
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './types.js';
2
+ export * from './dbSchema.js';
2
3
  export * from './parse/index.js';
3
4
  export * from './lang.js';
4
5
  export * from './def/validation.js';
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './types.js';
2
+ export * from './dbSchema.js';
2
3
  export * from './parse/index.js';
3
4
  export * from './lang.js';
4
5
  export * from './def/validation.js';
package/dist/infer.d.ts CHANGED
@@ -1,14 +1,24 @@
1
1
  import { Schema } from './types.js';
2
- type InferString = string;
3
- type InferNumber = number;
4
- type InferBoolean = boolean;
5
- type InferText = string;
6
- type InferJson = any;
7
- type InferTimestamp = number;
8
- type InferBinary = Uint8Array;
9
- type InferCardinality = number;
10
- type InferVector = Float32Array;
11
- type InferColvec = Float32Array;
2
+ type TypedArray = Uint8Array | Float32Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
3
+ type TypeMap = {
4
+ string: string;
5
+ number: number;
6
+ int8: number;
7
+ uint8: number;
8
+ int16: number;
9
+ uint16: number;
10
+ int32: number;
11
+ uint32: number;
12
+ boolean: boolean;
13
+ text: string;
14
+ json: any;
15
+ timestamp: number | string | Date;
16
+ binary: Uint8Array;
17
+ cardinality: number;
18
+ vector: TypedArray;
19
+ colvec: TypedArray;
20
+ alias: string;
21
+ };
12
22
  type InferEnum<T extends readonly (string | number | boolean)[]> = T[number];
13
23
  type InferReference<RefName extends string, Types> = RefName extends keyof Types ? {
14
24
  id: number;
@@ -25,38 +35,20 @@ type InferObject<T extends Record<string, any>, Types> = {
25
35
  };
26
36
  type InferSet<T, Types> = InferProp<T, Types>[];
27
37
  type InferProp<T, Types> = T extends {
28
- type: 'string';
29
- } ? InferString : T extends {
30
- type: 'number' | 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32';
31
- } ? InferNumber : T extends {
32
- type: 'boolean';
33
- } ? InferBoolean : T extends {
34
- type: 'text';
35
- } ? InferText : T extends {
36
- type: 'json';
37
- } ? InferJson : T extends {
38
- type: 'timestamp';
39
- } ? InferTimestamp : T extends {
40
- type: 'binary';
41
- } ? InferBinary : T extends {
42
- type: 'cardinality';
43
- } ? InferCardinality : T extends {
44
- type: 'vector';
45
- } ? InferVector : T extends {
46
- type: 'colvec';
47
- } ? InferColvec : T extends {
48
- enum: infer E;
49
- } ? E extends readonly (string | number | boolean)[] ? InferEnum<E> : never : T extends {
50
- ref: infer R extends string;
51
- } ? InferReference<R, Types> : T extends {
38
+ props: infer P;
39
+ } ? InferObject<P, Types> : T extends {
52
40
  items: {
53
41
  ref: infer R extends string;
54
42
  };
55
43
  } ? InferReferences<R, Types> : T extends {
56
44
  items: infer I;
57
45
  } ? InferSet<I, Types> : T extends {
58
- props: infer P;
59
- } ? InferObject<P, Types> : T extends string ? T extends 'string' ? InferString : T extends 'number' | 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32' ? InferNumber : T extends 'boolean' ? InferBoolean : T extends 'text' ? InferText : T extends 'json' ? InferJson : T extends 'timestamp' ? InferTimestamp : T extends 'binary' ? InferBinary : T extends 'cardinality' ? InferCardinality : T extends 'vector' ? InferVector : T extends 'colvec' ? InferColvec : never : never;
46
+ ref: infer R extends string;
47
+ } ? InferReference<R, Types> : T extends {
48
+ enum: infer E;
49
+ } ? E extends readonly (string | number | boolean)[] ? InferEnum<E> : never : T extends {
50
+ type: infer U;
51
+ } ? U extends keyof TypeMap ? TypeMap[U] : never : T extends keyof TypeMap ? TypeMap[T] : never;
60
52
  type InferSchemaType<T, Types> = T extends {
61
53
  props: infer Props;
62
54
  } ? {
@@ -504,6 +504,18 @@ p.cardinality = propParser(STUB, {
504
504
  default(val, prop, ctx) {
505
505
  return isDefault(val, prop, ctx);
506
506
  },
507
+ mode(val, prop, ctx) {
508
+ if (!['dense', 'sparse'].includes(val)) {
509
+ throw Error(INVALID_VALUE);
510
+ }
511
+ p.mode = val;
512
+ },
513
+ precision(val, prop, ctx) {
514
+ if (val < 2 || val > 16) {
515
+ throw Error(INVALID_VALUE);
516
+ }
517
+ p.precision = val;
518
+ },
507
519
  }, 0);
508
520
  p.json = propParser(STUB, {
509
521
  default(val, prop, ctx) {
package/dist/types.d.ts CHANGED
@@ -2,7 +2,7 @@ import type { LangName } from './lang.js';
2
2
  import type { Validation } from './def/validation.js';
3
3
  type Role = 'title' | 'source' | 'media' | string;
4
4
  export declare const numberDisplays: readonly ["short", "human", "ratio", "bytes", "euro", "dollar", "pound", "meter"];
5
- export declare const dateDisplays: readonly ["date", "date-time", "date-time-text", "date-time-human", "time", "time-precise"];
5
+ export declare const dateDisplays: readonly ["date", "date-time", "date-time-text", "date-time-human", "date-time-human-short", "time", "time-precise"];
6
6
  export declare const stringFormats: readonly ["alpha", "alphaLocales", "alphanumeric", "alphanumericLocales", "ascii", "base32", "base58", "base64", "BIC", "btcAddress", "clike", "code", "creditCard", "css", "currency", "dataURI", "EAN", "email", "ethereumAddress", "FQDN", "hexadecimal", "hexColor", "HSL", "html", "IBAN", "identityCard", "IMEI", "IP", "IPRange", "ISBN", "ISIN", "ISO31661Alpha2", "ISO31661Alpha3", "ISO4217", "ISO6391", "ISO8601", "ISRC", "ISSN", "javascript", "json", "JWT", "latLong", "licensePlate", "lowercase", "luhnNumber", "MACAddress", "magnetURI", "markdown", "MD5", "mimeType", "mobilePhone", "mobilePhoneLocales", "octal", "password", "passportNumber", "port", "postalCode", "postalCodeLocales", "python", "RFC3339", "rgbColor", "rust", "semVer", "slug", "surrogatePair", "taxID", "typescript", "uppercase", "URL", "UUID", "VAT", "multiline"];
7
7
  type DateDisplay = (typeof dateDisplays)[number];
8
8
  type NumberDisplay = (typeof numberDisplays)[number] | `round-${number}`;
@@ -85,11 +85,12 @@ export type SchemaBoolean = Prop<{
85
85
  type: 'boolean';
86
86
  default?: boolean;
87
87
  }>;
88
+ export type HLLRegisterRepresentation = 'sparse' | 'dense';
88
89
  export type SchemaCardinality = Prop<{
89
90
  type: 'cardinality';
90
91
  maxBytes?: number;
91
- mime?: Mime;
92
- format?: NumberDisplay;
92
+ precision?: number;
93
+ mode?: HLLRegisterRepresentation;
93
94
  }>;
94
95
  type VectorDefaultType = Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
95
96
  export type SchemaVectorBaseType = NumberType | 'float32' | 'float64';
package/dist/types.js CHANGED
@@ -14,6 +14,7 @@ export const dateDisplays = [
14
14
  'date-time',
15
15
  'date-time-text',
16
16
  'date-time-human',
17
+ 'date-time-human-short',
17
18
  'time',
18
19
  'time-precise',
19
20
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/schema",
3
- "version": "5.0.2",
3
+ "version": "5.0.4-alpha.1",
4
4
  "files": [
5
5
  "dist",
6
6
  "README.md",