tinybase 9.4.0-beta.0 → 9.4.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.
@@ -1011,6 +1011,138 @@ export type Select = {
1011
1011
  ): SelectedAs;
1012
1012
  };
1013
1013
 
1014
+ /**
1015
+ * The CellIdMapper type describes a function used by a SelectAll clause to map
1016
+ * a source Cell Id to a Cell Id in the query result.
1017
+ * @param cellId The Id of a Cell in the source Row.
1018
+ * @returns The Id to use for that Cell in the result Row.
1019
+ * @category Callback
1020
+ * @since v9.4.0
1021
+ */
1022
+ export type CellIdMapper = (cellId: Id) => Id;
1023
+
1024
+ /**
1025
+ * The SelectAll type describes a function that lets you select every Cell
1026
+ * present in a source Row.
1027
+ *
1028
+ * The SelectAll function is provided to the third `query` parameter of the
1029
+ * setQueryDefinition method. Different source Rows can contain different Cell
1030
+ * Ids, and each result Row will contain only the Cells present in its
1031
+ * corresponding source Row. The result Table's Cell Ids are therefore the union
1032
+ * of the Cells in its result Rows.
1033
+ *
1034
+ * Source Cell Ids are processed in lexical order, and selection clauses are
1035
+ * processed in the order they are declared. If multiple selected Cells map to
1036
+ * the same result Cell Id, the later one wins.
1037
+ *
1038
+ * When used in a grouped query, the current table-wide union of source Cell Ids
1039
+ * is expanded into individual selections. The query is rebuilt if this union
1040
+ * changes, and any selected Cell that is not grouped becomes a grouping
1041
+ * dimension as usual.
1042
+ * @category Definition
1043
+ * @since v9.4.0
1044
+ */
1045
+ export type SelectAll = {
1046
+ /**
1047
+ * Calling this function with no parameters will select every Cell present in
1048
+ * each Row of the query's root Table, retaining the source Cell Ids.
1049
+ * @example
1050
+ * This example selects all the Cells from heterogeneous root Rows:
1051
+ *
1052
+ * ```js
1053
+ * import {createQueries, createStore} from 'tinybase';
1054
+ *
1055
+ * const store = createStore().setTable('pets', {
1056
+ * fido: {species: 'dog', color: 'brown'},
1057
+ * felix: {species: 'cat', indoor: true},
1058
+ * });
1059
+ * const queries = createQueries(store);
1060
+ *
1061
+ * queries.setQueryDefinition('query', 'pets', ({selectAll}) => selectAll());
1062
+ *
1063
+ * console.log(queries.getResultRow('query', 'fido'));
1064
+ * // -> {color: 'brown', species: 'dog'}
1065
+ * console.log(queries.getResultRow('query', 'felix'));
1066
+ * // -> {indoor: true, species: 'cat'}
1067
+ * ```
1068
+ * @category Definition
1069
+ * @since v9.4.0
1070
+ */
1071
+ (): void;
1072
+ /**
1073
+ * Calling this function with a joined Table Id will select every Cell present
1074
+ * in the joined Row.
1075
+ *
1076
+ * An optional second parameter can map each Cell Id with a prefix or
1077
+ * callback.
1078
+ * @param joinedTableId The Id of the joined Table. If the Table was joined
1079
+ * 'as' a different Id, that should instead be used.
1080
+ * @param cellIdPrefixOrMapper An optional prefix to prepend to every Cell Id,
1081
+ * or a CellIdMapper callback that returns each result Cell Id.
1082
+ * @example
1083
+ * This example selects all the Cells from a joined Table, retaining their
1084
+ * Cell Ids:
1085
+ *
1086
+ * ```js
1087
+ * import {createQueries, createStore} from 'tinybase';
1088
+ *
1089
+ * const store = createStore()
1090
+ * .setTable('pets', {fido: {ownerId: '1'}})
1091
+ * .setTable('owners', {'1': {name: 'Alice', city: 'London'}});
1092
+ * const queries = createQueries(store);
1093
+ *
1094
+ * queries.setQueryDefinition('query', 'pets', ({selectAll, join}) => {
1095
+ * selectAll('owners');
1096
+ * join('owners', 'ownerId');
1097
+ * });
1098
+ *
1099
+ * console.log(queries.getResultRow('query', 'fido'));
1100
+ * // -> {city: 'London', name: 'Alice'}
1101
+ * ```
1102
+ * @category Definition
1103
+ * @since v9.4.0
1104
+ */
1105
+ (joinedTableId: Id, cellIdPrefixOrMapper?: Id | CellIdMapper): void;
1106
+ /**
1107
+ * Calling this function with `true` and a joined query Id will select every
1108
+ * Cell present in the joined result Row.
1109
+ *
1110
+ * An optional third parameter can map each Cell Id with a prefix or callback.
1111
+ * @param asQuery A flag indicating that the next Id is a query Id.
1112
+ * @param joinedQueryId The Id of the joined query result. If the query result
1113
+ * was joined 'as' a different Id, that should instead be used.
1114
+ * @param cellIdPrefixOrMapper An optional prefix to prepend to every Cell Id,
1115
+ * or a CellIdMapper callback that returns each result Cell Id.
1116
+ * @example
1117
+ * This example selects all the Cells from a joined query result, prefixing
1118
+ * each Cell Id:
1119
+ *
1120
+ * ```js
1121
+ * import {createQueries, createStore} from 'tinybase';
1122
+ *
1123
+ * const store = createStore()
1124
+ * .setTable('pets', {fido: {ownerId: '1'}})
1125
+ * .setTable('owners', {'1': {name: 'Alice', city: 'London'}});
1126
+ * const queries = createQueries(store)
1127
+ * .setQueryDefinition('owners', 'owners', ({selectAll}) => selectAll())
1128
+ * .setQueryDefinition('query', 'pets', ({selectAll, join}) => {
1129
+ * selectAll(true, 'owners', 'owner.');
1130
+ * join(true, 'owners', 'ownerId');
1131
+ * });
1132
+ *
1133
+ * console.log(queries.getResultRow('query', 'fido'));
1134
+ * // -> {'owner.city': 'London', 'owner.name': 'Alice'}
1135
+ * ```
1136
+ * @category Definition
1137
+ * @since v9.4.0
1138
+ */
1139
+ (
1140
+ asQuery: true,
1141
+ joinedQueryId: Id,
1142
+ cellIdPrefixOrMapper?: Id | CellIdMapper,
1143
+ ): void;
1144
+ };
1145
+
1014
1146
  /**
1015
1147
  * The SelectedAs type describes an object returned from calling a Select
1016
1148
  * function so that the selected Cell Id can be optionally aliased.
@@ -1257,8 +1389,8 @@ export type Join = {
1257
1389
  (joinedTableId: Id, on: Id): JoinedAs;
1258
1390
  /**
1259
1391
  * Calling this function with three parameters (where the first is `true`)
1260
- * will indicate that the join to a Row in an adjacent query result is made
1261
- * by finding its Id in a Cell of the query's root Table.
1392
+ * will indicate that the join to a Row in an adjacent query result is made by
1393
+ * finding its Id in a Cell of the query's root Table.
1262
1394
  * @param asQuery A flag indicating that the next Id is a query Id.
1263
1395
  * @param joinedQueryId The Id of the query result to join to.
1264
1396
  * @param on The Id of the Cell in the root Table that contains the joined
@@ -1323,8 +1455,8 @@ export type Join = {
1323
1455
  */
1324
1456
  (joinedTableId: Id, fromIntermediateJoinedTableId: Id, on: Id): JoinedAs;
1325
1457
  /**
1326
- * Calling this function with four parameters (where the first is `true`)
1327
- * will indicate that the join to a Row in a distant query result is made by
1458
+ * Calling this function with four parameters (where the first is `true`) will
1459
+ * indicate that the join to a Row in a distant query result is made by
1328
1460
  * finding its Id in a Cell of an intermediately joined Table.
1329
1461
  * @param asQuery A flag indicating that the next Id is a query Id.
1330
1462
  * @param joinedQueryId The Id of the distant query result to join to.
@@ -1371,9 +1503,9 @@ export type Join = {
1371
1503
  ): JoinedAs;
1372
1504
  /**
1373
1505
  * Calling this function with four parameters (where the first is `true` and
1374
- * the fourth is a function) will indicate that the join to a Row in a
1375
- * distant query result is made by calculating its Id from the Cells and the
1376
- * Row Id of an intermediately joined Table.
1506
+ * the fourth is a function) will indicate that the join to a Row in a distant
1507
+ * query result is made by calculating its Id from the Cells and the Row Id of
1508
+ * an intermediately joined Table.
1377
1509
  * @param asQuery A flag indicating that the next Id is a query Id.
1378
1510
  * @param joinedQueryId The Id of the query result to join to.
1379
1511
  * @param fromIntermediateJoinedTableId The Id of an intermediate Table (which
@@ -2102,6 +2234,7 @@ export interface Queries {
2102
2234
  tableId: Id,
2103
2235
  query: (keywords: {
2104
2236
  select: Select;
2237
+ selectAll: SelectAll;
2105
2238
  join: Join;
2106
2239
  where: Where;
2107
2240
  group: Group;
@@ -2116,6 +2249,7 @@ export interface Queries {
2116
2249
  rootQueryId: Id,
2117
2250
  query: (keywords: {
2118
2251
  select: Select;
2252
+ selectAll: SelectAll;
2119
2253
  join: Join;
2120
2254
  where: Where;
2121
2255
  group: Group;
@@ -1158,6 +1158,148 @@ export type Select<
1158
1158
  ): SelectedAs;
1159
1159
  };
1160
1160
 
1161
+ /**
1162
+ * The CellIdMapper type describes a function used by a SelectAll clause to map
1163
+ * a source Cell Id to a Cell Id in the query result.
1164
+ * @param cellId The Id of a Cell in the source Row.
1165
+ * @returns The Id to use for that Cell in the result Row.
1166
+ * @category Callback
1167
+ * @since v9.4.0
1168
+ */
1169
+ export type CellIdMapper<CellId extends Id = Id> = (cellId: CellId) => Id;
1170
+
1171
+ /**
1172
+ * The SelectAll type describes a function that lets you select every Cell
1173
+ * present in a source Row.
1174
+ *
1175
+ * The SelectAll function is provided to the third `query` parameter of the
1176
+ * setQueryDefinition method. Different source Rows can contain different Cell
1177
+ * Ids, and each result Row will contain only the Cells present in its
1178
+ * corresponding source Row. The result Table's Cell Ids are therefore the union
1179
+ * of the Cells in its result Rows.
1180
+ *
1181
+ * Source Cell Ids are processed in lexical order, and selection clauses are
1182
+ * processed in the order they are declared. If multiple selected Cells map to
1183
+ * the same result Cell Id, the later one wins.
1184
+ *
1185
+ * When used in a grouped query, the current table-wide union of source Cell Ids
1186
+ * is expanded into individual selections. The query is rebuilt if this union
1187
+ * changes, and any selected Cell that is not grouped becomes a grouping
1188
+ * dimension as usual.
1189
+ * @category Definition
1190
+ * @since v9.4.0
1191
+ */
1192
+ export type SelectAll<Schema extends OptionalTablesSchema> = {
1193
+ /**
1194
+ * Calling this function with no parameters will select every Cell present in
1195
+ * each Row of the query's root Table, retaining the source Cell Ids.
1196
+ * @example
1197
+ * This example selects all the Cells from heterogeneous root Rows:
1198
+ *
1199
+ * ```js
1200
+ * import {createQueries, createStore} from 'tinybase';
1201
+ *
1202
+ * const store = createStore().setTable('pets', {
1203
+ * fido: {species: 'dog', color: 'brown'},
1204
+ * felix: {species: 'cat', indoor: true},
1205
+ * });
1206
+ * const queries = createQueries(store);
1207
+ *
1208
+ * queries.setQueryDefinition('query', 'pets', ({selectAll}) => selectAll());
1209
+ *
1210
+ * console.log(queries.getResultRow('query', 'fido'));
1211
+ * // -> {color: 'brown', species: 'dog'}
1212
+ * console.log(queries.getResultRow('query', 'felix'));
1213
+ * // -> {indoor: true, species: 'cat'}
1214
+ * ```
1215
+ * @category Definition
1216
+ * @since v9.4.0
1217
+ */
1218
+ (): void;
1219
+ /**
1220
+ * Calling this function with a joined Table Id will select every Cell present
1221
+ * in the joined Row.
1222
+ *
1223
+ * This has schema-based typing. The following is a simplified representation:
1224
+ *
1225
+ * ```ts override
1226
+ * (joinedTableId: Id, cellIdPrefixOrMapper?: Id | CellIdMapper): void;
1227
+ * ```
1228
+ *
1229
+ * An optional second parameter can map each Cell Id with a prefix or
1230
+ * callback.
1231
+ * @param joinedTableId The Id of the joined Table. If the Table was joined
1232
+ * 'as' a different Id, that should instead be used.
1233
+ * @param cellIdPrefixOrMapper An optional prefix to prepend to every Cell Id,
1234
+ * or a CellIdMapper callback that returns each result Cell Id.
1235
+ * @example
1236
+ * This example selects all the Cells from a joined Table, retaining their
1237
+ * Cell Ids:
1238
+ *
1239
+ * ```js
1240
+ * import {createQueries, createStore} from 'tinybase';
1241
+ *
1242
+ * const store = createStore()
1243
+ * .setTable('pets', {fido: {ownerId: '1'}})
1244
+ * .setTable('owners', {'1': {name: 'Alice', city: 'London'}});
1245
+ * const queries = createQueries(store);
1246
+ *
1247
+ * queries.setQueryDefinition('query', 'pets', ({selectAll, join}) => {
1248
+ * selectAll('owners');
1249
+ * join('owners', 'ownerId');
1250
+ * });
1251
+ *
1252
+ * console.log(queries.getResultRow('query', 'fido'));
1253
+ * // -> {city: 'London', name: 'Alice'}
1254
+ * ```
1255
+ * @category Definition
1256
+ * @since v9.4.0
1257
+ */
1258
+ <JoinedTableId extends TableIdFromSchema<Schema> | Id>(
1259
+ joinedTableId: JoinedTableId,
1260
+ cellIdPrefixOrMapper?:
1261
+ Id | CellIdMapper<JoinedCellIdOrId<Schema, JoinedTableId>>,
1262
+ ): void;
1263
+ /**
1264
+ * Calling this function with `true` and a joined query Id will select every
1265
+ * Cell present in the joined result Row.
1266
+ *
1267
+ * An optional third parameter can map each Cell Id with a prefix or callback.
1268
+ * @param asQuery A flag indicating that the next Id is a query Id.
1269
+ * @param joinedQueryId The Id of the joined query result. If the query result
1270
+ * was joined 'as' a different Id, that should instead be used.
1271
+ * @param cellIdPrefixOrMapper An optional prefix to prepend to every Cell Id,
1272
+ * or a CellIdMapper callback that returns each result Cell Id.
1273
+ * @example
1274
+ * This example selects all the Cells from a joined query result, prefixing
1275
+ * each Cell Id:
1276
+ *
1277
+ * ```js
1278
+ * import {createQueries, createStore} from 'tinybase';
1279
+ *
1280
+ * const store = createStore()
1281
+ * .setTable('pets', {fido: {ownerId: '1'}})
1282
+ * .setTable('owners', {'1': {name: 'Alice', city: 'London'}});
1283
+ * const queries = createQueries(store)
1284
+ * .setQueryDefinition('owners', 'owners', ({selectAll}) => selectAll())
1285
+ * .setQueryDefinition('query', 'pets', ({selectAll, join}) => {
1286
+ * selectAll(true, 'owners', 'owner.');
1287
+ * join(true, 'owners', 'ownerId');
1288
+ * });
1289
+ *
1290
+ * console.log(queries.getResultRow('query', 'fido'));
1291
+ * // -> {'owner.city': 'London', 'owner.name': 'Alice'}
1292
+ * ```
1293
+ * @category Definition
1294
+ * @since v9.4.0
1295
+ */
1296
+ (
1297
+ asQuery: true,
1298
+ joinedQueryId: Id,
1299
+ cellIdPrefixOrMapper?: Id | CellIdMapper,
1300
+ ): void;
1301
+ };
1302
+
1161
1303
  /**
1162
1304
  * The SelectedAs type describes an object returned from calling a Select
1163
1305
  * function so that the selected Cell Id can be optionally aliased.
@@ -1410,8 +1552,8 @@ export type Join<
1410
1552
  ): JoinedAs;
1411
1553
  /**
1412
1554
  * Calling this function with three parameters (where the first is `true`)
1413
- * will indicate that the join to a Row in an adjacent query result is made
1414
- * by finding its Id in a Cell of the query's root Table.
1555
+ * will indicate that the join to a Row in an adjacent query result is made by
1556
+ * finding its Id in a Cell of the query's root Table.
1415
1557
  * @param asQuery A flag indicating that the next Id is a query Id.
1416
1558
  * @param joinedQueryId The Id of the query result to join to.
1417
1559
  * @param on The Id of the Cell in the root Table that contains the joined
@@ -1491,8 +1633,8 @@ export type Join<
1491
1633
  on: IntermediateJoinedCellId,
1492
1634
  ): JoinedAs;
1493
1635
  /**
1494
- * Calling this function with four parameters (where the first is `true`)
1495
- * will indicate that the join to a Row in a distant query result is made by
1636
+ * Calling this function with four parameters (where the first is `true`) will
1637
+ * indicate that the join to a Row in a distant query result is made by
1496
1638
  * finding its Id in a Cell of an intermediately joined Table.
1497
1639
  * @param asQuery A flag indicating that the next Id is a query Id.
1498
1640
  * @param joinedQueryId The Id of the distant query result to join to.
@@ -1553,9 +1695,9 @@ export type Join<
1553
1695
  ): JoinedAs;
1554
1696
  /**
1555
1697
  * Calling this function with four parameters (where the first is `true` and
1556
- * the fourth is a function) will indicate that the join to a Row in a
1557
- * distant query result is made by calculating its Id from the Cells and the
1558
- * Row Id of an intermediately joined Table.
1698
+ * the fourth is a function) will indicate that the join to a Row in a distant
1699
+ * query result is made by calculating its Id from the Cells and the Row Id of
1700
+ * an intermediately joined Table.
1559
1701
  * @param asQuery A flag indicating that the next Id is a query Id.
1560
1702
  * @param joinedQueryId The Id of the query result to join to.
1561
1703
  * @param fromIntermediateJoinedTableId The Id of an intermediate Table (which
@@ -2256,6 +2398,7 @@ export interface Queries<in out Schemas extends OptionalSchemas> {
2256
2398
  * tableId: Id,
2257
2399
  * query: (keywords: {
2258
2400
  * select: Select;
2401
+ * selectAll: SelectAll;
2259
2402
  * join: Join;
2260
2403
  * where: Where;
2261
2404
  * group: Group;
@@ -2270,6 +2413,7 @@ export interface Queries<in out Schemas extends OptionalSchemas> {
2270
2413
  * rootQueryId: Id,
2271
2414
  * query: (keywords: {
2272
2415
  * select: Select;
2416
+ * selectAll: SelectAll;
2273
2417
  * join: Join;
2274
2418
  * where: Where;
2275
2419
  * group: Group;
@@ -2348,6 +2492,7 @@ export interface Queries<in out Schemas extends OptionalSchemas> {
2348
2492
  tableId: RootTableId,
2349
2493
  query: (keywords: {
2350
2494
  select: Select<Schemas[0], RootTableId>;
2495
+ selectAll: SelectAll<Schemas[0]>;
2351
2496
  join: Join<Schemas[0], RootTableId>;
2352
2497
  where: Where<Schemas[0], RootTableId>;
2353
2498
  group: Group;
@@ -2362,6 +2507,7 @@ export interface Queries<in out Schemas extends OptionalSchemas> {
2362
2507
  rootQueryId: Id,
2363
2508
  query: (keywords: {
2364
2509
  select: Select<any, any>;
2510
+ selectAll: SelectAll<any>;
2365
2511
  join: Join<any, any>;
2366
2512
  where: Where<any, any>;
2367
2513
  group: Group;