Model

Models provide a way to interact with a specific table in your database.
Express Sweet provides a Sequelize based model class that offers some great features, including.
  • Automatic database connection.
  • Basic CRUD methods.
  • And more.
This class provides a solid base from which to build your own models, allowing you to rapidly build out your application’s model layer.

Configuration

The database configuration is defined in the config/database.js file.
Click here to download a sample ESM configuration and
here to download a sample CJS configuration.
Explanation of Values
Name Config Description
username: string The username which is used to authenticate against the database.
password: string|null The password which is used to authenticate against the database.
The default is no password (null).
database: string The name of the database.
host: string The host of the relational database.
port: number|null The port of the relational database.
The default is automatic selection (null).
dialect: string The dialect of the database you are connecting to.
One of mariadb, mysql, postgres, sqlite and mssql.
logging: boolean|(...message: any[]) => void Set true to output the executed query etc. to the log.
The default is no log output (false).
timezone Time zone dedicated to writing to the database.
For example, for the Japanese time zone, do the following.
timezone: '+09:00'

Accessing Model

Place the model in the "models" directory of the root directory.
When you load the model, you have immediate access to the model's functions for working with the database.
import BookModel from '../models/BookModel';

// INSERT INTO book (title) VALUES ('Beautiful')
await BookModel.create({title: 'Beautiful'});

// SELECT * FROM book
await BookModel.findAll();

// UPDATE book SET title = 'Beautiful' WHERE id= 1
await BookModel.update({title: 'Beautiful'}, {where: {id: 1}});

// DELETE FROM book WHERE id= 1
await BookModel.destroy({where: {id: 1}});
const BookModel = require('../models/BookModel');

// INSERT INTO book (title) VALUES ('Beautiful')
await BookModel.create({title: 'Beautiful'});

// SELECT * FROM book
await BookModel.findAll();

// UPDATE book SET title = 'Beautiful' WHERE id= 1
await BookModel.update({title: 'Beautiful'}, {where: {id: 1}});

// DELETE FROM book WHERE id= 1
await BookModel.destroy({where: {id: 1}});

Creating Model

To take advantage of Express Sweet’s model, you would simply create a new model class that extends express-sweet/database/Model.
This class provides convenient access to the database connection, the Query Builder, and a number of additional convenience methods.
For more information, see reference.
import * as sweet from 'express-sweet';

export default class extends sweet.database.Model {
  static get table() {
    return 'user';
  }

  static get attributes() {
    return {
      id: {
        type: this.DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      name: this.DataTypes.STRING,
      email: this.DataTypes.STRING,
      password: this.DataTypes.STRING,
      icon: this.DataTypes.STRING,
      created: this.DataTypes.DATE,
      modified: this.DataTypes.DATE
    };
  }
}
const Model = require('express-sweet').database.Model;

module.exports = class extends Model {
  static get table() {
    return 'user';
  }

  static get attributes() {
    return {
      id: {
        type: this.DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      name: this.DataTypes.STRING,
      email: this.DataTypes.STRING,
      password: this.DataTypes.STRING,
      icon: this.DataTypes.STRING,
      created: this.DataTypes.DATE,
      modified: this.DataTypes.DATE
    };
  }
}

Connect to Database

The database connection is automatic when the model is loaded.
The configuration related to the database connection is defined in the config/database.js file.
For more information on database configuration, see Database configuration.