* Initialize a model, representing a table in the DB, with attributes and options. * * The table columns are defined by the hash that is given as the first argument. * Each attribute of the hash represents a column. * * @example * Project.init({ * columnA: { * type: Seq
(attributes, options = {})
| 926 | * @returns {Model} |
| 927 | */ |
| 928 | static init(attributes, options = {}) { |
| 929 | if (!options.sequelize) { |
| 930 | throw new Error(class="st">'No Sequelize instance passed'); |
| 931 | } |
| 932 | |
| 933 | this.sequelize = options.sequelize; |
| 934 | |
| 935 | const globalOptions = this.sequelize.options; |
| 936 | |
| 937 | options = Utils.merge(_.cloneDeep(globalOptions.define), options); |
| 938 | |
| 939 | if (!options.modelName) { |
| 940 | options.modelName = this.name; |
| 941 | } |
| 942 | |
| 943 | options = Utils.merge({ |
| 944 | name: { |
| 945 | plural: Utils.pluralize(options.modelName), |
| 946 | singular: Utils.singularize(options.modelName) |
| 947 | }, |
| 948 | indexes: [], |
| 949 | omitNull: globalOptions.omitNull, |
| 950 | schema: globalOptions.schema |
| 951 | }, options); |
| 952 | |
| 953 | this.sequelize.runHooks(class="st">'beforeDefine', attributes, options); |
| 954 | |
| 955 | if (options.modelName !== this.name) { |
| 956 | Object.defineProperty(this, class="st">'name', { value: options.modelName }); |
| 957 | } |
| 958 | delete options.modelName; |
| 959 | |
| 960 | this.options = { |
| 961 | timestamps: true, |
| 962 | validate: {}, |
| 963 | freezeTableName: false, |
| 964 | underscored: false, |
| 965 | paranoid: false, |
| 966 | rejectOnEmpty: false, |
| 967 | whereCollection: null, |
| 968 | schema: null, |
| 969 | schemaDelimiter: class="st">'', |
| 970 | defaultScope: {}, |
| 971 | scopes: {}, |
| 972 | indexes: [], |
| 973 | ...options |
| 974 | }; |
| 975 | |
| 976 | class="cm">// if you call class="st">"define" multiple times for the same modelName, do not clutter the factory |
| 977 | if (this.sequelize.isDefined(this.name)) { |
| 978 | this.sequelize.modelManager.removeModel(this.sequelize.modelManager.getModel(this.name)); |
| 979 | } |
| 980 | |
| 981 | this.associations = {}; |
| 982 | this._setupHooks(options.hooks); |
| 983 | |
| 984 | this.underscored = this.options.underscored; |
| 985 |
no test coverage detected