js-bao 0.12.0-alpha.2 → 0.12.0-alpha.20

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/README.md CHANGED
@@ -1023,6 +1023,12 @@ const electronicsCount = await Product.count({
1023
1023
 
1024
1024
  Perform complex data analysis with grouping, statistical operations, and faceting.
1025
1025
 
1026
+ `aggregate` answers one row per group: the `groupBy` values under `group`, as
1027
+ stored (`null` for a missing field), and each operation's value beside it under
1028
+ `count` or `<type>_<field>` — even when it is the only operation. Rows come back
1029
+ in `sort` order; no matching records gives `[]`. Two `groupBy` entries that
1030
+ would share a key in `group` throw `Ambiguous aggregation group keys`.
1031
+
1026
1032
  ### Basic Aggregation
1027
1033
 
1028
1034
  ```typescript
@@ -1031,7 +1037,11 @@ const categoryCounts = await Product.aggregate({
1031
1037
  groupBy: ["category"],
1032
1038
  operations: [{ type: "count" }],
1033
1039
  });
1034
- // Result: { "Electronics": 25, "Books": 18, "Clothing": 12 }
1040
+ // Result: [
1041
+ // { group: { category: "Electronics" }, count: 25 },
1042
+ // { group: { category: "Books" }, count: 18 },
1043
+ // { group: { category: "Clothing" }, count: 12 }
1044
+ // ]
1035
1045
 
1036
1046
  // Multiple statistical operations
1037
1047
  const categoryStats = await Product.aggregate({
@@ -1044,8 +1054,9 @@ const categoryStats = await Product.aggregate({
1044
1054
  { type: "max", field: "price" },
1045
1055
  ],
1046
1056
  });
1047
- // Result: {
1048
- // "Electronics": {
1057
+ // Result: [
1058
+ // {
1059
+ // group: { category: "Electronics" },
1049
1060
  // count: 25,
1050
1061
  // avg_price: 299.99,
1051
1062
  // sum_price: 7499.75,
@@ -1053,7 +1064,7 @@ const categoryStats = await Product.aggregate({
1053
1064
  // max_price: 1299.99
1054
1065
  // },
1055
1066
  // ...
1056
- // }
1067
+ // ]
1057
1068
  ```
1058
1069
 
1059
1070
  ### Multi-Dimensional Grouping
@@ -1064,15 +1075,11 @@ const salesData = await Product.aggregate({
1064
1075
  groupBy: ["category", "brand"],
1065
1076
  operations: [{ type: "count" }, { type: "sum", field: "revenue" }],
1066
1077
  });
1067
- // Result: {
1068
- // "Electronics": {
1069
- // "Apple": { count: 12, sum_revenue: 15000 },
1070
- // "Samsung": { count: 8, sum_revenue: 9500 }
1071
- // },
1072
- // "Books": {
1073
- // "Penguin": { count: 25, sum_revenue: 450 }
1074
- // }
1075
- // }
1078
+ // Result: [
1079
+ // { group: { category: "Electronics", brand: "Apple" }, count: 12, sum_revenue: 15000 },
1080
+ // { group: { category: "Electronics", brand: "Samsung" }, count: 8, sum_revenue: 9500 },
1081
+ // { group: { category: "Books", brand: "Penguin" }, count: 25, sum_revenue: 450 }
1082
+ // ]
1076
1083
  ```
1077
1084
 
1078
1085
  ### StringSet Aggregation
@@ -1105,14 +1112,21 @@ const tagCounts = await Article.aggregate({
1105
1112
  groupBy: ["tags"], // StringSet faceting
1106
1113
  operations: [{ type: "count" }],
1107
1114
  });
1108
- // Result: { "javascript": 45, "react": 32, "tutorial": 28 }
1115
+ // Result: [
1116
+ // { group: { tags: "javascript" }, count: 45 },
1117
+ // { group: { tags: "react" }, count: 32 },
1118
+ // { group: { tags: "tutorial" }, count: 28 }
1119
+ // ]
1109
1120
 
1110
1121
  // Membership-based grouping (articles that have specific tag vs don't)
1111
1122
  const urgentCounts = await Article.aggregate({
1112
1123
  groupBy: [{ field: "tags", contains: "urgent" }],
1113
1124
  operations: [{ type: "count" }],
1114
1125
  });
1115
- // Result: { "true": 5, "false": 120 }
1126
+ // Result: [
1127
+ // { group: { has_tags_urgent: "true" }, count: 5 },
1128
+ // { group: { has_tags_urgent: "false" }, count: 120 }
1129
+ // ]
1116
1130
 
1117
1131
  // Complex aggregation with filtering and sorting
1118
1132
  const topTags = await Article.aggregate({
@@ -1128,7 +1142,11 @@ const topTags = await Article.aggregate({
1128
1142
 
1129
1143
  ```typescript
1130
1144
  interface AggregationOptions {
1131
- groupBy: (string | { field: string; contains: string })[];
1145
+ groupBy: (
1146
+ | string // a plain field, or a StringSet facet
1147
+ | { field: string; contains: string } // StringSet membership: has_<field>_<contains>
1148
+ | { field: string; period: "month" | "quarter" | "year" } // date period: <field>_<period>
1149
+ )[];
1132
1150
  operations: {
1133
1151
  type: "count" | "sum" | "avg" | "min" | "max";
1134
1152
  field?: string; // Required for sum, avg, min, max
@@ -1143,6 +1161,22 @@ interface AggregationOptions {
1143
1161
  }
1144
1162
  ```
1145
1163
 
1164
+ A period level groups a `date` (or `string`) field by calendar month, quarter
1165
+ or year. Its value in `group` is `YYYY-MM`, `YYYY-Qn` or `YYYY`, read from the
1166
+ stored text with no time-zone conversion (a UTC ISO timestamp buckets by its
1167
+ UTC date); a missing or non-date value is `null`. The keys sort
1168
+ chronologically, so `sort` can name them:
1169
+
1170
+ ```typescript
1171
+ const monthly = await Transaction.aggregate({
1172
+ filter: { date: { $gte: "2025-11-01", $lte: "2026-10-31" } },
1173
+ groupBy: ["categoryId", { field: "date", period: "month" }],
1174
+ operations: [{ type: "sum", field: "amount" }, { type: "count" }],
1175
+ sort: { field: "date_month", direction: 1 },
1176
+ });
1177
+ // [{ group: { categoryId: "food", date_month: "2025-11" }, sum_amount: -50, count: 2 }, ...]
1178
+ ```
1179
+
1146
1180
  ## StringSet Fields
1147
1181
 
1148
1182
  StringSet is a special field type optimized for tag-like data, providing efficient membership queries and faceting capabilities.
@@ -1254,7 +1288,10 @@ const tagStats = await Article.aggregate({
1254
1288
  groupBy: [{ field: "tags", contains: "tutorial" }],
1255
1289
  operations: [{ type: "count" }],
1256
1290
  });
1257
- // Result: { "true": 25, "false": 75 }
1291
+ // Result: [
1292
+ // { group: { has_tags_tutorial: "true" }, count: 25 },
1293
+ // { group: { has_tags_tutorial: "false" }, count: 75 }
1294
+ // ]
1258
1295
  ```
1259
1296
 
1260
1297
  ## Database Engine Specifics
@@ -1273,6 +1310,9 @@ public server path).
1273
1310
 
1274
1311
  - **Vite / Create React App**: copy `node_modules/sql.js/dist/sql-wasm.wasm` into
1275
1312
  your project's `public/` directory so it is served at `/sql-wasm.wasm`.
1313
+ Copy it from the sql.js that js-bao resolved, and again whenever that version
1314
+ changes: sql.js only loads the wasm built with it, and a wasm from another
1315
+ version rejects `ensureReady()` with a `sql.js wasm mismatch` error.
1276
1316
  - **Custom path**: if the wasm lives elsewhere, point at it via `DatabaseConfig`:
1277
1317
 
1278
1318
  ```typescript
@@ -1556,9 +1596,61 @@ See the `examples/` directory for complete working examples:
1556
1596
 
1557
1597
  ## Release notes
1558
1598
 
1559
- Changes to the on-disk behaviour of `js-bao/cloudflare/do` (`DurableObjectEngine`)
1560
- that a store's owner can observe. Newest first.
1561
-
1599
+ Changes a consumer of this package can observe, including the on-disk
1600
+ behaviour of `js-bao/cloudflare/do` (`DurableObjectEngine`). Newest first.
1601
+
1602
+ - **A db3 binds with no `Y.Doc`; the browser entry loads Yjs on demand**
1603
+ (unreleased). `connectDocument(id, null, permission, { documentFormat: 3 })`
1604
+ binds a db3, and the connected record's `yDoc` is `null` for a format-3
1605
+ document whatever was passed (`ConnectedDocument.yDoc` is now
1606
+ `Y.Doc | null`). A format-1 connect with `null` is refused, naming the fix.
1607
+ `ModelRegistry.initializeAllForDocument` and `BaseModel.initializeForDocument`
1608
+ take `Y.Doc | null` too. The browser entry (`js-bao/browser`, the `browser`
1609
+ condition) imports `yjs` only at the first format-1 `connectDocument`, or
1610
+ when `loadYjs()` is awaited, so an app that only uses db3s never loads it.
1611
+ Its CommonJS file (`dist/browser.cjs`) loads Yjs at the same moment with
1612
+ `require("yjs")`, so it shares the copy a CommonJS caller requires. Every entry exports `loadYjs()`, `yjsLoaded()` and `YjsNotLoadedError`; on
1613
+ the `node`, default and `cloudflare` entries Yjs is static and `loadYjs()`
1614
+ resolves at once. On the browser entry, every helper that takes a `Y.Doc`
1615
+ (`discoverSchema`, `syncModelMeta` and the rest) throws `YjsNotLoadedError`
1616
+ until Yjs is loaded: `await loadYjs()` first when you use one on a `Y.Doc` of
1617
+ your own before any format-1 document is connected. `yjs` stays a peer
1618
+ dependency; an app that wants it in its startup bundle imports it once
1619
+ statically.
1620
+ - **Breaking: format 2 (large document) is removed** (0.12.0). Every
1621
+ `models/format2*` export that served the epoch overlay is gone — the record
1622
+ store and its asynchronous host, the overlay observer and fold, the epoch
1623
+ handoff and rotation, the cold start and snapshot loader, the offline window
1624
+ (`DocumentOfflineWindowError`) and offline replay — with `utils/largeDocuments`
1625
+ and `utils/overlaySql`. `InitJsBaoOptions` loses `largeDocumentStorage` and
1626
+ `InitJsBaoResult` its `format2*` hooks and `whenFormat2ProjectionSettled`;
1627
+ `connectDocument` refuses `documentFormat: 2` with `DOCUMENT_FORMAT_UNSUPPORTED`.
1628
+ The OPFS worker and broker, `Format2StoreSql`, `createFormat2Sql()`,
1629
+ `largeDocumentStoreScope` and the row cache stay: a record-log document
1630
+ (`documentFormat: 3`) runs on them. The worker answers `serve`,
1631
+ `storedEpoch`, `exportLegacy`, `importLegacy` and `dropLegacy` as unknown
1632
+ commands.
1633
+ - **Breaking: `createModelClass` is removed** (#4285). It is no longer exported
1634
+ from `js-bao` (or re-exported from `js-bao-wss-client`). Define a model as
1635
+ `class X extends BaseModel {}` and attach its schema with
1636
+ `attachAndRegisterModel(X, schema)`, as [Defining Models](#defining-models-schema-first-no-decorators)
1637
+ shows; instance methods, getters and statics go on the class body. The v1
1638
+ codegen (`js-bao-codegen`) no longer recognises a `createModelClass(...)`
1639
+ call as a model definition; it reads the class-based pattern as before.
1640
+ - **New stores get a keyed StringSet table** (#3881). A store the engine
1641
+ creates declares `stringset_index` as `PRIMARY KEY (_type, _record_id, field,
1642
+ value) WITHOUT ROWID` with `idx_ss_type_field_value` as its one named index,
1643
+ so a StringSet value costs two rows written instead of three.
1644
+ `DURABLE_OBJECT_SCHEMA_OPTIONS.memberShape` is `"keyed"`, and
1645
+ `engine.memberTableShape` answers `"keyed"` for such a store. An existing
1646
+ store keeps its record-first or field-first table; all three shapes return
1647
+ the same results. On the platform, a plain document converts at its next
1648
+ projection rebuild; databases and large documents keep their shape.
1649
+ `rebuildMemberTable` now builds the keyed table. Rollback floor: a keyed table
1650
+ has no `rowid`, so once a keyed store exists do not roll back past the
1651
+ release that first returned StringSet values sorted by code point (it reads
1652
+ `ORDER BY value`); an older release fails on a keyed table with `no such
1653
+ column: rowid`.
1562
1654
  - **New stores get a record-first StringSet table** (#3877). A store the
1563
1655
  engine creates declares `stringset_index` with `UNIQUE(_type, _record_id,
1564
1656
  field, value)` (record-first) instead of `UNIQUE(_type, field, _record_id,