0
On Node, I created the following template to represent my user:
"use strict";
var Sequelize = require('sequelize');
module.exports = function() {
return sequelize.define('user', {
ID_USER: {
type: Sequelize.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
Name: {
type: Sequelize.STRING,
field: 'NAME',
allowNull : true
},
Email: {
type: Sequelize.STRING,
field: 'EMAIL',
allowNull : true
},
Photo: {
type: Sequelize.STRING,
field: 'PHOTO',
allowNull : true
},
Password: {
type: Sequelize.STRING,
field: 'PASSWORD',
allowNull : true
},
Country: {
type: Sequelize.STRING,
allowNull: true,
field: 'COUNTRY',
},
State: {
type: Sequelize.STRING,
allowNull: true,
field: 'STATE',
},
City: {
type: Sequelize.STRING,
allowNull: true,
field: 'CITY',
},
AddDate: {
type: Sequelize.DATE,
allowNull: true,
defaultValue: Sequelize.NOW,
field: 'ADD_DATE'
}
}, {
createdAt: false,
updatedAt: false,
tableName: 'user'
})};
And in my main application I created the following code:
var User = require('./User');
var Sequelize = require('sequelize')
, sequelize = new Sequelize('uri', 'root', 'tibia+_.0017', {
dialect: "mariadb",
port: 3306,
});
sequelize
.authenticate()
.then(function(err) {
console.log('Connection has been established successfully.');
}, function (err) {
console.log('Unable to connect to the database:', err);
});
var bruno = new User({
Name: 'Bruno',
Email: '[email protected]',
Photo : 'teste',
Password: 'cliente1234',
Country: 'Brazil',
State: 'SP',
City: 'São Paulo'});
bruno.create({
}).then(function(user) {
res.json(user);
});
When I run the main file you have the following error:
sequelize is not defined
My question is beyond the error, is how do I separate the project into several layers, because in my project I still can’t remove the connection to the model. My idea would be to have the following files:
Connection file. File that represents the model of the object (here I will have several files) File running the CRUD.
It would basically be a DDD structure
The part : Return authenticatedSequelize.defines( tarafrog ) Is the object passed by parameter ? The configureDatabase gave error "Cannot read Property 'define'", so I didn’t quite understand being that I called the sequelize
– Bruno Jesus Santos
Replaces the tarafrog by the actual example copying from the example above, to see if it becomes clearer. I also described literally the content of
configureDatabase.js
to avoid this mistake.– Rad Aragón