utils.js
2.29 KB
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
/**
* Utility methods to modify and customize auto generated models without affecting further auto generations.
* Require this module from model customization scripts located in path/to/model/definitions-files-custom directory.
* @class
* @author Özüm Eldoğan
*/
/*jslint node: true */
"use strict";
/**
* @constructor
* @param model
*/
var GeneratorUtil = function (model) {
this.model = model;
};
/**
* Searches and returns relation with the given alias. Alias is defined in sequelize options with parameter 'as'
* @method
* @param {string} as - Alias of the relation.
* @returns {Object}
*/
GeneratorUtil.prototype.getRelation = function (as) {
var i,
relations = this.model.relations;
for (i = 0; i < relations.length; i = i + 1) {
if (relations[i].details.as === as) {
return relations[i];
}
}
};
/**
* Searches and returns attribute with the given alias. Alias is defined in sequelize options with parameter 'as'
* @method
* @param {string} name - Name of the attribute.
* @returns {Object}
*/
GeneratorUtil.prototype.getAttribute = function (name) {
return this.model.attributes[name];
};
/**
* Searches and returns attribute with the given alias. Alias is defined in sequelize options with parameter 'as'
* @method
* @param {string} oldName - Name of the attribute which it's name to be changed.
* @param {string} newName - New name of the attribute.
* @throws Will throw error if there is already an attribute with new name exists or attribute with oldName does not exists.
*/
GeneratorUtil.prototype.renameAttribute = function (oldName, newName) {
if (this.model.attributes[newName] !== undefined) {
throw new Error('There is already an attribute with the same name ("' + newName + '") in table "' + this.model.modelName + '".');
}
if (this.model.attributes[oldName] === undefined) {
throw new Error('There is no attribute with the name "' + oldName + '". in table "' + this.model.modelName + '".');
}
this.model.attributes[newName] = this.model.attributes[oldName];
delete this.model.attributes[oldName];
};
/**
* Creates and returns new object with utility functions.
* @param model
* @returns {GeneratorUtil}
*/
module.exports = function (model) {
return new GeneratorUtil(model);
};