alchemy 0.81.4 → 0.82.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/bin/alchemy.js +1 -1
- 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/package.json +1 -1
- package/src/neon/branch.ts +134 -0
- package/workers/tunnel-proxy.js +1 -1
package/bin/alchemy.js
CHANGED
|
@@ -37096,7 +37096,7 @@ function envPaths(name$1, { suffix = "nodejs" } = {}) {
|
|
|
37096
37096
|
//#endregion
|
|
37097
37097
|
//#region package.json
|
|
37098
37098
|
var name = "alchemy";
|
|
37099
|
-
var version = "0.
|
|
37099
|
+
var version = "0.82.0";
|
|
37100
37100
|
var license = "Apache-2.0";
|
|
37101
37101
|
var author = "Sam Goodwin <sam@alchemy.run>";
|
|
37102
37102
|
var homepage = "https://alchemy.run";
|
package/lib/neon/branch.d.ts
CHANGED
|
@@ -33,6 +33,10 @@ export interface NeonBranchProps extends NeonApiOptions {
|
|
|
33
33
|
* The timestamp must be provided in ISO 8601 format; for example: `2024-02-26T12:00:00Z`.
|
|
34
34
|
*/
|
|
35
35
|
parentTimestamp?: string;
|
|
36
|
+
/**
|
|
37
|
+
* When `true`, will adopt an existing branch by `name`.
|
|
38
|
+
*/
|
|
39
|
+
adopt?: boolean;
|
|
36
40
|
/**
|
|
37
41
|
* The timestamp when the branch is scheduled to expire and be automatically deleted.
|
|
38
42
|
* 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).
|
|
@@ -136,22 +140,11 @@ export interface NeonBranch {
|
|
|
136
140
|
*/
|
|
137
141
|
connectionUris: NeonConnectionUri[];
|
|
138
142
|
}
|
|
139
|
-
export declare const NeonBranch: (((this: any, id: string, props?: {}) => never) & (new (_: never) => never)) | ((this: Context<NeonBranch>, id: string, props: NeonBranchProps) => Promise<
|
|
140
|
-
|
|
141
|
-
name: string;
|
|
143
|
+
export declare const NeonBranch: (((this: any, id: string, props?: {}) => never) & (new (_: never) => never)) | ((this: Context<NeonBranch>, id: string, props: NeonBranchProps) => Promise<NeonBranch>);
|
|
144
|
+
export interface NeonConnectionUriProps extends NeonApiOptions {
|
|
142
145
|
projectId: string;
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
parentTimestamp: string | undefined;
|
|
148
|
-
initSource: "schema-only" | "parent-data" | undefined;
|
|
149
|
-
createdAt: Date;
|
|
150
|
-
updatedAt: Date;
|
|
151
|
-
expiresAt: Date | undefined;
|
|
152
|
-
endpoints: neon.Endpoint[];
|
|
153
|
-
databases: neon.Database[];
|
|
154
|
-
roles: NeonRole[];
|
|
155
|
-
connectionUris: NeonConnectionUri[];
|
|
156
|
-
}>);
|
|
146
|
+
branchId: string;
|
|
147
|
+
databaseName: string;
|
|
148
|
+
roleName: string;
|
|
149
|
+
}
|
|
157
150
|
//# sourceMappingURL=branch.d.ts.map
|
package/lib/neon/branch.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"branch.d.ts","sourceRoot":"","sources":["../../src/neon/branch.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"branch.d.ts","sourceRoot":"","sources":["../../src/neon/branch.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAE7C,OAAO,EAAiB,KAAK,cAAc,EAAE,MAAM,UAAU,CAAC;AAE9D,OAAO,KAAK,KAAK,IAAI,MAAM,oBAAoB,CAAC;AAChD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAIL,KAAK,iBAAiB,EACtB,KAAK,QAAQ,EACd,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,eAAgB,SAAQ,cAAc;IACrD;;;OAGG;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E,CAAC;IAC3B;;OAEG;IACH,SAAS,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC;IAC3B;;OAEG;IACH,KAAK,EAAE,QAAQ,EAAE,CAAC;IAClB;;OAEG;IACH,cAAc,EAAE,iBAAiB,EAAE,CAAC;CACrC;AAED,eAAO,MAAM,UAAU,yFAGb,OAAO,CAAC,UAAU,CAAC,MACrB,MAAM,SACH,eAAe,yBAuIzB,CAAC;AA2GF,MAAM,WAAW,sBAAuB,SAAQ,cAAc;IAC5D,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;CAClB"}
|
package/lib/neon/branch.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { alchemy } from "../alchemy.js";
|
|
1
2
|
import { Resource } from "../resource.js";
|
|
2
3
|
import { createNeonApi } from "./api.js";
|
|
3
4
|
import { formatConnectionUri, formatRole, waitForOperations, } from "./utils.js";
|
|
@@ -27,6 +28,17 @@ export const NeonBranch = Resource("neon::Branch", async function (id, props) {
|
|
|
27
28
|
return this.destroy();
|
|
28
29
|
}
|
|
29
30
|
case "create": {
|
|
31
|
+
if (this.scope.adopt || props.adopt) {
|
|
32
|
+
try {
|
|
33
|
+
return await fetchBranch(api, { ...props, projectId, name });
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
if (!(error instanceof NeonBranchNotFound)) {
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
// branch not found, continue with creation
|
|
40
|
+
}
|
|
41
|
+
}
|
|
30
42
|
const { data } = await api.createProjectBranch({
|
|
31
43
|
path: {
|
|
32
44
|
project_id: projectId,
|
|
@@ -109,4 +121,93 @@ export const NeonBranch = Resource("neon::Branch", async function (id, props) {
|
|
|
109
121
|
}
|
|
110
122
|
}
|
|
111
123
|
});
|
|
124
|
+
async function fetchBranch(api, props) {
|
|
125
|
+
const branchList = [];
|
|
126
|
+
let branchesResponse = await api.listProjectBranches({
|
|
127
|
+
path: { project_id: props.projectId },
|
|
128
|
+
query: {
|
|
129
|
+
search: props.name,
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
branchList.push(...(branchesResponse.data?.branches ?? []));
|
|
133
|
+
while (branchesResponse.data?.pagination?.next) {
|
|
134
|
+
branchesResponse = await api.listProjectBranches({
|
|
135
|
+
path: { project_id: props.projectId },
|
|
136
|
+
query: {
|
|
137
|
+
search: props.name,
|
|
138
|
+
cursor: branchesResponse.data.pagination.next,
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
branchList.push(...(branchesResponse.data?.branches ?? []));
|
|
142
|
+
}
|
|
143
|
+
const branchesMatchingName = branchList.filter((b) => b.name === props.name);
|
|
144
|
+
if (branchesMatchingName.length === 0) {
|
|
145
|
+
throw new NeonBranchNotFound(props.name);
|
|
146
|
+
}
|
|
147
|
+
if (branchesMatchingName.length > 1) {
|
|
148
|
+
throw new Error(`Multiple branches found with name "${props.name}". Name must be unique when adopting a branch.`);
|
|
149
|
+
}
|
|
150
|
+
const branch = branchesMatchingName[0];
|
|
151
|
+
const databases = await api.listProjectBranchDatabases({
|
|
152
|
+
path: {
|
|
153
|
+
branch_id: branch.id,
|
|
154
|
+
project_id: props.projectId,
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
const endpoints = await api.listProjectBranchEndpoints({
|
|
158
|
+
path: { project_id: props.projectId, branch_id: branch.id },
|
|
159
|
+
});
|
|
160
|
+
const roles = await api.listProjectBranchRoles({
|
|
161
|
+
path: {
|
|
162
|
+
project_id: props.projectId,
|
|
163
|
+
branch_id: branch.id,
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
const connectionUris = await Promise.all(databases.data.databases.map((database) => api
|
|
167
|
+
.getConnectionUri({
|
|
168
|
+
path: { project_id: props.projectId },
|
|
169
|
+
query: {
|
|
170
|
+
branch_id: branch.id,
|
|
171
|
+
database_name: database.name,
|
|
172
|
+
role_name: database.owner_name,
|
|
173
|
+
pooled: false,
|
|
174
|
+
},
|
|
175
|
+
})
|
|
176
|
+
.then((res) => {
|
|
177
|
+
const url = new URL(res.data.uri);
|
|
178
|
+
return {
|
|
179
|
+
connection_uri: alchemy.secret(res.data.uri),
|
|
180
|
+
connection_parameters: {
|
|
181
|
+
database: database.name,
|
|
182
|
+
host: url.host,
|
|
183
|
+
port: 5432,
|
|
184
|
+
user: url.username,
|
|
185
|
+
password: alchemy.secret(url.password),
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
})));
|
|
189
|
+
return {
|
|
190
|
+
id: branch.id,
|
|
191
|
+
projectId: branch.project_id,
|
|
192
|
+
createdAt: new Date(branch.created_at),
|
|
193
|
+
updatedAt: new Date(branch.updated_at),
|
|
194
|
+
expiresAt: branch.expires_at ? new Date(branch.expires_at) : undefined,
|
|
195
|
+
initSource: branch.init_source,
|
|
196
|
+
name: branch.name,
|
|
197
|
+
protected: branch.protected,
|
|
198
|
+
default: branch.default,
|
|
199
|
+
parentBranchId: branch.parent_id,
|
|
200
|
+
parentTimestamp: branch.parent_timestamp,
|
|
201
|
+
parentLsn: branch.parent_lsn,
|
|
202
|
+
roles: roles.data.roles.map(formatRole),
|
|
203
|
+
endpoints: endpoints.data.endpoints,
|
|
204
|
+
databases: databases.data.databases,
|
|
205
|
+
connectionUris,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
class NeonBranchNotFound extends Error {
|
|
209
|
+
constructor(name) {
|
|
210
|
+
super(`Branch ${name} not found`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
112
213
|
//# sourceMappingURL=branch.js.map
|
package/lib/neon/branch.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"branch.js","sourceRoot":"","sources":["../../src/neon/branch.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"branch.js","sourceRoot":"","sources":["../../src/neon/branch.ts"],"names":[],"mappings":"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MAAM,kBAAmB,SAAQ,KAAK;IACpC,YAAY,IAAY;QACtB,KAAK,CAAC,UAAU,IAAI,YAAY,CAAC,CAAC;IACpC,CAAC;CACF"}
|
package/package.json
CHANGED
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
|
+
}
|
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.0</p>
|
|
87
87
|
</div>
|
|
88
88
|
</div>
|
|
89
89
|
</body>
|