Possibly the most important aspect of a relational database is that a record can reference other records in the same or different tables, allowing you to build a web of structured data. But setting up your own database, wiring primary key and foreign key constraints can be a headache, especially when you want to launch your app as soon as possible.
This is where Skapi can save you hours and hours of work. Using our top of the line API, you have access to a fully functional relational database. No more coding an entire database or setting a backend-server.
To start using Skapi on your project right now, follow this step-by-step guide and be ready to use our database and much more in just 5 minutes.
In our previous article, we gave you an overview of how to create and fetch some simple records and now we start to delve into some nuances of creating more complex data structures within Skapi.
Now, to illustrate let’s simulate a social media structure with posts and comments.
let data = {
post: ‘this is my post’
};
let config = {
table: {
name: 'Posts',
access_group: 'public'
}
};
let referenced_record_id;
skapi.postRecord(data, config).then(response => {
reference_record_id = response.record_id;
});
Once the original post is created, Skapi returns its unique record_id
. We’ll store this in the variable referenced_record_id
. That ID now acts as the anchor for any related records, such as comments.
let commentRecord = {
comment: ‘this is a good post’
};
let commentConfig = {
table: {
name: ‘Comments’,
access_group: 'public'
},
reference: referenced_record_id
};
skapi.postRecord(commentRecord, commentConfig);
And like this, the comment is posted. As you can see, the reference
property inside the config
data is what governs the relationship between records.
You can easily fetch all data referencing the original post using the getRecords()
method passing the reference
property. Just like this:
skapi.getRecords({
table: { name: 'Comments', access_group: 'public' },
reference: referenced_record_id
});
With this powerful config you can build an intricate web of data without having to write any DDL code.
It’s like setting the foreign key without having to write any SQL, all is done automatically by Skapi’s top-tier backend API to deliver you high speed data transfers. You just focus on your app logic.
Follow us on socials: X, YouTube, Instagram, LinkedIn. More tutorials, code drops, and dev hacks are coming soon.
Top comments (0)