config/database.js
file.
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.
|
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}});
express-sweet/database/Model
.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
};
}
}
config/database.js
file.