tinybase 9.4.0 → 9.5.0-beta.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.
Files changed (126) hide show
  1. package/@types/_internal/store/with-schemas/index.d.ts +43 -8
  2. package/@types/mergeable-store/with-schemas/index.d.ts +30 -11
  3. package/@types/queries/index.d.ts +11 -11
  4. package/@types/queries/with-schemas/index.d.ts +17 -3
  5. package/@types/schematizers/index.d.ts +6 -4
  6. package/@types/schematizers/schematizer-arktype/index.d.ts +6 -6
  7. package/@types/schematizers/schematizer-arktype/with-schemas/index.d.ts +112 -1
  8. package/@types/schematizers/schematizer-effect/index.d.ts +9 -11
  9. package/@types/schematizers/schematizer-effect/with-schemas/index.d.ts +114 -1
  10. package/@types/schematizers/schematizer-typebox/index.d.ts +7 -7
  11. package/@types/schematizers/schematizer-typebox/with-schemas/index.d.ts +114 -1
  12. package/@types/schematizers/schematizer-valibot/index.d.ts +6 -6
  13. package/@types/schematizers/schematizer-valibot/with-schemas/index.d.ts +112 -1
  14. package/@types/schematizers/schematizer-yup/index.d.ts +5 -4
  15. package/@types/schematizers/schematizer-yup/with-schemas/index.d.ts +112 -1
  16. package/@types/schematizers/schematizer-zod/index.d.ts +6 -6
  17. package/@types/schematizers/schematizer-zod/with-schemas/index.d.ts +112 -1
  18. package/@types/schematizers/with-schemas/index.d.ts +113 -4
  19. package/@types/store/index.d.ts +175 -16
  20. package/@types/store/with-schemas/index.d.ts +238 -55
  21. package/@types/synchronizers/with-schemas/index.d.ts +12 -2
  22. package/@types/ui-react/with-schemas/index.d.ts +576 -7
  23. package/@types/ui-react-dom/index.d.ts +10 -10
  24. package/@types/ui-react-dom/with-schemas/index.d.ts +94 -10
  25. package/@types/ui-react-dom-charts/index.d.ts +7 -7
  26. package/@types/ui-react-dom-charts/with-schemas/index.d.ts +7 -7
  27. package/@types/ui-react-inspector/index.d.ts +1 -1
  28. package/@types/ui-react-inspector/with-schemas/index.d.ts +1 -1
  29. package/@types/ui-solid/with-schemas/index.d.ts +340 -7
  30. package/@types/ui-solid-dom/index.d.ts +10 -10
  31. package/@types/ui-solid-dom/with-schemas/index.d.ts +94 -10
  32. package/@types/ui-solid-inspector/index.d.ts +1 -1
  33. package/@types/ui-solid-inspector/with-schemas/index.d.ts +1 -1
  34. package/@types/ui-svelte/with-schemas/index.d.ts +30 -0
  35. package/@types/ui-svelte-dom/index.d.ts +10 -10
  36. package/@types/ui-svelte-dom/with-schemas/index.d.ts +94 -10
  37. package/@types/ui-svelte-inspector/index.d.ts +1 -1
  38. package/@types/ui-svelte-inspector/with-schemas/index.d.ts +1 -1
  39. package/agents.md +4 -0
  40. package/index.js +76 -22
  41. package/mergeable-store/index.js +76 -22
  42. package/mergeable-store/with-schemas/index.js +76 -22
  43. package/min/index.js +1 -1
  44. package/min/index.js.gz +0 -0
  45. package/min/mergeable-store/index.js +1 -1
  46. package/min/mergeable-store/index.js.gz +0 -0
  47. package/min/mergeable-store/with-schemas/index.js +1 -1
  48. package/min/mergeable-store/with-schemas/index.js.gz +0 -0
  49. package/min/omni/index.js +1 -1
  50. package/min/omni/index.js.gz +0 -0
  51. package/min/omni/with-schemas/index.js +1 -1
  52. package/min/omni/with-schemas/index.js.gz +0 -0
  53. package/min/schematizers/index.js +1 -1
  54. package/min/schematizers/index.js.gz +0 -0
  55. package/min/schematizers/schematizer-arktype/index.js +1 -1
  56. package/min/schematizers/schematizer-arktype/index.js.gz +0 -0
  57. package/min/schematizers/schematizer-arktype/with-schemas/index.js +1 -1
  58. package/min/schematizers/schematizer-arktype/with-schemas/index.js.gz +0 -0
  59. package/min/schematizers/schematizer-effect/index.js +1 -1
  60. package/min/schematizers/schematizer-effect/index.js.gz +0 -0
  61. package/min/schematizers/schematizer-effect/with-schemas/index.js +1 -1
  62. package/min/schematizers/schematizer-effect/with-schemas/index.js.gz +0 -0
  63. package/min/schematizers/schematizer-typebox/index.js +1 -1
  64. package/min/schematizers/schematizer-typebox/index.js.gz +0 -0
  65. package/min/schematizers/schematizer-typebox/with-schemas/index.js +1 -1
  66. package/min/schematizers/schematizer-typebox/with-schemas/index.js.gz +0 -0
  67. package/min/schematizers/schematizer-valibot/index.js +1 -1
  68. package/min/schematizers/schematizer-valibot/index.js.gz +0 -0
  69. package/min/schematizers/schematizer-valibot/with-schemas/index.js +1 -1
  70. package/min/schematizers/schematizer-valibot/with-schemas/index.js.gz +0 -0
  71. package/min/schematizers/schematizer-yup/index.js +1 -1
  72. package/min/schematizers/schematizer-yup/index.js.gz +0 -0
  73. package/min/schematizers/schematizer-yup/with-schemas/index.js +1 -1
  74. package/min/schematizers/schematizer-yup/with-schemas/index.js.gz +0 -0
  75. package/min/schematizers/schematizer-zod/index.js +1 -1
  76. package/min/schematizers/schematizer-zod/index.js.gz +0 -0
  77. package/min/schematizers/schematizer-zod/with-schemas/index.js +1 -1
  78. package/min/schematizers/schematizer-zod/with-schemas/index.js.gz +0 -0
  79. package/min/schematizers/with-schemas/index.js +1 -1
  80. package/min/schematizers/with-schemas/index.js.gz +0 -0
  81. package/min/store/index.js +1 -1
  82. package/min/store/index.js.gz +0 -0
  83. package/min/store/with-schemas/index.js +1 -1
  84. package/min/store/with-schemas/index.js.gz +0 -0
  85. package/min/ui-react-inspector/index.js +1 -1
  86. package/min/ui-react-inspector/index.js.gz +0 -0
  87. package/min/ui-react-inspector/with-schemas/index.js +1 -1
  88. package/min/ui-react-inspector/with-schemas/index.js.gz +0 -0
  89. package/min/ui-solid-inspector/index.js +1 -1
  90. package/min/ui-solid-inspector/index.js.gz +0 -0
  91. package/min/ui-solid-inspector/with-schemas/index.js +1 -1
  92. package/min/ui-solid-inspector/with-schemas/index.js.gz +0 -0
  93. package/min/ui-svelte-inspector/index.js +1 -1
  94. package/min/ui-svelte-inspector/index.js.gz +0 -0
  95. package/min/ui-svelte-inspector/with-schemas/index.js +1 -1
  96. package/min/ui-svelte-inspector/with-schemas/index.js.gz +0 -0
  97. package/min/with-schemas/index.js +1 -1
  98. package/min/with-schemas/index.js.gz +0 -0
  99. package/omni/index.js +76 -22
  100. package/omni/with-schemas/index.js +76 -22
  101. package/package.json +2 -2
  102. package/readme.md +14 -14
  103. package/releases.md +95 -65
  104. package/schematizers/index.js +54 -6
  105. package/schematizers/schematizer-arktype/index.js +99 -19
  106. package/schematizers/schematizer-arktype/with-schemas/index.js +99 -19
  107. package/schematizers/schematizer-effect/index.js +101 -11
  108. package/schematizers/schematizer-effect/with-schemas/index.js +101 -11
  109. package/schematizers/schematizer-typebox/index.js +87 -11
  110. package/schematizers/schematizer-typebox/with-schemas/index.js +87 -11
  111. package/schematizers/schematizer-valibot/index.js +125 -21
  112. package/schematizers/schematizer-valibot/with-schemas/index.js +125 -21
  113. package/schematizers/schematizer-yup/index.js +58 -14
  114. package/schematizers/schematizer-yup/with-schemas/index.js +58 -14
  115. package/schematizers/schematizer-zod/index.js +131 -12
  116. package/schematizers/schematizer-zod/with-schemas/index.js +131 -12
  117. package/schematizers/with-schemas/index.js +54 -6
  118. package/store/index.js +76 -22
  119. package/store/with-schemas/index.js +76 -22
  120. package/ui-react-inspector/index.js +76 -22
  121. package/ui-react-inspector/with-schemas/index.js +76 -22
  122. package/ui-solid-inspector/index.js +76 -22
  123. package/ui-solid-inspector/with-schemas/index.js +76 -22
  124. package/ui-svelte-inspector/index.js +76 -22
  125. package/ui-svelte-inspector/with-schemas/index.js +76 -22
  126. package/with-schemas/index.js +76 -22
@@ -21,6 +21,50 @@ import type {
21
21
  Sorter,
22
22
  } from '../common/index.d.ts';
23
23
 
24
+ type SchemaType = 'string' | 'number' | 'boolean' | 'object' | 'array';
25
+
26
+ type SchemaTypeArray = readonly [SchemaType, ...SchemaType[]];
27
+
28
+ type CellOrValueFromSchemaType<Type> = Type extends readonly (infer Type)[]
29
+ ? CellOrValueFromSchemaType<Type>
30
+ : Type extends 'string'
31
+ ? string
32
+ : Type extends 'number'
33
+ ? number
34
+ : Type extends 'boolean'
35
+ ? boolean
36
+ : Type extends 'object'
37
+ ? AnyObject
38
+ : Type extends 'array'
39
+ ? AnyArray
40
+ : never;
41
+
42
+ type CellOrValueFromSchema<Schema> = Schema extends {
43
+ enum: readonly (infer Enum)[];
44
+ }
45
+ ? Enum
46
+ : Schema extends {type: infer Type}
47
+ ? CellOrValueFromSchemaType<Type>
48
+ : never;
49
+
50
+ type NullFromSchema<Schema> = Schema extends {allowNull: true} ? null : never;
51
+
52
+ type ValidDefault<Schema> = Schema extends {default: infer Default}
53
+ ? [Default] extends [CellOrValueFromSchema<Schema> | NullFromSchema<Schema>]
54
+ ? unknown
55
+ : never
56
+ : unknown;
57
+
58
+ type ValidTablesSchema<Schema extends TablesSchema> = {
59
+ [TableId in keyof Schema]: {
60
+ [CellId in keyof Schema[TableId]]: ValidDefault<Schema[TableId][CellId]>;
61
+ };
62
+ };
63
+
64
+ type ValidValuesSchema<Schema extends ValuesSchema> = {
65
+ [ValueId in keyof Schema]: ValidDefault<Schema[ValueId]>;
66
+ };
67
+
24
68
  /**
25
69
  * The TablesSchema type describes the tabular structure of a Store in terms of
26
70
  * valid Table Ids and the types of Cell that can exist within them.
@@ -52,17 +96,25 @@ export type TablesSchema = {[tableId: Id]: {[cellId: Id]: CellSchema}};
52
96
  * The CellSchema type describes what values are allowed for each Cell in a
53
97
  * Table.
54
98
  *
55
- * A CellSchema specifies the type of the Cell (`string`, `boolean`, `number`,
56
- * `null` since v7.0, or `object` or `array` since v8.0), and what the default
57
- * value can be when an explicit value is not specified.
99
+ * A CellSchema specifies either the type of the Cell (`string`, `boolean`,
100
+ * `number`, `object`, or `array`), a non-empty array of those types, or a
101
+ * non-empty `enum` of exact primitive values that are allowed. Multiple type
102
+ * names form a union, such as `type: ['string', 'number']`, and repeated names
103
+ * have no additional effect. The `type` and `enum` properties are mutually
104
+ * exclusive, and enum members can be strings, finite numbers, or booleans,
105
+ * including a mixture of those types.
58
106
  *
59
107
  * For `object` and `array` types, TinyBase automatically serializes values to
60
108
  * and from JSON when storing and retrieving them. Their contents should
61
109
  * recursively be strings, finite numbers, booleans, `null`, plain objects, or
62
110
  * arrays to ensure they are preserved.
63
111
  *
64
- * If a default value is provided (and its type is correct), you can be certain
65
- * that that Cell will always be present in a Row. You can also set `required`
112
+ * Set `allowNull` to `true` to also allow `null`, whether the schema uses
113
+ * `type` or `enum`. A default value is used only when it has the correct type,
114
+ * matches any member of a type union, is an enum member when applicable, or is
115
+ * `null` when allowed. Literal schemas passed to Store schema setters are
116
+ * checked against these default rules by TypeScript. A valid default means
117
+ * that the Cell will always be present in a Row. You can also set `required`
66
118
  * to `true` to indicate to schema-based typing that the Cell should be present
67
119
  * even if it does not have a default.
68
120
  *
@@ -105,6 +157,30 @@ export type TablesSchema = {[tableId: Id]: {[cellId: Id]: CellSchema}};
105
157
  * default: {},
106
158
  * };
107
159
  * ```
160
+ * @example
161
+ * When applied to a Store, this CellSchema allows either a string or numeric
162
+ * Cell.
163
+ *
164
+ * ```js
165
+ * import type {CellSchema} from 'tinybase';
166
+ *
167
+ * export const referenceCell: CellSchema = {
168
+ * type: ['string', 'number'],
169
+ * };
170
+ * ```
171
+ * @example
172
+ * When applied to a Store, this CellSchema allows one of three exact primitive
173
+ * values, or `null`.
174
+ *
175
+ * ```js
176
+ * import type {CellSchema} from 'tinybase';
177
+ *
178
+ * export const ratingCell: CellSchema = {
179
+ * enum: ['good', 5, true],
180
+ * allowNull: true,
181
+ * default: 'good',
182
+ * };
183
+ * ```
108
184
  * @category Schema
109
185
  * @since v1.0.0
110
186
  */
@@ -114,28 +190,50 @@ export type CellSchema =
114
190
  default?: string | null;
115
191
  allowNull?: boolean;
116
192
  required?: boolean;
193
+ enum?: never;
117
194
  }
118
195
  | {
119
196
  type: 'number';
120
197
  default?: number | null;
121
198
  allowNull?: boolean;
122
199
  required?: boolean;
200
+ enum?: never;
123
201
  }
124
202
  | {
125
203
  type: 'boolean';
126
204
  default?: boolean | null;
127
205
  allowNull?: boolean;
128
206
  required?: boolean;
207
+ enum?: never;
129
208
  }
130
209
  | {
131
210
  type: 'object';
132
- default?: AnyObject;
211
+ default?: AnyObject | null;
133
212
  allowNull?: boolean;
134
213
  required?: boolean;
214
+ enum?: never;
135
215
  }
136
216
  | {
137
217
  type: 'array';
138
- default?: AnyArray;
218
+ default?: AnyArray | null;
219
+ allowNull?: boolean;
220
+ required?: boolean;
221
+ enum?: never;
222
+ }
223
+ | {
224
+ type: SchemaTypeArray;
225
+ default?: string | number | boolean | AnyObject | AnyArray | null;
226
+ allowNull?: boolean;
227
+ required?: boolean;
228
+ enum?: never;
229
+ }
230
+ | {
231
+ enum: readonly [
232
+ string | number | boolean,
233
+ ...(string | number | boolean)[],
234
+ ];
235
+ type?: never;
236
+ default?: string | number | boolean | null;
139
237
  allowNull?: boolean;
140
238
  required?: boolean;
141
239
  };
@@ -166,16 +264,24 @@ export type ValuesSchema = {[valueId: Id]: ValueSchema};
166
264
  * The ValueSchema type describes what values are allowed for keyed Values in a
167
265
  * Store.
168
266
  *
169
- * A ValueSchema specifies the type of the Value (`string`, `boolean`, `number`,
170
- * `null` since v7.0, or `object` or `array` since v8.0), and what the default
171
- * value can be when an explicit value is not specified.
267
+ * A ValueSchema specifies either the type of the Value (`string`, `boolean`,
268
+ * `number`, `object`, or `array`), a non-empty array of those types, or a
269
+ * non-empty `enum` of exact primitive values that are allowed. Multiple type
270
+ * names form a union, such as `type: ['string', 'number']`, and repeated names
271
+ * have no additional effect. The `type` and `enum` properties are mutually
272
+ * exclusive, and enum members can be strings, finite numbers, or booleans,
273
+ * including a mixture of those types.
172
274
  *
173
275
  * For `object` and `array` types, TinyBase automatically serializes values to
174
276
  * and from JSON when storing and retrieving them. Their contents should
175
277
  * recursively be strings, finite numbers, booleans, `null`, plain objects, or
176
278
  * arrays to ensure they are preserved.
177
279
  *
178
- * If a default value is provided (and its type is correct), you can be certain
280
+ * Set `allowNull` to `true` to also allow `null`, whether the schema uses
281
+ * `type` or `enum`. A default value is used only when it has the correct type,
282
+ * matches any member of a type union, is an enum member when applicable, or is
283
+ * `null` when allowed. Literal schemas passed to Store schema setters are
284
+ * checked against these default rules by TypeScript. A valid default means
179
285
  * that the Value will always be present in a Store. You can also set `required`
180
286
  * to `true` to indicate to schema-based typing that the Value should be present
181
287
  * even if it does not have a default.
@@ -219,6 +325,30 @@ export type ValuesSchema = {[valueId: Id]: ValueSchema};
219
325
  * default: [],
220
326
  * };
221
327
  * ```
328
+ * @example
329
+ * When applied to a Store, this ValueSchema allows either a string or numeric
330
+ * Value.
331
+ *
332
+ * ```js
333
+ * import type {ValueSchema} from 'tinybase';
334
+ *
335
+ * export const referenceValue: ValueSchema = {
336
+ * type: ['string', 'number'],
337
+ * };
338
+ * ```
339
+ * @example
340
+ * When applied to a Store, this ValueSchema allows one of three exact primitive
341
+ * values, or `null`.
342
+ *
343
+ * ```js
344
+ * import type {ValueSchema} from 'tinybase';
345
+ *
346
+ * export const ratingValue: ValueSchema = {
347
+ * enum: ['good', 5, true],
348
+ * allowNull: true,
349
+ * default: 'good',
350
+ * };
351
+ * ```
222
352
  * @category Schema
223
353
  * @since v3.0.0
224
354
  */
@@ -228,28 +358,50 @@ export type ValueSchema =
228
358
  default?: string | null;
229
359
  allowNull?: boolean;
230
360
  required?: boolean;
361
+ enum?: never;
231
362
  }
232
363
  | {
233
364
  type: 'number';
234
365
  default?: number | null;
235
366
  allowNull?: boolean;
236
367
  required?: boolean;
368
+ enum?: never;
237
369
  }
238
370
  | {
239
371
  type: 'boolean';
240
372
  default?: boolean | null;
241
373
  allowNull?: boolean;
242
374
  required?: boolean;
375
+ enum?: never;
243
376
  }
244
377
  | {
245
378
  type: 'object';
246
- default?: AnyObject;
379
+ default?: AnyObject | null;
247
380
  allowNull?: boolean;
248
381
  required?: boolean;
382
+ enum?: never;
249
383
  }
250
384
  | {
251
385
  type: 'array';
252
- default?: AnyArray;
386
+ default?: AnyArray | null;
387
+ allowNull?: boolean;
388
+ required?: boolean;
389
+ enum?: never;
390
+ }
391
+ | {
392
+ type: SchemaTypeArray;
393
+ default?: string | number | boolean | AnyObject | AnyArray | null;
394
+ allowNull?: boolean;
395
+ required?: boolean;
396
+ enum?: never;
397
+ }
398
+ | {
399
+ enum: readonly [
400
+ string | number | boolean,
401
+ ...(string | number | boolean)[],
402
+ ];
403
+ type?: never;
404
+ default?: string | number | boolean | null;
253
405
  allowNull?: boolean;
254
406
  required?: boolean;
255
407
  };
@@ -3918,7 +4070,9 @@ export interface Store {
3918
4070
  * @category Setter
3919
4071
  * @since v3.0.0
3920
4072
  */
3921
- setTablesSchema(tablesSchema: TablesSchema): this;
4073
+ setTablesSchema<const TS extends TablesSchema>(
4074
+ tablesSchema: TS & ValidTablesSchema<TS>,
4075
+ ): this;
3922
4076
 
3923
4077
  /**
3924
4078
  * The setValuesSchema method lets you specify the ValuesSchema of the keyed
@@ -3949,7 +4103,9 @@ export interface Store {
3949
4103
  * @category Setter
3950
4104
  * @since v3.0.0
3951
4105
  */
3952
- setValuesSchema(valuesSchema: ValuesSchema): this;
4106
+ setValuesSchema<const VS extends ValuesSchema>(
4107
+ valuesSchema: VS & ValidValuesSchema<VS>,
4108
+ ): this;
3953
4109
 
3954
4110
  /**
3955
4111
  * The setSchema method lets you specify the TablesSchema and ValuesSchema of
@@ -4012,7 +4168,10 @@ export interface Store {
4012
4168
  * @category Setter
4013
4169
  * @since v1.0.0
4014
4170
  */
4015
- setSchema(tablesSchema: TablesSchema, valuesSchema?: ValuesSchema): this;
4171
+ setSchema<const TS extends TablesSchema, const VS extends ValuesSchema>(
4172
+ tablesSchema: TS & ValidTablesSchema<TS>,
4173
+ valuesSchema?: VS & ValidValuesSchema<VS>,
4174
+ ): this;
4016
4175
 
4017
4176
  /**
4018
4177
  * The delTables method lets you remove all of the data in a Store.