File Attachments in Xata IO

Hero
4 min readSep 12, 2023

Aim of adding file attachments in Xata Database by their team:

We often see engineers using a relational database with a separate storage service to store the data used by their application. In most cases the binary files are related to the relational data, therefore the common pattern is to store a file link in the database. This experience adds unnecessary friction and we saw an opportunity to simplify. Because file storage use cases are so closely correlated to data, we decided to embed file attachments directly into our server less database and bring a new experience for building data apps that require binary storage.

Points to remember when working with files:

  1. Relational and storage APIs should share the same endpoints and the same connections. It is easier to work with and maintain one service rather than two.
  2. APIs must share the same authorization scheme and the same permissions model. Avoid having to use different credentials and keep permissions in sync.
  3. Relational data and binary object data should share the same region. Both data types should reside in the same compliance boundary and have similar guarantees.

Steps to add file column in your database:

  1. First create an account in Xata IO for free
  2. Go to workspaces
  3. Create a Workspace and a Database by clicking on + Add Database

Give your database a name and click on create

Click on the DB
Click on Add a table

Time to add columns to are table 😊

Click on the + icon

Click on String’, we are creating this column to give our image a display name. It’s totally optional

Click on “Create Column” to create the column

Again click on the “+” icon and this time select file. We are going to create this column for our files.

Give the column a name and click on “Create Column”

Time to add records to are table 😊

Give the image a name of your choice and click on Add files and select a file from your workspace. After that click on Create Record

Point to remember:
By default, these images are going to be private.

if you can access a record, you can also access its attached files. Image file types also get some extra functionality allowing you to request them at any size and style with built-in transformations. With this release, we aim to simplify your application architecture and reduce the number of services you need to manage.

So to access some images publicly for everyone you need to do some changes.

Click on the image

Click on this icon

This will make your file public. Now copy the url of your image by clicking on the share icon

Open a new tab and paste the URL, you should be able to see the image.

This is how the whole process works:

Image from official documentation of Xata

Behind the scenes, the file data is actually stored in two places. The file content is stored as an AWS S3 object while the file metadata (name, type, size, S3 pointer) is stored as a JSON object in the PostgreSQL database table.

Till that if you haven’t read my previous blog which is too on Xata, how CSV files work, go and have a read

Much more coming soon!!
Till that have a look at the Xata Docs for more such interesting things !

I am a freelancer & content-writer

Link of my portfolio

Follow me on medium for more such superb blogs :)

Any doubts?
Contact me:
Discord: hero.xyz
Twitter

I hope you’ve enjoyed reading it.

Will be back soon with another blog, to get updated with my blogs follow me on medium or subscribe to the newsletter.

Till that time, have a nice day ahead, and always keep smiling. :)

Thank You,

Hero

--

--

Hero

Full Stack Developer 🖥️ | Freelancer | Content Writer ✍ | Open Source Contributor 🤝| Loves to participate in hackathons♥|Always Open For Collabs & Client Work