alchemy 0.82.0 → 0.82.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.
@@ -0,0 +1,52 @@
1
+ import crypto from "node:crypto";
2
+ import fs from "node:fs/promises";
3
+ import path from "pathe";
4
+
5
+ export interface D1SqlFile {
6
+ id: string;
7
+ sql: string;
8
+ hash: string;
9
+ }
10
+
11
+ /**
12
+ * Lists SQL files from the directory, sorted by filename.
13
+ * @param directory Directory containing .sql files
14
+ */
15
+ export async function listSqlFiles(directory: string): Promise<D1SqlFile[]> {
16
+ const files = await Array.fromAsync(
17
+ fs.glob("**/*.sql", {
18
+ cwd: directory,
19
+ }),
20
+ );
21
+
22
+ const sortedFiles = files.sort((a: string, b: string) => {
23
+ const aNum = getPrefix(a);
24
+ const bNum = getPrefix(b);
25
+
26
+ if (aNum !== null && bNum !== null) return aNum - bNum;
27
+ if (aNum !== null) return -1;
28
+ if (bNum !== null) return 1;
29
+
30
+ return a.localeCompare(b);
31
+ });
32
+
33
+ return Promise.all(sortedFiles.map((id) => readSqlFile(directory, id)));
34
+ }
35
+
36
+ export async function readSqlFile(
37
+ directory: string,
38
+ name: string,
39
+ ): Promise<D1SqlFile> {
40
+ const sql = await fs.readFile(path.resolve(directory, name), "utf-8");
41
+ const hash = crypto.createHash("sha256").update(sql).digest("hex");
42
+ const file: D1SqlFile = { id: name, sql, hash };
43
+ // Make the sql property non-enumerable so it's not included in state. This prevents state store errors caused by large sql files.
44
+ Object.defineProperty(file, "sql", { enumerable: false });
45
+ return file;
46
+ }
47
+
48
+ const getPrefix = (name: string) => {
49
+ const prefix = name.split("_")[0];
50
+ const num = Number.parseInt(prefix, 10);
51
+ return Number.isNaN(num) ? null : num;
52
+ };
@@ -78,12 +78,11 @@ const upsertDatabase = async (api: CloudflareApi, databaseName: string) => {
78
78
  const { listDatabases, createDatabase } = await import(
79
79
  "../cloudflare/d1-database.ts"
80
80
  );
81
- const { applyMigrations, listMigrationsFiles } = await import(
82
- "../cloudflare/d1-migrations.ts"
83
- );
81
+ const { listSqlFiles } = await import("../cloudflare/d1-sql-file.ts");
82
+ const { applyMigrations } = await import("../cloudflare/d1-migrations.ts");
84
83
  const migrate = async (databaseId: string) => {
85
84
  await applyMigrations({
86
- migrationsFiles: await listMigrationsFiles(MIGRATIONS_DIRECTORY),
85
+ migrationsFiles: await listSqlFiles(MIGRATIONS_DIRECTORY),
87
86
  migrationsTable: "migrations",
88
87
  accountId: api.accountId,
89
88
  databaseId,
@@ -93,17 +92,16 @@ const upsertDatabase = async (api: CloudflareApi, databaseName: string) => {
93
92
  };
94
93
  const databases = await listDatabases(api, databaseName);
95
94
  if (databases[0]) {
96
- await migrate(databases[0].id);
95
+ await migrate(databases[0].uuid);
97
96
  return {
98
- id: databases[0].id,
97
+ id: databases[0].uuid,
99
98
  };
100
99
  }
101
- const res = await createDatabase(api, databaseName, {
100
+ const database = await createDatabase(api, databaseName, {
102
101
  readReplication: { mode: "disabled" },
103
102
  });
104
- assert(res.result.uuid, "Missing UUID for database");
105
- await migrate(res.result.uuid);
103
+ await migrate(database.uuid);
106
104
  return {
107
- id: res.result.uuid,
105
+ id: database.uuid,
108
106
  };
109
107
  };
@@ -83,7 +83,7 @@ const renderErrorHtml = (props) => `
83
83
  Alchemy</a>.</p>
84
84
  </div>
85
85
  <div class="bg-slate-200 px-5 py-3 flex flex-col w-full max-w-lg">
86
- <p class="text-sm text-slate-500">Alchemy 0.82.0</p>
86
+ <p class="text-sm text-slate-500">Alchemy 0.82.1</p>
87
87
  </div>
88
88
  </div>
89
89
  </body>