<commit_before>import { Injectable, Inject } from '@angular/core'
import RxDB from 'rxdb/plugins/core'
import RxDBSchemaCheckModule from 'rxdb/plugins/schema-check'
import RxDBValidateModule from 'rxdb/plugins/validate'
import RxDBLeaderElectionModule from 'rxdb/plugins/leader-election'
import RxDBReplicationModule from 'rxdb/plugins/replication'
import KeycompressionPlugin from 'rxdb/plugins/key-compression'

import { environment } from '../../../environments/environment'

import { RxPresentationDocument, PresentationsDatabase } from '../../marketing/presentations/data/models/presentation'
import { RxDatabase, RxCollection } from 'rxdb';

if (environment.production) {
    // schema-checks should be used in dev-mode only
    RxDB.plugin(RxDBSchemaCheckModule)
}
RxDB.plugin(KeycompressionPlugin);
RxDB.plugin(RxDBValidateModule)
RxDB.plugin(RxDBLeaderElectionModule)
RxDB.plugin(RxDBReplicationModule)
RxDB.plugin(require('pouchdb-adapter-http'))

RxDB.QueryChangeDetector.enable()
RxDB.QueryChangeDetector.enableDebugging()

RxDB.plugin(require('pouchdb-adapter-idb'))

const collections = [
    {
        name: 'presentation',
        schema: require('../../marketing/presentations/data/models/presentation.json'),
        sync: true,
    }
]

const syncURL = 'http://127.0.0.1:5984/'

@Injectable()
export class DatabaseService {

    schemas: any
    dbs: RxDatabase[]

    // static dbPromise: Promise<PresentationsDatabase> = null

    constructor(
        @Inject("APP_SCHEMAS") schemas: any,
    ) {
        this.schemas = schemas
        this.setup()
    }

    private async setup() {

        this.schemas.forEach(schema => {
            this.dbs[schema.name] = RxDB.create({
                name: schema.name,
                adapter: "idb",
                // password: '<PASSWORD>' // no password needed
            })
        });
        window['dbs'] = this.dbs

        await Promise.all(this.schemas.map(schema => {
            return this.dbs[schema.name].collection(schema)
        }))

        // sync
        // collections
        //     .filter(col => col.sync)
        //     .map(col => col.name)
        //     .forEach(colName => db[colName].sync({ remote: syncURL + colName + '/' }))

        // return db
    }

    public get<T>(database: string): Promise<RxCollection<T>> {
        return this.dbs[database].data
    }
}<commit_msg>add sync<commit_after>import { Injectable, Inject } from '@angular/core'
import RxDB from 'rxdb/plugins/core'
import RxDBSchemaCheckModule from 'rxdb/plugins/schema-check'
import RxDBValidateModule from 'rxdb/plugins/validate'
import RxDBLeaderElectionModule from 'rxdb/plugins/leader-election'
import RxDBReplicationModule from 'rxdb/plugins/replication'
import KeycompressionPlugin from 'rxdb/plugins/key-compression'

import { environment } from '../../../environments/environment'

import { RxPresentationDocument, PresentationsDatabase } from '../../marketing/presentations/data/models/presentation'
import { RxDatabase, RxCollection } from 'rxdb';

if (environment.production) {
    // schema-checks should be used in dev-mode only
    RxDB.plugin(RxDBSchemaCheckModule)
}
RxDB.plugin(KeycompressionPlugin);
RxDB.plugin(RxDBValidateModule)
RxDB.plugin(RxDBLeaderElectionModule)
RxDB.plugin(RxDBReplicationModule)
RxDB.plugin(require('pouchdb-adapter-http'))

RxDB.QueryChangeDetector.enable()
RxDB.QueryChangeDetector.enableDebugging()

RxDB.plugin(require('pouchdb-adapter-idb'))

const syncURL = 'http://1172.16.58.3:5984/'

@Injectable()
export class DatabaseService {

    schemas: any
    dbs: RxDatabase[]

    // static dbPromise: Promise<PresentationsDatabase> = null

    constructor(
        @Inject("APP_SCHEMAS") schemas: any,
    ) {
        this.schemas = schemas
        this.setup()
    }

    private async setup() {

        this.schemas.forEach(schema => {
            this.dbs[schema.name] = RxDB.create({
                name: schema.name,
                adapter: "idb",
                // password: '<PASSWORD>' // no password needed
            })
        });
        window['dbs'] = this.dbs

        await Promise.all(this.schemas.map(schema => {
            return this.dbs[schema.name].collection(schema)
        }))

        // sync
        this.schemas
            .filter(col => col.sync)
            .map(col => col.name)
            .forEach(colName => this.dbs[colName]["data"].sync({
                remote: syncURL + colName + '/'
            }))

        // return db
    }

    public get<T>(database: string): Promise<RxCollection<T>> {
        return this.dbs[database].data
    }
}
{'fraction_non_alphanumeric': 0.12994350282485875, 'fraction_numerical': 0.004997827031725337, 'mean_word_length': 2.8843881856540086, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4930426', 'n_tokens_mistral': 1430, 'n_tokens_neox': 1377, 'n_words': 255}