@recordtimelabel/core 0.1.8 → 0.1.9

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
@@ -20,7 +20,7 @@ During local development an app can consume a sibling checkout with:
20
20
  For release builds, consume a fixed npm package, git tag, or private registry version so builds do not depend on a sibling folder path. The current published release is:
21
21
 
22
22
  ```json
23
- "@recordtimelabel/core": "0.1.7"
23
+ "@recordtimelabel/core": "0.1.9"
24
24
  ```
25
25
 
26
26
  If this checkout's `package.json` is ahead of the published version, publish the new package before updating consumers to that version.
@@ -43,6 +43,7 @@ If this checkout's `package.json` is ahead of the published version, publish the
43
43
  - `buildFirestoreV1UserPatch(state, options)`
44
44
  - `buildFirestoreV2LogicalPaths(userId)`
45
45
  - `buildFirestoreV2DocumentsFromState(state, options)`
46
+ - `buildFirestoreV2DocumentChangeSet(previousDocuments, nextDocuments, options)`
46
47
  - `buildStateFromFirestoreV2Documents(documents, options)`
47
48
  - `hasMeaningfulRecordTimeLabelCloudState(data, options)`
48
49
  - `buildRecordTimeLabelContentFingerprint(data)`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@recordtimelabel/core",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "type": "module",
5
5
  "description": "Shared RecordTimeLabel data model, merge logic, operations, and sync engine.",
6
6
  "main": "./src/index.js",
package/src/index.js CHANGED
@@ -5,7 +5,7 @@ const REQUIRED_FOLDERS = [
5
5
  { id: DEFAULT_FOLDER_ID, name: 'Uncategorized' }
6
6
  ];
7
7
 
8
- export const RECORD_TIMELABEL_CORE_VERSION = '0.1.8';
8
+ export const RECORD_TIMELABEL_CORE_VERSION = '0.1.9';
9
9
 
10
10
  export const OPERATION_TYPES = Object.freeze({
11
11
  RECORD_CREATE: 'record.create',
@@ -1274,6 +1274,94 @@ export const buildFirestoreV2DocumentsFromState = (state = {}, options = {}) =>
1274
1274
  };
1275
1275
  };
1276
1276
 
1277
+ const canonicalizeFirestoreV2DocumentValue = (value) => {
1278
+ if (Array.isArray(value)) {
1279
+ return value.map((entry) => canonicalizeFirestoreV2DocumentValue(entry));
1280
+ }
1281
+ if (value instanceof Date) {
1282
+ return value.toISOString();
1283
+ }
1284
+ if (!value || typeof value !== 'object') {
1285
+ return value;
1286
+ }
1287
+
1288
+ return Object.keys(value)
1289
+ .sort()
1290
+ .reduce((result, key) => {
1291
+ result[key] = canonicalizeFirestoreV2DocumentValue(value[key]);
1292
+ return result;
1293
+ }, {});
1294
+ };
1295
+
1296
+ const areFirestoreV2DocumentValuesEqual = (left, right) => (
1297
+ JSON.stringify(canonicalizeFirestoreV2DocumentValue(left)) ===
1298
+ JSON.stringify(canonicalizeFirestoreV2DocumentValue(right))
1299
+ );
1300
+
1301
+ const buildFirestoreV2CollectionChangeSet = (previousCollection, nextCollection, allowDeletes) => {
1302
+ const previous = previousCollection && typeof previousCollection === 'object'
1303
+ ? previousCollection
1304
+ : {};
1305
+ const next = nextCollection && typeof nextCollection === 'object'
1306
+ ? nextCollection
1307
+ : {};
1308
+ const upserts = {};
1309
+
1310
+ Object.keys(next).sort().forEach((documentId) => {
1311
+ if (!Object.prototype.hasOwnProperty.call(previous, documentId) ||
1312
+ !areFirestoreV2DocumentValuesEqual(previous[documentId], next[documentId])) {
1313
+ upserts[documentId] = next[documentId];
1314
+ }
1315
+ });
1316
+
1317
+ const deleteIds = allowDeletes
1318
+ ? Object.keys(previous).filter((documentId) => (
1319
+ !Object.prototype.hasOwnProperty.call(next, documentId)
1320
+ )).sort()
1321
+ : [];
1322
+
1323
+ return { upserts, deleteIds };
1324
+ };
1325
+
1326
+ export const buildFirestoreV2DocumentChangeSet = (
1327
+ previousDocuments = {},
1328
+ nextDocuments = {},
1329
+ options = {}
1330
+ ) => {
1331
+ const allowDeletes = options.allowDeletes === true;
1332
+ const previousRoot = previousDocuments?.root;
1333
+ const nextRoot = nextDocuments?.root;
1334
+ const rootUpsert = nextRoot && !areFirestoreV2DocumentValuesEqual(previousRoot, nextRoot)
1335
+ ? nextRoot
1336
+ : null;
1337
+ const records = buildFirestoreV2CollectionChangeSet(
1338
+ previousDocuments?.records,
1339
+ nextDocuments?.records,
1340
+ allowDeletes
1341
+ );
1342
+ const folders = buildFirestoreV2CollectionChangeSet(
1343
+ previousDocuments?.folders,
1344
+ nextDocuments?.folders,
1345
+ allowDeletes
1346
+ );
1347
+ const ops = buildFirestoreV2CollectionChangeSet(
1348
+ previousDocuments?.ops,
1349
+ nextDocuments?.ops,
1350
+ allowDeletes
1351
+ );
1352
+ const hasCollectionChanges = [records, folders, ops].some((changeSet) => (
1353
+ Object.keys(changeSet.upserts).length > 0 || changeSet.deleteIds.length > 0
1354
+ ));
1355
+
1356
+ return {
1357
+ hasChanges: Boolean(rootUpsert) || hasCollectionChanges,
1358
+ root: { upsert: rootUpsert },
1359
+ records,
1360
+ folders,
1361
+ ops
1362
+ };
1363
+ };
1364
+
1277
1365
  const removeV2Metadata = (data = {}) => {
1278
1366
  const { schemaVersion, folderId, ...rest } = data || {};
1279
1367
  return rest;
@@ -2125,6 +2213,7 @@ export default {
2125
2213
  buildFirestoreV1UserPatch,
2126
2214
  buildFirestoreV2LogicalPaths,
2127
2215
  buildFirestoreV2DocumentsFromState,
2216
+ buildFirestoreV2DocumentChangeSet,
2128
2217
  buildStateFromFirestoreV2Documents,
2129
2218
  flushPendingOperations,
2130
2219
  mergeRemoteStateIntoLocal,