@foretag/tanstack-db-surrealdb 0.1.15 → 0.1.17

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/dist/index.js CHANGED
@@ -30,7 +30,9 @@ var import_surrealdb = require("surrealdb");
30
30
  function manageTable(db, useLoro, { name, ...args }) {
31
31
  const fields = args.fields ?? "*";
32
32
  const listAll = async () => {
33
- return await db.select(new import_surrealdb.Table(name)).where(args.where).fields(...fields);
33
+ let q = db.select(new import_surrealdb.Table(name));
34
+ if (args.where) q = q.where(args.where);
35
+ return await q.fields(...fields);
34
36
  };
35
37
  const listActive = async () => {
36
38
  if (!useLoro) return listAll();
@@ -40,35 +42,46 @@ function manageTable(db, useLoro, { name, ...args }) {
40
42
  await db.create(new import_surrealdb.Table(name)).content(data);
41
43
  };
42
44
  const update = async (id, data) => {
43
- if (useLoro) {
45
+ if (!useLoro) {
46
+ await db.update(id).merge({
47
+ ...data
48
+ });
49
+ return;
50
+ }
51
+ try {
44
52
  await db.update(id).merge({
45
53
  ...data,
46
54
  sync_deleted: false,
47
55
  updated_at: Date.now()
48
56
  });
49
- } else {
50
- await db.update(id).merge({
51
- ...data
52
- });
57
+ } catch (error) {
58
+ console.warn(
59
+ `Please ensure the table ${name} has sync_deleted and updated_at fields defined`
60
+ );
61
+ console.error(
62
+ "Failed to update record with Loro (CRDTs) with: ",
63
+ error
64
+ );
53
65
  }
54
66
  };
55
67
  const remove = async (id) => {
56
68
  await db.delete(id);
57
69
  };
58
70
  const softDelete = async (id) => {
59
- if (useLoro) {
60
- await db.update(id).merge({
61
- sync_deleted: true,
62
- updated_at: Date.now()
63
- });
64
- } else {
71
+ if (!useLoro) {
65
72
  await db.delete(id);
73
+ return;
66
74
  }
75
+ await db.upsert(id).merge({
76
+ sync_deleted: true,
77
+ updated_at: Date.now()
78
+ });
67
79
  };
68
80
  const subscribe = (cb) => {
69
81
  let killed = false;
70
82
  let live;
71
83
  const on = ({ action, value }) => {
84
+ console.debug("[Surreal Live]", name, action, value);
72
85
  if (action === "KILLED") return;
73
86
  if (action === "CREATE") cb({ type: "insert", row: value });
74
87
  else if (action === "UPDATE")
@@ -276,6 +289,11 @@ function surrealCollectionOptions({
276
289
  commit,
277
290
  markReady
278
291
  }) => {
292
+ if (!db.isFeatureSupported(import_surrealdb2.Features.LiveQueries)) {
293
+ markReady();
294
+ return () => {
295
+ };
296
+ }
279
297
  let offLive = null;
280
298
  const makeTombstone = (id2) => ({
281
299
  id: new import_surrealdb2.RecordId(config.table.name, id2).toString(),
@@ -286,11 +304,8 @@ function surrealCollectionOptions({
286
304
  try {
287
305
  const serverRows = await table.listAll();
288
306
  begin();
289
- if (useLoro) {
290
- reconcileBoot(serverRows, write);
291
- } else {
292
- diffAndEmit(serverRows, write);
293
- }
307
+ if (useLoro) reconcileBoot(serverRows, write);
308
+ else diffAndEmit(serverRows, write);
294
309
  commit();
295
310
  markReady();
296
311
  await flushPushQueue();
@@ -373,9 +388,7 @@ function surrealCollectionOptions({
373
388
  for (const m of p.transaction.mutations) {
374
389
  if (m.type !== "delete") continue;
375
390
  const id2 = m.key;
376
- if (useLoro) {
377
- loroRemove(keyOf(id2));
378
- }
391
+ if (useLoro) loroRemove(keyOf(id2));
379
392
  await table.softDelete(new import_surrealdb2.RecordId(config.table.name, keyOf(id2)));
380
393
  }
381
394
  return resultRows;
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/index.ts
2
2
  import { LoroDoc } from "loro-crdt";
3
- import { RecordId } from "surrealdb";
3
+ import { Features as Features2, RecordId } from "surrealdb";
4
4
 
5
5
  // src/table.ts
6
6
  import {
@@ -12,7 +12,9 @@ import {
12
12
  function manageTable(db, useLoro, { name, ...args }) {
13
13
  const fields = args.fields ?? "*";
14
14
  const listAll = async () => {
15
- return await db.select(new Table(name)).where(args.where).fields(...fields);
15
+ let q = db.select(new Table(name));
16
+ if (args.where) q = q.where(args.where);
17
+ return await q.fields(...fields);
16
18
  };
17
19
  const listActive = async () => {
18
20
  if (!useLoro) return listAll();
@@ -22,35 +24,46 @@ function manageTable(db, useLoro, { name, ...args }) {
22
24
  await db.create(new Table(name)).content(data);
23
25
  };
24
26
  const update = async (id, data) => {
25
- if (useLoro) {
27
+ if (!useLoro) {
28
+ await db.update(id).merge({
29
+ ...data
30
+ });
31
+ return;
32
+ }
33
+ try {
26
34
  await db.update(id).merge({
27
35
  ...data,
28
36
  sync_deleted: false,
29
37
  updated_at: Date.now()
30
38
  });
31
- } else {
32
- await db.update(id).merge({
33
- ...data
34
- });
39
+ } catch (error) {
40
+ console.warn(
41
+ `Please ensure the table ${name} has sync_deleted and updated_at fields defined`
42
+ );
43
+ console.error(
44
+ "Failed to update record with Loro (CRDTs) with: ",
45
+ error
46
+ );
35
47
  }
36
48
  };
37
49
  const remove = async (id) => {
38
50
  await db.delete(id);
39
51
  };
40
52
  const softDelete = async (id) => {
41
- if (useLoro) {
42
- await db.update(id).merge({
43
- sync_deleted: true,
44
- updated_at: Date.now()
45
- });
46
- } else {
53
+ if (!useLoro) {
47
54
  await db.delete(id);
55
+ return;
48
56
  }
57
+ await db.upsert(id).merge({
58
+ sync_deleted: true,
59
+ updated_at: Date.now()
60
+ });
49
61
  };
50
62
  const subscribe = (cb) => {
51
63
  let killed = false;
52
64
  let live;
53
65
  const on = ({ action, value }) => {
66
+ console.debug("[Surreal Live]", name, action, value);
54
67
  if (action === "KILLED") return;
55
68
  if (action === "CREATE") cb({ type: "insert", row: value });
56
69
  else if (action === "UPDATE")
@@ -258,6 +271,11 @@ function surrealCollectionOptions({
258
271
  commit,
259
272
  markReady
260
273
  }) => {
274
+ if (!db.isFeatureSupported(Features2.LiveQueries)) {
275
+ markReady();
276
+ return () => {
277
+ };
278
+ }
261
279
  let offLive = null;
262
280
  const makeTombstone = (id2) => ({
263
281
  id: new RecordId(config.table.name, id2).toString(),
@@ -268,11 +286,8 @@ function surrealCollectionOptions({
268
286
  try {
269
287
  const serverRows = await table.listAll();
270
288
  begin();
271
- if (useLoro) {
272
- reconcileBoot(serverRows, write);
273
- } else {
274
- diffAndEmit(serverRows, write);
275
- }
289
+ if (useLoro) reconcileBoot(serverRows, write);
290
+ else diffAndEmit(serverRows, write);
276
291
  commit();
277
292
  markReady();
278
293
  await flushPushQueue();
@@ -355,9 +370,7 @@ function surrealCollectionOptions({
355
370
  for (const m of p.transaction.mutations) {
356
371
  if (m.type !== "delete") continue;
357
372
  const id2 = m.key;
358
- if (useLoro) {
359
- loroRemove(keyOf(id2));
360
- }
373
+ if (useLoro) loroRemove(keyOf(id2));
361
374
  await table.softDelete(new RecordId(config.table.name, keyOf(id2)));
362
375
  }
363
376
  return resultRows;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@foretag/tanstack-db-surrealdb",
3
3
  "description": "Add Offline / Local First Caching & Syncing to your SurrealDB app with TanstackDB and Loro (CRDTs)",
4
- "version": "0.1.15",
4
+ "version": "0.1.17",
5
5
  "files": [
6
6
  "dist"
7
7
  ],
@@ -40,17 +40,17 @@
40
40
  "build": "tsup src/index.ts --dts --format esm,cjs"
41
41
  },
42
42
  "dependencies": {
43
- "loro-crdt": "^1.9.0"
43
+ "loro-crdt": "^1.10.2"
44
44
  },
45
45
  "peerDependencies": {
46
- "@tanstack/db": "^0.5.0",
46
+ "@tanstack/db": "^0.5.10",
47
47
  "@tanstack/query-db-collection": "^1.0.0",
48
- "surrealdb": "2.0.0-alpha.13"
48
+ "surrealdb": "2.0.0-alpha.14"
49
49
  },
50
50
  "devDependencies": {
51
- "@biomejs/biome": "^2.3.5",
52
- "@tanstack/svelte-db": "^0.1.43",
53
- "surrealdb": "2.0.0-alpha.13",
51
+ "@biomejs/biome": "^2.3.8",
52
+ "@tanstack/svelte-db": "^0.1.53",
53
+ "surrealdb": "2.0.0-alpha.14",
54
54
  "tsup": "^8.5.1"
55
55
  }
56
56
  }