File attachments
All Xata record APIs can be used to create, read, update, delete and query files.
const record = await xata.db.Users.create({
name: 'Keanu',
photo: {
name: 'file.png',
mediaType: 'image/png',
base64Content:
'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNk+M9QzwAEjDAGACCDAv8cI7IoAAAAAElFTkSuQmCC'
}
});
record = xata.records().insert("Users", {
"name": "Keanu",
"photo": {
"name": "file.png",
"mediaType": "image/png",
"base64Content": "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNk+M9QzwAEjDAGACCDAv8cI7IoAAAAAElFTkSuQmCC",
}
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/data
{
"name": "Keanu",
"photo": {
"name": "file.png",
"mediaType": "image/png",
"base64Content": "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNk+M9QzwAEjDAGACCDAv8cI7IoAAAAAElFTkSuQmCC"
}
}
const user = await xata.db.Users.update('record_id', {
photo: {
base64Content:
'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNk+M9QzwAEjDAGACCDAv8cI7IoAAAAAElFTkSuQmCC'
}
});
// or, using the `update` method on the record object:
user.update({
photo: {
base64Content:
'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNk+M9QzwAEjDAGACCDAv8cI7IoAAAAAElFTkSuQmCC'
}
});
record = xata.records().update("Users", "record_id", {
"photo": {
"base64Content": "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNk+M9QzwAEjDAGACCDAv8cI7IoAAAAAElFTkSuQmCC"
}
})
// PATCH https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/data/record_id
{
"photo": {
"base64Content": "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNk+M9QzwAEjDAGACCDAv8cI7IoAAAAAElFTkSuQmCC"
}
}
In the following example the photos
column is of type file[]
(file array).
The existing file ids from the array must be present in the update.
const user = await xata.db.Users.update('record_id', {
photos: [
{
id: 'existing_file_id'
},
{
id: 'new_id',
base64Content:
'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNk+M9QzwAEjDAGACCDAv8cI7IoAAAAAElFTkSuQmCC'
}
]
});
// or, using the `update` method on the record object:
user.update({
photos: [
{
id: 'existing_file_id'
},
{
id: 'new_id',
base64Content:
'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNk+M9QzwAEjDAGACCDAv8cI7IoAAAAAElFTkSuQmCC'
}
]
});
record = xata.records().update("Users", "record_id", {
"photos": [
{
"id": "existing_file_id"
},
{
"id": "new_id",
"base64Content":
"iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNk+M9QzwAEjDAGACCDAv8cI7IoAAAAAElFTkSuQmCC"
}
]
})
// PATCH https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/data/record_id
{
"photos": [
{
"id": "existing_file_id"
},
{
"id": "new_id",
"base64Content":
"iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNk+M9QzwAEjDAGACCDAv8cI7IoAAAAAElFTkSuQmCC"
}
]
}
const user = await xata.db.Users.read('record_id', ['photo.name', 'photo.base64Content']);
user = xata.records().get("Users", "record_id", columns=["photo.name", "photo.base64Content"])
curl 'https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/data/{record_id}?columns=photo.name,photo.base64Content' \
--header 'authorization: Bearer xau_REDACTED'
Response:
{
"id": "record_id",
"photo": {
"name": "file.png",
"base64Content": "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNk+M9QzwAEjDAGACCDAv8cI7IoAAAAAElFTkSuQmCC"
}
}
const user = await xata.db.Users.update('record_id', { photo: null });
// or, using the `update` method on the record object:
await user.update({ photo: null });
record = xata.records().update("Users", "record_id", {"photo": None})
// PATCH https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/data/record_id
{
"photo": null
}
A file array item is deleted by setting the array to the set of ids that need to be kept.
const user = await xata.db.Users.update('record_id', { photos: [{id: 'id_to_keep_1'}, {id: 'id_to_keep_2'} ] });
// or, using the `update` method on the record object:
await user.update({ photos: [{id: 'id_to_keep_1'}, {id: 'id_to_keep_2'} ] });
record = xata.records().update("Users", "record_id", {"photos": [{id: 'id_to_keep_1'}, {id: 'id_to_keep_2'} ]})
// PATCH https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/data/record_id
{
"photos": [{id: 'id_to_keep_1'}, {id: 'id_to_keep_2'} ]
}
Here is an example of retrieving access URLs for all files from the photo column, that are image/png
, sorted by file size:
const photos = await xata.db.Users.select(['name', 'photo.url', 'photo.size'])
.filter({ 'photo.mediaType': 'image/png' })
.sort('photo.size', 'desc')
.getMany();
photos = xata.data().query("Users", {
"columns": ["name", "photo.url", "photo.size"],
"filter": { "photo.mediaType": "image/png" },
"sort": { "photo.size": "desc" }
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
{
"columns": ["name", "photo.url", "photo.size"],
"filter": { "photo.mediaType": "image/png" },
"sort": { "photo.size": "desc" }
}
Since all record APIs use JSON for both request and response body, the file content needs to be encoded. For reasons like performance or data size on the wire, encoding the content might not be desired. To work directly with binary file content, Xata introduces new file APIs. Similar to the other Xata APIs, the file APIs require the Authorization header and a valid API key.
file
column type:
await xata.files.upload({ table: 'table_name', column: 'column_name', record: 'record_id' }, file);
response = xata.files().put("table_name", "record_id", "column_name", file_content)
curl --request PUT 'https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/data/{record_id}/column/{column_name}/file' \
--header 'Content-Type: image/jpeg' \
--header 'Authorization: Bearer xau_REDACTED' \
--data-binary '@/path/to/file'
Column type is file[]
(file array).
The fileId
is optional and a unique id will be automatically generated if not provided.
await xata.files.upload({ table: 'table_name', column: 'column_name', record: 'record_id', fileId: 'id' }, file);
response = xata.files().put_item("table_name", "record_id", "column_name", "file_id", file_content)
curl --request PUT 'https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/data/{record_id}/column/{column_name}/file/{file_id}' \
--header 'Content-Type: image/jpeg' \
--header 'Authorization: Bearer xau_REDACTED' \
--data-binary '@/path/to/file'
file
column type:
const file = await xata.files.download({ table: 'table_name', column: 'column_name', record: 'record_id' });
file = xata.files().get("table_name", "record_id", "column_name")
curl 'https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/data/{record_id}/column/{column_name}/file' \
--header 'Authorization: Bearer xau_REDACTED' \
--output download.jpeg
file[]
(file array) column type:
const file = await xata.files.download({
table: 'table_name',
column: 'column_name',
record: 'record_id',
fileId: 'file_id'
});
file = xata.files().get_item("table_name", "record_id", "column_name", "file_id")
curl 'https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/data/{record_id}/column/{column_name}/file/{file_id}' \
--header 'Authorization: Bearer xau_REDACTED' \
--output download.jpeg
Column type is file[]
(file array).
fileId
is required to identify the array item to be deleted.
await xata.files.delete({ table: 'table_name', column: 'column_name', record: 'record_id', fileId: 'id' });
response = xata.files().delete_item("table_name", "record_id", "column_name", "file_id")
curl --request DELETE 'https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/data/{record_id}/column/{column_name}/file/{file_id}' \
--header 'Authorization: Bearer xau_REDACTED' \