-
-
Save ivanleoncz/92706f3281ac26510cdeddd82f5823ba to your computer and use it in GitHub Desktop.
Building SQLite models with Sequelize.js.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Sequelize = require('sequelize'); | |
var sequelize = new Sequelize({ | |
dialect:"sqlite", | |
storage:"./db.sqlite", | |
}); | |
// DB Connection | |
sequelize.authenticate().then( | |
function (err) {console.log("Connection established!");}, | |
function (err) {console.log("Unable to connect!", err);} | |
); | |
// Model Definition: Employees table | |
var Employees = sequelize.define('Employee', { | |
Id: { | |
type: Sequelize.INTEGER, | |
primaryKey: true, | |
autoincement: true, | |
}, | |
FirstName: Sequelize.STRING, | |
LastName: Sequelize.STRING, | |
BirthDate: Sequelize.DATE, | |
Role: Sequelize.STRING, | |
Degree: Sequelize.STRING, | |
DegreeYear: Sequelize.DATE, | |
StudyField: Sequelize.STRING, | |
School: Sequelize.STRING | |
}); | |
// Model Definition: Departments table | |
var Departments = sequelize.define('Department', { | |
Id: { | |
type: Sequelize.INTEGER, | |
primaryKey: true, | |
autoIncrement: true, | |
}, | |
Name: Sequelize.STRING, | |
}); | |
// Employees FK | |
Employees.hasMany(Departments); | |
Departments.belongsTo(Employees); | |
// Build Models | |
sequelize.sync({force:true}).then( | |
function (err) {console.log('Model successfully created!');}, | |
function (err) {console.log('Fail while creating model:', err);} | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"main": "index.js", | |
"dependencies": { | |
"sequelize": "^3.4.1", | |
"sqlite3": "^3.0.9" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment