DEV Community

Cover image for "Soul", SQLite REST and realtime server is now extendable.
Vahid Al
Vahid Al

Posted on

"Soul", SQLite REST and realtime server is now extendable.

Hi Folks,
For those who are not familiar with Soul it's a REST and realtime server as the title says, basically it takes an SQLite database file and gives you a set of CRUD APIs to interact with your db and also a websocket to subscribe to its changes. All the feature's up to here is automatic and you do nothing.

But what if you wanted to create custom APIs to serve custom needs?
Now that's available thanks to first member of Soul's Extensions, "API Extensions." With an syntax similar to Express.js, you can add new custom APIs to own your backend!

Let's see how it works with a real example:

# Download a sample database
wget https://raw.githubusercontent.com/lerocha/chinook-database/master/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite

# Install Soul
npm install -g soul-cli
Enter fullscreen mode Exit fullscreen mode

Ok let's create a folder for out extensions:

# Create a folder named _extensions
mkdir _extensions
cd _extensions

# make a file named api.js
touch api.js
Enter fullscreen mode Exit fullscreen mode

Now copy and pase the following code inside api.js

const hello = {
  method: 'GET',
  path: '/api/hello-soul',
  handler: (req, res, db) => {
    res.status(200).json({ 
        message: 'Hello Soul!'
    });
  },
};


module.exports = {
  hello,
};
Enter fullscreen mode Exit fullscreen mode

Now let's run Soul with our extension

# And run it
soul -d ./Chinook_Sqlite.sqlite -p 8000 -e "/path/to/_extensions/"
Enter fullscreen mode Exit fullscreen mode

Ok, let's test it now

curl http://localhost:8000/api/hello-soul
Enter fullscreen mode Exit fullscreen mode

It should return

{
  "message": "Hello Soul!"
}
Enter fullscreen mode Exit fullscreen mode

Awesome you've just created your first custom API inside Soul!

For more examples checkout here:
https://github.com/thevahidal/soul/blob/main/docs/extensions-examples.md

Soon we'll be tackling new extensions such as middlewares and callbacks. So make sure to watch Soul's project.

Here's the link to Soul repo: https://github.com/thevahidal/soul

Let me know what you think about this new feature, And I'll see you in the next one.

Top comments (0)