@breeztech/breez-sdk-spark 0.14.0 → 0.15.0
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/breez-sdk-spark.tgz +0 -0
- package/bundler/breez_sdk_spark_wasm.d.ts +114 -40
- package/bundler/breez_sdk_spark_wasm.js +1 -1
- package/bundler/breez_sdk_spark_wasm_bg.js +118 -104
- package/bundler/breez_sdk_spark_wasm_bg.wasm +0 -0
- package/bundler/breez_sdk_spark_wasm_bg.wasm.d.ts +12 -11
- package/deno/breez_sdk_spark_wasm.d.ts +114 -40
- package/deno/breez_sdk_spark_wasm.js +118 -104
- package/deno/breez_sdk_spark_wasm_bg.wasm +0 -0
- package/deno/breez_sdk_spark_wasm_bg.wasm.d.ts +12 -11
- package/nodejs/breez_sdk_spark_wasm.d.ts +114 -40
- package/nodejs/breez_sdk_spark_wasm.js +121 -106
- package/nodejs/breez_sdk_spark_wasm_bg.wasm +0 -0
- package/nodejs/breez_sdk_spark_wasm_bg.wasm.d.ts +12 -11
- package/nodejs/index.mjs +3 -2
- package/nodejs/mysql-session-manager/index.cjs +26 -8
- package/nodejs/mysql-session-manager/migrations.cjs +40 -3
- package/nodejs/mysql-storage/index.cjs +67 -48
- package/nodejs/mysql-storage/migrations.cjs +220 -85
- package/nodejs/mysql-token-store/index.cjs +133 -68
- package/nodejs/mysql-token-store/migrations.cjs +309 -80
- package/nodejs/mysql-tree-store/index.cjs +76 -41
- package/nodejs/mysql-tree-store/migrations.cjs +254 -71
- package/nodejs/postgres-session-manager/index.cjs +27 -9
- package/nodejs/postgres-session-manager/migrations.cjs +45 -6
- package/nodejs/postgres-storage/index.cjs +81 -62
- package/nodejs/postgres-storage/migrations.cjs +207 -79
- package/nodejs/postgres-token-store/index.cjs +111 -67
- package/nodejs/postgres-token-store/migrations.cjs +153 -61
- package/nodejs/postgres-tree-store/index.cjs +60 -42
- package/nodejs/postgres-tree-store/migrations.cjs +130 -46
- package/package.json +1 -1
- package/ssr/index.js +14 -9
- package/web/breez_sdk_spark_wasm.d.ts +126 -51
- package/web/breez_sdk_spark_wasm.js +118 -104
- package/web/breez_sdk_spark_wasm_bg.wasm +0 -0
- package/web/breez_sdk_spark_wasm_bg.wasm.d.ts +12 -11
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
const { SessionManagerError } = require("./errors.cjs");
|
|
7
7
|
|
|
8
|
-
const SESSION_MIGRATIONS_TABLE = "
|
|
8
|
+
const SESSION_MIGRATIONS_TABLE = "brz_session_schema_migrations";
|
|
9
9
|
const MIGRATION_LOCK_NAME = "breez_mysql_session_manager_migration_lock";
|
|
10
10
|
const MIGRATION_LOCK_TIMEOUT = 60;
|
|
11
11
|
|
|
@@ -31,6 +31,8 @@ class MysqlSessionManagerMigrationManager {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
try {
|
|
34
|
+
await this._applySchemaRenames(conn);
|
|
35
|
+
|
|
34
36
|
await conn.query("START TRANSACTION");
|
|
35
37
|
|
|
36
38
|
await conn.query(`
|
|
@@ -81,12 +83,38 @@ class MysqlSessionManagerMigrationManager {
|
|
|
81
83
|
}
|
|
82
84
|
}
|
|
83
85
|
|
|
86
|
+
/**
|
|
87
|
+
* Pre-prefix rename. Canary-gated on the legacy `session_schema_migrations`
|
|
88
|
+
* table.
|
|
89
|
+
* @param {import('mysql2/promise').PoolConnection} conn
|
|
90
|
+
*/
|
|
91
|
+
async _applySchemaRenames(conn) {
|
|
92
|
+
if (!(await _mysqlTableExists(conn, "session_schema_migrations"))) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (
|
|
97
|
+
(await _mysqlTableExists(conn, "sessions")) &&
|
|
98
|
+
!(await _mysqlTableExists(conn, "brz_sessions"))
|
|
99
|
+
) {
|
|
100
|
+
await conn.query("RENAME TABLE `sessions` TO `brz_sessions`");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (await _mysqlTableExists(conn, "session_schema_migrations")) {
|
|
104
|
+
if (!(await _mysqlTableExists(conn, SESSION_MIGRATIONS_TABLE))) {
|
|
105
|
+
await conn.query(
|
|
106
|
+
`RENAME TABLE \`session_schema_migrations\` TO \`${SESSION_MIGRATIONS_TABLE}\``
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
84
112
|
_getMigrations() {
|
|
85
113
|
return [
|
|
86
114
|
{
|
|
87
|
-
name: "Create
|
|
115
|
+
name: "Create brz_sessions table",
|
|
88
116
|
sql: [
|
|
89
|
-
`CREATE TABLE IF NOT EXISTS
|
|
117
|
+
`CREATE TABLE IF NOT EXISTS brz_sessions (
|
|
90
118
|
user_id VARBINARY(33) NOT NULL,
|
|
91
119
|
service_identity_key VARBINARY(33) NOT NULL,
|
|
92
120
|
token TEXT NOT NULL,
|
|
@@ -99,4 +127,13 @@ class MysqlSessionManagerMigrationManager {
|
|
|
99
127
|
}
|
|
100
128
|
}
|
|
101
129
|
|
|
130
|
+
async function _mysqlTableExists(conn, tableName) {
|
|
131
|
+
const [rows] = await conn.query(
|
|
132
|
+
`SELECT COUNT(*) AS c FROM information_schema.tables
|
|
133
|
+
WHERE table_schema = DATABASE() AND table_name = ?`,
|
|
134
|
+
[tableName]
|
|
135
|
+
);
|
|
136
|
+
return Number(rows[0].c) > 0;
|
|
137
|
+
}
|
|
138
|
+
|
|
102
139
|
module.exports = { MysqlSessionManagerMigrationManager };
|
|
@@ -75,12 +75,12 @@ const SELECT_PAYMENT_SQL = `
|
|
|
75
75
|
lrm.sender_comment AS lnurl_sender_comment,
|
|
76
76
|
lrm.payment_hash AS lnurl_payment_hash,
|
|
77
77
|
pm.parent_payment_id
|
|
78
|
-
FROM
|
|
79
|
-
LEFT JOIN
|
|
80
|
-
LEFT JOIN
|
|
81
|
-
LEFT JOIN
|
|
82
|
-
LEFT JOIN
|
|
83
|
-
LEFT JOIN
|
|
78
|
+
FROM brz_payments p
|
|
79
|
+
LEFT JOIN brz_payment_details_lightning l ON p.id = l.payment_id AND p.user_id = l.user_id
|
|
80
|
+
LEFT JOIN brz_payment_details_token t ON p.id = t.payment_id AND p.user_id = t.user_id
|
|
81
|
+
LEFT JOIN brz_payment_details_spark s ON p.id = s.payment_id AND p.user_id = s.user_id
|
|
82
|
+
LEFT JOIN brz_payment_metadata pm ON p.id = pm.payment_id AND p.user_id = pm.user_id
|
|
83
|
+
LEFT JOIN brz_lnurl_receive_metadata lrm ON l.payment_hash = lrm.payment_hash AND l.user_id = lrm.user_id`;
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
86
|
* mysql2 may return JSON columns as either parsed objects or raw strings
|
|
@@ -107,20 +107,23 @@ class MysqlStorage {
|
|
|
107
107
|
* so that multiple instances with distinct identities can share one DB.
|
|
108
108
|
* @param {object} [logger]
|
|
109
109
|
*/
|
|
110
|
-
constructor(pool, identity, logger = null) {
|
|
110
|
+
constructor(pool, identity, logger = null, runMigration = true) {
|
|
111
111
|
if (!identity) {
|
|
112
112
|
throw new StorageError("MysqlStorage requires a tenant identity");
|
|
113
113
|
}
|
|
114
114
|
this.pool = pool;
|
|
115
115
|
this.identity = Buffer.from(identity);
|
|
116
116
|
this.logger = logger;
|
|
117
|
+
this.runMigration = runMigration;
|
|
117
118
|
}
|
|
118
119
|
|
|
119
120
|
/** Initialize the database (run migrations). */
|
|
120
121
|
async initialize() {
|
|
121
122
|
try {
|
|
122
|
-
|
|
123
|
-
|
|
123
|
+
if (this.runMigration) {
|
|
124
|
+
const migrationManager = new MysqlMigrationManager(this.logger);
|
|
125
|
+
await migrationManager.migrate(this.pool, this.identity);
|
|
126
|
+
}
|
|
124
127
|
return this;
|
|
125
128
|
} catch (error) {
|
|
126
129
|
throw new StorageError(
|
|
@@ -165,7 +168,7 @@ class MysqlStorage {
|
|
|
165
168
|
async getCachedItem(key) {
|
|
166
169
|
try {
|
|
167
170
|
const [rows] = await this.pool.query(
|
|
168
|
-
"SELECT value FROM
|
|
171
|
+
"SELECT value FROM brz_settings WHERE user_id = ? AND `key` = ?",
|
|
169
172
|
[this.identity, key]
|
|
170
173
|
);
|
|
171
174
|
return rows.length > 0 ? rows[0].value : null;
|
|
@@ -180,7 +183,7 @@ class MysqlStorage {
|
|
|
180
183
|
async setCachedItem(key, value) {
|
|
181
184
|
try {
|
|
182
185
|
await this.pool.query(
|
|
183
|
-
"INSERT INTO
|
|
186
|
+
"INSERT INTO brz_settings (user_id, `key`, value) VALUES (?, ?, ?) " +
|
|
184
187
|
"ON DUPLICATE KEY UPDATE value = VALUES(value)",
|
|
185
188
|
[this.identity, key, value]
|
|
186
189
|
);
|
|
@@ -195,7 +198,7 @@ class MysqlStorage {
|
|
|
195
198
|
async deleteCachedItem(key) {
|
|
196
199
|
try {
|
|
197
200
|
await this.pool.query(
|
|
198
|
-
"DELETE FROM
|
|
201
|
+
"DELETE FROM brz_settings WHERE user_id = ? AND `key` = ?",
|
|
199
202
|
[this.identity, key]
|
|
200
203
|
);
|
|
201
204
|
} catch (error) {
|
|
@@ -371,7 +374,7 @@ class MysqlStorage {
|
|
|
371
374
|
const spark = payment.details?.type === "spark" ? 1 : null;
|
|
372
375
|
|
|
373
376
|
await conn.query(
|
|
374
|
-
`INSERT INTO
|
|
377
|
+
`INSERT INTO brz_payments (user_id, id, payment_type, status, amount, fees, timestamp, method, withdraw_tx_id, deposit_tx_id, spark)
|
|
375
378
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
376
379
|
ON DUPLICATE KEY UPDATE
|
|
377
380
|
payment_type=VALUES(payment_type),
|
|
@@ -404,7 +407,7 @@ class MysqlStorage {
|
|
|
404
407
|
payment.details.htlcDetails != null)
|
|
405
408
|
) {
|
|
406
409
|
await conn.query(
|
|
407
|
-
`INSERT INTO
|
|
410
|
+
`INSERT INTO brz_payment_details_spark (user_id, payment_id, invoice_details, htlc_details)
|
|
408
411
|
VALUES (?, ?, ?, ?)
|
|
409
412
|
ON DUPLICATE KEY UPDATE
|
|
410
413
|
invoice_details=COALESCE(VALUES(invoice_details), invoice_details),
|
|
@@ -424,7 +427,7 @@ class MysqlStorage {
|
|
|
424
427
|
|
|
425
428
|
if (payment.details?.type === "lightning") {
|
|
426
429
|
await conn.query(
|
|
427
|
-
`INSERT INTO
|
|
430
|
+
`INSERT INTO brz_payment_details_lightning
|
|
428
431
|
(user_id, payment_id, invoice, payment_hash, destination_pubkey, description, preimage, htlc_status, htlc_expiry_time)
|
|
429
432
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
430
433
|
ON DUPLICATE KEY UPDATE
|
|
@@ -451,7 +454,7 @@ class MysqlStorage {
|
|
|
451
454
|
|
|
452
455
|
if (payment.details?.type === "token") {
|
|
453
456
|
await conn.query(
|
|
454
|
-
`INSERT INTO
|
|
457
|
+
`INSERT INTO brz_payment_details_token
|
|
455
458
|
(user_id, payment_id, metadata, tx_hash, tx_type, invoice_details)
|
|
456
459
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
457
460
|
ON DUPLICATE KEY UPDATE
|
|
@@ -539,7 +542,7 @@ class MysqlStorage {
|
|
|
539
542
|
|
|
540
543
|
// Early exit if no related payments exist for this tenant. mysql2 returns EXISTS as 0/1.
|
|
541
544
|
const [hasRelatedRows] = await this.pool.query(
|
|
542
|
-
"SELECT EXISTS(SELECT 1 FROM
|
|
545
|
+
"SELECT EXISTS(SELECT 1 FROM brz_payment_metadata WHERE user_id = ? AND parent_payment_id IS NOT NULL LIMIT 1) AS has_related",
|
|
543
546
|
[this.identity]
|
|
544
547
|
);
|
|
545
548
|
if (!hasRelatedRows[0].has_related) {
|
|
@@ -573,7 +576,7 @@ class MysqlStorage {
|
|
|
573
576
|
async insertPaymentMetadata(paymentId, metadata) {
|
|
574
577
|
try {
|
|
575
578
|
await this.pool.query(
|
|
576
|
-
`INSERT INTO
|
|
579
|
+
`INSERT INTO brz_payment_metadata (user_id, payment_id, parent_payment_id, lnurl_pay_info, lnurl_withdraw_info, lnurl_description, conversion_info, conversion_status)
|
|
577
580
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
578
581
|
ON DUPLICATE KEY UPDATE
|
|
579
582
|
parent_payment_id = COALESCE(VALUES(parent_payment_id), parent_payment_id),
|
|
@@ -612,7 +615,7 @@ class MysqlStorage {
|
|
|
612
615
|
async addDeposit(txid, vout, amountSats, isMature) {
|
|
613
616
|
try {
|
|
614
617
|
await this.pool.query(
|
|
615
|
-
`INSERT INTO
|
|
618
|
+
`INSERT INTO brz_unclaimed_deposits (user_id, txid, vout, amount_sats, is_mature)
|
|
616
619
|
VALUES (?, ?, ?, ?, ?)
|
|
617
620
|
ON DUPLICATE KEY UPDATE is_mature = VALUES(is_mature), amount_sats = VALUES(amount_sats)`,
|
|
618
621
|
[this.identity, txid, vout, amountSats, isMature ? 1 : 0]
|
|
@@ -628,7 +631,7 @@ class MysqlStorage {
|
|
|
628
631
|
async deleteDeposit(txid, vout) {
|
|
629
632
|
try {
|
|
630
633
|
await this.pool.query(
|
|
631
|
-
"DELETE FROM
|
|
634
|
+
"DELETE FROM brz_unclaimed_deposits WHERE user_id = ? AND txid = ? AND vout = ?",
|
|
632
635
|
[this.identity, txid, vout]
|
|
633
636
|
);
|
|
634
637
|
} catch (error) {
|
|
@@ -642,7 +645,7 @@ class MysqlStorage {
|
|
|
642
645
|
async listDeposits() {
|
|
643
646
|
try {
|
|
644
647
|
const [rows] = await this.pool.query(
|
|
645
|
-
"SELECT txid, vout, amount_sats, is_mature, claim_error, refund_tx, refund_tx_id FROM
|
|
648
|
+
"SELECT txid, vout, amount_sats, is_mature, claim_error, refund_tx, refund_tx_id FROM brz_unclaimed_deposits WHERE user_id = ?",
|
|
646
649
|
[this.identity]
|
|
647
650
|
);
|
|
648
651
|
|
|
@@ -668,14 +671,14 @@ class MysqlStorage {
|
|
|
668
671
|
try {
|
|
669
672
|
if (payload.type === "claimError") {
|
|
670
673
|
await this.pool.query(
|
|
671
|
-
`UPDATE
|
|
674
|
+
`UPDATE brz_unclaimed_deposits
|
|
672
675
|
SET claim_error = ?, refund_tx = NULL, refund_tx_id = NULL
|
|
673
676
|
WHERE user_id = ? AND txid = ? AND vout = ?`,
|
|
674
677
|
[JSON.stringify(payload.error), this.identity, txid, vout]
|
|
675
678
|
);
|
|
676
679
|
} else if (payload.type === "refund") {
|
|
677
680
|
await this.pool.query(
|
|
678
|
-
`UPDATE
|
|
681
|
+
`UPDATE brz_unclaimed_deposits
|
|
679
682
|
SET refund_tx = ?, refund_tx_id = ?, claim_error = NULL
|
|
680
683
|
WHERE user_id = ? AND txid = ? AND vout = ?`,
|
|
681
684
|
[payload.refundTx, payload.refundTxid, this.identity, txid, vout]
|
|
@@ -697,7 +700,7 @@ class MysqlStorage {
|
|
|
697
700
|
await this._withTransaction(async (conn) => {
|
|
698
701
|
for (const item of metadata) {
|
|
699
702
|
await conn.query(
|
|
700
|
-
`INSERT INTO
|
|
703
|
+
`INSERT INTO brz_lnurl_receive_metadata (user_id, payment_hash, nostr_zap_request, nostr_zap_receipt, sender_comment)
|
|
701
704
|
VALUES (?, ?, ?, ?, ?)
|
|
702
705
|
ON DUPLICATE KEY UPDATE
|
|
703
706
|
nostr_zap_request = VALUES(nostr_zap_request),
|
|
@@ -825,7 +828,7 @@ class MysqlStorage {
|
|
|
825
828
|
|
|
826
829
|
const [rows] = await this.pool.query(
|
|
827
830
|
`SELECT id, name, payment_identifier, created_at, updated_at
|
|
828
|
-
FROM
|
|
831
|
+
FROM brz_contacts
|
|
829
832
|
WHERE user_id = ?
|
|
830
833
|
ORDER BY name ASC
|
|
831
834
|
LIMIT ? OFFSET ?`,
|
|
@@ -851,7 +854,7 @@ class MysqlStorage {
|
|
|
851
854
|
try {
|
|
852
855
|
const [rows] = await this.pool.query(
|
|
853
856
|
`SELECT id, name, payment_identifier, created_at, updated_at
|
|
854
|
-
FROM
|
|
857
|
+
FROM brz_contacts
|
|
855
858
|
WHERE user_id = ? AND id = ?`,
|
|
856
859
|
[this.identity, id]
|
|
857
860
|
);
|
|
@@ -876,7 +879,7 @@ class MysqlStorage {
|
|
|
876
879
|
async insertContact(contact) {
|
|
877
880
|
try {
|
|
878
881
|
await this.pool.query(
|
|
879
|
-
`INSERT INTO
|
|
882
|
+
`INSERT INTO brz_contacts (user_id, id, name, payment_identifier, created_at, updated_at)
|
|
880
883
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
881
884
|
ON DUPLICATE KEY UPDATE
|
|
882
885
|
name = VALUES(name),
|
|
@@ -902,7 +905,7 @@ class MysqlStorage {
|
|
|
902
905
|
async deleteContact(id) {
|
|
903
906
|
try {
|
|
904
907
|
await this.pool.query(
|
|
905
|
-
"DELETE FROM
|
|
908
|
+
"DELETE FROM brz_contacts WHERE user_id = ? AND id = ?",
|
|
906
909
|
[this.identity, id]
|
|
907
910
|
);
|
|
908
911
|
} catch (error) {
|
|
@@ -920,13 +923,13 @@ class MysqlStorage {
|
|
|
920
923
|
return await this._withTransaction(async (conn) => {
|
|
921
924
|
// The local queue revision is per-tenant — two tenants don't share a queue.
|
|
922
925
|
const [revisionRows] = await conn.query(
|
|
923
|
-
"SELECT COALESCE(MAX(revision), 0) + 1 AS revision FROM
|
|
926
|
+
"SELECT COALESCE(MAX(revision), 0) + 1 AS revision FROM brz_sync_outgoing WHERE user_id = ?",
|
|
924
927
|
[this.identity]
|
|
925
928
|
);
|
|
926
929
|
const revision = BigInt(revisionRows[0].revision);
|
|
927
930
|
|
|
928
931
|
await conn.query(
|
|
929
|
-
`INSERT INTO
|
|
932
|
+
`INSERT INTO brz_sync_outgoing (
|
|
930
933
|
user_id,
|
|
931
934
|
record_type,
|
|
932
935
|
data_id,
|
|
@@ -961,12 +964,12 @@ class MysqlStorage {
|
|
|
961
964
|
try {
|
|
962
965
|
await this._withTransaction(async (conn) => {
|
|
963
966
|
const [deleteResult] = await conn.query(
|
|
964
|
-
"DELETE FROM
|
|
967
|
+
"DELETE FROM brz_sync_outgoing WHERE user_id = ? AND record_type = ? AND data_id = ? AND revision = ?",
|
|
965
968
|
[this.identity, record.id.type, record.id.dataId, localRevision.toString()]
|
|
966
969
|
);
|
|
967
970
|
|
|
968
971
|
if (deleteResult.affectedRows === 0) {
|
|
969
|
-
const msg = `complete_outgoing_sync: DELETE from
|
|
972
|
+
const msg = `complete_outgoing_sync: DELETE from brz_sync_outgoing matched 0 rows (type=${record.id.type}, data_id=${record.id.dataId}, revision=${localRevision})`;
|
|
970
973
|
if (this.logger && typeof this.logger.log === "function") {
|
|
971
974
|
this.logger.log({ line: msg, level: "warn" });
|
|
972
975
|
} else {
|
|
@@ -976,7 +979,7 @@ class MysqlStorage {
|
|
|
976
979
|
}
|
|
977
980
|
|
|
978
981
|
await conn.query(
|
|
979
|
-
`INSERT INTO
|
|
982
|
+
`INSERT INTO brz_sync_state (
|
|
980
983
|
user_id,
|
|
981
984
|
record_type,
|
|
982
985
|
data_id,
|
|
@@ -1003,7 +1006,7 @@ class MysqlStorage {
|
|
|
1003
1006
|
|
|
1004
1007
|
// Upsert this tenant's revision row; fresh tenants without a row get one.
|
|
1005
1008
|
await conn.query(
|
|
1006
|
-
`INSERT INTO
|
|
1009
|
+
`INSERT INTO brz_sync_revision (user_id, revision) VALUES (?, ?)
|
|
1007
1010
|
ON DUPLICATE KEY UPDATE revision = GREATEST(revision, VALUES(revision))`,
|
|
1008
1011
|
[this.identity, record.revision.toString()]
|
|
1009
1012
|
);
|
|
@@ -1031,8 +1034,8 @@ class MysqlStorage {
|
|
|
1031
1034
|
e.commit_time AS existing_commit_time,
|
|
1032
1035
|
e.data AS existing_data,
|
|
1033
1036
|
e.revision AS existing_revision
|
|
1034
|
-
FROM
|
|
1035
|
-
LEFT JOIN
|
|
1037
|
+
FROM brz_sync_outgoing o
|
|
1038
|
+
LEFT JOIN brz_sync_state e ON
|
|
1036
1039
|
o.record_type = e.record_type AND
|
|
1037
1040
|
o.data_id = e.data_id AND
|
|
1038
1041
|
o.user_id = e.user_id
|
|
@@ -1074,7 +1077,7 @@ class MysqlStorage {
|
|
|
1074
1077
|
try {
|
|
1075
1078
|
// A tenant that hasn't synced anything yet may have no row; treat as 0.
|
|
1076
1079
|
const [rows] = await this.pool.query(
|
|
1077
|
-
"SELECT revision FROM
|
|
1080
|
+
"SELECT revision FROM brz_sync_revision WHERE user_id = ?",
|
|
1078
1081
|
[this.identity]
|
|
1079
1082
|
);
|
|
1080
1083
|
return rows.length > 0 ? BigInt(rows[0].revision) : BigInt(0);
|
|
@@ -1095,7 +1098,7 @@ class MysqlStorage {
|
|
|
1095
1098
|
await this._withTransaction(async (conn) => {
|
|
1096
1099
|
for (const record of records) {
|
|
1097
1100
|
await conn.query(
|
|
1098
|
-
`INSERT INTO
|
|
1101
|
+
`INSERT INTO brz_sync_incoming (
|
|
1099
1102
|
user_id,
|
|
1100
1103
|
record_type,
|
|
1101
1104
|
data_id,
|
|
@@ -1132,7 +1135,7 @@ class MysqlStorage {
|
|
|
1132
1135
|
async syncDeleteIncomingRecord(record) {
|
|
1133
1136
|
try {
|
|
1134
1137
|
await this.pool.query(
|
|
1135
|
-
`DELETE FROM
|
|
1138
|
+
`DELETE FROM brz_sync_incoming
|
|
1136
1139
|
WHERE user_id = ?
|
|
1137
1140
|
AND record_type = ?
|
|
1138
1141
|
AND data_id = ?
|
|
@@ -1159,8 +1162,8 @@ class MysqlStorage {
|
|
|
1159
1162
|
e.commit_time AS existing_commit_time,
|
|
1160
1163
|
e.data AS existing_data,
|
|
1161
1164
|
e.revision AS existing_revision
|
|
1162
|
-
FROM
|
|
1163
|
-
LEFT JOIN
|
|
1165
|
+
FROM brz_sync_incoming i
|
|
1166
|
+
LEFT JOIN brz_sync_state e ON i.record_type = e.record_type AND i.data_id = e.data_id AND i.user_id = e.user_id
|
|
1164
1167
|
WHERE i.user_id = ?
|
|
1165
1168
|
ORDER BY i.revision ASC
|
|
1166
1169
|
LIMIT ?`,
|
|
@@ -1209,8 +1212,8 @@ class MysqlStorage {
|
|
|
1209
1212
|
e.commit_time AS existing_commit_time,
|
|
1210
1213
|
e.data AS existing_data,
|
|
1211
1214
|
e.revision AS existing_revision
|
|
1212
|
-
FROM
|
|
1213
|
-
LEFT JOIN
|
|
1215
|
+
FROM brz_sync_outgoing o
|
|
1216
|
+
LEFT JOIN brz_sync_state e ON
|
|
1214
1217
|
o.record_type = e.record_type AND
|
|
1215
1218
|
o.data_id = e.data_id AND
|
|
1216
1219
|
o.user_id = e.user_id
|
|
@@ -1256,7 +1259,7 @@ class MysqlStorage {
|
|
|
1256
1259
|
try {
|
|
1257
1260
|
await this._withTransaction(async (conn) => {
|
|
1258
1261
|
await conn.query(
|
|
1259
|
-
`INSERT INTO
|
|
1262
|
+
`INSERT INTO brz_sync_state (
|
|
1260
1263
|
user_id,
|
|
1261
1264
|
record_type,
|
|
1262
1265
|
data_id,
|
|
@@ -1282,7 +1285,7 @@ class MysqlStorage {
|
|
|
1282
1285
|
);
|
|
1283
1286
|
|
|
1284
1287
|
await conn.query(
|
|
1285
|
-
`INSERT INTO
|
|
1288
|
+
`INSERT INTO brz_sync_revision (user_id, revision) VALUES (?, ?)
|
|
1286
1289
|
ON DUPLICATE KEY UPDATE revision = GREATEST(revision, VALUES(revision))`,
|
|
1287
1290
|
[this.identity, record.revision.toString()]
|
|
1288
1291
|
);
|
|
@@ -1314,6 +1317,7 @@ function defaultMysqlStorageConfig(connectionString) {
|
|
|
1314
1317
|
maxPoolSize: 10,
|
|
1315
1318
|
createTimeoutSecs: 0,
|
|
1316
1319
|
recycleTimeoutSecs: 10,
|
|
1320
|
+
runMigration: true,
|
|
1317
1321
|
};
|
|
1318
1322
|
}
|
|
1319
1323
|
|
|
@@ -1338,8 +1342,18 @@ function createMysqlPool(config) {
|
|
|
1338
1342
|
* @param {Buffer|Uint8Array} identity - 33-byte tenant identity (secp256k1 pubkey)
|
|
1339
1343
|
* @param {object} [logger]
|
|
1340
1344
|
*/
|
|
1341
|
-
async function createMysqlStorageWithPool(
|
|
1342
|
-
|
|
1345
|
+
async function createMysqlStorageWithPool(
|
|
1346
|
+
pool,
|
|
1347
|
+
identity,
|
|
1348
|
+
logger = null,
|
|
1349
|
+
runMigration = true
|
|
1350
|
+
) {
|
|
1351
|
+
const storage = new MysqlStorage(
|
|
1352
|
+
pool,
|
|
1353
|
+
identity,
|
|
1354
|
+
logger,
|
|
1355
|
+
runMigration
|
|
1356
|
+
);
|
|
1343
1357
|
await storage.initialize();
|
|
1344
1358
|
return storage;
|
|
1345
1359
|
}
|
|
@@ -1354,7 +1368,12 @@ async function createMysqlStorageWithPool(pool, identity, logger = null) {
|
|
|
1354
1368
|
*/
|
|
1355
1369
|
async function createMysqlStorage(config, identity, logger = null) {
|
|
1356
1370
|
const pool = createMysqlPool(config);
|
|
1357
|
-
return createMysqlStorageWithPool(
|
|
1371
|
+
return createMysqlStorageWithPool(
|
|
1372
|
+
pool,
|
|
1373
|
+
identity,
|
|
1374
|
+
logger,
|
|
1375
|
+
config.runMigration !== false
|
|
1376
|
+
);
|
|
1358
1377
|
}
|
|
1359
1378
|
|
|
1360
1379
|
module.exports = {
|