Skip to content
This repository was archived by the owner on Aug 17, 2022. It is now read-only.

Commit 1e084b2

Browse files
authored
Merge pull request #31 from flexxnn/new-fixes
2 parents 2af12c3 + 5a32bac commit 1e084b2

File tree

15 files changed

+1773
-132
lines changed

15 files changed

+1773
-132
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ jspm_packages
3535

3636
# Optional REPL history
3737
.node_repl_history
38+
39+
*.sqlite

bin/makemigration.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ if (options.help)
3232
process.exit(0);
3333
}
3434

35-
let {migrationsDir, modelsDir} = pathConfig(options);
35+
if(!process.env.PWD){
36+
process.env.PWD = process.cwd()
37+
}
38+
39+
const {
40+
migrationsDir,
41+
modelsDir
42+
} = pathConfig(options);
3643

3744
// current state
3845
const currentState = {

bin/runmigration.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ const optionDefinitions = [
2020

2121
const options = commandLineArgs(optionDefinitions);
2222

23-
let {migrationsDir, modelsDir} = pathConfig(options);
23+
if(!process.env.PWD){
24+
process.env.PWD = process.cwd()
25+
}
26+
27+
let {
28+
migrationsDir,
29+
modelsDir
30+
} = pathConfig(options);
2431

2532
if (options.help)
2633
{

example/config/config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"development": {
3+
"storage": "database.sqlite",
4+
"dialect": "sqlite"
5+
}
6+
}

example/models/account.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* eslint-disable key-spacing, no-multi-spaces */
2+
3+
module.exports = function (DB, { INTEGER, BIGINT, DATE, STRING, ENUM, BOOLEAN, DATEONLY, NOW }) {
4+
const Model = DB.define('account',
5+
{
6+
id: { type: INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
7+
test_param: { type: BIGINT, allowNull: false, defaultValue: 1000 },
8+
first_name: { type: STRING, allowNull: true, defaultValue: 'abc', field: 'first-name' },
9+
last_name: { type: STRING, allowNull: false, defaultValue: '' },
10+
nickname: { type: STRING, allowNull: false, defaultValue: '' },
11+
gender: { type: ENUM, allowNull: false, values: ['male', 'female', 'unknown'], defaultValue: 'unknown' },
12+
birth_date: { type: DATEONLY, allowNull: true },
13+
last_login_dt: { type: DATE, allowNull: true },
14+
created_at: { type: DATE, allowNull: true, defaultValue: NOW },
15+
email: { type: STRING, allowNull: false },
16+
password: { type: STRING, allowNull: false },
17+
is_deleted: { type: BOOLEAN, allowNull: false, defaultValue: false },
18+
is_blocked: { type: BOOLEAN, allowNull: false, defaultValue: false },
19+
// city_id -> city.id
20+
},
21+
{
22+
timestamps: false,
23+
underscored: true,
24+
tableName: 'account'
25+
}
26+
)
27+
28+
Model.associate = (models) => {
29+
Model.belongsTo(models.city)
30+
// Model.hasMany(models.team, { foreignKey: { allowNull: false } })
31+
}
32+
33+
return Model
34+
}

example/models/city.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* eslint-disable key-spacing, no-multi-spaces */
2+
3+
module.exports = function (DB, { INTEGER, STRING, BOOLEAN }) {
4+
const Model = DB.define('city',
5+
{
6+
id: { type: INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
7+
title: { type: STRING, allowNull: false },
8+
display: { type: BOOLEAN, allowNull: false, defaultValue: true }
9+
// country_id -> country.id
10+
},
11+
{
12+
timestamps: false,
13+
underscored: true,
14+
tableName: 'city',
15+
indexes: [
16+
{ fields: [ 'country_id' ] },
17+
{ fields: [ 'title' ] }
18+
]
19+
}
20+
)
21+
22+
Model.associate = (models) => {
23+
Model.hasMany(models.account, { foreignKey: { allowNull: false } })
24+
Model.belongsTo(models.country)
25+
}
26+
27+
return Model
28+
}

example/models/country.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* eslint-disable key-spacing, no-multi-spaces */
2+
3+
module.exports = function (DB, { INTEGER, STRING, BOOLEAN }) {
4+
const Model = DB.define('country',
5+
{
6+
id: { type: INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
7+
title: { type: STRING, allowNull: false },
8+
display: { type: BOOLEAN, allowNull: false, defaultValue: true }
9+
},
10+
{
11+
timestamps: false,
12+
underscored: true,
13+
tableName: 'country',
14+
indexes: [
15+
{ fields: [ 'title' ] },
16+
{ fields: [ 'display' ] }
17+
]
18+
}
19+
)
20+
21+
Model.associate = (models) => {
22+
Model.hasMany(models.city, { foreignKey: { allowNull: false } })
23+
}
24+
25+
return Model
26+
}

example/models/geo.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* eslint-disable key-spacing, no-multi-spaces */
2+
3+
module.exports = function (DB, { INTEGER, GEOMETRY, GEOGRAPHY }) {
4+
const Model = DB.define('geo',
5+
{
6+
id: { type: INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
7+
geometry_1: { type: GEOMETRY, allowNull: false },
8+
geometry_2: { type: GEOMETRY('POINT'), allowNull: false },
9+
geometry_3: { type: GEOMETRY('POINT', 4326), allowNull: false },
10+
}
11+
)
12+
13+
return Model
14+
}
15+

example/models/index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const Sequelize = require('sequelize');
6+
const basename = path.basename(__filename);
7+
const env = process.env.NODE_ENV || 'development';
8+
const config = require(__dirname + '/../config/config.json')[env];
9+
const db = {};
10+
11+
let sequelize;
12+
if (config.use_env_variable) {
13+
sequelize = new Sequelize(process.env[config.use_env_variable], config);
14+
} else {
15+
sequelize = new Sequelize(config.database, config.username, config.password, config);
16+
}
17+
18+
fs
19+
.readdirSync(__dirname)
20+
.filter(file => {
21+
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
22+
})
23+
.forEach(file => {
24+
const model = sequelize['import'](path.join(__dirname, file));
25+
db[model.name] = model;
26+
});
27+
28+
Object.keys(db).forEach(modelName => {
29+
if (db[modelName].associate) {
30+
db[modelName].associate(db);
31+
}
32+
});
33+
34+
db.sequelize = sequelize;
35+
db.Sequelize = Sequelize;
36+
37+
module.exports = db;

example/models/purchaseProducts.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* eslint-disable key-spacing, no-multi-spaces */
2+
3+
// @fix: https://github.com/flexxnn/sequelize-auto-migrations/issues/21
4+
5+
module.exports = function (DB, { INTEGER, DECIMAL }) {
6+
const purchaseProducts = DB.define('purchaseProducts', {
7+
id: {
8+
type: INTEGER.UNSIGNED,
9+
allowNull: false,
10+
autoIncrement: true,
11+
primaryKey: true,
12+
},
13+
price: {
14+
type: DECIMAL(6, 2),
15+
allowNull: false,
16+
},
17+
});
18+
19+
return purchaseProducts;
20+
}
21+

0 commit comments

Comments
 (0)