@verdant-web/store 4.1.1 → 4.1.2
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.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createOid, decomposeOid, getIndexValues, } from '@verdant-web/common';
|
|
2
2
|
import { IdbService } from '../IdbService.js';
|
|
3
|
-
import { getRange } from './ranges.js';
|
|
4
3
|
import { closeDatabase, getSizeOfObjectStore, isAbortError } from '../util.js';
|
|
4
|
+
import { getRange } from './ranges.js';
|
|
5
5
|
export class IdbDocumentDb extends IdbService {
|
|
6
6
|
constructor(db, context) {
|
|
7
7
|
super(db, { log: context.log });
|
|
@@ -101,7 +101,7 @@ export class IdbDocumentDb extends IdbService {
|
|
|
101
101
|
abort: optsAndInfo.abort,
|
|
102
102
|
}),
|
|
103
103
|
};
|
|
104
|
-
await Promise.
|
|
104
|
+
const results = await Promise.allSettled(entities.map(async (e) => {
|
|
105
105
|
const snapshot = e.getSnapshot();
|
|
106
106
|
try {
|
|
107
107
|
await this.saveDocument(e.oid, snapshot, options);
|
|
@@ -116,6 +116,17 @@ export class IdbDocumentDb extends IdbService {
|
|
|
116
116
|
}
|
|
117
117
|
}
|
|
118
118
|
}));
|
|
119
|
+
const failures = results.filter((r) => r.status === 'rejected');
|
|
120
|
+
if (failures.length) {
|
|
121
|
+
// in the case of a failure to save a document, it doesn't quite make sense to cancel or rollback whatever is
|
|
122
|
+
// currently happening. when restoring imports, etc, this makes the app stuck. if only a few docs failed, maybe
|
|
123
|
+
// there's some data corruption somewhere, but we can just lose those without affecting the rest of the data.
|
|
124
|
+
if (failures.length === results.length) {
|
|
125
|
+
// but if ALL of them failed, that's trouble...
|
|
126
|
+
throw new Error('Failed to save any documents. Something must be quite wrong.');
|
|
127
|
+
}
|
|
128
|
+
this.ctx.log('error', 'Failed to save documents:', failures, ". See logs above. This only affects querying these documents. Let's hope a future attempt will correct them...");
|
|
129
|
+
}
|
|
119
130
|
options.transaction.commit();
|
|
120
131
|
};
|
|
121
132
|
this.reset = async () => {
|
|
@@ -128,23 +139,29 @@ export class IdbDocumentDb extends IdbService {
|
|
|
128
139
|
this.saveDocument = async (oid, doc, { transaction }) => {
|
|
129
140
|
this.ctx.log('debug', `Saving document indexes for querying ${oid}`);
|
|
130
141
|
const { collection, id } = decomposeOid(oid);
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
142
|
+
try {
|
|
143
|
+
if (!doc) {
|
|
144
|
+
await this.run(collection, (store) => store.delete(id), {
|
|
145
|
+
mode: 'readwrite',
|
|
146
|
+
transaction,
|
|
147
|
+
});
|
|
148
|
+
this.ctx.log('debug', `Deleted document indexes for querying ${oid}`);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
const schema = this.ctx.schema.collections[collection];
|
|
152
|
+
// no need to validate before storing; the entity's snapshot is already validated.
|
|
153
|
+
const indexes = getIndexValues(schema, doc);
|
|
154
|
+
indexes['@@@snapshot'] = JSON.stringify(doc);
|
|
155
|
+
await this.run(collection, (store) => store.put(indexes), {
|
|
156
|
+
mode: 'readwrite',
|
|
157
|
+
transaction,
|
|
158
|
+
});
|
|
159
|
+
this.ctx.log('debug', `Save complete for ${oid}`, indexes);
|
|
160
|
+
}
|
|
137
161
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
const indexes = getIndexValues(schema, doc);
|
|
142
|
-
indexes['@@@snapshot'] = JSON.stringify(doc);
|
|
143
|
-
await this.run(collection, (store) => store.put(indexes), {
|
|
144
|
-
mode: 'readwrite',
|
|
145
|
-
transaction,
|
|
146
|
-
});
|
|
147
|
-
this.ctx.log('debug', `Save complete for ${oid}`, indexes);
|
|
162
|
+
catch (err) {
|
|
163
|
+
this.ctx.log('error', `Error saving document ${oid}`, err);
|
|
164
|
+
throw err;
|
|
148
165
|
}
|
|
149
166
|
};
|
|
150
167
|
this.ctx = context;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IdbDocumentDb.js","sourceRoot":"","sources":["../../../../../src/persistence/idb/queries/IdbDocumentDb.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,SAAS,EACT,YAAY,EACZ,cAAc,GAEd,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"IdbDocumentDb.js","sourceRoot":"","sources":["../../../../../src/persistence/idb/queries/IdbDocumentDb.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,SAAS,EACT,YAAY,EACZ,cAAc,GAEd,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC/E,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,MAAM,OAAO,aAAc,SAAQ,UAAU;IAE5C,YAAY,EAAe,EAAE,OAA6C;QACzE,KAAK,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;QAQjC,UAAK,GAAG,KAAK,IAAI,EAAE;YAClB,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;QACtB,CAAC,CAAC;QAEF,UAAK,GAAG,KAAK,IAEX,EAAE;YACH,MAAM,eAAe,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;YACjE,MAAM,WAAW,GAAoD,EAAE,CAAC;YACxE,MAAM,OAAO,CAAC,GAAG,CAChB,eAAe,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;gBAClC,MAAM,IAAI,GAAG,MAAM,oBAAoB,CAAC,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;gBACvD,WAA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@@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@verdant-web/store",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.2",
|
|
4
4
|
"access": "public",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/esm/index.js",
|
|
@@ -30,11 +30,9 @@
|
|
|
30
30
|
"src/"
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@a-type/kysely": "^1.0.1",
|
|
34
33
|
"cuid": "^2.1.8",
|
|
35
34
|
"jszip": "^3.10.1",
|
|
36
35
|
"jwt-decode": "^3.1.2",
|
|
37
|
-
"kysely": "^0.27.3",
|
|
38
36
|
"weak-event": "^2.0.5",
|
|
39
37
|
"@verdant-web/common": "2.7.0"
|
|
40
38
|
},
|
|
@@ -8,8 +8,8 @@ import {
|
|
|
8
8
|
import { Context } from '../../../context/context.js';
|
|
9
9
|
import { PersistenceDocumentDb } from '../../interfaces.js';
|
|
10
10
|
import { IdbService } from '../IdbService.js';
|
|
11
|
-
import { getRange } from './ranges.js';
|
|
12
11
|
import { closeDatabase, getSizeOfObjectStore, isAbortError } from '../util.js';
|
|
12
|
+
import { getRange } from './ranges.js';
|
|
13
13
|
|
|
14
14
|
export class IdbDocumentDb extends IdbService implements PersistenceDocumentDb {
|
|
15
15
|
private ctx;
|
|
@@ -149,7 +149,7 @@ export class IdbDocumentDb extends IdbService implements PersistenceDocumentDb {
|
|
|
149
149
|
}),
|
|
150
150
|
};
|
|
151
151
|
|
|
152
|
-
await Promise.
|
|
152
|
+
const results = await Promise.allSettled(
|
|
153
153
|
entities.map(async (e) => {
|
|
154
154
|
const snapshot = e.getSnapshot();
|
|
155
155
|
try {
|
|
@@ -168,6 +168,26 @@ export class IdbDocumentDb extends IdbService implements PersistenceDocumentDb {
|
|
|
168
168
|
}
|
|
169
169
|
}),
|
|
170
170
|
);
|
|
171
|
+
|
|
172
|
+
const failures = results.filter((r) => r.status === 'rejected');
|
|
173
|
+
if (failures.length) {
|
|
174
|
+
// in the case of a failure to save a document, it doesn't quite make sense to cancel or rollback whatever is
|
|
175
|
+
// currently happening. when restoring imports, etc, this makes the app stuck. if only a few docs failed, maybe
|
|
176
|
+
// there's some data corruption somewhere, but we can just lose those without affecting the rest of the data.
|
|
177
|
+
if (failures.length === results.length) {
|
|
178
|
+
// but if ALL of them failed, that's trouble...
|
|
179
|
+
throw new Error(
|
|
180
|
+
'Failed to save any documents. Something must be quite wrong.',
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
this.ctx.log(
|
|
184
|
+
'error',
|
|
185
|
+
'Failed to save documents:',
|
|
186
|
+
failures,
|
|
187
|
+
". See logs above. This only affects querying these documents. Let's hope a future attempt will correct them...",
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
171
191
|
options.transaction.commit();
|
|
172
192
|
};
|
|
173
193
|
|
|
@@ -190,22 +210,27 @@ export class IdbDocumentDb extends IdbService implements PersistenceDocumentDb {
|
|
|
190
210
|
) => {
|
|
191
211
|
this.ctx.log('debug', `Saving document indexes for querying ${oid}`);
|
|
192
212
|
const { collection, id } = decomposeOid(oid);
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
213
|
+
try {
|
|
214
|
+
if (!doc) {
|
|
215
|
+
await this.run(collection, (store) => store.delete(id), {
|
|
216
|
+
mode: 'readwrite',
|
|
217
|
+
transaction,
|
|
218
|
+
});
|
|
219
|
+
this.ctx.log('debug', `Deleted document indexes for querying ${oid}`);
|
|
220
|
+
} else {
|
|
221
|
+
const schema = this.ctx.schema.collections[collection];
|
|
222
|
+
// no need to validate before storing; the entity's snapshot is already validated.
|
|
223
|
+
const indexes = getIndexValues(schema, doc);
|
|
224
|
+
indexes['@@@snapshot'] = JSON.stringify(doc);
|
|
225
|
+
await this.run(collection, (store) => store.put(indexes), {
|
|
226
|
+
mode: 'readwrite',
|
|
227
|
+
transaction,
|
|
228
|
+
});
|
|
229
|
+
this.ctx.log('debug', `Save complete for ${oid}`, indexes);
|
|
230
|
+
}
|
|
231
|
+
} catch (err) {
|
|
232
|
+
this.ctx.log('error', `Error saving document ${oid}`, err);
|
|
233
|
+
throw err;
|
|
209
234
|
}
|
|
210
235
|
};
|
|
211
236
|
}
|