@based/schema 5.0.2 → 5.0.3

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, } 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, } 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) {
@@ -247,15 +213,16 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
247
213
  }
248
214
  }
249
215
  }
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);
216
+ }
217
+ const mainProps = vals.filter((v) => !v.separate).sort(sortMainProps);
218
+ for (const f of mainProps) {
219
+ if (!result.mainLen) {
220
+ len += 2;
258
221
  }
222
+ len += 1;
223
+ f.start = result.mainLen;
224
+ result.mainLen += f.len;
225
+ setByPath(result.tree, f.path, f);
259
226
  }
260
227
  if (result.hasSeperateDefaults) {
261
228
  result.separateDefaults.bufferTmp = new Uint8Array(biggestSeperatePropDefault + 1);
@@ -6,4 +6,7 @@ export declare const propIsNumerical: (prop: PropDef | PropDefEdge) => boolean;
6
6
  export declare const schemaVectorBaseTypeToEnum: (vector: SchemaVectorBaseType) => VectorBaseType;
7
7
  export declare function getPropLen(schemaProp: SchemaProp): number;
8
8
  export declare const parseMinMaxStep: (val: any) => string | number;
9
+ export declare const sortMainProps: (a: PropDef | PropDefEdge, b: PropDef | PropDefEdge) => 0 | 1 | -1;
10
+ export declare const propIndexOffset: (prop: PropDef) => 0 | 300 | -300;
11
+ export declare const reorderProps: (props: PropDef[]) => void;
9
12
  //# sourceMappingURL=utils.d.ts.map
package/dist/def/utils.js CHANGED
@@ -1,4 +1,4 @@
1
- import { INT16, INT32, INT8, UINT16, UINT32, UINT8, NUMBER, TIMESTAMP, SIZE_MAP, VECTOR_BASE_TYPE_SIZE_MAP, VectorBaseType, } 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
2
  import { isPropType } from '../types.js';
3
3
  import { getPropType } from '../parse/utils.js';
4
4
  import { convertToTimestamp } from '@based/utils';
@@ -86,4 +86,44 @@ export const parseMinMaxStep = (val) => {
86
86
  return val;
87
87
  }
88
88
  };
89
+ export const sortMainProps = (a, b) => {
90
+ const sizeA = REVERSE_SIZE_MAP[a.typeIndex];
91
+ const sizeB = REVERSE_SIZE_MAP[b.typeIndex];
92
+ if (sizeA === 8) {
93
+ return -1;
94
+ }
95
+ if (sizeA === 4 && sizeB !== 8) {
96
+ return -1;
97
+ }
98
+ if (sizeA === sizeB) {
99
+ return 0;
100
+ }
101
+ return 1;
102
+ };
103
+ export const propIndexOffset = (prop) => {
104
+ if (!prop.separate) {
105
+ return 0;
106
+ }
107
+ switch (prop.typeIndex) {
108
+ case REFERENCES:
109
+ case REFERENCE:
110
+ return -300;
111
+ case ALIAS:
112
+ case ALIASES:
113
+ case COLVEC:
114
+ return 300;
115
+ default:
116
+ return 0;
117
+ }
118
+ };
119
+ export const reorderProps = (props) => {
120
+ props.sort((a, b) => a.prop + propIndexOffset(a) - (b.prop + propIndexOffset(b)));
121
+ // Reassign prop indices
122
+ let lastProp = 0;
123
+ for (const p of props) {
124
+ if (p.separate) {
125
+ p.prop = ++lastProp;
126
+ }
127
+ }
128
+ };
89
129
  //# 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
  } ? {
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}`;
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.3",
4
4
  "files": [
5
5
  "dist",
6
6
  "README.md",