@easy-cms/drizzle 0.8.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
@@ -58,8 +58,11 @@ interface Dialect {
58
58
  * (any element when it returns undefined). Anything but an array has no elements.
59
59
  */
60
60
  someElement(array: AnyColumn | SQL, condition: (el: JsonElement) => SQL | undefined): SQL;
61
- /** The value at `path` in the first element of a JSON array that has one, for sorting. */
62
- firstElementValue(array: AnyColumn | SQL, path: readonly string[], type: JsonValueType): SQL;
61
+ /**
62
+ * For sorting: the value at `path` in the first element that has one, stepping through the
63
+ * nested `lists` in each element (in order) like `someElement`.
64
+ */
65
+ firstElementValue(array: AnyColumn | SQL, lists: readonly (readonly string[])[], path: readonly string[], type: JsonValueType): SQL;
63
66
  /** Placeholder for the n-th parameter (1-based) in raw SQL. */
64
67
  param(n: number): string;
65
68
  /** CREATE TABLE IF NOT EXISTS for the migrations table. */
@@ -82,6 +85,11 @@ interface DrizzleAdapterOptions {
82
85
  interface Connection {
83
86
  readonly db: DrizzleDb;
84
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;
85
93
  close(): Promise<void>;
86
94
  }
87
95
  /**
package/dist/index.js CHANGED
@@ -968,11 +968,12 @@ var WhereBuilder = class {
968
968
  }
969
969
  return subquery(this.build(table, { [rest.join(".")]: { [op]: value } }));
970
970
  }
971
- /** A column, or the value in the first block that has it. */
971
+ /** A column, or the first value in blocks (and the lists in them, in order) that has it. */
972
972
  sortValue(target, path) {
973
973
  if (target.kind === "column") return target.column;
974
- if (target.kind === "blocks" && target.value.lists.length === 0 && target.value.path) {
975
- return this.dialect.firstElementValue(target.column, target.value.path, target.value.type);
974
+ if (target.kind === "blocks" && target.value.path) {
975
+ const { lists, path: inner, type } = target.value;
976
+ return this.dialect.firstElementValue(target.column, lists, inner, type);
976
977
  }
977
978
  throw new QueryError(`Cannot sort by "${path}"`);
978
979
  }
@@ -992,6 +993,7 @@ var WhereBuilder = class {
992
993
  // src/database.ts
993
994
  var DEFAULT_TABLE_PREFIX = "ecms_";
994
995
  var DEFAULT_MIGRATION_DIR = "easy-cms/migrations";
996
+ var writeQueues = /* @__PURE__ */ new Map();
995
997
  async function connectDatabase(dialect, connection, args, options = {}) {
996
998
  const prefix = options.tablePrefix ?? DEFAULT_TABLE_PREFIX;
997
999
  try {
@@ -1083,12 +1085,23 @@ var DrizzleDatabase = class {
1083
1085
  const [row] = await this.db.select({ total: count() }).from(model.root.table).where(this.where(args.collection, args.where));
1084
1086
  return Number(row?.total ?? 0);
1085
1087
  }
1086
- writing = Promise.resolve();
1087
- /** 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
+ */
1088
1092
  write(run) {
1089
1093
  if (!this.dialect.singleWriter) return run();
1090
- const result = this.writing.then(run);
1091
- 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);
1092
1105
  });
1093
1106
  return result;
1094
1107
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/drizzle",
3
- "version": "0.8.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.8.0"
26
+ "@easy-cms/core": "^0.9.1"
27
27
  },
28
28
  "devDependencies": {
29
- "@easy-cms/core": "^0.8.0"
29
+ "@easy-cms/core": "^0.9.1"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"