forked from expo-js/expo-sequelize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
190 lines (159 loc) · 4.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var fs = require('fs');
var path = require('path');
var extend = require('util')._extend;
var ExpoSequelize = module.exports = function(app) {
app.on('cli', function(app, cli) {
cli
.command('db-create')
.description("Creates the environment's database")
.action(function() {
app.load();
databaseSetup(app, function(dbname, dialect) {
app.sequelize()
.query('CREATE DATABASE ' + quotify(dbname, dialect) + ';')
.success(function() {
app.log.info("[db] Database '%s' created", dbname);
})
.error(function(err) {
app.log.error("[db] Database '%s' failed to be created", dbname);
app.log.error("[db] => %s", err.message);
process.exit(1);
});
});
});
cli
.command('db-drop')
.description("Drops the environment's database")
.action(function() {
app.load();
databaseSetup(app, function(dbname, dialect) {
app.sequelize()
.query('DROP DATABASE ' + quotify(dbname, dialect) + ';')
.success(function() {
app.log.info("[db] Database '%s' dropped", dbname);
})
.error(function(err) {
app.log.error("[db] Database '%s' failed to be dropped", dbname);
app.log.error("[db] => %s", err.message);
process.exit(1);
});
});
});
cli
.command('db-migrate')
.description("Run database migrations")
.action(function() {
app.load();
var options = { path: app.path('app/migrations') };
var migrator = app.sequelize().getMigrator(options, true);
migrator.migrate({ method: 'up' });
});
cli
.command('db-sync [force]')
.description("Performs Sequelize syncing")
.action(function(force) {
var isForce = (force === 'force');
app.load();
loadModels(app);
app.log.log("");
app.log.info(isForce ? "Force-syncing..." : "Syncing...");
app.sequelize().sync({force: isForce})
.then(function() {
app.log.info("Success");
}, function(e) {
app.log.error("failed");
app.log.error(e);
process.exit(1);
});
});
cli
.command('gen-migration [name]')
.description('Creates a migration file in migrations/')
.action(function(name) {
name = name || '';
var bin = path.resolve(__dirname, 'node_modules', 'sequelize', 'bin', 'sequelize');
var exec = require('child_process').exec;
var cmd = bin + " --create-migration " + name;
process.chdir(app.path('app'));
exec(cmd, function(cin, cout, cerr) {
console.log(cout); console.warn(cerr);
});
});
});
/**
* Loads sequelize.
*/
app.sequelize = function() {
if (!app._sequelize) {
var url = process.env.DATABASE_URL;
var conf;
if (url) {
conf = parseURL(url);
} else {
conf = app.conf('database');
}
app.events.emit('sequelize:before', app);
app._sequelize = getSequelizeFromConfig(app, conf);
app.events.emit('sequelize:after', app, app._sequelize);
}
return app._sequelize;
};
};
/**
* Returns a `Sequelize` instance from a given URL.
*/
function parseURL(url) {
var match = url.match(/([^:]+):\/\/([^:]*):([^@]*)@([^:]+):(\d+)\/(.+)/);
if (!match) throw "Wrong DATABASE_URL format";
return {
dialect: match[1],
username: match[2],
password: match[3],
host: match[4],
port: match[5],
database: match[6]
};
}
/**
* Returns a `Sequelize` instance from a given configuration hash.
*/
function getSequelizeFromConfig(app, conf) {
var options = extend({}, conf);
var isDev = (app.get('env') === 'development');
extend(options, {
logging: isDev ? (function(m) { app.log.debug("[sql] %s", m); }) : (function(){})
});
// Load custom config in 'sequelize options'.
var custom = app.get('sequelize options');
if (custom) extend(options, custom);
var Sequelize = require('sequelize');
return new Sequelize(conf.database, conf.username, conf.password, options);
}
/**
* Loads the app environment with no specific database.
* Used to create and drop databases.
*/
function databaseSetup(app, callback) {
var db = app.conf('database');
var dbname = db.database;
db.database = undefined;
if (!dbname) {
app.log.warn("[db] No database to work on");
process.exit(0);
}
var dialect = db.dialect;
callback(dbname, dialect);
}
function quotify(str, dialect) {
if (dialect === 'mysql') return "`"+str+"`";
return str;
}
function loadModels(app) {
app.log.info('Loading models:');
var mpath = app.path('lib/models');
var files = fs.readdirSync(mpath);
files.forEach(function(file) {
app.log.log(' %s', file);
require(path.join(mpath, file));
});
}