alchemy 0.81.4 → 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.
- package/bin/alchemy.js +1 -1
- package/lib/cloudflare/d1-database.d.ts +21 -35
- package/lib/cloudflare/d1-database.d.ts.map +1 -1
- package/lib/cloudflare/d1-database.js +57 -100
- package/lib/cloudflare/d1-database.js.map +1 -1
- package/lib/cloudflare/d1-local-migrations.d.ts +3 -4
- package/lib/cloudflare/d1-local-migrations.d.ts.map +1 -1
- package/lib/cloudflare/d1-local-migrations.js +40 -12
- package/lib/cloudflare/d1-local-migrations.js.map +1 -1
- package/lib/cloudflare/d1-migrations.d.ts +0 -8
- package/lib/cloudflare/d1-migrations.d.ts.map +1 -1
- package/lib/cloudflare/d1-migrations.js +0 -35
- package/lib/cloudflare/d1-migrations.js.map +1 -1
- package/lib/cloudflare/d1-sql-file.d.ts +12 -0
- package/lib/cloudflare/d1-sql-file.d.ts.map +1 -0
- package/lib/cloudflare/d1-sql-file.js +38 -0
- package/lib/cloudflare/d1-sql-file.js.map +1 -0
- package/lib/neon/branch.d.ts +10 -17
- package/lib/neon/branch.d.ts.map +1 -1
- package/lib/neon/branch.js +101 -0
- package/lib/neon/branch.js.map +1 -1
- package/lib/state/d1-state-store.js +8 -8
- package/lib/state/d1-state-store.js.map +1 -1
- package/package.json +1 -1
- package/src/cloudflare/d1-database.ts +125 -187
- package/src/cloudflare/d1-local-migrations.ts +56 -18
- package/src/cloudflare/d1-migrations.ts +0 -43
- package/src/cloudflare/d1-sql-file.ts +52 -0
- package/src/neon/branch.ts +134 -0
- package/src/state/d1-state-store.ts +8 -10
- package/workers/tunnel-proxy.js +1 -1
|
@@ -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
|
+
};
|
package/src/neon/branch.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { alchemy } from "../alchemy.ts";
|
|
1
2
|
import type { Context } from "../context.ts";
|
|
2
3
|
import { Resource } from "../resource.ts";
|
|
3
4
|
import { createNeonApi, type NeonApiOptions } from "./api.ts";
|
|
5
|
+
import type { NeonClient } from "./api/sdk.gen.ts";
|
|
4
6
|
import type * as neon from "./api/types.gen.ts";
|
|
5
7
|
import type { NeonProject } from "./project.ts";
|
|
6
8
|
import {
|
|
@@ -41,6 +43,10 @@ export interface NeonBranchProps extends NeonApiOptions {
|
|
|
41
43
|
* The timestamp must be provided in ISO 8601 format; for example: `2024-02-26T12:00:00Z`.
|
|
42
44
|
*/
|
|
43
45
|
parentTimestamp?: string;
|
|
46
|
+
/**
|
|
47
|
+
* When `true`, will adopt an existing branch by `name`.
|
|
48
|
+
*/
|
|
49
|
+
adopt?: boolean;
|
|
44
50
|
/**
|
|
45
51
|
* The timestamp when the branch is scheduled to expire and be automatically deleted.
|
|
46
52
|
* Must be set by the client following the [RFC 3339, section 5.6](https://tools.ietf.org/html/rfc3339#section-5.6) format with precision up to seconds (such as 2025-06-09T18:02:16Z).
|
|
@@ -182,6 +188,16 @@ export const NeonBranch = Resource(
|
|
|
182
188
|
return this.destroy();
|
|
183
189
|
}
|
|
184
190
|
case "create": {
|
|
191
|
+
if (this.scope.adopt || props.adopt) {
|
|
192
|
+
try {
|
|
193
|
+
return await fetchBranch(api, { ...props, projectId, name });
|
|
194
|
+
} catch (error) {
|
|
195
|
+
if (!(error instanceof NeonBranchNotFound)) {
|
|
196
|
+
throw error;
|
|
197
|
+
}
|
|
198
|
+
// branch not found, continue with creation
|
|
199
|
+
}
|
|
200
|
+
}
|
|
185
201
|
const { data } = await api.createProjectBranch({
|
|
186
202
|
path: {
|
|
187
203
|
project_id: projectId,
|
|
@@ -277,3 +293,121 @@ export const NeonBranch = Resource(
|
|
|
277
293
|
}
|
|
278
294
|
},
|
|
279
295
|
);
|
|
296
|
+
|
|
297
|
+
async function fetchBranch(
|
|
298
|
+
api: NeonClient,
|
|
299
|
+
props: NeonBranchProps & { name: string; projectId: string },
|
|
300
|
+
): Promise<NeonBranch> {
|
|
301
|
+
const branchList: neon.Branch[] = [];
|
|
302
|
+
|
|
303
|
+
let branchesResponse = await api.listProjectBranches({
|
|
304
|
+
path: { project_id: props.projectId },
|
|
305
|
+
query: {
|
|
306
|
+
search: props.name,
|
|
307
|
+
},
|
|
308
|
+
});
|
|
309
|
+
branchList.push(...(branchesResponse.data?.branches ?? []));
|
|
310
|
+
|
|
311
|
+
while (branchesResponse.data?.pagination?.next) {
|
|
312
|
+
branchesResponse = await api.listProjectBranches({
|
|
313
|
+
path: { project_id: props.projectId },
|
|
314
|
+
query: {
|
|
315
|
+
search: props.name,
|
|
316
|
+
cursor: branchesResponse.data.pagination.next,
|
|
317
|
+
},
|
|
318
|
+
});
|
|
319
|
+
branchList.push(...(branchesResponse.data?.branches ?? []));
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const branchesMatchingName = branchList.filter((b) => b.name === props.name);
|
|
323
|
+
|
|
324
|
+
if (branchesMatchingName.length === 0) {
|
|
325
|
+
throw new NeonBranchNotFound(props.name);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (branchesMatchingName.length > 1) {
|
|
329
|
+
throw new Error(
|
|
330
|
+
`Multiple branches found with name "${props.name}". Name must be unique when adopting a branch.`,
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const branch = branchesMatchingName[0];
|
|
335
|
+
|
|
336
|
+
const databases = await api.listProjectBranchDatabases({
|
|
337
|
+
path: {
|
|
338
|
+
branch_id: branch.id,
|
|
339
|
+
project_id: props.projectId,
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
const endpoints = await api.listProjectBranchEndpoints({
|
|
344
|
+
path: { project_id: props.projectId, branch_id: branch.id },
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
const roles = await api.listProjectBranchRoles({
|
|
348
|
+
path: {
|
|
349
|
+
project_id: props.projectId,
|
|
350
|
+
branch_id: branch.id,
|
|
351
|
+
},
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
const connectionUris: NeonConnectionUri[] = await Promise.all(
|
|
355
|
+
databases.data.databases.map((database) =>
|
|
356
|
+
api
|
|
357
|
+
.getConnectionUri({
|
|
358
|
+
path: { project_id: props.projectId },
|
|
359
|
+
query: {
|
|
360
|
+
branch_id: branch.id,
|
|
361
|
+
database_name: database.name,
|
|
362
|
+
role_name: database.owner_name,
|
|
363
|
+
pooled: false,
|
|
364
|
+
},
|
|
365
|
+
})
|
|
366
|
+
.then((res) => {
|
|
367
|
+
const url = new URL(res.data.uri);
|
|
368
|
+
return {
|
|
369
|
+
connection_uri: alchemy.secret(res.data.uri),
|
|
370
|
+
connection_parameters: {
|
|
371
|
+
database: database.name,
|
|
372
|
+
host: url.host,
|
|
373
|
+
port: 5432,
|
|
374
|
+
user: url.username,
|
|
375
|
+
password: alchemy.secret(url.password),
|
|
376
|
+
},
|
|
377
|
+
};
|
|
378
|
+
}),
|
|
379
|
+
),
|
|
380
|
+
);
|
|
381
|
+
|
|
382
|
+
return {
|
|
383
|
+
id: branch.id,
|
|
384
|
+
projectId: branch.project_id,
|
|
385
|
+
createdAt: new Date(branch.created_at),
|
|
386
|
+
updatedAt: new Date(branch.updated_at),
|
|
387
|
+
expiresAt: branch.expires_at ? new Date(branch.expires_at) : undefined,
|
|
388
|
+
initSource: branch.init_source as "schema-only" | "parent-data" | undefined,
|
|
389
|
+
name: branch.name,
|
|
390
|
+
protected: branch.protected,
|
|
391
|
+
default: branch.default,
|
|
392
|
+
parentBranchId: branch.parent_id,
|
|
393
|
+
parentTimestamp: branch.parent_timestamp,
|
|
394
|
+
parentLsn: branch.parent_lsn,
|
|
395
|
+
roles: roles.data.roles.map(formatRole),
|
|
396
|
+
endpoints: endpoints.data.endpoints,
|
|
397
|
+
databases: databases.data.databases,
|
|
398
|
+
connectionUris,
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export interface NeonConnectionUriProps extends NeonApiOptions {
|
|
403
|
+
projectId: string;
|
|
404
|
+
branchId: string;
|
|
405
|
+
databaseName: string;
|
|
406
|
+
roleName: string;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
class NeonBranchNotFound extends Error {
|
|
410
|
+
constructor(name: string) {
|
|
411
|
+
super(`Branch ${name} not found`);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
@@ -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 {
|
|
82
|
-
|
|
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
|
|
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].
|
|
95
|
+
await migrate(databases[0].uuid);
|
|
97
96
|
return {
|
|
98
|
-
id: databases[0].
|
|
97
|
+
id: databases[0].uuid,
|
|
99
98
|
};
|
|
100
99
|
}
|
|
101
|
-
const
|
|
100
|
+
const database = await createDatabase(api, databaseName, {
|
|
102
101
|
readReplication: { mode: "disabled" },
|
|
103
102
|
});
|
|
104
|
-
|
|
105
|
-
await migrate(res.result.uuid);
|
|
103
|
+
await migrate(database.uuid);
|
|
106
104
|
return {
|
|
107
|
-
id:
|
|
105
|
+
id: database.uuid,
|
|
108
106
|
};
|
|
109
107
|
};
|
package/workers/tunnel-proxy.js
CHANGED
|
@@ -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.
|
|
86
|
+
<p class="text-sm text-slate-500">Alchemy 0.82.1</p>
|
|
87
87
|
</div>
|
|
88
88
|
</div>
|
|
89
89
|
</body>
|