11'use strict' ;
22
33const Sequelize = require ( 'sequelize' ) ;
4- const EmptyModel = require ( './model' ) ;
4+ const MySQLModel = require ( './model' ) ;
5+ const { Schema } = MySQLModel ;
56
67const { DatabaseLoader } = require ( '@booljs/api' ) ;
78const { 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 ( ) {
0 commit comments