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/cli/index.js CHANGED
@@ -136,18 +136,24 @@ function openDatabase(options = {}) {
136
136
  const dbOpts = {};
137
137
  if (options.syncUrl) {
138
138
  dbOpts.syncUrl = options.syncUrl;
139
- const metaPath = `${dbPath}.meta`;
140
- if (existsSync(dbPath) && !existsSync(metaPath)) {
139
+ }
140
+ if (options.authToken) {
141
+ dbOpts.authToken = options.authToken;
142
+ }
143
+ let db;
144
+ try {
145
+ db = new Database(dbPath, dbOpts);
146
+ } catch (err) {
147
+ if (options.syncUrl && err.message?.includes("InvalidLocalState")) {
141
148
  for (const suffix of ["", "-wal", "-shm"]) {
142
149
  const f = `${dbPath}${suffix}`;
143
- if (existsSync(f)) rmSync(f);
150
+ if (existsSync(f)) rmSync(f, { force: true });
144
151
  }
152
+ db = new Database(dbPath, dbOpts);
153
+ } else {
154
+ throw err;
145
155
  }
146
156
  }
147
- if (options.authToken) {
148
- dbOpts.authToken = options.authToken;
149
- }
150
- const db = new Database(dbPath, dbOpts);
151
157
  db.pragma("journal_mode = WAL");
152
158
  db.pragma("foreign_keys = ON");
153
159
  db.pragma("busy_timeout = 5000");
@@ -165,6 +171,8 @@ function openDatabaseWithSync(options = {}) {
165
171
  const syncUrl = db.prepare("SELECT value FROM user_config WHERE key = ?").get("turso.url");
166
172
  const authToken = db.prepare("SELECT value FROM user_config WHERE key = ?").get("turso.token");
167
173
  if (!syncUrl || !authToken) return db;
174
+ db.pragma("wal_checkpoint(TRUNCATE)");
175
+ db.pragma("journal_mode = DELETE");
168
176
  db.close();
169
177
  return openDatabase({ ...options, syncUrl: syncUrl.value, authToken: authToken.value });
170
178
  }