Table of contents
No headings in the article.
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 the first member of Soul's Extensions, "API Extensions." With a 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
Ok let's create a folder for our extensions:
# Create a folder named _extensions
mkdir _extensions
cd _extensions
# make a file named api.js
touch api.js
Now copy and paste 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,
};
Now let's run Soul with our extension
# And run it
soul -d ./Chinook_Sqlite.sqlite -p 8000 -e "/path/to/_extensions/"
Ok, let's test it now
curl http://localhost:8000/api/hello-soul
It should return
{
"message": "Hello Soul!"
}
Awesome you've just created your first custom API inside Soul!
For more examples check out here: github.com/thevahidal/soul/blob/main/docs/e..
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 the Soul repo: github.com/thevahidal/soul
Let me know what you think about this new feature, And I'll see you in the next one.