@easy-cms/drizzle 0.13.0 → 0.14.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/dist/index.d.ts CHANGED
@@ -70,6 +70,11 @@ interface Dialect {
70
70
  firstElementValue(array: AnyColumn | SQL, lists: readonly (readonly string[])[], path: readonly string[], type: JsonValueType): SQL;
71
71
  /** SQL that copies the whole database to a file while it is in use (SQLite: `VACUUM INTO`). */
72
72
  backupSQL?(file: string): string;
73
+ /**
74
+ * SQL that moves a serial column's sequence past its largest value, after rows were inserted
75
+ * with their ids (Postgres). SQLite needs none: AUTOINCREMENT follows inserted ids.
76
+ */
77
+ resetSequenceSQL?(table: string, column: string): string;
73
78
  /** Placeholder for the n-th parameter (1-based) in raw SQL. */
74
79
  param(n: number): string;
75
80
  /** CREATE TABLE IF NOT EXISTS for the migrations table. */
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // src/database.ts
2
2
  import { resolve } from "path";
3
3
  import { QueryError as QueryError2 } from "@easy-cms/core";
4
- import { count, eq as eq3, inArray as inArray3 } from "drizzle-orm";
4
+ import { asc as asc3, count, eq as eq3, getTableColumns, getTableName, inArray as inArray3 } from "drizzle-orm";
5
5
 
6
6
  // src/documents.ts
7
7
  import { asc, eq, inArray } from "drizzle-orm";
@@ -1196,6 +1196,47 @@ var DrizzleDatabase = class {
1196
1196
  await this.connection.runner.query(backupSQL(file));
1197
1197
  });
1198
1198
  }
1199
+ get transfer() {
1200
+ const { tables, hash } = this.schema;
1201
+ const table = (name) => {
1202
+ const found = tables[name];
1203
+ if (!found) throw new QueryError2(`Unknown table "${name}"`);
1204
+ return found;
1205
+ };
1206
+ const key = (t) => {
1207
+ const columns = getTableColumns(t);
1208
+ return columns.id ?? columns.slug;
1209
+ };
1210
+ return {
1211
+ schemaHash: hash,
1212
+ tables: () => Object.keys(tables),
1213
+ count: async (name) => {
1214
+ const [row] = await this.db.select({ n: count() }).from(table(name));
1215
+ return Number(row?.n ?? 0);
1216
+ },
1217
+ read: (name, offset, limit) => {
1218
+ const t = table(name);
1219
+ return this.db.select().from(t).orderBy(asc3(key(t))).limit(limit).offset(offset);
1220
+ },
1221
+ write: async (name, rows) => {
1222
+ const t = table(name);
1223
+ const size = Math.max(1, Math.floor(1e4 / Object.keys(getTableColumns(t)).length));
1224
+ for (let i = 0; i < rows.length; i += size) {
1225
+ const batch = rows.slice(i, i + size);
1226
+ await this.write(() => this.db.insert(t).values(batch));
1227
+ }
1228
+ },
1229
+ finish: async () => {
1230
+ const { resetSequenceSQL } = this.dialect;
1231
+ if (!resetSequenceSQL) return;
1232
+ for (const t of Object.values(tables)) {
1233
+ const id = getTableColumns(t).id;
1234
+ if (id?.dataType === "number")
1235
+ await this.connection.runner.query(resetSequenceSQL(getTableName(t), "id"));
1236
+ }
1237
+ }
1238
+ };
1239
+ }
1199
1240
  async destroy() {
1200
1241
  await this.connection.close();
1201
1242
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/drizzle",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
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.13.0"
26
+ "@easy-cms/core": "^0.14.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@easy-cms/core": "^0.13.0"
29
+ "@easy-cms/core": "^0.14.0"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"