Skip to content

Commit

Permalink
fix: return extended class definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
pateketrueke committed Jan 14, 2021
1 parent e4a4ac2 commit 72b1181
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const AUTOLOAD = {
'instance.js': 'instanceMethods',
};

function makeModel() {
return class extends Sequelize.Model {};
}

function JSONSchemaSequelizer(settings, refs, cwd) {
if (typeof settings === 'string') {
settings = {
Expand Down Expand Up @@ -145,16 +149,20 @@ function JSONSchemaSequelizer(settings, refs, cwd) {
});

// append model from settings
this.add = model => {
this.add = (model, _class) => {
/* istanbul ignore else */
if (!(model.$schema && model.$schema.id)) {
throw new Error(`Missing $schema and/or id, given '${_util.inspect(model)}'`);
}

if (!_defns[model.$schema.id]) {
_defns[model.$schema.id] = model;
_defns[model.$schema.id].$class = class extends Sequelize.Model {};
_defns[model.$schema.id].$class = makeModel();
} else {
if (!_defns[model.$schema.id].$class) {
_defns[model.$schema.id].$class = makeModel();
}

Object.keys(model).forEach(key => {
/* istanbul ignore else */
if (!_defns[model.$schema.id][key]) {
Expand All @@ -165,7 +173,7 @@ function JSONSchemaSequelizer(settings, refs, cwd) {
});
}

return this;
return _class ? _defns[model.$schema.id].$class : this;
};

// append model from filesystem
Expand Down

0 comments on commit 72b1181

Please sign in to comment.