-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Sequelize CLI. Created migrations, moved models based on defaul…
…t used by Sequelize. Changed references. Added MODELS readme.
- Loading branch information
Showing
16 changed files
with
563 additions
and
106 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Models | ||
|
||
## Getting Started | ||
|
||
- Ensure that sequelize-cli is installed (should be already due to yarn install) | ||
``` | ||
yarn add sequelize-cli | ||
npx sequelize-cli init | ||
``` | ||
|
||
Creates a default project structure with a config, migrations, models and seeders directory. | ||
|
||
## Generating a model | ||
|
||
Generate the basic model. Associations will be done in a separate migration. | ||
|
||
``` | ||
npx sequelize-cli model:generate --name Owner --attributes firstName:string,lastName:string | ||
``` | ||
|
||
### Adding Associations | ||
|
||
Generate a migration for an association. | ||
|
||
``` | ||
npx sequelize-cli migration:generate --name add-some-assocation | ||
``` | ||
|
||
Add the up and down portions of the migration. Check the association documentation for more details. | ||
|
||
## Run Migrations | ||
|
||
1. Run all your migrations using `npx sequelize-cli db:migrate`. | ||
1. Revert a migration by using `npx sequelize-cli db:migrate:undo`. | ||
1. Revert all migrations by using `npx sequelize-cli db:migrate:undo:all` | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"development": { | ||
"username": "postgres", | ||
"password": null, | ||
"database": "postgres", | ||
"host": "127.0.0.1", | ||
"port": 5433, | ||
"dialect": "postgres" | ||
}, | ||
"test": { | ||
"username": "postgres", | ||
"password": null, | ||
"database": "postgres", | ||
"host": "127.0.0.1", | ||
"port": 5433, | ||
"dialect": "postgres" | ||
}, | ||
"production": { | ||
"username": "postgres", | ||
"password": null, | ||
"database": "postgres", | ||
"host": "127.0.0.1", | ||
"port": 5433, | ||
"dialect": "postgres" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Owners', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
firstName: { | ||
type: Sequelize.STRING | ||
}, | ||
lastName: { | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('Owners'); | ||
} | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Companies', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
chargesGST: { | ||
type: Sequelize.BOOLEAN | ||
}, | ||
chargesPST: { | ||
type: Sequelize.BOOLEAN | ||
}, | ||
code: { | ||
type: Sequelize.STRING | ||
}, | ||
name: { | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('Companies'); | ||
} | ||
}; |
26 changes: 26 additions & 0 deletions
26
migrations/20191029210549-add-owner-company-assoications.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.addColumn( | ||
'Companies', | ||
'OwnerId', | ||
{ | ||
type: Sequelize.INTEGER, | ||
references: { | ||
model: 'Owners', | ||
key: 'id' | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'SET NULL', | ||
} | ||
) | ||
}, | ||
|
||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.removeColumn( | ||
'Companies', | ||
'OwnerId' | ||
) | ||
} | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
module.exports = (sequelize, DataTypes) => { | ||
const Company = sequelize.define('Company', { | ||
chargesGST: DataTypes.BOOLEAN, | ||
chargesPST: DataTypes.BOOLEAN, | ||
code: DataTypes.STRING, | ||
name: DataTypes.STRING | ||
}, {}); | ||
Company.associate = function(models) { | ||
Company.belongsTo(models.Owner); | ||
}; | ||
return Company; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'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 = sequelize['import'](path.join(__dirname, file)); | ||
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; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
module.exports = (sequelize, DataTypes) => { | ||
const Owner = sequelize.define('Owner', { | ||
firstName: DataTypes.STRING, | ||
lastName: DataTypes.STRING | ||
}, {}); | ||
Owner.associate = function(models) { | ||
Owner.hasMany(models.Company); | ||
}; | ||
return Owner; | ||
}; |
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Oops, something went wrong.