<commit_before>var orm      = require('orm');
var database = null;

//TODO: auto load models
var models = [
    'order',
    'product'
];

module.exports = function(db_setting, config, callback) {
    if(database) return callback(null, database);

    orm.connect(db_setting, function(err, db) {
        if(err) return callback(err);

        //orm config
        config(orm, db);
            
        //TODO: auto load models
        //load all models in folders
        for(var i = 0; i < models.length; i++) {
            require('../test/models/' + models[i])(orm, db);
        }

        //sync database table
        db.sync(function(err) {
            if(err) return callback(err);

            //cache
            database = db;

            callback(null, database);
        });
    });
};<commit_msg>auto load all models, js file only<commit_after>var path     = require('path');
var orm      = require('orm');
var helper   = require('./_helper');
var database = null;

module.exports = function(opt, callback) {
    if(database) return callback(null, database);

    orm.connect(opt.setting, function(err, db) {
        if(err) return callback(err);

        //orm config hook
        opt.hook(orm, db);
            
        //load all models in folders
        helper.listJSFiles(opt.path, function(err, files) {
            if(err) return callback(err);

            for(var i = 0; i < files.length; i++) {
                require(files[i])(orm, db);
            }

            //sync database table
            db.sync(function(err) {
                if(err) return callback(err);

                //cache
                database = db;

                callback(null, database);
            });
        });
    });
};
{'fraction_non_alphanumeric': 0.13683602771362588, 'fraction_numerical': 0.0011547344110854503, 'mean_word_length': 1.746434231378764, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1244019', 'n_tokens_mistral': 506, 'n_tokens_neox': 484, 'n_words': 129}