tinybase 9.1.0-beta.0 → 9.1.0-beta.2
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.
- package/@types/_internal/store/with-schemas/index.d.ts +133 -2
- package/@types/common/index.d.ts +17 -2
- package/@types/common/with-schemas/index.d.ts +18 -2
- package/@types/indexes/index.d.ts +97 -0
- package/@types/indexes/with-schemas/index.d.ts +137 -0
- package/@types/store/index.d.ts +133 -25
- package/@types/store/with-schemas/index.d.ts +145 -51
- package/@types/ui-react/index.d.ts +48 -2
- package/@types/ui-react/with-schemas/index.d.ts +108 -34
- package/@types/ui-solid/index.d.ts +51 -2
- package/@types/ui-solid/with-schemas/index.d.ts +111 -34
- package/@types/ui-svelte/index.d.ts +84 -2
- package/@types/ui-svelte/with-schemas/index.d.ts +122 -25
- package/index.js +155 -38
- package/indexes/index.js +39 -0
- package/indexes/with-schemas/index.js +39 -0
- package/mergeable-store/index.js +116 -38
- package/mergeable-store/with-schemas/index.js +116 -38
- package/min/index.js +1 -1
- package/min/index.js.gz +0 -0
- package/min/indexes/index.js +1 -1
- package/min/indexes/index.js.gz +0 -0
- package/min/indexes/with-schemas/index.js +1 -1
- package/min/indexes/with-schemas/index.js.gz +0 -0
- package/min/mergeable-store/index.js +1 -1
- package/min/mergeable-store/index.js.gz +0 -0
- package/min/mergeable-store/with-schemas/index.js +1 -1
- package/min/mergeable-store/with-schemas/index.js.gz +0 -0
- package/min/omni/index.js +1 -1
- package/min/omni/index.js.gz +0 -0
- package/min/omni/with-schemas/index.js +1 -1
- package/min/omni/with-schemas/index.js.gz +0 -0
- package/min/store/index.js +1 -1
- package/min/store/index.js.gz +0 -0
- package/min/store/with-schemas/index.js +1 -1
- package/min/store/with-schemas/index.js.gz +0 -0
- package/min/ui-react/index.js +1 -1
- package/min/ui-react/index.js.gz +0 -0
- package/min/ui-react/with-schemas/index.js +1 -1
- package/min/ui-react/with-schemas/index.js.gz +0 -0
- package/min/ui-react-dom/index.js +1 -1
- package/min/ui-react-dom/index.js.gz +0 -0
- package/min/ui-react-dom/with-schemas/index.js +1 -1
- package/min/ui-react-dom/with-schemas/index.js.gz +0 -0
- package/min/ui-react-dom-charts/index.js +1 -1
- package/min/ui-react-dom-charts/index.js.gz +0 -0
- package/min/ui-react-dom-charts/with-schemas/index.js +1 -1
- package/min/ui-react-dom-charts/with-schemas/index.js.gz +0 -0
- package/min/ui-react-inspector/index.js +1 -1
- package/min/ui-react-inspector/index.js.gz +0 -0
- package/min/ui-react-inspector/with-schemas/index.js +1 -1
- package/min/ui-react-inspector/with-schemas/index.js.gz +0 -0
- package/min/ui-solid/index.js +1 -1
- package/min/ui-solid/index.js.gz +0 -0
- package/min/ui-solid/with-schemas/index.js +1 -1
- package/min/ui-solid/with-schemas/index.js.gz +0 -0
- package/min/ui-solid-dom/index.js +1 -1
- package/min/ui-solid-dom/index.js.gz +0 -0
- package/min/ui-solid-dom/with-schemas/index.js +1 -1
- package/min/ui-solid-dom/with-schemas/index.js.gz +0 -0
- package/min/ui-solid-inspector/index.js +1 -1
- package/min/ui-solid-inspector/index.js.gz +0 -0
- package/min/ui-solid-inspector/with-schemas/index.js +1 -1
- package/min/ui-solid-inspector/with-schemas/index.js.gz +0 -0
- package/min/ui-svelte/index.js +1 -1
- package/min/ui-svelte/index.js.gz +0 -0
- package/min/ui-svelte/with-schemas/index.js +1 -1
- package/min/ui-svelte/with-schemas/index.js.gz +0 -0
- package/min/ui-svelte-dom/index.js +1 -1
- package/min/ui-svelte-dom/index.js.gz +0 -0
- package/min/ui-svelte-dom/with-schemas/index.js +1 -1
- package/min/ui-svelte-dom/with-schemas/index.js.gz +0 -0
- package/min/ui-svelte-inspector/index.js +1 -1
- package/min/ui-svelte-inspector/index.js.gz +0 -0
- package/min/ui-svelte-inspector/with-schemas/index.js +1 -1
- package/min/ui-svelte-inspector/with-schemas/index.js.gz +0 -0
- package/min/with-schemas/index.js +1 -1
- package/min/with-schemas/index.js.gz +0 -0
- package/omni/index.js +155 -38
- package/omni/with-schemas/index.js +155 -38
- package/package.json +3 -3
- package/readme.md +14 -14
- package/releases.md +101 -60
- package/store/index.js +116 -38
- package/store/with-schemas/index.js +116 -38
- package/ui-react/index.js +52 -6
- package/ui-react/with-schemas/index.js +52 -6
- package/ui-react-dom/index.js +24 -4
- package/ui-react-dom/with-schemas/index.js +24 -4
- package/ui-react-dom-charts/index.js +24 -4
- package/ui-react-dom-charts/with-schemas/index.js +24 -4
- package/ui-react-inspector/index.js +140 -42
- package/ui-react-inspector/with-schemas/index.js +140 -42
- package/ui-solid/index.js +48 -2
- package/ui-solid/with-schemas/index.js +48 -2
- package/ui-solid-dom/index.js +17 -1
- package/ui-solid-dom/with-schemas/index.js +17 -1
- package/ui-solid-inspector/index.js +133 -39
- package/ui-solid-inspector/with-schemas/index.js +133 -39
- package/ui-svelte/index.js +91 -31
- package/ui-svelte/with-schemas/index.js +91 -31
- package/ui-svelte-dom/index.js +35 -14
- package/ui-svelte-dom/with-schemas/index.js +35 -14
- package/ui-svelte-inspector/index.js +151 -52
- package/ui-svelte-inspector/with-schemas/index.js +151 -52
- package/with-schemas/index.js +155 -38
package/@types/store/index.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ import type {
|
|
|
18
18
|
IdOrNull,
|
|
19
19
|
Ids,
|
|
20
20
|
Json,
|
|
21
|
+
Sorter,
|
|
21
22
|
} from '../common/index.d.ts';
|
|
22
23
|
|
|
23
24
|
/**
|
|
@@ -60,11 +61,13 @@ export type TablesSchema = {[tableId: Id]: {[cellId: Id]: CellSchema}};
|
|
|
60
61
|
* object or array Cell directly, and get back the same structure.
|
|
61
62
|
*
|
|
62
63
|
* If a default value is provided (and its type is correct), you can be certain
|
|
63
|
-
* that that Cell will always be present in a Row.
|
|
64
|
+
* that that Cell will always be present in a Row. You can also set `required`
|
|
65
|
+
* to `true` to indicate to schema-based typing that the Cell should be present
|
|
66
|
+
* even if it does not have a default.
|
|
64
67
|
*
|
|
65
|
-
* If
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
+
* If neither a default value nor `required: true` is provided, the Cell may be
|
|
69
|
+
* missing from the Row, but when present you can be guaranteed it is of the
|
|
70
|
+
* correct type.
|
|
68
71
|
* @example
|
|
69
72
|
* When applied to a Store, this CellSchema ensures a boolean Cell is always
|
|
70
73
|
* present, and defaults it to `false`.
|
|
@@ -78,6 +81,18 @@ export type TablesSchema = {[tableId: Id]: {[cellId: Id]: CellSchema}};
|
|
|
78
81
|
* };
|
|
79
82
|
* ```
|
|
80
83
|
* @example
|
|
84
|
+
* When applied to a Store, this CellSchema expects a string Cell to be present
|
|
85
|
+
* without providing a default value.
|
|
86
|
+
*
|
|
87
|
+
* ```js
|
|
88
|
+
* import type {CellSchema} from 'tinybase';
|
|
89
|
+
*
|
|
90
|
+
* export const requiredString: CellSchema = {
|
|
91
|
+
* type: 'string',
|
|
92
|
+
* required: true,
|
|
93
|
+
* };
|
|
94
|
+
* ```
|
|
95
|
+
* @example
|
|
81
96
|
* When applied to a Store, this CellSchema allows an object Cell containing
|
|
82
97
|
* arbitrary data, defaulting to an empty object.
|
|
83
98
|
*
|
|
@@ -93,11 +108,36 @@ export type TablesSchema = {[tableId: Id]: {[cellId: Id]: CellSchema}};
|
|
|
93
108
|
* @since v1.0.0
|
|
94
109
|
*/
|
|
95
110
|
export type CellSchema =
|
|
96
|
-
| {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
111
|
+
| {
|
|
112
|
+
type: 'string';
|
|
113
|
+
default?: string | null;
|
|
114
|
+
allowNull?: boolean;
|
|
115
|
+
required?: boolean;
|
|
116
|
+
}
|
|
117
|
+
| {
|
|
118
|
+
type: 'number';
|
|
119
|
+
default?: number | null;
|
|
120
|
+
allowNull?: boolean;
|
|
121
|
+
required?: boolean;
|
|
122
|
+
}
|
|
123
|
+
| {
|
|
124
|
+
type: 'boolean';
|
|
125
|
+
default?: boolean | null;
|
|
126
|
+
allowNull?: boolean;
|
|
127
|
+
required?: boolean;
|
|
128
|
+
}
|
|
129
|
+
| {
|
|
130
|
+
type: 'object';
|
|
131
|
+
default?: AnyObject;
|
|
132
|
+
allowNull?: boolean;
|
|
133
|
+
required?: boolean;
|
|
134
|
+
}
|
|
135
|
+
| {
|
|
136
|
+
type: 'array';
|
|
137
|
+
default?: AnyArray;
|
|
138
|
+
allowNull?: boolean;
|
|
139
|
+
required?: boolean;
|
|
140
|
+
};
|
|
101
141
|
|
|
102
142
|
/**
|
|
103
143
|
* The ValuesSchema type describes the keyed Values that can be set in a Store
|
|
@@ -133,11 +173,13 @@ export type ValuesSchema = {[valueId: Id]: ValueSchema};
|
|
|
133
173
|
* and from JSON when storing and retrieving them.
|
|
134
174
|
*
|
|
135
175
|
* If a default value is provided (and its type is correct), you can be certain
|
|
136
|
-
* that the Value will always be present in a Store.
|
|
176
|
+
* that the Value will always be present in a Store. You can also set `required`
|
|
177
|
+
* to `true` to indicate to schema-based typing that the Value should be
|
|
178
|
+
* present even if it does not have a default.
|
|
137
179
|
*
|
|
138
|
-
* If
|
|
139
|
-
*
|
|
140
|
-
*
|
|
180
|
+
* If neither a default value nor `required: true` is provided, the Value may
|
|
181
|
+
* not be present in the Store, but when present you can be guaranteed it is of
|
|
182
|
+
* the correct type.
|
|
141
183
|
* @example
|
|
142
184
|
* When applied to a Store, this ValueSchema ensures a boolean Value is always
|
|
143
185
|
* present, and defaults it to `false`.
|
|
@@ -151,6 +193,18 @@ export type ValuesSchema = {[valueId: Id]: ValueSchema};
|
|
|
151
193
|
* };
|
|
152
194
|
* ```
|
|
153
195
|
* @example
|
|
196
|
+
* When applied to a Store, this ValueSchema expects a string Value to be
|
|
197
|
+
* present without providing a default value.
|
|
198
|
+
*
|
|
199
|
+
* ```js
|
|
200
|
+
* import type {ValueSchema} from 'tinybase';
|
|
201
|
+
*
|
|
202
|
+
* export const requiredString: ValueSchema = {
|
|
203
|
+
* type: 'string',
|
|
204
|
+
* required: true,
|
|
205
|
+
* };
|
|
206
|
+
* ```
|
|
207
|
+
* @example
|
|
154
208
|
* When applied to a Store, this ValueSchema allows an array Value containing
|
|
155
209
|
* a list of items, defaulting to an empty array.
|
|
156
210
|
*
|
|
@@ -166,11 +220,36 @@ export type ValuesSchema = {[valueId: Id]: ValueSchema};
|
|
|
166
220
|
* @since v3.0.0
|
|
167
221
|
*/
|
|
168
222
|
export type ValueSchema =
|
|
169
|
-
| {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
223
|
+
| {
|
|
224
|
+
type: 'string';
|
|
225
|
+
default?: string | null;
|
|
226
|
+
allowNull?: boolean;
|
|
227
|
+
required?: boolean;
|
|
228
|
+
}
|
|
229
|
+
| {
|
|
230
|
+
type: 'number';
|
|
231
|
+
default?: number | null;
|
|
232
|
+
allowNull?: boolean;
|
|
233
|
+
required?: boolean;
|
|
234
|
+
}
|
|
235
|
+
| {
|
|
236
|
+
type: 'boolean';
|
|
237
|
+
default?: boolean | null;
|
|
238
|
+
allowNull?: boolean;
|
|
239
|
+
required?: boolean;
|
|
240
|
+
}
|
|
241
|
+
| {
|
|
242
|
+
type: 'object';
|
|
243
|
+
default?: AnyObject;
|
|
244
|
+
allowNull?: boolean;
|
|
245
|
+
required?: boolean;
|
|
246
|
+
}
|
|
247
|
+
| {
|
|
248
|
+
type: 'array';
|
|
249
|
+
default?: AnyArray;
|
|
250
|
+
allowNull?: boolean;
|
|
251
|
+
required?: boolean;
|
|
252
|
+
};
|
|
174
253
|
|
|
175
254
|
/**
|
|
176
255
|
* The NoTablesSchema type is a TablesSchema-like type for when one has not been
|
|
@@ -625,7 +704,7 @@ export type DoRollback = (store: Store) => boolean;
|
|
|
625
704
|
* the getSortedRowIds method.
|
|
626
705
|
*
|
|
627
706
|
* It's an object containing the Id of the Table in the Store, and optional
|
|
628
|
-
* `cellId`, `descending`, `offset`, and `
|
|
707
|
+
* `cellId`, `descending`, `offset`, `limit`, and `sorter` parameters. See the
|
|
629
708
|
* getSortedRowIds method for specific examples.
|
|
630
709
|
* @category Store
|
|
631
710
|
* @since v6.1.0
|
|
@@ -662,6 +741,13 @@ export type SortedRowIdsArgs = {
|
|
|
662
741
|
* @since v6.1.0
|
|
663
742
|
*/
|
|
664
743
|
limit?: number;
|
|
744
|
+
/**
|
|
745
|
+
* A custom function for comparing the sorting values, defaulting to the
|
|
746
|
+
* defaultSorter function.
|
|
747
|
+
* @category Argument
|
|
748
|
+
* @since v9.1.0
|
|
749
|
+
*/
|
|
750
|
+
sorter?: Sorter;
|
|
665
751
|
};
|
|
666
752
|
|
|
667
753
|
/**
|
|
@@ -2196,10 +2282,11 @@ export interface Store {
|
|
|
2196
2282
|
* The getSortedRowIds method returns the Ids of every Row in a given Table,
|
|
2197
2283
|
* sorted according to the values in a specified Cell.
|
|
2198
2284
|
*
|
|
2199
|
-
*
|
|
2200
|
-
* should be in descending order. The `offset` and `limit`
|
|
2201
|
-
* to paginate results, but default to `0` and
|
|
2202
|
-
* available Row Ids if not specified.
|
|
2285
|
+
* By default the sorting of the rows is alphanumeric, and you can indicate
|
|
2286
|
+
* whether it should be in descending order. The `offset` and `limit`
|
|
2287
|
+
* parameters are used to paginate results, but default to `0` and
|
|
2288
|
+
* `undefined` to return all available Row Ids if not specified. The `sorter`
|
|
2289
|
+
* parameter can provide a custom comparison function.
|
|
2203
2290
|
*
|
|
2204
2291
|
* Note that every call to this method will perform the sorting afresh - there
|
|
2205
2292
|
* is no caching of the results - and so you are advised to memoize the
|
|
@@ -2216,6 +2303,8 @@ export interface Store {
|
|
|
2216
2303
|
* any.
|
|
2217
2304
|
* @param limit The maximum number of Row Ids to return, or `undefined` for
|
|
2218
2305
|
* all.
|
|
2306
|
+
* @param sorter A custom function for comparing the sorting values, or
|
|
2307
|
+
* `undefined` to use the defaultSorter function.
|
|
2219
2308
|
* @returns An array of the sorted Ids of appropriate Rows in the Table.
|
|
2220
2309
|
* @example
|
|
2221
2310
|
* This example retrieves sorted Row Ids in a Table.
|
|
@@ -2287,6 +2376,24 @@ export interface Store {
|
|
|
2287
2376
|
* // -> ['cujo', 'felix', 'fido']
|
|
2288
2377
|
* ```
|
|
2289
2378
|
* @example
|
|
2379
|
+
* This example retrieves Row Ids sorted numerically by their own value.
|
|
2380
|
+
*
|
|
2381
|
+
* ```js
|
|
2382
|
+
* import {createStore} from 'tinybase';
|
|
2383
|
+
*
|
|
2384
|
+
* const store = createStore();
|
|
2385
|
+
* ['1', '10', '2'].forEach((rowId) =>
|
|
2386
|
+
* store.setRow('pets', rowId, {sold: false}),
|
|
2387
|
+
* );
|
|
2388
|
+
* const numericSorter = (sortKey1, sortKey2) =>
|
|
2389
|
+
* Number(sortKey1) - Number(sortKey2);
|
|
2390
|
+
*
|
|
2391
|
+
* console.log(
|
|
2392
|
+
* store.getSortedRowIds({tableId: 'pets', sorter: numericSorter}),
|
|
2393
|
+
* );
|
|
2394
|
+
* // -> ['1', '2', '10']
|
|
2395
|
+
* ```
|
|
2396
|
+
* @example
|
|
2290
2397
|
* This example retrieves the sorted Row Ids of a Table that does not exist,
|
|
2291
2398
|
* returning an empty array.
|
|
2292
2399
|
*
|
|
@@ -2306,13 +2413,14 @@ export interface Store {
|
|
|
2306
2413
|
descending?: boolean,
|
|
2307
2414
|
offset?: number,
|
|
2308
2415
|
limit?: number,
|
|
2416
|
+
sorter?: Sorter,
|
|
2309
2417
|
): Ids;
|
|
2310
2418
|
|
|
2311
2419
|
/**
|
|
2312
2420
|
* When called with one object argument, the getSortedRowIds method
|
|
2313
2421
|
* destructures it to make it easier to skip optional parameters.
|
|
2314
2422
|
* @param args A SortedRowIdsArgs object containing the Id of the Table in the
|
|
2315
|
-
* Store, and optional `cellId`, `descending`, `offset`, and `
|
|
2423
|
+
* Store, and optional `cellId`, `descending`, `offset`, `limit`, and `sorter`
|
|
2316
2424
|
* parameters.
|
|
2317
2425
|
* @returns An array of the sorted Ids of appropriate Rows in the Table.
|
|
2318
2426
|
* @example
|
|
@@ -5813,7 +5921,7 @@ export interface Store {
|
|
|
5813
5921
|
* addSortedRowIdsListener method destructures it to make it easier to skip
|
|
5814
5922
|
* optional parameters.
|
|
5815
5923
|
* @param args A SortedRowIdsArgs object containing the Id of the Table in the
|
|
5816
|
-
* Store, and optional `cellId`, `descending`, `offset`, and `
|
|
5924
|
+
* Store, and optional `cellId`, `descending`, `offset`, `limit`, and `sorter`
|
|
5817
5925
|
* parameters.
|
|
5818
5926
|
* @param listener The function that will be called whenever the sorted Row
|
|
5819
5927
|
* Ids in the Table change.
|
|
@@ -13,10 +13,12 @@
|
|
|
13
13
|
import type {
|
|
14
14
|
AllCellIdFromSchema,
|
|
15
15
|
CellIdFromSchema,
|
|
16
|
-
DefaultCellIdFromSchema,
|
|
17
|
-
DefaultValueIdFromSchema,
|
|
18
16
|
DefaultedCellFromSchema,
|
|
19
17
|
DefaultedValueFromSchema,
|
|
18
|
+
PresentCellIdFromSchema,
|
|
19
|
+
PresentValueIdFromSchema,
|
|
20
|
+
RequiredNonDefaultCellIdFromSchema,
|
|
21
|
+
RequiredNonDefaultValueIdFromSchema,
|
|
20
22
|
StoreAlias,
|
|
21
23
|
TableIdFromSchema,
|
|
22
24
|
Truncate,
|
|
@@ -29,6 +31,7 @@ import type {
|
|
|
29
31
|
IdOrNull,
|
|
30
32
|
Ids,
|
|
31
33
|
Json,
|
|
34
|
+
Sorter,
|
|
32
35
|
} from '../../common/with-schemas/index.d.ts';
|
|
33
36
|
|
|
34
37
|
/**
|
|
@@ -71,11 +74,13 @@ export type TablesSchema = {[tableId: Id]: {[cellId: Id]: CellSchema}};
|
|
|
71
74
|
* object or array Cell directly, and get back the same structure.
|
|
72
75
|
*
|
|
73
76
|
* If a default value is provided (and its type is correct), you can be certain
|
|
74
|
-
* that that Cell will always be present in a Row.
|
|
77
|
+
* that that Cell will always be present in a Row. You can also set `required`
|
|
78
|
+
* to `true` to indicate to schema-based typing that the Cell should be present
|
|
79
|
+
* even if it does not have a default.
|
|
75
80
|
*
|
|
76
|
-
* If
|
|
77
|
-
*
|
|
78
|
-
*
|
|
81
|
+
* If neither a default value nor `required: true` is provided, the Cell may be
|
|
82
|
+
* missing from the Row, but when present you can be guaranteed it is of the
|
|
83
|
+
* correct type.
|
|
79
84
|
* @example
|
|
80
85
|
* When applied to a Store, this CellSchema ensures a boolean Cell is always
|
|
81
86
|
* present, and defaults it to `false`.
|
|
@@ -89,6 +94,18 @@ export type TablesSchema = {[tableId: Id]: {[cellId: Id]: CellSchema}};
|
|
|
89
94
|
* };
|
|
90
95
|
* ```
|
|
91
96
|
* @example
|
|
97
|
+
* When applied to a Store, this CellSchema expects a string Cell to be present
|
|
98
|
+
* without providing a default value.
|
|
99
|
+
*
|
|
100
|
+
* ```js
|
|
101
|
+
* import type {CellSchema} from 'tinybase';
|
|
102
|
+
*
|
|
103
|
+
* export const requiredString: CellSchema = {
|
|
104
|
+
* type: 'string',
|
|
105
|
+
* required: true,
|
|
106
|
+
* };
|
|
107
|
+
* ```
|
|
108
|
+
* @example
|
|
92
109
|
* When applied to a Store, this CellSchema allows an object Cell containing
|
|
93
110
|
* arbitrary data, defaulting to an empty object.
|
|
94
111
|
*
|
|
@@ -104,11 +121,11 @@ export type TablesSchema = {[tableId: Id]: {[cellId: Id]: CellSchema}};
|
|
|
104
121
|
* @since v1.0.0
|
|
105
122
|
*/
|
|
106
123
|
export type CellSchema =
|
|
107
|
-
| {type: 'string'; default?: string}
|
|
108
|
-
| {type: 'number'; default?: number}
|
|
109
|
-
| {type: 'boolean'; default?: boolean}
|
|
110
|
-
| {type: 'object'; default?: AnyObject}
|
|
111
|
-
| {type: 'array'; default?: AnyArray};
|
|
124
|
+
| {type: 'string'; default?: string; required?: boolean}
|
|
125
|
+
| {type: 'number'; default?: number; required?: boolean}
|
|
126
|
+
| {type: 'boolean'; default?: boolean; required?: boolean}
|
|
127
|
+
| {type: 'object'; default?: AnyObject; required?: boolean}
|
|
128
|
+
| {type: 'array'; default?: AnyArray; required?: boolean};
|
|
112
129
|
|
|
113
130
|
/**
|
|
114
131
|
* The ValuesSchema type describes the keyed Values that can be set in a Store
|
|
@@ -144,11 +161,13 @@ export type ValuesSchema = {[valueId: Id]: ValueSchema};
|
|
|
144
161
|
* and from JSON when storing and retrieving them.
|
|
145
162
|
*
|
|
146
163
|
* If a default value is provided (and its type is correct), you can be certain
|
|
147
|
-
* that the Value will always be present in a Store.
|
|
164
|
+
* that the Value will always be present in a Store. You can also set `required`
|
|
165
|
+
* to `true` to indicate to schema-based typing that the Value should be
|
|
166
|
+
* present even if it does not have a default.
|
|
148
167
|
*
|
|
149
|
-
* If
|
|
150
|
-
*
|
|
151
|
-
*
|
|
168
|
+
* If neither a default value nor `required: true` is provided, the Value may
|
|
169
|
+
* not be present in the Store, but when present you can be guaranteed it is of
|
|
170
|
+
* the correct type.
|
|
152
171
|
* @example
|
|
153
172
|
* When applied to a Store, this ValueSchema ensures a boolean Value is always
|
|
154
173
|
* present, and defaults it to `false`.
|
|
@@ -162,6 +181,18 @@ export type ValuesSchema = {[valueId: Id]: ValueSchema};
|
|
|
162
181
|
* };
|
|
163
182
|
* ```
|
|
164
183
|
* @example
|
|
184
|
+
* When applied to a Store, this ValueSchema expects a string Value to be
|
|
185
|
+
* present without providing a default value.
|
|
186
|
+
*
|
|
187
|
+
* ```js
|
|
188
|
+
* import type {ValueSchema} from 'tinybase';
|
|
189
|
+
*
|
|
190
|
+
* export const requiredString: ValueSchema = {
|
|
191
|
+
* type: 'string',
|
|
192
|
+
* required: true,
|
|
193
|
+
* };
|
|
194
|
+
* ```
|
|
195
|
+
* @example
|
|
165
196
|
* When applied to a Store, this ValueSchema allows an array Value containing
|
|
166
197
|
* a list of items, defaulting to an empty array.
|
|
167
198
|
*
|
|
@@ -177,11 +208,11 @@ export type ValuesSchema = {[valueId: Id]: ValueSchema};
|
|
|
177
208
|
* @since v3.0.0
|
|
178
209
|
*/
|
|
179
210
|
export type ValueSchema =
|
|
180
|
-
| {type: 'string'; default?: string}
|
|
181
|
-
| {type: 'number'; default?: number}
|
|
182
|
-
| {type: 'boolean'; default?: boolean}
|
|
183
|
-
| {type: 'object'; default?: AnyObject}
|
|
184
|
-
| {type: 'array'; default?: AnyArray};
|
|
211
|
+
| {type: 'string'; default?: string; required?: boolean}
|
|
212
|
+
| {type: 'number'; default?: number; required?: boolean}
|
|
213
|
+
| {type: 'boolean'; default?: boolean; required?: boolean}
|
|
214
|
+
| {type: 'object'; default?: AnyObject; required?: boolean}
|
|
215
|
+
| {type: 'array'; default?: AnyArray; required?: boolean};
|
|
185
216
|
|
|
186
217
|
/**
|
|
187
218
|
* The NoTablesSchema type is a TablesSchema-like type for when one has not been
|
|
@@ -370,22 +401,40 @@ export type Row<
|
|
|
370
401
|
Schema extends OptionalTablesSchema,
|
|
371
402
|
TableId extends TableIdFromSchema<Schema>,
|
|
372
403
|
WhenSet extends boolean = false,
|
|
373
|
-
> =
|
|
374
|
-
?
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
404
|
+
> = WhenSet extends true
|
|
405
|
+
? RequiredNonDefaultCellIdFromSchema<Schema, TableId> extends never
|
|
406
|
+
? PartialRow<Schema, TableId>
|
|
407
|
+
: {
|
|
408
|
+
-readonly [
|
|
409
|
+
CellId in RequiredNonDefaultCellIdFromSchema<Schema, TableId>
|
|
410
|
+
]: Cell<Schema, TableId, CellId>;
|
|
411
|
+
} & PartialRow<Schema, TableId>
|
|
381
412
|
: {
|
|
382
|
-
-readonly [CellId in
|
|
413
|
+
-readonly [CellId in PresentCellIdFromSchema<Schema, TableId>]: Cell<
|
|
383
414
|
Schema,
|
|
384
415
|
TableId,
|
|
385
416
|
CellId
|
|
386
417
|
>;
|
|
387
|
-
}
|
|
388
|
-
|
|
418
|
+
} & {
|
|
419
|
+
-readonly [
|
|
420
|
+
CellId in PresentCellIdFromSchema<Schema, TableId, false>
|
|
421
|
+
]?: Cell<Schema, TableId, CellId>;
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* The PartialRow type is a Row-like type used when setting part of a row.
|
|
426
|
+
*
|
|
427
|
+
* In schema-based typing, a PartialRow allows any known Cell to be omitted,
|
|
428
|
+
* including Cells marked with `required: true`, while still preventing unknown
|
|
429
|
+
* Cell Ids from being used.
|
|
430
|
+
* @category Store
|
|
431
|
+
* @since v9.1.0
|
|
432
|
+
*/
|
|
433
|
+
export type PartialRow<
|
|
434
|
+
Schema extends OptionalTablesSchema,
|
|
435
|
+
TableId extends TableIdFromSchema<Schema>,
|
|
436
|
+
> = {
|
|
437
|
+
-readonly [CellId in CellIdFromSchema<Schema, TableId>]?: Cell<
|
|
389
438
|
Schema,
|
|
390
439
|
TableId,
|
|
391
440
|
CellId
|
|
@@ -479,23 +528,38 @@ export type CellOrUndefined<
|
|
|
479
528
|
export type Values<
|
|
480
529
|
Schema extends OptionalValuesSchema,
|
|
481
530
|
WhenSet extends boolean = false,
|
|
482
|
-
> =
|
|
483
|
-
?
|
|
484
|
-
|
|
531
|
+
> = WhenSet extends true
|
|
532
|
+
? RequiredNonDefaultValueIdFromSchema<Schema> extends never
|
|
533
|
+
? PartialValues<Schema>
|
|
534
|
+
: {
|
|
535
|
+
-readonly [
|
|
536
|
+
ValueId in RequiredNonDefaultValueIdFromSchema<Schema>
|
|
537
|
+
]: Value<Schema, ValueId>;
|
|
538
|
+
} & PartialValues<Schema>
|
|
539
|
+
: {
|
|
540
|
+
-readonly [ValueId in PresentValueIdFromSchema<Schema>]: Value<
|
|
485
541
|
Schema,
|
|
486
542
|
ValueId
|
|
487
543
|
>;
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
-readonly [ValueId in DefaultValueIdFromSchema<Schema>]: Value<
|
|
544
|
+
} & {
|
|
545
|
+
-readonly [ValueId in PresentValueIdFromSchema<Schema, false>]?: Value<
|
|
491
546
|
Schema,
|
|
492
547
|
ValueId
|
|
493
548
|
>;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* The PartialValues type is a Values-like type used when setting some keyed
|
|
553
|
+
* values.
|
|
554
|
+
*
|
|
555
|
+
* In schema-based typing, PartialValues allows any known Value to be omitted,
|
|
556
|
+
* including Values marked with `required: true`, while still preventing unknown
|
|
557
|
+
* Value Ids from being used.
|
|
558
|
+
* @category Store
|
|
559
|
+
* @since v9.1.0
|
|
560
|
+
*/
|
|
561
|
+
export type PartialValues<Schema extends OptionalValuesSchema> = {
|
|
562
|
+
-readonly [ValueId in ValueIdFromSchema<Schema>]?: Value<Schema, ValueId>;
|
|
499
563
|
};
|
|
500
564
|
|
|
501
565
|
/**
|
|
@@ -947,7 +1011,7 @@ export type DoRollback<Schemas extends OptionalSchemas> = (
|
|
|
947
1011
|
* the getSortedRowIds method.
|
|
948
1012
|
*
|
|
949
1013
|
* It's an object containing the Id of the Table in the Store, and optional
|
|
950
|
-
* `cellId`, `descending`, `offset`, and `
|
|
1014
|
+
* `cellId`, `descending`, `offset`, `limit`, and `sorter` parameters. See the
|
|
951
1015
|
* getSortedRowIds method for specific examples.
|
|
952
1016
|
* @category Store
|
|
953
1017
|
* @since v6.1.0
|
|
@@ -989,6 +1053,13 @@ export type SortedRowIdsArgs<
|
|
|
989
1053
|
* @since v6.1.0
|
|
990
1054
|
*/
|
|
991
1055
|
limit?: number;
|
|
1056
|
+
/**
|
|
1057
|
+
* A custom function for comparing the sorting values, defaulting to the
|
|
1058
|
+
* defaultSorter function.
|
|
1059
|
+
* @category Argument
|
|
1060
|
+
* @since v9.1.0
|
|
1061
|
+
*/
|
|
1062
|
+
sorter?: Sorter;
|
|
992
1063
|
};
|
|
993
1064
|
|
|
994
1065
|
/**
|
|
@@ -3216,13 +3287,15 @@ export interface Store<in out Schemas extends OptionalSchemas> {
|
|
|
3216
3287
|
* descending?: boolean,
|
|
3217
3288
|
* offset?: number,
|
|
3218
3289
|
* limit?: number,
|
|
3290
|
+
* sorter?: Sorter,
|
|
3219
3291
|
* ): Ids;
|
|
3220
3292
|
* ```
|
|
3221
3293
|
*
|
|
3222
|
-
*
|
|
3223
|
-
* should be in descending order. The `offset` and `limit`
|
|
3224
|
-
* to paginate results, but default to `0` and
|
|
3225
|
-
* available Row Ids if not specified.
|
|
3294
|
+
* By default the sorting of the rows is alphanumeric, and you can indicate
|
|
3295
|
+
* whether it should be in descending order. The `offset` and `limit`
|
|
3296
|
+
* parameters are used to paginate results, but default to `0` and
|
|
3297
|
+
* `undefined` to return all available Row Ids if not specified. The `sorter`
|
|
3298
|
+
* parameter can provide a custom comparison function.
|
|
3226
3299
|
*
|
|
3227
3300
|
* Note that every call to this method will perform the sorting afresh - there
|
|
3228
3301
|
* is no caching of the results - and so you are advised to memoize the
|
|
@@ -3239,6 +3312,8 @@ export interface Store<in out Schemas extends OptionalSchemas> {
|
|
|
3239
3312
|
* any.
|
|
3240
3313
|
* @param limit The maximum number of Row Ids to return, or `undefined` for
|
|
3241
3314
|
* all.
|
|
3315
|
+
* @param sorter A custom function for comparing the sorting values, or
|
|
3316
|
+
* `undefined` to use the defaultSorter function.
|
|
3242
3317
|
* @returns An array of the sorted Ids of appropriate Rows in the Table.
|
|
3243
3318
|
* @example
|
|
3244
3319
|
* This example retrieves sorted Row Ids in a Table.
|
|
@@ -3310,6 +3385,24 @@ export interface Store<in out Schemas extends OptionalSchemas> {
|
|
|
3310
3385
|
* // -> ['cujo', 'felix', 'fido']
|
|
3311
3386
|
* ```
|
|
3312
3387
|
* @example
|
|
3388
|
+
* This example retrieves Row Ids sorted numerically by their own value.
|
|
3389
|
+
*
|
|
3390
|
+
* ```js
|
|
3391
|
+
* import {createStore} from 'tinybase';
|
|
3392
|
+
*
|
|
3393
|
+
* const store = createStore();
|
|
3394
|
+
* ['1', '10', '2'].forEach((rowId) =>
|
|
3395
|
+
* store.setRow('pets', rowId, {sold: false}),
|
|
3396
|
+
* );
|
|
3397
|
+
* const numericSorter = (sortKey1, sortKey2) =>
|
|
3398
|
+
* Number(sortKey1) - Number(sortKey2);
|
|
3399
|
+
*
|
|
3400
|
+
* console.log(
|
|
3401
|
+
* store.getSortedRowIds({tableId: 'pets', sorter: numericSorter}),
|
|
3402
|
+
* );
|
|
3403
|
+
* // -> ['1', '2', '10']
|
|
3404
|
+
* ```
|
|
3405
|
+
* @example
|
|
3313
3406
|
* This example retrieves the sorted Row Ids of a Table that does not exist,
|
|
3314
3407
|
* returning an empty array.
|
|
3315
3408
|
*
|
|
@@ -3329,13 +3422,14 @@ export interface Store<in out Schemas extends OptionalSchemas> {
|
|
|
3329
3422
|
descending?: boolean,
|
|
3330
3423
|
offset?: number,
|
|
3331
3424
|
limit?: number,
|
|
3425
|
+
sorter?: Sorter,
|
|
3332
3426
|
): Ids;
|
|
3333
3427
|
|
|
3334
3428
|
/**
|
|
3335
3429
|
* When called with one object argument, the getSortedRowIds method
|
|
3336
3430
|
* destructures it to make it easier to skip optional parameters.
|
|
3337
3431
|
* @param args A SortedRowIdsArgs object containing the Id of the Table in the
|
|
3338
|
-
* Store, and optional `cellId`, `descending`, `offset`, and `
|
|
3432
|
+
* Store, and optional `cellId`, `descending`, `offset`, `limit`, and `sorter`
|
|
3339
3433
|
* parameters.
|
|
3340
3434
|
* @returns An array of the sorted Ids of appropriate Rows in the Table.
|
|
3341
3435
|
* @example
|
|
@@ -4512,7 +4606,7 @@ export interface Store<in out Schemas extends OptionalSchemas> {
|
|
|
4512
4606
|
setPartialRow<TableId extends TableIdFromSchema<Schemas[0]>>(
|
|
4513
4607
|
tableId: TableId,
|
|
4514
4608
|
rowId: Id,
|
|
4515
|
-
partialRow:
|
|
4609
|
+
partialRow: PartialRow<Schemas[0], TableId>,
|
|
4516
4610
|
): this;
|
|
4517
4611
|
|
|
4518
4612
|
/**
|
|
@@ -4709,7 +4803,7 @@ export interface Store<in out Schemas extends OptionalSchemas> {
|
|
|
4709
4803
|
* @category Setter
|
|
4710
4804
|
* @since v3.0.0
|
|
4711
4805
|
*/
|
|
4712
|
-
setPartialValues(partialValues:
|
|
4806
|
+
setPartialValues(partialValues: PartialValues<Schemas[1]>): this;
|
|
4713
4807
|
|
|
4714
4808
|
/**
|
|
4715
4809
|
* The setValue method sets a single keyed Value in the Store.
|
|
@@ -7295,7 +7389,7 @@ export interface Store<in out Schemas extends OptionalSchemas> {
|
|
|
7295
7389
|
* addSortedRowIdsListener method destructures it to make it easier to skip
|
|
7296
7390
|
* optional parameters.
|
|
7297
7391
|
* @param args A SortedRowIdsArgs object containing the Id of the Table in the
|
|
7298
|
-
* Store, and optional `cellId`, `descending`, `offset`, and `
|
|
7392
|
+
* Store, and optional `cellId`, `descending`, `offset`, `limit`, and `sorter`
|
|
7299
7393
|
* parameters.
|
|
7300
7394
|
* @param listener The function that will be called whenever the sorted Row
|
|
7301
7395
|
* Ids in the Table change.
|