MCPcopy
hub / github.com/sequelize/sequelize / init

Method init

lib/model.js:928–1080  ·  lib/model.js::Model.init

* 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 = {})

Source from the content-addressed store, hash-verified

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

Callers 12

sscce.jsFile · 0.80
model.tsFile · 0.80
upsert.tsFile · 0.80
hooks.tsFile · 0.80
validators.tsFile · 0.80
User.tsFile · 0.80
UserGroup.tsFile · 0.80
docs-example.tsFile · 0.80
ModelInit.tsFile · 0.80
defineMethod · 0.80
findOne.test.jsFile · 0.80

Calls 14

getTableNameMethod · 0.95
_conformIndexMethod · 0.95
_addDefaultAttributesMethod · 0.95
refreshAttributesMethod · 0.95
pluralizeMethod · 0.80
singularizeMethod · 0.80
isDefinedMethod · 0.80
removeModelMethod · 0.80
getModelMethod · 0.80
addModelMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected