I am trying to outsource my models for the dbInit
function from my dbController because I have several models which makes the dbController to big.
So I am calling initDb
from my db_controller.js
which looks like this (I use that docu http://docs.sequelizejs.com/manual/getting-started.html )
const userModel = require('../model/user')
const subjectModel = require('../model/subject')
const Sequelize = require('sequelize')
const seq = new Sequelize({
dialect: 'sqlite',
storage: './user.db'
})
async function initDb () {
await userModel.user.initUser()
await subjectModel.subject.initSubject()
userModel.user.userClass.hasMany(subjectModel.subject.subjectClass)
}
The user in the user.js
looks like this:
const Sequelize = require('sequelize')
const seq = new Sequelize({
dialect: 'sqlite',
storage: './user.db'
})
class User extends Sequelize.Model {
}
exports.user = {
initUser: initUser,
userClass: User
}
async function initUser () {
return new Promise(resolve => {
User.init(
// attributes
{
firstName: {
type: Sequelize.STRING,
allowNull: false
},
lastName: {
type: Sequelize.STRING,
allowNull: false
},
email: {
type: Sequelize.STRING,
allowNull: false
}
},
// options
{
seq,
modelName: 'user'
}
)
resolve()
})
}
and pretty much the same for the subject.js
const Sequelize = require('sequelize')
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: './user.db'
})
class Subject extends Sequelize.Model {
}
exports.subject = {
initSubject: initSubject,
subjectClass: Subject
}
async function initSubject () {
return new Promise(resolve => {
Subject.init(
// attributes
{
name: {
type: Sequelize.STRING,
allowNull: false
}
},
// options
{
seq: sequelize,
modelName: 'subject'
}
)
resolve()
})
}
So when I try to execute this via node db_controller.js
I receive this error (shortened)
(node:12444) UnhandledPromiseRejectionWarning: Error: No Sequelize instance passed
at Function.init (D:\Git\ppb\node_modules\sequelize\lib\model.js:915:13)
at resolve (D:\Git\ppb\src\model\user.js:26:10)
at new Promise (<anonymous>)
at Object.initUser (D:\Git\ppb\src\model\user.js:25:10)
at initDb (D:\Git\ppb\src\controller\db_controller.js:18:24)
at Object.<anonymous> (D:\Git\ppb\src\controller\db_controller.js:45:1)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
Thank you very much for any advice in advance!
Instances are the individual results from a Sequelize call. In SequelizeMock, these objects have most of the same attributes as in the normal Sequelize Instance, however they also have a few additional bits of functionality.
To insert new rows into your SQL table, you need to first create a Sequelize Model object. In Sequelize, a Model is an object that represents your SQL table, giving information to Sequelize about the name, the columns, and their data types.
Sequelize set up Install Sequelize database driver for the database you would like to use by running one of the commands below. Install npm package Sequelize-CLI. Create a project folder. In your project folder path, run the command below to initialize Sequelize in the folder.
By default, Sequelize will assume the table uses an id column as its primary key. Now you can call destroy() from the Invoice model to delete a row by its id value: const count = await Invoice. destroy({ where: { id: 2 } }); console.
I thought it was peculiar that you are passing seq
instead of sequelize
in the options.
Checking the docs here http://docs.sequelizejs.com/ I see in the example that they pass the property sequelize
and not seq
.
So I recommend changing:
{
seq: sequelize,
modelName: 'subject'
}
to
{
sequelize: sequelize,
modelName: 'subject'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With