|
|
@ -3,8 +3,6 @@ import { getDbQueryWrapper, dbCollection } from '../data_layer'; |
|
|
|
import { DbConnector } from './db_connector'; |
|
|
|
import { DbConnector } from './db_connector'; |
|
|
|
import { DbConnectorFactory } from './db_connector/factory'; |
|
|
|
import { DbConnectorFactory } from './db_connector/factory'; |
|
|
|
|
|
|
|
|
|
|
|
import * as deasync from 'deasync'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export enum SortingOrder { ASCENDING = 1, DESCENDING = -1 }; |
|
|
|
export enum SortingOrder { ASCENDING = 1, DESCENDING = -1 }; |
|
|
|
|
|
|
|
|
|
|
@ -24,38 +22,78 @@ export type DBQ = { |
|
|
|
removeMany: (query: string[] | object) => Promise<number> |
|
|
|
removeMany: (query: string[] | object) => Promise<number> |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const queryWrapper = getDbQueryWrapper(); |
|
|
|
export class DataService { |
|
|
|
let db: Map<Collection, dbCollection>; |
|
|
|
private static _instance: DataService; |
|
|
|
|
|
|
|
private _queryWrapper = getDbQueryWrapper(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private constructor() { |
|
|
|
|
|
|
|
if(DataService._instance !== undefined) { |
|
|
|
|
|
|
|
throw new Error(`Can't create 2nd instance of singleton class`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static getInstance(): DataService { |
|
|
|
|
|
|
|
if(DataService._instance === undefined) { |
|
|
|
|
|
|
|
DataService._instance = new DataService(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return DataService._instance; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public makeDBQ(collection: Collection): DBQ { |
|
|
|
|
|
|
|
return { |
|
|
|
|
|
|
|
findOne: async (query: object | string) => { |
|
|
|
|
|
|
|
const dbCollection = await this._getDbCollectionFromCollection(collection); |
|
|
|
|
|
|
|
return this._queryWrapper.dbFindOne(dbCollection, query); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
findMany: async (query: object | string[], sortQuery: object) => { |
|
|
|
|
|
|
|
const dbCollection = await this._getDbCollectionFromCollection(collection); |
|
|
|
|
|
|
|
return this._queryWrapper.dbFindMany(dbCollection, query, sortQuery); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
insertOne: async (doc: object) => { |
|
|
|
|
|
|
|
const dbCollection = await this._getDbCollectionFromCollection(collection); |
|
|
|
|
|
|
|
return this._queryWrapper.dbInsertOne(dbCollection, doc); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
insertMany: async (docs: object[]) => { |
|
|
|
|
|
|
|
const dbCollection = await this._getDbCollectionFromCollection(collection); |
|
|
|
|
|
|
|
return this._queryWrapper.dbInsertMany(dbCollection, docs); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
updateOne: async(query: object | string, updateQuery: object) => { |
|
|
|
|
|
|
|
const dbCollection = await this._getDbCollectionFromCollection(collection); |
|
|
|
|
|
|
|
return this._queryWrapper.dbUpdateOne(dbCollection, query, updateQuery); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
updateMany: async (query: object | string[], updateQuery: object) => { |
|
|
|
|
|
|
|
const dbCollection = await this._getDbCollectionFromCollection(collection); |
|
|
|
|
|
|
|
return this._queryWrapper.dbUpdateMany(dbCollection, query, updateQuery); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
removeOne: async (query: string | object) => { |
|
|
|
|
|
|
|
const dbCollection = await this._getDbCollectionFromCollection(collection); |
|
|
|
|
|
|
|
return this._queryWrapper.dbRemoveOne(dbCollection, query); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
removeMany: async (query: object | string[]) => { |
|
|
|
|
|
|
|
const dbCollection = await this._getDbCollectionFromCollection(collection); |
|
|
|
|
|
|
|
return this._queryWrapper.dbRemoveMany(dbCollection, query); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async _getConnector(): Promise<DbConnector> { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
const connector = await DbConnectorFactory.getDbConnector(); |
|
|
|
|
|
|
|
return connector; |
|
|
|
|
|
|
|
} catch (err) { |
|
|
|
|
|
|
|
console.log(`data service got an error while connecting to database: ${err}`); |
|
|
|
|
|
|
|
throw err; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async _getDbCollectionFromCollection(collection: Collection): Promise<dbCollection> { |
|
|
|
|
|
|
|
const connector = await this._getConnector(); |
|
|
|
|
|
|
|
const db = connector.db; |
|
|
|
|
|
|
|
|
|
|
|
function dbCollectionFromCollection(collection: Collection): dbCollection { |
|
|
|
|
|
|
|
let dbCollection = db.get(collection); |
|
|
|
let dbCollection = db.get(collection); |
|
|
|
if(dbCollection === undefined) { |
|
|
|
if(dbCollection === undefined) { |
|
|
|
throw new Error('Can`t find collection ' + collection); |
|
|
|
throw new Error('Can`t find collection ' + collection); |
|
|
|
} |
|
|
|
} |
|
|
|
return dbCollection; |
|
|
|
return dbCollection; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function makeDBQ(collection: Collection): DBQ { |
|
|
|
|
|
|
|
return { |
|
|
|
|
|
|
|
findOne: queryWrapper.dbFindOne.bind(null, dbCollectionFromCollection(collection)), |
|
|
|
|
|
|
|
findMany: queryWrapper.dbFindMany.bind(null, dbCollectionFromCollection(collection)), |
|
|
|
|
|
|
|
insertOne: queryWrapper.dbInsertOne.bind(null, dbCollectionFromCollection(collection)), |
|
|
|
|
|
|
|
insertMany: queryWrapper.dbInsertMany.bind(null, dbCollectionFromCollection(collection)), |
|
|
|
|
|
|
|
updateOne: queryWrapper.dbUpdateOne.bind(null, dbCollectionFromCollection(collection)), |
|
|
|
|
|
|
|
updateMany: queryWrapper.dbUpdateMany.bind(null, dbCollectionFromCollection(collection)), |
|
|
|
|
|
|
|
removeOne: queryWrapper.dbRemoveOne.bind(null, dbCollectionFromCollection(collection)), |
|
|
|
|
|
|
|
removeMany: queryWrapper.dbRemoveMany.bind(null, dbCollectionFromCollection(collection)) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let done = false; |
|
|
|
|
|
|
|
DbConnectorFactory.getDbConnector().then((connector: DbConnector) => { |
|
|
|
|
|
|
|
done = true; |
|
|
|
|
|
|
|
db = connector.db; |
|
|
|
|
|
|
|
}).catch((err) => { |
|
|
|
|
|
|
|
console.log(`data service got error while connect to data base ${err}`); |
|
|
|
|
|
|
|
//TODO: choose best practice for error handling
|
|
|
|
|
|
|
|
throw err; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
deasync.loopWhile(() => !done); |
|
|
|
|
|
|
|