zam-core 0.3.1 → 0.3.3

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
@@ -128,18 +128,24 @@ function openDatabase(options = {}) {
128
128
  const dbOpts = {};
129
129
  if (options.syncUrl) {
130
130
  dbOpts.syncUrl = options.syncUrl;
131
- const metaPath = `${dbPath}.meta`;
132
- if (existsSync(dbPath) && !existsSync(metaPath)) {
131
+ }
132
+ if (options.authToken) {
133
+ dbOpts.authToken = options.authToken;
134
+ }
135
+ let db;
136
+ try {
137
+ db = new Database(dbPath, dbOpts);
138
+ } catch (err) {
139
+ if (options.syncUrl && err.message?.includes("InvalidLocalState")) {
133
140
  for (const suffix of ["", "-wal", "-shm"]) {
134
141
  const f = `${dbPath}${suffix}`;
135
- if (existsSync(f)) rmSync(f);
142
+ if (existsSync(f)) rmSync(f, { force: true });
136
143
  }
144
+ db = new Database(dbPath, dbOpts);
145
+ } else {
146
+ throw err;
137
147
  }
138
148
  }
139
- if (options.authToken) {
140
- dbOpts.authToken = options.authToken;
141
- }
142
- const db = new Database(dbPath, dbOpts);
143
149
  db.pragma("journal_mode = WAL");
144
150
  db.pragma("foreign_keys = ON");
145
151
  db.pragma("busy_timeout = 5000");
@@ -157,6 +163,8 @@ function openDatabaseWithSync(options = {}) {
157
163
  const syncUrl = db.prepare("SELECT value FROM user_config WHERE key = ?").get("turso.url");
158
164
  const authToken = db.prepare("SELECT value FROM user_config WHERE key = ?").get("turso.token");
159
165
  if (!syncUrl || !authToken) return db;
166
+ db.pragma("wal_checkpoint(TRUNCATE)");
167
+ db.pragma("journal_mode = DELETE");
160
168
  db.close();
161
169
  return openDatabase({ ...options, syncUrl: syncUrl.value, authToken: authToken.value });
162
170
  }