@foretag/tanstack-db-surrealdb 0.1.13 → 0.1.14

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
@@ -35,8 +35,11 @@ function manageTable(db, { name, ...args }) {
35
35
  const listActive = async () => {
36
36
  return await db.select(new import_surrealdb.Table(name)).where((0, import_surrealdb.and)(args.where, (0, import_surrealdb.eq)("sync_deleted", false))).fields(...fields);
37
37
  };
38
- const upsert = async (id, data) => {
39
- await db.upsert(id).merge({
38
+ const create = async (data) => {
39
+ await db.create(new import_surrealdb.Table(name)).content(data);
40
+ };
41
+ const update = async (id, data) => {
42
+ await db.update(id).merge({
40
43
  ...data,
41
44
  sync_deleted: false,
42
45
  updated_at: Date.now()
@@ -79,7 +82,8 @@ function manageTable(db, { name, ...args }) {
79
82
  return {
80
83
  listAll,
81
84
  listActive,
82
- upsert,
85
+ create,
86
+ update,
83
87
  remove,
84
88
  softDelete,
85
89
  subscribe
@@ -115,23 +119,25 @@ function surrealCollectionOptions({
115
119
  loroMap.delete(id2);
116
120
  loro?.doc?.commit?.();
117
121
  };
122
+ const pushQueue = [];
123
+ const enqueuePush = (op) => pushQueue.push(op);
118
124
  const flushPushQueue = async () => {
119
125
  const ops = pushQueue.splice(0, pushQueue.length);
120
126
  for (const op of ops) {
121
- if (op.kind === "upsert") {
127
+ if (op.kind === "create") await table.create(op.row);
128
+ if (op.kind === "update") {
122
129
  const rid = new import_surrealdb2.RecordId(
123
130
  config.table.name,
124
131
  op.row.id.toString()
125
132
  );
126
- await table.upsert(rid, op.row);
127
- } else {
133
+ await table.update(rid, op.row);
134
+ }
135
+ if (op.kind === "delete") {
128
136
  const rid = new import_surrealdb2.RecordId(config.table.name, op.id.toString());
129
137
  await table.softDelete(rid);
130
138
  }
131
139
  }
132
140
  };
133
- const pushQueue = [];
134
- const enqueuePush = (op) => pushQueue.push(op);
135
141
  const newer = (a, b) => (a?.getTime() ?? -1) > (b?.getTime() ?? -1);
136
142
  const reconcileBoot = (serverRows, write) => {
137
143
  const localRows = useLoro ? loroToArray() : [];
@@ -169,7 +175,7 @@ function surrealCollectionOptions({
169
175
  }
170
176
  } else {
171
177
  if (newer(l.updated_at, s.updated_at)) {
172
- enqueuePush({ kind: "upsert", row: l });
178
+ enqueuePush({ kind: "update", row: l });
173
179
  applyLocal(l);
174
180
  current.push(l);
175
181
  } else {
@@ -190,7 +196,7 @@ function surrealCollectionOptions({
190
196
  applyLocal(l);
191
197
  current.push(l);
192
198
  } else {
193
- enqueuePush({ kind: "upsert", row: l });
199
+ enqueuePush({ kind: "create", row: l });
194
200
  applyLocal(l);
195
201
  current.push(l);
196
202
  }
@@ -296,11 +302,10 @@ function surrealCollectionOptions({
296
302
  const row = {
297
303
  ...m.modified,
298
304
  updated_at: now(),
299
- deleted: false
305
+ sync_deleted: false
300
306
  };
301
307
  if (useLoro) loroPut(row);
302
- const rid = new import_surrealdb2.RecordId(config.table.name, getKey(row));
303
- await table.upsert(rid, row);
308
+ await table.create(row);
304
309
  resultRows.push(row);
305
310
  }
306
311
  return resultRows;
@@ -313,7 +318,7 @@ function surrealCollectionOptions({
313
318
  const merged = { ...m.modified, id: id2, updated_at: now() };
314
319
  if (useLoro) loroPut(merged);
315
320
  const rid = new import_surrealdb2.RecordId(config.table.name, keyOf(id2));
316
- await table.upsert(rid, merged);
321
+ await table.update(rid, merged);
317
322
  resultRows.push(merged);
318
323
  }
319
324
  return resultRows;
package/dist/index.mjs CHANGED
@@ -17,8 +17,11 @@ function manageTable(db, { name, ...args }) {
17
17
  const listActive = async () => {
18
18
  return await db.select(new Table(name)).where(and(args.where, eq("sync_deleted", false))).fields(...fields);
19
19
  };
20
- const upsert = async (id, data) => {
21
- await db.upsert(id).merge({
20
+ const create = async (data) => {
21
+ await db.create(new Table(name)).content(data);
22
+ };
23
+ const update = async (id, data) => {
24
+ await db.update(id).merge({
22
25
  ...data,
23
26
  sync_deleted: false,
24
27
  updated_at: Date.now()
@@ -61,7 +64,8 @@ function manageTable(db, { name, ...args }) {
61
64
  return {
62
65
  listAll,
63
66
  listActive,
64
- upsert,
67
+ create,
68
+ update,
65
69
  remove,
66
70
  softDelete,
67
71
  subscribe
@@ -97,23 +101,25 @@ function surrealCollectionOptions({
97
101
  loroMap.delete(id2);
98
102
  loro?.doc?.commit?.();
99
103
  };
104
+ const pushQueue = [];
105
+ const enqueuePush = (op) => pushQueue.push(op);
100
106
  const flushPushQueue = async () => {
101
107
  const ops = pushQueue.splice(0, pushQueue.length);
102
108
  for (const op of ops) {
103
- if (op.kind === "upsert") {
109
+ if (op.kind === "create") await table.create(op.row);
110
+ if (op.kind === "update") {
104
111
  const rid = new RecordId(
105
112
  config.table.name,
106
113
  op.row.id.toString()
107
114
  );
108
- await table.upsert(rid, op.row);
109
- } else {
115
+ await table.update(rid, op.row);
116
+ }
117
+ if (op.kind === "delete") {
110
118
  const rid = new RecordId(config.table.name, op.id.toString());
111
119
  await table.softDelete(rid);
112
120
  }
113
121
  }
114
122
  };
115
- const pushQueue = [];
116
- const enqueuePush = (op) => pushQueue.push(op);
117
123
  const newer = (a, b) => (a?.getTime() ?? -1) > (b?.getTime() ?? -1);
118
124
  const reconcileBoot = (serverRows, write) => {
119
125
  const localRows = useLoro ? loroToArray() : [];
@@ -151,7 +157,7 @@ function surrealCollectionOptions({
151
157
  }
152
158
  } else {
153
159
  if (newer(l.updated_at, s.updated_at)) {
154
- enqueuePush({ kind: "upsert", row: l });
160
+ enqueuePush({ kind: "update", row: l });
155
161
  applyLocal(l);
156
162
  current.push(l);
157
163
  } else {
@@ -172,7 +178,7 @@ function surrealCollectionOptions({
172
178
  applyLocal(l);
173
179
  current.push(l);
174
180
  } else {
175
- enqueuePush({ kind: "upsert", row: l });
181
+ enqueuePush({ kind: "create", row: l });
176
182
  applyLocal(l);
177
183
  current.push(l);
178
184
  }
@@ -278,11 +284,10 @@ function surrealCollectionOptions({
278
284
  const row = {
279
285
  ...m.modified,
280
286
  updated_at: now(),
281
- deleted: false
287
+ sync_deleted: false
282
288
  };
283
289
  if (useLoro) loroPut(row);
284
- const rid = new RecordId(config.table.name, getKey(row));
285
- await table.upsert(rid, row);
290
+ await table.create(row);
286
291
  resultRows.push(row);
287
292
  }
288
293
  return resultRows;
@@ -295,7 +300,7 @@ function surrealCollectionOptions({
295
300
  const merged = { ...m.modified, id: id2, updated_at: now() };
296
301
  if (useLoro) loroPut(merged);
297
302
  const rid = new RecordId(config.table.name, keyOf(id2));
298
- await table.upsert(rid, merged);
303
+ await table.update(rid, merged);
299
304
  resultRows.push(merged);
300
305
  }
301
306
  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.13",
4
+ "version": "0.1.14",
5
5
  "files": [
6
6
  "dist"
7
7
  ],