Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

sequelize manual model/index.js

//Version of sequelize 6.4
'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
Comment

sequelize documentation

const { Sequelize, Model, DataTypes } = require('sequelize');
const sequelize = new Sequelize('sqlite::memory:');

class User extends Model {}
User.init({
  username: DataTypes.STRING,
  birthday: DataTypes.DATE
}, { sequelize, modelName: 'user' });

sequelize.sync()
  .then(() => User.create({
    username: 'janedoe',
    birthday: new Date(1980, 6, 20)
  }))
  .then(jane => {
    console.log(jane.toJSON());
  });
Comment

PREVIOUS NEXT
Code Example
Javascript :: process nexttick 
Javascript :: string length js 
Javascript :: from json timestamp to date python 
Javascript :: react multiple classnames 
Javascript :: jquery camera priview 
Javascript :: why bigint js 
Javascript :: ajax django send array 
Javascript :: nodejs sequelize find 
Javascript :: how to update state in react 
Javascript :: React social login button 
Javascript :: find multiples of a number 
Javascript :: hooks developed by react native 
Javascript :: js download 
Javascript :: Usage rate-limiter 
Javascript :: sort object with certain value at start of array js 
Javascript :: foreach in the elements with a data attibute jquery 
Javascript :: how to remove last character from string in javascript 
Javascript :: what does useref do react 
Javascript :: check if computer online js 
Javascript :: add kendo ui dropdown to angular 
Javascript :: react using button props child 
Javascript :: remove btn 
Javascript :: mock click function functinal component enzyme 
Javascript :: javascript create date object for midnight for a timezone 
Javascript :: load jquery in console 
Javascript :: javascript Strict Mode in Variable 
Javascript :: save data response from fetch as global param js 
Javascript :: webdriver-manager node known as a command 
Javascript :: normal function vs arrow function 
Javascript :: create a drop down to select time javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =