Edits history of script submission #13903 for ' Execute a transaction on a branch (xata)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Xata = {
    	apiKey: string
    	workspaceUrl: string
    }
    /**
     * Execute a transaction on a branch
     *
     */
    export async function main(
    	auth: Xata,
    	db_branch_name: string,
    	body: {
    		operations:
    			| {
    					insert: {
    						table: string
    						record: {}
    						ifVersion?: number
    						createOnly?: false | true
    						columns?: string[]
    					}
    			  }
    			| {
    					update: {
    						table: string
    						id: string
    						fields: {}
    						ifVersion?: number
    						upsert?: false | true
    						columns?: string[]
    					}
    			  }
    			| {
    					delete: {
    						table: string
    						id: string
    						failIfMissing?: false | true
    						columns?: string[]
    					}
    			  }
    			| { get: { table: string; id: string; columns?: string[] } }[]
    	}
    ) {
    	const url = new URL(`${auth.workspaceUrl}/db/${db_branch_name}/transaction`)
    
    	const response = await fetch(url, {
    		method: 'POST',
    		headers: {
    			'Content-Type': 'application/json',
    			Authorization: 'Bearer ' + auth.apiKey
    		},
    		body: JSON.stringify(body)
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 428 days ago