lib/loader.js
function loadDatabase(config = {}) {
    if (typeof config.ignore === 'string' || Array.isArray(config.ignore)) {
      app.deprecate(`[egg-sequelize] if you want to exclude ${config.ignore} when load models, please set to config.sequelize.exclude instead of config.sequelize.ignore`);
      config.exclude = config.ignore;
      delete config.ignore;
    }

    const sequelize = new app.Sequelize(config.database, config.username, config.password, config);

    const delegate = config.delegate.split('.');
    const len = delegate.length;

    let model = app;
    let context = app.context;
    for (let i = 0; i < len - 1; i++) {
      model = model[delegate[i]] = model[delegate[i]] || {};
      context = context[delegate[i]] = context[delegate[i]] || {};
    }

    if (model[delegate[len - 1]]) {
      throw new Error(`[egg-sequelize] app[${config.delegate}] is already defined`);
    }

    Object.defineProperty(model, delegate[len - 1], {
      value: sequelize,
      writable: false,
      configurable: true,
    });

    const DELEGATE = Symbol(`context#sequelize_${config.delegate}`);
    Object.defineProperty(context, delegate[len - 1], {
      get() {
        // context.model is different with app.model
        // so we can change the properties of ctx.model.xxx
        if (!this[DELEGATE]) this[DELEGATE] = Object.create(model[delegate[len - 1]]);
        return this[DELEGATE];
      },
      configurable: true,
    });

    const modelDir = path.join(app.baseDir, 'app', config.baseDir);

    const models = [];
    const target = Symbol(config.delegate);

    app.loader.loadToApp(modelDir, target, {
      caseStyle: 'upper',
      ignore: config.exclude,
      filter(model) {
        if (!model || !model.sequelize) return false;
        models.push(model);
        return true;
      },
      initializer(factory) {
        if (typeof factory === 'function') {
          return factory(app, sequelize);
        }
      },
    });
    Object.assign(model[delegate[len - 1]], app[target]);

    models.forEach(model => {
      typeof model.associate === 'function' && model.associate();
    });

    return model[delegate[len - 1]];
  }
function loadDatabase(config = {}) {
    if (typeof config.ignore === 'string' || Array.isArray(config.ignore)) {
      app.deprecate(`[egg-sequelize] if you want to exclude ${config.ignore} when load models, please set to config.sequelize.exclude instead of config.sequelize.ignore`);
      config.exclude = config.ignore;
      delete config.ignore;
    }

    const sequelize = new app.Sequelize(config.database, config.username, config.password, config);

    const delegate = config.delegate.split('.');
    const len = delegate.length;

    let model = app;
    let context = app.context;
    for (let i = 0; i < len - 1; i++) {
      model = model[delegate[i]] = model[delegate[i]] || {};
      context = context[delegate[i]] = context[delegate[i]] || {};
    }

    if (model[delegate[len - 1]]) {
      throw new Error(`[egg-sequelize] app[${config.delegate}] is already defined`);
    }

    Object.defineProperty(model, delegate[len - 1], {
      value: sequelize,
      writable: false,
      configurable: true,
    });

    const DELEGATE = Symbol(`context#sequelize_${config.delegate}`);
    Object.defineProperty(context, delegate[len - 1], {
      get() {
        // context.model is different with app.model
        // so we can change the properties of ctx.model.xxx
        if (!this[DELEGATE]) this[DELEGATE] = Object.create(model[delegate[len - 1]]);
        return this[DELEGATE];
      },
      configurable: true,
    });

    const modelDir = path.join(app.baseDir, 'app', config.baseDir);

    const models = [];
    const target = Symbol(config.delegate);

    app.loader.loadToApp(modelDir, target, {
      caseStyle: 'upper',
      ignore: config.exclude,
      filter(model) {
        if (!model || !model.sequelize) return false;
        models.push(model);
        return true;
      },
      initializer(factory) {
        if (typeof factory === 'function') {
          return factory(app, sequelize);
        }
      },
    });
    Object.assign(model[delegate[len - 1]], app[target]);

    models.forEach(model => {
      typeof model.associate === 'function' && model.associate();
    });

    return model[delegate[len - 1]];
  }
function loadDatabase ( config = { } ) { if ( typeof config . ignore === 'string' || Array . isArray ( config . ignore ) ) { app . deprecate ( ` ${ config . ignore } ` ) ; config . exclude = config . ignore ; delete config . ignore ; } const sequelize = new app . Sequelize ( config . database , config . username , config . password , config ) ; const delegate = config . delegate . split ( '.' ) ; const len = delegate . length ; let model = app ; let context = app . context ; for ( let i = 0 ; i < len - 1 ; i ++ ) { model = model [ delegate [ i ] ] = model [ delegate [ i ] ] || { } ; context = context [ delegate [ i ] ] = context [ delegate [ i ] ] || { } ; } if ( model [ delegate [ len - 1 ] ] ) { throw new Error ( ` ${ config . delegate } ` ) ; } Object . defineProperty ( model , delegate [ len - 1 ] , { value : sequelize , writable : false , configurable : true , } ) ; const DELEGATE = Symbol ( ` ${ config . delegate } ` ) ; Object . defineProperty ( context , delegate [ len - 1 ] , { get ( ) { // context.model is different with app.model // so we can change the properties of ctx.model.xxx if ( ! this [ DELEGATE ] ) this [ DELEGATE ] = Object . create ( model [ delegate [ len - 1 ] ] ) ; return this [ DELEGATE ] ; } , configurable : true , } ) ; const modelDir = path . join ( app . baseDir , 'app' , config . baseDir ) ; const models = [ ] ; const target = Symbol ( config . delegate ) ; app . loader . loadToApp ( modelDir , target , { caseStyle : 'upper' , ignore : config . exclude , filter ( model ) { if ( ! model || ! model . sequelize ) return false ; models . push ( model ) ; return true ; } , initializer ( factory ) { if ( typeof factory === 'function' ) { return factory ( app , sequelize ) ; } } , } ) ; Object . assign ( model [ delegate [ len - 1 ] ] , app [ target ] ) ; models . forEach ( model => { typeof model . associate === 'function' && model . associate ( ) ; } ) ; return model [ delegate [ len - 1 ] ] ; }
load databse to app[config.delegate
@param {Object} config config for load
- delegate: load model to app[delegate]
- baeDir: where model located
- other sequelize configures(databasem username, password, etc...)
@return {Object} sequelize instance
load databse to app [ config . delegate