1 | |
2 | type Xata = { |
3 | apiKey: string |
4 | workspaceUrl: string |
5 | } |
6 | |
7 | * Execute a transaction on a branch |
8 | * |
9 | */ |
10 | export async function main( |
11 | auth: Xata, |
12 | db_branch_name: string, |
13 | body: { |
14 | operations: |
15 | | { |
16 | insert: { |
17 | table: string |
18 | record: {} |
19 | ifVersion?: number |
20 | createOnly?: false | true |
21 | columns?: string[] |
22 | } |
23 | } |
24 | | { |
25 | update: { |
26 | table: string |
27 | id: string |
28 | fields: {} |
29 | ifVersion?: number |
30 | upsert?: false | true |
31 | columns?: string[] |
32 | } |
33 | } |
34 | | { |
35 | delete: { |
36 | table: string |
37 | id: string |
38 | failIfMissing?: false | true |
39 | columns?: string[] |
40 | } |
41 | } |
42 | | { get: { table: string; id: string; columns?: string[] } }[] |
43 | } |
44 | ) { |
45 | const url = new URL(`${auth.workspaceUrl}/db/${db_branch_name}/transaction`) |
46 |
|
47 | const response = await fetch(url, { |
48 | method: 'POST', |
49 | headers: { |
50 | 'Content-Type': 'application/json', |
51 | Authorization: 'Bearer ' + auth.apiKey |
52 | }, |
53 | body: JSON.stringify(body) |
54 | }) |
55 | if (!response.ok) { |
56 | const text = await response.text() |
57 | throw new Error(`${response.status} ${text}`) |
58 | } |
59 | return await response.json() |
60 | } |
61 |
|