Skip to content

Commit 2485aa3

Browse files
authored
Merge pull request #8 from BoolJS/develop
Version bump
2 parents c7bf6ca + c96bf6e commit 2485aa3

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

index.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

33
const Sequelize = require('sequelize');
4-
const EmptyModel = require('./model');
4+
const MySQLModel = require('./model');
5+
const { Schema } = MySQLModel;
56

67
const { DatabaseLoader } = require('@booljs/api');
78
const { readFileSync } = require('fs');
@@ -54,16 +55,42 @@ module.exports = class BoolJSMySQL extends DatabaseLoader {
5455
const args = [ null, app, { connection, Sequelize, models } ];
5556

5657
const SchemaClass = Function.prototype.bind.apply(Component, args);
57-
const schemaInstance = new SchemaClass();
58-
const schema = schemaInstance.__schema;
5958

59+
const {
60+
[Schema]: schema,
61+
...instanceProps
62+
} = new SchemaClass();
63+
64+
Object.assign(schema.prototype, instanceProps);
6065
models[name] = schema;
6166

6267
if (Component.associations !== undefined &&
6368
typeof Component.associations === 'function') {
6469
this.__associations.push(Component.associations);
6570
}
6671

72+
const readOnly = [ 'constructor', 'name', 'length', 'prototype' ];
73+
74+
const staticsKeys = Object
75+
.getOwnPropertyNames(Component)
76+
.filter(key => Component[key] !== Component.associations &&
77+
!readOnly.includes(key));
78+
79+
for (let key of staticsKeys) {
80+
schema[key] = Component[key].apply(schema);
81+
}
82+
83+
const methodsKeys = Object
84+
.getOwnPropertyNames(Component.prototype)
85+
.filter(key => typeof Component.prototype[key] === 'function' &&
86+
!readOnly.includes(key));
87+
88+
for (let key of methodsKeys) {
89+
schema.prototype[key] = function (...args) {
90+
return Component.prototype[key].apply(this, args);
91+
};
92+
}
93+
6794
return schema;
6895
}
6996

@@ -74,7 +101,7 @@ module.exports = class BoolJSMySQL extends DatabaseLoader {
74101
}
75102

76103
modelClass () {
77-
return EmptyModel;
104+
return MySQLModel;
78105
}
79106

80107
modelTemplate () {

model.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
const { DatabaseModel } = require('@booljs/api');
44

5+
const Schema = Symbol('schema');
6+
57
module.exports = class SequelizeModel extends DatabaseModel {
8+
static get Schema () {
9+
return Schema;
10+
}
11+
612
constructor (schema) {
713
super();
8-
this.__schema = schema;
14+
this[Schema] = schema;
915
}
1016
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@booljs/sequelize",
3-
"version": "0.1.1",
3+
"version": "0.2.0",
44
"description": "Sequelize driver for BoolJS",
55
"main": "index.js",
66
"license": "GPL-3.0",

0 commit comments

Comments
 (0)