|
|
@ -9,37 +9,48 @@ export enum Collection { ANALYTIC_UNITS, SEGMENTS }; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Class which helps to make queries to your collection |
|
|
|
* Class which helps to make queries to your collection |
|
|
|
*
|
|
|
|
* |
|
|
|
* @param { string | object } query: a key as a string or mongodb-style query |
|
|
|
* @param { string | object } query: a key as a string or mongodb-style query |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
export type DBQ = { |
|
|
|
export type DBQ = { |
|
|
|
|
|
|
|
findOne: (query: string | object) => any, |
|
|
|
|
|
|
|
findMany: (query: string[] | object) => any[], |
|
|
|
insertOne: (document: object) => string, |
|
|
|
insertOne: (document: object) => string, |
|
|
|
insertMany: (documents: object[]) => string[], |
|
|
|
insertMany: (documents: object[]) => string[], |
|
|
|
updateOne: (query: string | object, updateQuery: any) => void, |
|
|
|
updateOne: (query: string | object, updateQuery: any) => void, |
|
|
|
findOne: (query: string | object) => any, |
|
|
|
removeOne: (query: string) => boolean |
|
|
|
removeOne: (query: string | object) => number |
|
|
|
removeMany: (query: string[] | object) => number |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function makeDBQ(collection: Collection): DBQ { |
|
|
|
export function makeDBQ(collection: Collection): DBQ { |
|
|
|
return { |
|
|
|
return { |
|
|
|
insertOne: dbInsert.bind(null, collection), |
|
|
|
|
|
|
|
insertMany: dbInsertMany.bind(null, collection), |
|
|
|
|
|
|
|
updateOne: dbUpdate.bind(null, collection), |
|
|
|
|
|
|
|
findOne: dbFindOne.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') { |
|
|
|
if(typeof query === 'string') { |
|
|
|
return { _id: query }; |
|
|
|
return { _id: query }; |
|
|
|
} |
|
|
|
} |
|
|
|
return 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>(); |
|
|
|
const db = new Map<Collection, nedb>(); |
|
|
|
|
|
|
|
|
|
|
|
let dbInsert = (collection: Collection, doc: object) => { |
|
|
|
let dbInsertOne = (collection: Collection, doc: object) => { |
|
|
|
return new Promise<string>((resolve, reject) => { |
|
|
|
return new Promise<string>((resolve, reject) => { |
|
|
|
db[collection].insert(doc, (err, newDoc) => { |
|
|
|
db[collection].insert(doc, (err, newDoc) => { |
|
|
|
if(err) { |
|
|
|
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); |
|
|
|
query = wrapIdToQuery(query); |
|
|
|
return new Promise<void>((resolve, reject) => { |
|
|
|
return new Promise<void>((resolve, reject) => { |
|
|
|
db[collection].update(query, updateQuery, { /* options */ }, (err: Error) => { |
|
|
|
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) => { |
|
|
|
let dbFindMany = (collection: Collection, query: string[] | object) => { |
|
|
|
query = wrapIdToQuery(query); |
|
|
|
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) => { |
|
|
|
return new Promise<number>((resolve, reject) => { |
|
|
|
db[collection].remove(query, (err, numRemoved) => { |
|
|
|
db[collection].remove(query, (err, numRemoved) => { |
|
|
|
if(err) { |
|
|
|
if(err) { |
|
|
@ -102,6 +143,7 @@ let dbRemove = (collection: Collection, query: string | object) => { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function maybeCreateDir(path: string): void { |
|
|
|
function maybeCreateDir(path: string): void { |
|
|
|
if(fs.existsSync(path)) { |
|
|
|
if(fs.existsSync(path)) { |
|
|
|
return; |
|
|
|
return; |
|
|
|