@foretag/tanstack-db-surrealdb 0.1.15 → 0.1.16

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")
package/dist/index.mjs CHANGED
@@ -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")
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.16",
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
  }