|
|
|
@ -13,33 +13,44 @@ export enum Collection { ANALYTIC_UNITS, SEGMENTS };
|
|
|
|
|
* @param { string | object } query: a key as a string or mongodb-style query |
|
|
|
|
*/ |
|
|
|
|
export type DBQ = { |
|
|
|
|
findOne: (query: string | object) => any, |
|
|
|
|
findMany: (query: string[] | object) => any[], |
|
|
|
|
insertOne: (document: object) => string, |
|
|
|
|
insertMany: (documents: object[]) => string[], |
|
|
|
|
updateOne: (query: string | object, updateQuery: any) => void, |
|
|
|
|
findOne: (query: string | object) => any, |
|
|
|
|
removeOne: (query: string | object) => number |
|
|
|
|
removeOne: (query: string) => boolean |
|
|
|
|
removeMany: (query: string[] | object) => number |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function makeDBQ(collection: Collection): DBQ { |
|
|
|
|
return { |
|
|
|
|
insertOne: dbInsert.bind(null, collection), |
|
|
|
|
insertMany: dbInsertMany.bind(null, collection), |
|
|
|
|
updateOne: dbUpdate.bind(null, collection), |
|
|
|
|
findOne: dbFindOne.bind(null, collection), |
|
|
|
|
removeOne: dbRemove.bind(null, collection) |
|
|
|
|
findMany: dbFindMany.bind(null, collection), |
|
|
|
|
insertOne: dbInsertOne.bind(null, collection), |
|
|
|
|
insertMany: dbInsertMany.bind(null, collection), |
|
|
|
|
updateOne: dbUpdateOne.bind(null, collection), |
|
|
|
|
removeOne: dbRemoveOne.bind(null, collection), |
|
|
|
|
removeMany: dbRemoveMany.bind(null, collection) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function wrapIdToQuery(query: string | object) { |
|
|
|
|
function wrapIdToQuery(query: string | object): any { |
|
|
|
|
if(typeof query === 'string') { |
|
|
|
|
return { _id: query }; |
|
|
|
|
} |
|
|
|
|
return query; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function wrapIdsToQuery(query: string[] | object): any { |
|
|
|
|
if(Array.isArray(query)) { |
|
|
|
|
return { _id: { $in: query } }; |
|
|
|
|
} |
|
|
|
|
return query; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const db = new Map<Collection, nedb>(); |
|
|
|
|
|
|
|
|
|
let dbInsert = (collection: Collection, doc: object) => { |
|
|
|
|
let dbInsertOne = (collection: Collection, doc: object) => { |
|
|
|
|
return new Promise<string>((resolve, reject) => { |
|
|
|
|
db[collection].insert(doc, (err, newDoc) => { |
|
|
|
|
if(err) { |
|
|
|
@ -63,7 +74,7 @@ let dbInsertMany = (collection: Collection, docs: object[]) => {
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let dbUpdate = (collection: Collection, query: string | object, updateQuery: object) => { |
|
|
|
|
let dbUpdateOne = (collection: Collection, query: string | object, updateQuery: object) => { |
|
|
|
|
query = wrapIdToQuery(query); |
|
|
|
|
return new Promise<void>((resolve, reject) => { |
|
|
|
|
db[collection].update(query, updateQuery, { /* options */ }, (err: Error) => { |
|
|
|
@ -89,8 +100,38 @@ let dbFindOne = (collection: Collection, query: string | object) => {
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let dbRemove = (collection: Collection, query: string | object) => { |
|
|
|
|
query = wrapIdToQuery(query); |
|
|
|
|
let dbFindMany = (collection: Collection, query: string[] | object) => { |
|
|
|
|
query = wrapIdsToQuery(query); |
|
|
|
|
return new Promise<any[]>((resolve, reject) => { |
|
|
|
|
db[collection].findOne(query, (err, docs) => { |
|
|
|
|
if(err) { |
|
|
|
|
reject(err); |
|
|
|
|
} else { |
|
|
|
|
resolve(docs); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let dbRemoveOne = (collection: Collection, id: string) => { |
|
|
|
|
let query = { _id: id }; |
|
|
|
|
return new Promise<boolean>((resolve, reject) => { |
|
|
|
|
db[collection].remove(query, (err, numRemoved) => { |
|
|
|
|
if(err) { |
|
|
|
|
reject(err); |
|
|
|
|
} else { |
|
|
|
|
if(numRemoved > 1) { |
|
|
|
|
throw new Error(`Removed ${numRemoved} elements with id: ${id}. Only one is Ok.`); |
|
|
|
|
} else { |
|
|
|
|
resolve(numRemoved == 1); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let dbRemoveMany = (collection: Collection, query: string[] | object) => { |
|
|
|
|
query = wrapIdsToQuery(query); |
|
|
|
|
return new Promise<number>((resolve, reject) => { |
|
|
|
|
db[collection].remove(query, (err, numRemoved) => { |
|
|
|
|
if(err) { |
|
|
|
@ -102,6 +143,7 @@ let dbRemove = (collection: Collection, query: string | object) => {
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function maybeCreateDir(path: string): void { |
|
|
|
|
if(fs.existsSync(path)) { |
|
|
|
|
return; |
|
|
|
|