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'
  }
});
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'
  }
});

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'
    }
  ]
});
const user = await xata.db.Users.read('record_id', ['photo.name', 'photo.base64Content']);

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 });
 

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'} ] });
 

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();

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);

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);

file column type:

const file = await xata.files.download({ table: 'table_name', column: 'column_name', record: 'record_id' });

file[] (file array) column type:

const file = await xata.files.download({
  table: 'table_name',
  column: 'column_name',
  record: 'record_id',
  fileId: 'file_id'
});

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' });