Using the MariaDB Node.js driver with Sequelize

spacer

Since releasing the MariaDB Node.js connector we’ve seen quite a bit of interest and positive feedback from the community on being able to access and communicate with MariaDB using JavaScript. It’s probably not news to you at this point, but JavaScript is a popular programming language.

Over the past several years we, as an industry, have seen the demand for JavaScript based solutions continue to grow. Gone are the days of using it for some simple client side scripting. Instead it’s evolved into a truly full-stack medium for being able to solve all kinds of modern software problems.

Object Relational Mapping with Sequelize

At the heart of this, like most things, are developers and their ability to innovate. Node.js has helped enable developers to easily share their innovation through the use of packages. These JavaScript packages, or modules, provide a self-contained library that can be used in Node.js and many other types of JavaScript projects.

And with all the vast potential that exists within the Node.js ecosystem it got me thinking, was there a package that could be used with the MariaDB’s Node.js connector to make my life easier?

Enter Sequelize, a Node.js object-relational mapping (ORM) library capable of working with a variety of relational databases, including MariaDB. Now, if you’re unfamiliar with ORM libraries, the gist, at the highest level, is that they map object syntax to database schemas.

For instance, consider the following Task model.

const Task = sequelize.define("task", {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true
    },
    description: {
      type: Sequelize.STRING
    },
    completed: {
      type: Sequelize.BOOLEAN
    }
  });
  return Task;
};

In this example Sequelize maps the Task model to a tasks table that exists within the database. Then it can be used to simplify things like data creation.

const jane = await Task.create({ description: "Task 1" });

And data retrieval.

const tasks = await Task.findAll();

Fascinating, right? Well, that’s only the tip of the iceberg as Sequelize is chock-full of features like transactional support, model relationship management, eager and lazy loading, model validations and more.

All of which is why I’m excited to introduce a new Node.js project that uses Sequelize and MariaDB Connector/Node.js to connect to and communicate with MariaDB databases. I’ve created a simple task tracking application that demonstrates how to setup, configure and execute basic CRUD (create-read-update-delete) operations using Sequelize with a MariaDB database.

Learn more

If you’d like to learn even more about what’s possible with MariaDB, be sure to check out the Developer Hub and our new Developer Code Central GitHub organization. There you can find much more content just like this spanning a variety of technologies, use cases and programming languages, including a new repository that will guide you through the process of quickly getting started with MariaDB, Connector/Node.js and Sequelize.

You can also dive even deeper into MariaDB capabilities in the official documentation.

And, as always, we’d be nothing without our awesome community! If you’d like to help contribute you can find us on GitHub, send feedback directly to us at developers@mariadb.com or join the conversation in the new MariaDB Community Slack!