@based/schema 5.0.0-alpha.7 → 5.0.0-alpha.8

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.
@@ -8,6 +8,7 @@ export declare const createEmptyDef: (typeName: string, type: StrictSchemaType |
8
8
  reverseProps: {};
9
9
  idUint8: Uint8Array;
10
10
  id: number;
11
+ mainEmpty: Uint8Array;
11
12
  mainLen: number;
12
13
  separate: any[];
13
14
  tree: {};
@@ -8,7 +8,9 @@ export const createEmptyDef = (typeName, type, locales) => {
8
8
  props: {},
9
9
  reverseProps: {},
10
10
  idUint8: new Uint8Array([0, 0]),
11
+ // empty main buffer
11
12
  id: 0,
13
+ mainEmpty: new Uint8Array(0),
12
14
  mainLen: 0,
13
15
  separate: [],
14
16
  tree: {},
@@ -0,0 +1,5 @@
1
+ import { PropDef } from './types.js';
2
+ export declare const ENCODER: TextEncoder;
3
+ export declare const fillEmptyMain: (vals: PropDef[], mainLen: number) => Uint8Array;
4
+ export declare const isZeroes: (buf: Uint8Array) => boolean;
5
+ //# sourceMappingURL=fillEmptyMain.d.ts.map
@@ -0,0 +1,56 @@
1
+ import { BINARY, BOOLEAN, CREATED, ENUM, INT16, INT32, INT8, NUMBER, STRING, TIMESTAMP, UINT16, UINT32, UINT8, UPDATED, } from './types.js';
2
+ // Lets add validation of values in here - need to validate DEFAULT!
3
+ export const ENCODER = new TextEncoder();
4
+ export const fillEmptyMain = (vals, mainLen) => {
5
+ const mainEmpty = new Uint8Array(mainLen);
6
+ for (const f of vals) {
7
+ if (f.separate) {
8
+ continue;
9
+ }
10
+ const t = f.typeIndex;
11
+ const s = f.start;
12
+ let val = f.default;
13
+ if (t === BOOLEAN || t === INT8 || t === UINT8 || t === ENUM) {
14
+ mainEmpty[s] = val === true ? 1 : 0;
15
+ }
16
+ else if (t === UINT32 || t === INT32) {
17
+ mainEmpty[s] = val;
18
+ mainEmpty[s + 1] = val >>>= 8;
19
+ mainEmpty[s + 2] = val >>>= 8;
20
+ mainEmpty[s + 3] = val >>>= 8;
21
+ }
22
+ else if (t === UINT16 || t === INT16) {
23
+ mainEmpty[s] = val;
24
+ mainEmpty[s + 1] = val >>>= 8;
25
+ }
26
+ else if (t === NUMBER ||
27
+ t === TIMESTAMP ||
28
+ t === CREATED ||
29
+ t === UPDATED) {
30
+ const view = new DataView(mainEmpty.buffer, s, 8);
31
+ view.setFloat64(0, val, true);
32
+ }
33
+ else if (t === STRING) {
34
+ val = ENCODER.encode(val);
35
+ mainEmpty[s] = val.byteLength;
36
+ mainEmpty.set(val, s + 1);
37
+ }
38
+ else if (t === BINARY) {
39
+ if (val !== undefined) {
40
+ mainEmpty.set(val, s);
41
+ }
42
+ }
43
+ }
44
+ return mainEmpty;
45
+ };
46
+ export const isZeroes = (buf) => {
47
+ let i = 0;
48
+ while (i < buf.byteLength) {
49
+ if (buf[i] !== 0) {
50
+ return false;
51
+ }
52
+ i++;
53
+ }
54
+ return true;
55
+ };
56
+ //# sourceMappingURL=fillEmptyMain.js.map
@@ -1,4 +1,4 @@
1
- import { REVERSE_SIZE_MAP } from './types.js';
1
+ import { DEFAULT_MAP, REVERSE_SIZE_MAP, } from './types.js';
2
2
  export const readFromPacked = (packed) => {
3
3
  const size = (packed[0] | (packed[1] << 8)) >>> 0;
4
4
  const props = [];
@@ -86,6 +86,8 @@ export const readFromPacked = (packed) => {
86
86
  bufferTmp: new Uint8Array([]),
87
87
  props: [],
88
88
  },
89
+ mainEmpty: new Uint8Array([]),
90
+ mainEmptyAllZeroes: true,
89
91
  // need this...
90
92
  locales: {},
91
93
  localeSize: 0,
@@ -98,6 +100,7 @@ export const readFromPacked = (packed) => {
98
100
  separate: false,
99
101
  __isPropDef: true,
100
102
  start: s,
103
+ default: DEFAULT_MAP[p.typeIndex], // tmp
101
104
  typeIndex: p.typeIndex,
102
105
  path: p.path.split('.'),
103
106
  len,
@@ -113,6 +116,7 @@ export const readFromPacked = (packed) => {
113
116
  __isPropDef: true,
114
117
  start: 0,
115
118
  typeIndex: p.typeIndex,
119
+ default: DEFAULT_MAP[p.typeIndex], // tmp
116
120
  path: p.path.split('.'),
117
121
  len: 0,
118
122
  compression: 1,
@@ -1,6 +1,6 @@
1
1
  import { isPropType, getPropType, } from '../index.js';
2
2
  import { setByPath } from '@saulx/utils';
3
- import { TYPE_INDEX_MAP, REFERENCES, REFERENCE, } from './types.js';
3
+ import { TYPE_INDEX_MAP, REFERENCES, REFERENCE, DEFAULT_MAP, } from './types.js';
4
4
  import { makePacked } from './makePacked.js';
5
5
  import { makeSeparateTextSort } from './makeSeparateTextSort.js';
6
6
  import { makeSeparateSort } from './makeSeparateSort.js';
@@ -9,6 +9,7 @@ import { isSeparate } from './utils.js';
9
9
  import { addEdges } from './addEdges.js';
10
10
  import { createEmptyDef } from './createEmptyDef.js';
11
11
  import { hashObjectIgnoreKeyOrder } from '@saulx/hash';
12
+ import { fillEmptyMain, isZeroes } from './fillEmptyMain.js';
12
13
  export const DEFAULT_BLOCK_CAPACITY = 100_000;
13
14
  export const updateTypeDefs = (schema, schemaTypesParsed, schemaTypesParsedById) => {
14
15
  for (const field in schemaTypesParsed) {
@@ -57,6 +58,7 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
57
58
  let separateSortProps = 0;
58
59
  let separateSortText = 0;
59
60
  for (const key in target) {
61
+ // Create prop def
60
62
  const schemaProp = target[key];
61
63
  const propPath = [...path, key];
62
64
  const propType = getPropType(schemaProp);
@@ -89,6 +91,7 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
89
91
  path: propPath,
90
92
  start: 0,
91
93
  len,
94
+ default: schemaProp.default ?? DEFAULT_MAP[TYPE_INDEX_MAP[propType]],
92
95
  prop: isseparate ? ++result.cnt : 0,
93
96
  };
94
97
  if (isPropType('enum', schemaProp)) {
@@ -138,6 +141,7 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
138
141
  }
139
142
  }
140
143
  if (top) {
144
+ // Put top level together
141
145
  const vals = Object.values(result.props);
142
146
  vals.sort((a, b) => {
143
147
  if (b.separate &&
@@ -168,6 +172,8 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
168
172
  setByPath(result.tree, f.path, f);
169
173
  }
170
174
  }
175
+ result.mainEmpty = fillEmptyMain(vals, result.mainLen);
176
+ result.mainEmptyAllZeroes = isZeroes(result.mainEmpty);
171
177
  makePacked(result, typeName, vals, len);
172
178
  if (separateSortText > 0) {
173
179
  makeSeparateTextSort(result);
@@ -69,6 +69,7 @@ export type PropDef = {
69
69
  inversePropNumber?: number;
70
70
  enum?: any[];
71
71
  dependent?: boolean;
72
+ default: any;
72
73
  edgeMainLen?: 0;
73
74
  reverseEnum?: {
74
75
  [key: string]: number;
@@ -83,6 +84,7 @@ export type PropDef = {
83
84
  reverseMainEdges?: {
84
85
  [start: string]: PropDefEdge;
85
86
  };
87
+ edgeMainEmpty?: Uint8Array;
86
88
  __isEdge?: boolean;
87
89
  };
88
90
  export type PropDefEdge = Partial<PropDef> & {
@@ -133,6 +135,8 @@ export type SchemaTypeDef = {
133
135
  main: {
134
136
  [start: string]: PropDef;
135
137
  };
138
+ mainEmpty: Uint8Array;
139
+ mainEmptyAllZeroes: boolean;
136
140
  tree: SchemaPropTree;
137
141
  hasSeperateSort: boolean;
138
142
  seperateSort: SchemaSortUndefinedHandler;
@@ -148,7 +152,8 @@ export type SchemaTypeDef = {
148
152
  localeSize: number;
149
153
  };
150
154
  export declare const SIZE_MAP: Record<InternalSchemaProp, number>;
151
- export declare let REVERSE_SIZE_MAP: Record<TypeIndex, number>;
155
+ export declare const DEFAULT_MAP: Record<TypeIndex, any>;
156
+ export declare const REVERSE_SIZE_MAP: Record<TypeIndex, number>;
152
157
  export declare const REVERSE_TYPE_INDEX_MAP: Record<TypeIndex, InternalSchemaProp>;
153
158
  export declare const ID_FIELD_DEF: PropDef;
154
159
  export declare const EMPTY_MICRO_BUFFER: PropDef;
package/dist/def/types.js CHANGED
@@ -83,9 +83,34 @@ const reverseMap = {};
83
83
  for (const k in TYPE_INDEX_MAP) {
84
84
  reverseMap[TYPE_INDEX_MAP[k]] = k;
85
85
  }
86
- export let REVERSE_SIZE_MAP;
87
- // @ts-ignore
88
- REVERSE_SIZE_MAP = {};
86
+ // TODO update defaults
87
+ export const DEFAULT_MAP = {
88
+ [TYPE_INDEX_MAP.alias]: '',
89
+ [TYPE_INDEX_MAP.binary]: undefined,
90
+ [TYPE_INDEX_MAP.boolean]: false,
91
+ [TYPE_INDEX_MAP.cardinality]: 0,
92
+ [TYPE_INDEX_MAP.created]: 0,
93
+ [TYPE_INDEX_MAP.updated]: 0,
94
+ [TYPE_INDEX_MAP.number]: 0,
95
+ [TYPE_INDEX_MAP.timestamp]: 0,
96
+ [TYPE_INDEX_MAP.enum]: 0,
97
+ [TYPE_INDEX_MAP.id]: 0,
98
+ [TYPE_INDEX_MAP.int16]: 0,
99
+ [TYPE_INDEX_MAP.int32]: 0,
100
+ [TYPE_INDEX_MAP.int8]: 0,
101
+ [TYPE_INDEX_MAP.uint8]: 0,
102
+ [TYPE_INDEX_MAP.uint16]: 0,
103
+ [TYPE_INDEX_MAP.uint32]: 0,
104
+ [TYPE_INDEX_MAP.json]: undefined,
105
+ [TYPE_INDEX_MAP.microbuffer]: undefined,
106
+ [TYPE_INDEX_MAP.reference]: undefined,
107
+ [TYPE_INDEX_MAP.references]: [],
108
+ [TYPE_INDEX_MAP.string]: '',
109
+ [TYPE_INDEX_MAP.aliases]: [],
110
+ [TYPE_INDEX_MAP.text]: '',
111
+ [TYPE_INDEX_MAP.vector]: undefined, // maybe not can set a vec with 0
112
+ };
113
+ export const REVERSE_SIZE_MAP = {};
89
114
  for (const k in SIZE_MAP) {
90
115
  REVERSE_SIZE_MAP[TYPE_INDEX_MAP[k]] = SIZE_MAP[k];
91
116
  }
@@ -96,6 +121,7 @@ export const ID_FIELD_DEF = {
96
121
  path: ['id'],
97
122
  start: 0,
98
123
  prop: 255,
124
+ default: 0,
99
125
  len: 4,
100
126
  __isPropDef: true,
101
127
  };
@@ -104,6 +130,7 @@ export const EMPTY_MICRO_BUFFER = {
104
130
  separate: true,
105
131
  path: [''],
106
132
  start: 0,
133
+ default: undefined,
107
134
  prop: 0,
108
135
  len: 1,
109
136
  __isPropDef: true,
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 | 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>;
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>;
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];
@@ -12,7 +12,7 @@ export declare class SchemaParser {
12
12
  parseTypes(): void;
13
13
  parseProps(props: any, schemaType?: SchemaType): void;
14
14
  parseLocales(): void;
15
- parse(): void;
15
+ parse(): StrictSchema;
16
16
  }
17
17
  export declare const print: (schema: Schema, path: string[]) => string;
18
18
  export declare const parse: (schema: Schema) => {
@@ -3,10 +3,11 @@ import { getPropType } from './utils.js';
3
3
  import propParsers from './props.js';
4
4
  import pc from 'picocolors';
5
5
  import { expectBoolean, expectObject } from './assert.js';
6
+ import { deepCopy } from '@saulx/utils';
6
7
  export { getPropType };
7
8
  export class SchemaParser {
8
9
  constructor(schema) {
9
- this.schema = schema;
10
+ this.schema = deepCopy(schema);
10
11
  }
11
12
  isItems;
12
13
  inQuery;
@@ -97,6 +98,7 @@ export class SchemaParser {
97
98
  throw Error(UNKNOWN_PROP);
98
99
  }
99
100
  }
101
+ return this.schema;
100
102
  }
101
103
  }
102
104
  export const print = (schema, path) => {
@@ -120,9 +122,7 @@ export const print = (schema, path) => {
120
122
  export const parse = (schema) => {
121
123
  const parser = new SchemaParser(schema);
122
124
  try {
123
- parser.parse();
124
- // @ts-ignore
125
- return { schema };
125
+ return { schema: parser.parse() };
126
126
  }
127
127
  catch (e) {
128
128
  const cause = parser.path.slice(0, Math.min(4, parser.lvl) + 1);
@@ -270,6 +270,7 @@ p.text = propParser({
270
270
  throw Error(TEXT_REQUIRES_LOCALES);
271
271
  },
272
272
  }, {
273
+ format: binaryOpts.format,
273
274
  default(val, prop) {
274
275
  console.warn('MAKE DEFAULT VALUE FOR TEXT');
275
276
  },
package/dist/types.d.ts CHANGED
@@ -40,6 +40,7 @@ export type SchemaReferencesOneWay = Prop<{
40
40
  export type SchemaText = Prop<{
41
41
  type: 'text';
42
42
  default?: Record<string, string>;
43
+ format?: StringFormat;
43
44
  }>;
44
45
  type NumberType = 'number' | 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32';
45
46
  export type SchemaNumber = Prop<{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/schema",
3
- "version": "5.0.0-alpha.7",
3
+ "version": "5.0.0-alpha.8",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist",
@@ -8,6 +8,7 @@
8
8
  "package.json",
9
9
  "!dist/**/*.map"
10
10
  ],
11
+ "main": "./dist/index.js",
11
12
  "exports": {
12
13
  "./def": "./dist/def/index.js",
13
14
  ".": "./dist/index.js"
@@ -28,7 +29,7 @@
28
29
  "typescript": "^5.6.3"
29
30
  },
30
31
  "dependencies": {
31
- "@saulx/utils": "^5.0.0",
32
+ "@saulx/utils": "^6.1.1",
32
33
  "picocolors": "^1.1.0"
33
34
  }
34
- }
35
+ }