DEV Community

Cover image for No More DDL Headaches: Use Skapi’s Zero-Setup Relational Database API
Skapi
Skapi

Posted on

No More DDL Headaches: Use Skapi’s Zero-Setup Relational Database API

Databases are the lifeblood of modern services. From user accounts to transaction records, being able to save and retrieve relational data is what makes dynamic applications possible.

But writing DDL scripts is not for everyone. Personally, we’ve never liked to write SQL schemas and define constraints, mapping classes, matching data types, managing NOT NULLs and wiring foreign keys to build a functional web of related data.

And that’s where Skapi comes in. We provide, amongst other tools, a high-performance database, ready to use and with no setup required. Just connect your project to your Skapi service by following this step-by-step guide and in 5 minutes you’ll be ready to use our powerful database and much more.

In this article, we'll give you an overview of our database methods. How to create, fetch, update records and create tables.

To create a new record on Skapi’s database, you’ll need to use our postRecord() method. You can do this through the onsubmit function on a <form> tag since Skapi is able to process FormData directly, or if you prefer a little more control of your object’s data structure, you can call the function from javascript.

For HTML forms:


<form onsubmit="skapi.postRecord(event, { table: { name: 'my_collection', access_group: 'public' } })">
    <input name="something" placeholder="Say something" value="Hello World" />
    <input type="submit" value="Submit" />
</form>

Enter fullscreen mode Exit fullscreen mode

For Javascript:


// Data to be saved in key:value pairs
let data = {
    something: "Hello World"
}

// Configuration for the record to be uploaded
let config = {
    table: { name: 'my_collection', access_group: 'public' }
}

skapi.postRecord(data, config);

Enter fullscreen mode Exit fullscreen mode

You’ll notice that in both situations there are two main objects to be sent. The data and the config. The data is self-explanatory, it’s whatever you need to save to the database, files included. But the config is where you start taking advantage of the multitude of features from our database.

Let’s start with the most basic, the table config.

This is where you set a table for your database, but you’ll notice that you did not have to write any SQL or match datatypes. That’s right, Skapi automatically creates new tables for you.

When you post a new record, it automatically looks at the table config and searches the database for a matching table and inserts the record. But if none exists, it creates a new one ready to store your data.
Let’s take a look at how to do it:

In the previous example, we created a record with the table config name: ‘my collection’


let data = {
    something: "Hello World"
}

let config = {
    table: { name: 'my_collection', access_group: 'public' }
}

skapi.postRecord(data, config);

Enter fullscreen mode Exit fullscreen mode

Now we add a second record to the same table like so:


let data = {
    something: "This is the new data"
}

let config = {
    table: { name: 'my_collection', access_group: 'public' }
}

skapi.postRecord(data, config);

Enter fullscreen mode Exit fullscreen mode

Skapi will automatically detect that the table exists and add the new data to the same one, allowing you to fetch data by filtering by table if you desire.
And to talk about fetching data, let us explore how to do it with the getRecords() method.


let query = {
    table: { name: 'my_collection',
    access_group: 'public'
}

skapi.getRecords(query);

Enter fullscreen mode Exit fullscreen mode

This would return a response with a list object containing the RecordData of all public objects in the ‘my_collection’ table.

Now, RecordData includes all the data you set, as well as some other important properties set by Skapi automatically, like the property record_id. With it, you are able to reference a single record directly, something essential to the function of every application. Here is how to fetch the data from a single record using record_id: 1234:


let query = {
    record_id: 1234
}

skapi.getRecords(query);

Enter fullscreen mode Exit fullscreen mode

This returns to you a response with a list with a single Json object containing the data of the selected record.

Another important function that you need the record_id for is updating an existing record. The way to do this is very simple, just use the postRecord()function, but add the record_id string to the config object. Let me show you how to do it for our record with the record_id: 1234 from before.


let data = {
    something: "New data"
}

let config = {
    record_id: 1234;
    table: { name: 'my_collection', access_group: 'public' }
}

skapi.postRecord(data, config);

Enter fullscreen mode Exit fullscreen mode

And we’re done, your record is updated with the new data.

As you can see, Skapi is very easy to use. You’re able to start using our database right away without the massive overhead of building a custom schema and a back-end API.

Follow us on socials: X, YouTube, Instagram, LinkedIn. More tutorials, code drops, and dev hacks are coming soon.

Top comments (0)