UNPKG

3.2 kBJavaScriptView Raw
1/*
2
3 This file is a part of node-on-train project.
4
5 Copyright (C) Thanh D. Dang <thanhdd.it@gmail.com>
6
7 node-on-train is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 node-on-train is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20*/
21
22
23var fs = require('fs');
24var path = require('path');
25var inflection = require('inflection');
26var root_app = process.cwd();
27var migrationTime = require('../lib/helpers/migration_time.js');
28var train_generate = require('./train_generate.js');
29
30module.exports = function() {
31 var model_name = process.argv[4];
32 var model = model_name.toLowerCase();
33 var model_plural = inflection.pluralize(model);
34 var migration_file_name = migrationTime() + '_create_' + model_plural;
35
36 var model_attrs = "";
37 var migration_attrs = "";
38 var require_modules = "";
39
40 for (var i = 5; i < process.argv.length; i++) {
41 var attr_str = process.argv[i].split(':');
42 var attr_name = attr_str[0].toLowerCase();
43 var db_type = attr_str[1].toUpperCase();
44 if (db_type == 'REFERENCES') {
45 // model.js
46 model_attrs += '\t' + attr_name + '_id: {\n';
47 model_attrs += '\t\ttype: Sequelize.INTEGER,\n';
48 model_attrs += '\t\treferences: {\n';
49 model_attrs += "\t\t\tmodel: '"+attr_name+"',\n";
50 model_attrs += "\t\t\tkey: 'id'\n";
51 model_attrs += '\t\t}\n';
52 model_attrs += '\t},\n';
53
54 // migration
55 migration_attrs += "\t\t\t" + attr_name + "_id: {\n";
56 migration_attrs += "\t\t\t\ttype: DataTypes.INTEGER,\n";
57 migration_attrs += "\t\t\t\treferences: {\n";
58 migration_attrs += "\t\t\t\t\tmodel: '"+attr_name+"',\n";
59 migration_attrs += "\t\t\t\t\tkey: 'id',\n";
60 migration_attrs += "\t\t\t\t}\n";
61 migration_attrs += "\t\t\t},\n";
62 } else {
63 // model.js
64 model_attrs += '\t' + attr_name + ': {\n';
65 model_attrs += '\t\ttype: Sequelize.' + db_type + ',\n';
66 model_attrs += '\t},\n';
67
68 // migration
69 migration_attrs += "\t\t\t" + attr_name + ": DataTypes." + db_type + ",\n";
70 }
71 }
72
73 var file_templates = {
74 'app/models/model.js': [
75 {
76 file_path: 'app/models/' + model + '.js',
77 info_render: {
78 model_name: model_name,
79 model_attrs: model_attrs,
80 model: model,
81 require_modules: require_modules
82 }
83 }
84 ],
85 'db/migrate/migration.js': [
86 {
87 file_path: 'db/migrate/' + migration_file_name + '.js',
88 info_render: {
89 model: model,
90 migration_attrs: migration_attrs
91 }
92 }
93 ],
94 'test/models/model_test.js': [
95 {
96 file_path: 'test/models/' + model + '_test.js',
97 info_render: {
98 model_name: model_name
99 }
100 }
101 ],
102 }
103
104 var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../');
105 var path_templ = lib + 'template/model';
106
107 train_generate(path_templ, null, file_templates);
108}