<commit_before>/// <reference path='../../typings/tsd.d.ts' />

import {MongoCallback}      from 'mongodb';
import * as fs              from 'fs';
import {PopulationLevel}    from 'ts-objectschema';
import {StringMapAny}       from './Interfaces';

/**
 * Interface for a DbCon module in order to provide interoperability with the ConnectionBase
 */
export interface DbCon {
    read(model: string, query: Object, limit: number, cb: MongoCallback<any>): void;
    vread(model: string, version: number, cb: MongoCallback<any>): void;
    update(model: string, query: Object, dbUpdate: Object, cb: MongoCallback<any>, dbCreate: Object): void;
    create(model: string, query: Object, dbUpdate: Object, cb: MongoCallback<any>): void;
    delete(model: any, query: Object, cb: MongoCallback<any>): void;
    createIndex(model: string, path: string): void;
}

/**
 * Base for the connection to any database
 */
export class DBManager {
    /**
     * Conected database
     * @type {DbCon}
     */
    public db: DbCon;
    /**
     * Container of models by their name
     * @type {StringMapAny}
     */
    public models: StringMapAny;
    /**
     * 
     */
    constructor() {

        // init models
        this.models = {};
    }
    /**
     * init the connect to mongodb by supplying a open db socket
     */
    public init(db: any): void {
        // holder for meta data
        let meta: Array<string>;
        let name: string;

        // save the database
        this.db = db;

        // read all models
        fs.readdir('build/resources/models', (err: Error, files: Array<string>) => {

            // debug cant find folders
            if (err) {
                throw err;
            }

            // foreach found file
            files.forEach((file: string) => {
                meta = file.split('.');

                name = meta[0];

                // keep javascript models
                if (meta[1] === 'js') {
                    // ensure index
                    this.db.createIndex(name, 'id');
                    // save model
                    this.models[name] = require('../resources/models/' + name)[name];<commit_msg>Move index creation to db connector modules they shuold optimze the process<commit_after>/// <reference path='../../typings/tsd.d.ts' />

import {MongoCallback}      from 'mongodb';
import * as fs              from 'fs';
import {PopulationLevel}    from 'ts-objectschema';
import {StringMapAny}       from './Interfaces';

/**
 * Base for the connection to any database
 */
export class DBManager {
    /**
     * Container of models by their name
     * @type {StringMapAny}
     */
    public models: StringMapAny;

    constructor(){
        
    }
    public init(): void {
        // holder for meta data
        let meta: Array<string>;
        let name: string;

        // read all models
        fs.readdir('build/resources/models', (err: Error, files: Array<string>) => {

            // debug cant find folders
            if (err) {
                throw err;
            }

            // foreach found file
            files.forEach((file: string) => {
                meta = file.split('.');

                name = meta[0];

                // keep javascript models
                if (meta[1] === 'js') {
                    // ensure index
                    
                    // save model
                    this.models[name] = require('../resources/models/' + name)[name];
{'fraction_non_alphanumeric': 0.12734623159110597, 'fraction_numerical': 0.001155067860236789, 'mean_word_length': 1.736176935229068, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 18, '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': '194739', 'n_tokens_mistral': 966, 'n_tokens_neox': 911, 'n_words': 289}