//native
type Xata = {
apiKey: string
workspaceUrl: string
}
/**
* Insert record with ID
* By default, IDs are auto-generated when data is inserted into Xata. Sending a request to this endpoint allows us to insert a record with a pre-existing ID, bypassing the default automatic ID generation.
*/
export async function main(
auth: Xata,
db_branch_name: string,
table_name: string,
record_id: string,
columns: string | undefined,
createOnly: string | undefined,
ifVersion: string | undefined,
body: {}
) {
const url = new URL(
`${auth.workspaceUrl}/db/${db_branch_name}/tables/${table_name}/data/${record_id}`
)
for (const [k, v] of [
['columns', columns],
['createOnly', createOnly],
['ifVersion', ifVersion]
]) {
if (v !== undefined && v !== '' && k !== undefined) {
url.searchParams.append(k, v)
}
}
const response = await fetch(url, {
method: 'PUT',
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