@easy-cms/drizzle 0.9.0 → 0.9.1

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.d.ts CHANGED
@@ -85,6 +85,11 @@ interface DrizzleAdapterOptions {
85
85
  interface Connection {
86
86
  readonly db: DrizzleDb;
87
87
  readonly runner: SqlRunner;
88
+ /**
89
+ * With a single-writer dialect: identifies the database (e.g. the SQLite file), so every
90
+ * instance in this process that writes to it shares one queue.
91
+ */
92
+ readonly writeQueue?: string;
88
93
  close(): Promise<void>;
89
94
  }
90
95
  /**
package/dist/index.js CHANGED
@@ -993,6 +993,7 @@ var WhereBuilder = class {
993
993
  // src/database.ts
994
994
  var DEFAULT_TABLE_PREFIX = "ecms_";
995
995
  var DEFAULT_MIGRATION_DIR = "easy-cms/migrations";
996
+ var writeQueues = /* @__PURE__ */ new Map();
996
997
  async function connectDatabase(dialect, connection, args, options = {}) {
997
998
  const prefix = options.tablePrefix ?? DEFAULT_TABLE_PREFIX;
998
999
  try {
@@ -1084,12 +1085,23 @@ var DrizzleDatabase = class {
1084
1085
  const [row] = await this.db.select({ total: count() }).from(model.root.table).where(this.where(args.collection, args.where));
1085
1086
  return Number(row?.total ?? 0);
1086
1087
  }
1087
- writing = Promise.resolve();
1088
- /** Runs a write; with a single-writer database, after the writes before it are done. */
1088
+ /**
1089
+ * Runs a write; with a single-writer database, after the writes before it in this process are
1090
+ * done. (Other processes are waited for by the database's busy timeout.)
1091
+ */
1089
1092
  write(run) {
1090
1093
  if (!this.dialect.singleWriter) return run();
1091
- const result = this.writing.then(() => retryWhileBusy(run));
1092
- this.writing = result.catch(() => {
1094
+ const key = this.connection.writeQueue ?? "";
1095
+ const result = (writeQueues.get(key) ?? Promise.resolve()).then(run);
1096
+ const settled = result.then(
1097
+ () => {
1098
+ },
1099
+ () => {
1100
+ }
1101
+ );
1102
+ writeQueues.set(key, settled);
1103
+ void settled.then(() => {
1104
+ if (writeQueues.get(key) === settled) writeQueues.delete(key);
1093
1105
  });
1094
1106
  return result;
1095
1107
  }
@@ -1154,26 +1166,6 @@ var DrizzleDatabase = class {
1154
1166
  await this.connection.close();
1155
1167
  }
1156
1168
  };
1157
- var BUSY_TIMEOUT = 1e4;
1158
- async function retryWhileBusy(run) {
1159
- const until = Date.now() + BUSY_TIMEOUT;
1160
- for (let delay = 5; ; delay = Math.min(delay * 2, 250)) {
1161
- try {
1162
- return await run();
1163
- } catch (error) {
1164
- if (!isBusy(error) || Date.now() + delay > until) throw error;
1165
- await new Promise((resolve2) => setTimeout(resolve2, delay + Math.random() * delay));
1166
- }
1167
- }
1168
- }
1169
- function isBusy(error) {
1170
- for (let e = error; e; e = e.cause) {
1171
- const code = e.code;
1172
- if (typeof code === "string" && code.startsWith("SQLITE_BUSY")) return true;
1173
- if (e === e.cause) break;
1174
- }
1175
- return false;
1176
- }
1177
1169
  export {
1178
1170
  DEFAULT_MIGRATION_DIR,
1179
1171
  DEFAULT_TABLE_PREFIX,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/drizzle",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "Shared Drizzle ORM layer for Easy CMS database adapters",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -23,10 +23,10 @@
23
23
  "drizzle-orm": "0.45.3"
24
24
  },
25
25
  "peerDependencies": {
26
- "@easy-cms/core": "^0.9.0"
26
+ "@easy-cms/core": "^0.9.1"
27
27
  },
28
28
  "devDependencies": {
29
- "@easy-cms/core": "^0.9.0"
29
+ "@easy-cms/core": "^0.9.1"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"