tsondb 0.19.4 → 0.19.6

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.
@@ -21,7 +21,7 @@ import { checkCustomConstraintsForAllEntities } from "./utils/customConstraints.
21
21
  import { DatabaseInMemory } from "./utils/databaseInMemory.js";
22
22
  import { applyStepsToDisk } from "./utils/databaseOnDisk.js";
23
23
  import { getAllInstanceOverviewsByEntityName, getDisplayNameFromEntityInstance, getInstanceOverview, getInstanceOverviewsByEntityName, } from "./utils/displayName.js";
24
- import { countError, countErrors, getErrorMessageForDisplay, wrapErrorsIfAny, } from "./utils/error.js";
24
+ import { countError, countErrors, getErrorMessageForDisplay, HTTPError, wrapErrorsIfAny, } from "./utils/error.js";
25
25
  import { getFileNameForId, writeInstance } from "./utils/files.js";
26
26
  import { attachGitStatusToDatabaseInMemory } from "./utils/git.js";
27
27
  import { getReferencesToInstances, isReferencedByOtherInstances, } from "./utils/references.js";
@@ -94,6 +94,7 @@ export class TSONDB {
94
94
  #referencesToInstances;
95
95
  #validationOptions;
96
96
  #gitWrapper;
97
+ #locked = false;
97
98
  constructor(options) {
98
99
  this.#dataRootPath = options.dataRootPath;
99
100
  this.#data = options.data;
@@ -287,35 +288,46 @@ export class TSONDB {
287
288
  * @throws {Error} when the transaction could not be completed
288
289
  */
289
290
  async runTransaction(fn) {
290
- const getEntity = this.#schema.getEntity.bind(this.#schema);
291
- const [txn, res] = fn(new Transaction({
292
- data: this.#data,
293
- getEntity,
294
- referencesToInstances: this.#referencesToInstances,
295
- validate: this.#strucurallyValidateInstance.bind(this),
296
- localeEntity: this.#schema.localeEntity,
297
- steps: [],
298
- }));
299
- const txtResult = txn.getResult();
300
- const { data: newData, referencesToInstances: newRefs, steps } = txtResult;
301
- const errors = this.#validate(newData, true);
302
- if (errors.length > 0) {
303
- throw new AggregateError(errors, "Validation errors occurred");
304
- }
305
- const diskResult = await applyStepsToDisk(this.#dataRootPath, steps);
306
- if (isError(diskResult)) {
307
- throw diskResult.error;
291
+ if (this.#locked) {
292
+ throw new HTTPError(503, "Another transaction is currently running. Transactions cannot be run concurrently.");
308
293
  }
309
- if (this.#git) {
310
- const status = await this.#git.client.status();
311
- const newDbWithUpdatedGit = attachGitStatusToDatabaseInMemory(newData, this.#dataRootPath, this.#git.root, status);
312
- this.#data = newDbWithUpdatedGit;
294
+ this.#locked = true;
295
+ try {
296
+ const getEntity = this.#schema.getEntity.bind(this.#schema);
297
+ const [txn, res] = fn(new Transaction({
298
+ data: this.#data,
299
+ getEntity,
300
+ referencesToInstances: this.#referencesToInstances,
301
+ validate: this.#strucurallyValidateInstance.bind(this),
302
+ localeEntity: this.#schema.localeEntity,
303
+ steps: [],
304
+ }));
305
+ const txtResult = txn.getResult();
306
+ const { data: newData, referencesToInstances: newRefs, steps } = txtResult;
307
+ const errors = this.#validate(newData, true);
308
+ if (errors.length > 0) {
309
+ throw new AggregateError(errors, "Validation errors occurred");
310
+ }
311
+ const diskResult = await applyStepsToDisk(this.#dataRootPath, steps);
312
+ if (isError(diskResult)) {
313
+ throw diskResult.error;
314
+ }
315
+ if (this.#git) {
316
+ const status = await this.#git.client.status();
317
+ const newDbWithUpdatedGit = attachGitStatusToDatabaseInMemory(newData, this.#dataRootPath, this.#git.root, status);
318
+ this.#data = newDbWithUpdatedGit;
319
+ }
320
+ else {
321
+ this.#data = newData;
322
+ }
323
+ this.#referencesToInstances = newRefs;
324
+ this.#locked = false;
325
+ return res;
313
326
  }
314
- else {
315
- this.#data = newData;
327
+ catch (error) {
328
+ this.#locked = false;
329
+ throw error;
316
330
  }
317
- this.#referencesToInstances = newRefs;
318
- return res;
319
331
  }
320
332
  /**
321
333
  * Creates a new instance in the database.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsondb",
3
- "version": "0.19.4",
3
+ "version": "0.19.6",
4
4
  "description": "",
5
5
  "license": "ISC",
6
6
  "author": "Lukas Obermann",