Now let's see how to back our GraphQL server by a SQLite database and Sequelize ORM. Using an object-relational mapper library (ORM) seems to be a good choice for SQL data access, especially when you concerned about the speed of development, and the best part is that even with an ORM you can always fall back to writing raw SQL queries when needed.
You start by creating a data/sequelize.js
file that initializes a new instance of Sequelize client:
importSequelize from 'sequelize'; constsequelize = new Sequelize('sqlite:database.sqlite', { define: { freezeTableName: true } }); export default sequelize;
Now, create data models for each of the business entities of your app. For example, for a User
entity you may want to create a model similar to this (data/model/User.js
):
importDataType from 'sequelize'; import Model from '../sequelize'; const User = Model.define('User', { id: { type: DataType.INTEGER, primaryKey: true, autoIncrement...