import { Service } from "../lib/service" import * as async from "async" import * as mongodb from "mongodb" export namespace MongodbService { export type Settings = { database?: { port?: number host?: string name?: string option?: any } } } export class MongodbService extends Service { constructor(settings: MongodbService.Settings) { super({ database: { port: 27017, host: "localhost", name: "test", } }, settings) } services: {} name = "MongodbService" as "MongodbService" db: mongodb.Db initialize(done) { let { host, port, name } = this.settings.database console.log("Connecting to database", `mongodb://${host}:${port}/${name}`) mongodb.MongoClient.connect(`mongodb://${host}:${port}`, { reconnectTries: 100000, // wait 1 second before retrying reconnectInterval: 2000, ... this.settings.database.option || {} }, (err, client) => { this.db = client.db(name) done(err) }) } asBuiltIn() { let { host, port, name } = this.settings.database console.log(`MongodbService using mongo://${host}:${port}/${name}`) } createIndexBuilder() { return new IndexBuilder() } } export class IndexBuilder { cols: CollectionIndexInfo[] = [] current: CollectionIndexInfo = null collection(collection: mongodb.Collection) { for (let col of this.cols) { if (col.collection === collection) { this.current = col return } } this.current = { collection, indexes: [] } this.cols.push(this.current) return this } index(key: string | { [k: string]: number } | string[], option: { unique?: boolean, sparse?: boolean } = {}) { if (!this.current) { throw new Error("Set index before specify collection, please call IndexBuilder.collection first.") } if (typeof key == "string") { key = { [key]: 1 } } if (key instanceof Array) { let _k = {} for (let v of key) { if (v[0] == "-") { _k[v.slice(1)] = -1 } else { _k[v] = 1 } } key = _k } this.current.indexes.push({ key, option }) return this } private lastIndex() { if (!this.current) { throw new Error("Should specify collection") } let info = this.current.indexes[this.current.indexes.length - 1] if (!info) { throw new Error("Should specify colelction and index") } return info } unique() { let info = this.lastIndex() info.option.unique = true return this } nonunique() { let info = this.lastIndex() info.option.unique = false return this } sparse() { let info = this.lastIndex() info.option.sparse = true return this } nonsparse() { let info = this.lastIndex() info.option.sparse = false return this } apply(callback: Callback) { let cols = this.cols async.eachSeries(cols, (colInfo, done) => { async.eachSeries(colInfo.indexes, (data, done2) => { if (!data.option) data.option = {} console.warn(`Indexing ${colInfo.collection.namespace}:${Object.keys(data.key).join("&")} => ${data.option.unique && "unique" || "none-unique"} ${data.option.sparse && "sparse" || ""}`) colInfo.collection.createIndex(data.key, { unique: data.option.unique, sparse: data.option.sparse }, done2) }, done) }, callback) } } export interface CollectionIndexInfo { collection: mongodb.Collection, indexes: { key: any, option?: { unique?: boolean, sparse?: boolean } }[] }