@saltcorn/data 1.4.0-beta.4 → 1.4.0-beta.5

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.
@@ -750,22 +750,28 @@ class Table {
750
750
  }
751
751
  }
752
752
  async addDeleteSyncInfo(ids, timestamp) {
753
- if (ids.length > 0) {
754
- const schema = db_1.default.getTenantSchemaPrefix();
755
- const pkName = this.pk_name || "id";
756
- if (isNode()) {
757
- await db_1.default.query(`delete from ${schema}"${db_1.default.sqlsanitize(this.name)}_sync_info" where ref in (
753
+ await db_1.default.tryCatchInTransaction(async () => {
754
+ if (ids.length > 0) {
755
+ const schema = db_1.default.getTenantSchemaPrefix();
756
+ const pkName = this.pk_name || "id";
757
+ if (isNode()) {
758
+ await db_1.default.query(`delete from ${schema}"${db_1.default.sqlsanitize(this.name)}_sync_info" where ref in (
758
759
  ${ids.map((row) => row[pkName]).join(",")})`);
759
- await db_1.default.query(`insert into ${schema}"${db_1.default.sqlsanitize(this.name)}_sync_info" values ${ids
760
- .map((row) => `(${row[pkName]}, date_trunc('milliseconds', to_timestamp( ${timestamp.valueOf() / 1000.0} ) ), true)`)
761
- .join(",")}`);
762
- }
763
- else {
764
- await db_1.default.query(`update "${db_1.default.sqlsanitize(this.name)}_sync_info"
760
+ await db_1.default.query(`insert into ${schema}"${db_1.default.sqlsanitize(this.name)}_sync_info" values ${ids
761
+ .map((row) => `(${row[pkName]}, date_trunc('milliseconds', to_timestamp( ${timestamp.valueOf() / 1000.0} ) ), true)`)
762
+ .join(",")}`);
763
+ }
764
+ else {
765
+ await db_1.default.query(`update "${db_1.default.sqlsanitize(this.name)}_sync_info"
765
766
  set deleted = true, modified_local = true
766
767
  where ref in (${ids.map((row) => row[pkName]).join(",")})`);
768
+ }
767
769
  }
768
- }
770
+ }, (e) => {
771
+ require("../db/state")
772
+ .getState()
773
+ .log(2, `Error in addDeleteSyncInfo: ${e.message}`);
774
+ });
769
775
  }
770
776
  /**
771
777
  * Delete rows from table. The first argument is a where expression indicating the conditions for the rows to be deleted
@@ -1382,40 +1388,61 @@ class Table {
1382
1388
  return rows?.length === 1 ? rows[0] : null;
1383
1389
  }
1384
1390
  async latestSyncInfos(ids) {
1385
- const schema = db_1.default.getTenantSchemaPrefix();
1386
- const dbResult = await db_1.default.query(`select max(last_modified) "last_modified", ref
1387
- from ${schema}"${db_1.default.sqlsanitize(this.name)}_sync_info"
1388
- group by ref having ref = ${db_1.default.isSQLite ? "" : "ANY"} ($1)`, db_1.default.isSQLite ? ids : [ids]);
1389
- return dbResult.rows;
1391
+ return await db_1.default.tryCatchInTransaction(async () => {
1392
+ const schema = db_1.default.getTenantSchemaPrefix();
1393
+ const dbResult = await db_1.default.query(`select max(last_modified) "last_modified", ref
1394
+ from ${schema}"${db_1.default.sqlsanitize(this.name)}_sync_info"
1395
+ group by ref having ref = ${db_1.default.isSQLite ? "" : "ANY"} ($1)`, db_1.default.isSQLite ? ids : [ids]);
1396
+ return dbResult.rows;
1397
+ }, (e) => {
1398
+ require("../db/state")
1399
+ .getState()
1400
+ .log(2, `Error in latestSyncInfos: ${e.message}`);
1401
+ return null;
1402
+ });
1390
1403
  }
1391
1404
  async insertSyncInfo(id, syncTimestamp) {
1392
- const schema = db_1.default.getTenantSchemaPrefix();
1393
- if (isNode()) {
1394
- await db_1.default.query(`insert into ${schema}"${db_1.default.sqlsanitize(this.name)}_sync_info" values($1,
1405
+ await db_1.default.tryCatchInTransaction(async () => {
1406
+ const schema = db_1.default.getTenantSchemaPrefix();
1407
+ if (isNode()) {
1408
+ await db_1.default.query(`insert into ${schema}"${db_1.default.sqlsanitize(this.name)}_sync_info" values($1,
1395
1409
  date_trunc('milliseconds', to_timestamp($2)))`, [
1396
- id,
1397
- (syncTimestamp ? syncTimestamp : await db_1.default.time()).valueOf() / 1000.0,
1398
- ]);
1399
- }
1400
- else {
1401
- await db_1.default.query(`insert into "${db_1.default.sqlsanitize(this.name)}_sync_info"
1410
+ id,
1411
+ (syncTimestamp ? syncTimestamp : await db_1.default.time()).valueOf() /
1412
+ 1000.0,
1413
+ ]);
1414
+ }
1415
+ else {
1416
+ await db_1.default.query(`insert into "${db_1.default.sqlsanitize(this.name)}_sync_info"
1402
1417
  (ref, modified_local, deleted)
1403
1418
  values('${id}', true, false)`);
1404
- }
1419
+ }
1420
+ }, (e) => {
1421
+ require("../db/state")
1422
+ .getState()
1423
+ .log(2, `Error in insertSyncInfo: ${e.message}`);
1424
+ });
1405
1425
  }
1406
1426
  async updateSyncInfo(id, oldLastModified, syncTimestamp) {
1407
- const schema = db_1.default.getTenantSchemaPrefix();
1408
- if (!db_1.default.isSQLite) {
1409
- await db_1.default.query(`update ${schema}"${db_1.default.sqlsanitize(this.name)}_sync_info" set last_modified=date_trunc('milliseconds', to_timestamp($1)) where ref=$2 and last_modified = to_timestamp($3)`, [
1410
- (syncTimestamp ? syncTimestamp : await db_1.default.time()).valueOf() / 1000.0,
1411
- id,
1412
- oldLastModified.valueOf() / 1000.0,
1413
- ]);
1414
- }
1415
- else {
1416
- await db_1.default.query(`update "${db_1.default.sqlsanitize(this.name)}_sync_info" set modified_local = true
1427
+ await db_1.default.tryCatchInTransaction(async () => {
1428
+ const schema = db_1.default.getTenantSchemaPrefix();
1429
+ if (!db_1.default.isSQLite) {
1430
+ await db_1.default.query(`update ${schema}"${db_1.default.sqlsanitize(this.name)}_sync_info" set last_modified=date_trunc('milliseconds', to_timestamp($1)) where ref=$2 and last_modified = to_timestamp($3)`, [
1431
+ (syncTimestamp ? syncTimestamp : await db_1.default.time()).valueOf() /
1432
+ 1000.0,
1433
+ id,
1434
+ oldLastModified.valueOf() / 1000.0,
1435
+ ]);
1436
+ }
1437
+ else {
1438
+ await db_1.default.query(`update "${db_1.default.sqlsanitize(this.name)}_sync_info" set modified_local = true
1417
1439
  where ref = ${id} and last_modified = ${oldLastModified ? oldLastModified.valueOf() : "null"}`);
1418
- }
1440
+ }
1441
+ }, (e) => {
1442
+ require("../db/state")
1443
+ .getState()
1444
+ .log(2, `Error in updateSyncInfo: ${e.message}`);
1445
+ });
1419
1446
  }
1420
1447
  /**
1421
1448
  * Try to Update row
@@ -1654,17 +1681,21 @@ class Table {
1654
1681
  _time: new Date(),
1655
1682
  });
1656
1683
  if (this.has_sync_info) {
1657
- if (isNode()) {
1658
- const schemaPrefix = db_1.default.getTenantSchemaPrefix();
1659
- await db_1.default.query(`insert into ${schemaPrefix}"${db_1.default.sqlsanitize(this.name)}_sync_info"
1684
+ await db_1.default.tryCatchInTransaction(async () => {
1685
+ if (isNode()) {
1686
+ const schemaPrefix = db_1.default.getTenantSchemaPrefix();
1687
+ await db_1.default.query(`insert into ${schemaPrefix}"${db_1.default.sqlsanitize(this.name)}_sync_info"
1660
1688
  values(${id}, date_trunc('milliseconds', to_timestamp(${(syncTimestamp ? syncTimestamp : await db_1.default.time()).valueOf() /
1661
- 1000.0})))`);
1662
- }
1663
- else {
1664
- await db_1.default.query(`insert into "${db_1.default.sqlsanitize(this.name)}_sync_info"
1689
+ 1000.0})))`);
1690
+ }
1691
+ else {
1692
+ await db_1.default.query(`insert into "${db_1.default.sqlsanitize(this.name)}_sync_info"
1665
1693
  (last_modified, ref, modified_local, deleted)
1666
1694
  values(NULL, ${id}, true, false)`);
1667
- }
1695
+ }
1696
+ }, (e) => {
1697
+ state.log(2, `Error inserting sync info for table ${this.name}: ${e.message}`);
1698
+ });
1668
1699
  }
1669
1700
  const newRow = { [pk_name]: id, ...v };
1670
1701
  await this.auto_update_calc_aggregations(newRow);
@@ -1955,21 +1986,33 @@ class Table {
1955
1986
  );`);
1956
1987
  }
1957
1988
  async create_sync_info_table() {
1958
- const schemaPrefix = db_1.default.getTenantSchemaPrefix();
1959
- const fields = this.fields;
1960
- const pk = fields.find((f) => f.primary_key)?.name;
1961
- if (!pk) {
1962
- throw new Error("Unable to find a field with a primary key.");
1963
- }
1964
- await db_1.default.query(`create table ${schemaPrefix}"${(0, internal_1.sqlsanitize)(this.name)}_sync_info" (ref integer, last_modified timestamp, deleted boolean default false)`);
1965
- await db_1.default.query(`create index "${(0, internal_1.sqlsanitize)(this.name)}_sync_info_ref_index" on ${schemaPrefix}"${(0, internal_1.sqlsanitize)(this.name)}_sync_info"(ref)`);
1966
- await db_1.default.query(`create index "${(0, internal_1.sqlsanitize)(this.name)}_sync_info_lm_index" on ${schemaPrefix}"${(0, internal_1.sqlsanitize)(this.name)}_sync_info"(last_modified)`);
1967
- await db_1.default.query(`create index "${(0, internal_1.sqlsanitize)(this.name)}_sync_info_deleted_index" on ${schemaPrefix}"${(0, internal_1.sqlsanitize)(this.name)}_sync_info"(deleted)`);
1989
+ await db_1.default.tryCatchInTransaction(async () => {
1990
+ const schemaPrefix = db_1.default.getTenantSchemaPrefix();
1991
+ const fields = this.fields;
1992
+ const pk = fields.find((f) => f.primary_key)?.name;
1993
+ if (!pk) {
1994
+ throw new Error("Unable to find a field with a primary key.");
1995
+ }
1996
+ await db_1.default.query(`create table ${schemaPrefix}"${(0, internal_1.sqlsanitize)(this.name)}_sync_info" (ref integer, last_modified timestamp, deleted boolean default false)`);
1997
+ await db_1.default.query(`create index "${(0, internal_1.sqlsanitize)(this.name)}_sync_info_ref_index" on ${schemaPrefix}"${(0, internal_1.sqlsanitize)(this.name)}_sync_info"(ref)`);
1998
+ await db_1.default.query(`create index "${(0, internal_1.sqlsanitize)(this.name)}_sync_info_lm_index" on ${schemaPrefix}"${(0, internal_1.sqlsanitize)(this.name)}_sync_info"(last_modified)`);
1999
+ await db_1.default.query(`create index "${(0, internal_1.sqlsanitize)(this.name)}_sync_info_deleted_index" on ${schemaPrefix}"${(0, internal_1.sqlsanitize)(this.name)}_sync_info"(deleted)`);
2000
+ }, (e) => {
2001
+ require("../db/state")
2002
+ .getState()
2003
+ .log(2, `Error creating sync_info table for ${this.name}: ${e.message}`);
2004
+ });
1968
2005
  }
1969
2006
  async drop_sync_table() {
1970
- const schemaPrefix = db_1.default.getTenantSchemaPrefix();
1971
- await db_1.default.query(`
1972
- drop table ${schemaPrefix}"${(0, internal_1.sqlsanitize)(this.name)}_sync_info";`);
2007
+ await db_1.default.tryCatchInTransaction(async () => {
2008
+ const schemaPrefix = db_1.default.getTenantSchemaPrefix();
2009
+ await db_1.default.query(`
2010
+ drop table ${schemaPrefix}"${(0, internal_1.sqlsanitize)(this.name)}_sync_info";`);
2011
+ }, (e) => {
2012
+ require("../db/state")
2013
+ .getState()
2014
+ .log(2, `Error dropping sync_info table for ${this.name}: ${e.message}`);
2015
+ });
1973
2016
  }
1974
2017
  /**
1975
2018
  * Restore Row Version