@naturalcycles/db-lib
Advanced tools
Comparing version 1.5.4 to 1.6.0
@@ -0,1 +1,8 @@ | ||
# [1.6.0](https://github.com/NaturalCycles/db-lib/compare/v1.5.4...v1.6.0) (2019-08-18) | ||
### Features | ||
* InMemoryCacheDB ([1b77f8d](https://github.com/NaturalCycles/db-lib/commit/1b77f8d)) | ||
## [1.5.4](https://github.com/NaturalCycles/db-lib/compare/v1.5.3...v1.5.4) (2019-08-17) | ||
@@ -2,0 +9,0 @@ |
@@ -5,3 +5,4 @@ import { CommonDao, CommonDaoCfg, CommonDaoLogLevel } from './common.dao'; | ||
import { InMemoryDB } from './inMemory.db'; | ||
import { CacheCommonDBOptions, CacheCommonDBSaveOptions, InMemoryCacheDB, InMemoryCacheDBCfg } from './inMemoryCache.db'; | ||
import { createdUpdatedFields, createdUpdatedIdFields, deserializeJsonField, idField, serializeJsonField } from './model.util'; | ||
export { DBQuery, DBQueryFilterOperator, DBQueryFilter, DBQueryOrder, CommonDaoLogLevel, CommonDaoOptions, CommonDaoSaveOptions, CommonDBOptions, CommonDBSaveOptions, CommonDB, DBRelation, DBModelType, CreatedUpdated, CreatedUpdatedId, CreatedUpdatedVer, ObjectWithId, BaseDBEntity, baseDBEntitySchema, unsavedDBEntitySchema, UnsavedDBEntity, Unsaved, CommonDaoCfg, CommonDao, createdUpdatedFields, createdUpdatedIdFields, idField, InMemoryDB, serializeJsonField, deserializeJsonField, }; | ||
export { DBQuery, DBQueryFilterOperator, DBQueryFilter, DBQueryOrder, CommonDaoLogLevel, CommonDaoOptions, CommonDaoSaveOptions, CommonDBOptions, CommonDBSaveOptions, CommonDB, DBRelation, DBModelType, CreatedUpdated, CreatedUpdatedId, CreatedUpdatedVer, ObjectWithId, BaseDBEntity, baseDBEntitySchema, unsavedDBEntitySchema, UnsavedDBEntity, Unsaved, CommonDaoCfg, CommonDao, createdUpdatedFields, createdUpdatedIdFields, idField, InMemoryDB, serializeJsonField, deserializeJsonField, InMemoryCacheDBCfg, CacheCommonDBOptions, CacheCommonDBSaveOptions, InMemoryCacheDB, }; |
@@ -15,2 +15,4 @@ "use strict"; | ||
exports.InMemoryDB = inMemory_db_1.InMemoryDB; | ||
const inMemoryCache_db_1 = require("./inMemoryCache.db"); | ||
exports.InMemoryCacheDB = inMemoryCache_db_1.InMemoryCacheDB; | ||
const model_util_1 = require("./model.util"); | ||
@@ -17,0 +19,0 @@ exports.createdUpdatedFields = model_util_1.createdUpdatedFields; |
@@ -19,1 +19,2 @@ import { StringMap } from '@naturalcycles/js-lib'; | ||
} | ||
export declare function queryInMemory<DBM>(q: DBQuery<DBM>, tableCache?: StringMap<DBM>): DBM[]; |
@@ -62,57 +62,50 @@ "use strict"; | ||
async runQuery(q, opts) { | ||
const { table } = q; | ||
let rows = Object.values(this.data[table] || []); | ||
// .filter | ||
rows = q._filters.reduce((rows, filter) => { | ||
return rows.filter(row => { | ||
const fn = FILTER_FNS[filter.op]; | ||
if (!fn) | ||
throw new Error(`InMemoryDB query filter op not supported: ${filter.op}`); | ||
return fn(row[filter.name], filter.val); | ||
}); | ||
}, rows); | ||
// .select(fieldNames) | ||
if (q._selectedFieldNames) { | ||
rows = rows.map(row => js_lib_1._pick(row, q._selectedFieldNames.length ? q._selectedFieldNames : ['id'])); | ||
} | ||
// todo: only one order is supported (first) | ||
const [order] = q._orders; | ||
if (order) { | ||
const { name, descending } = order; | ||
rows = rows.sort((a, b) => { | ||
// tslint:disable-next-line:triple-equals | ||
if (a[name] == b[name]) | ||
return 0; | ||
if (descending) { | ||
return a[name] < b[name] ? 1 : -1; | ||
} | ||
else { | ||
return a[name] > b[name] ? 1 : -1; | ||
} | ||
}); | ||
} | ||
// .limit() | ||
if (q._limitValue) { | ||
rows = rows.slice(0, Math.min(q._limitValue, rows.length)); | ||
} | ||
return rows; | ||
return queryInMemory(q, this.data[q.table]); | ||
} | ||
async runQueryCount(q, opts) { | ||
const rows = await this.runQuery(q); | ||
return rows.length; | ||
return queryInMemory(q, this.data[q.table]).length; | ||
} | ||
streamQuery(q, opts) { | ||
const subj = new rxjs_1.Subject(); | ||
this.runQuery(q) | ||
.then(rows => { | ||
rows.forEach(row => subj.next(row)); | ||
subj.complete(); | ||
}) | ||
.catch(err => { | ||
subj.error(err); | ||
return rxjs_1.of(...queryInMemory(q, this.data[q.table])); | ||
} | ||
} | ||
exports.InMemoryDB = InMemoryDB; | ||
function queryInMemory(q, tableCache) { | ||
let rows = Object.values(tableCache || []); | ||
// .filter | ||
rows = q._filters.reduce((rows, filter) => { | ||
return rows.filter(row => { | ||
const fn = FILTER_FNS[filter.op]; | ||
if (!fn) | ||
throw new Error(`InMemoryDB query filter op not supported: ${filter.op}`); | ||
return fn(row[filter.name], filter.val); | ||
}); | ||
return subj; | ||
}, rows); | ||
// .select(fieldNames) | ||
if (q._selectedFieldNames) { | ||
rows = rows.map(row => js_lib_1._pick(row, q._selectedFieldNames.length ? q._selectedFieldNames : ['id'])); | ||
} | ||
// todo: only one order is supported (first) | ||
const [order] = q._orders; | ||
if (order) { | ||
const { name, descending } = order; | ||
rows = rows.sort((a, b) => { | ||
// tslint:disable-next-line:triple-equals | ||
if (a[name] == b[name]) | ||
return 0; | ||
if (descending) { | ||
return a[name] < b[name] ? 1 : -1; | ||
} | ||
else { | ||
return a[name] > b[name] ? 1 : -1; | ||
} | ||
}); | ||
} | ||
// .limit() | ||
if (q._limitValue) { | ||
rows = rows.slice(0, Math.min(q._limitValue, rows.length)); | ||
} | ||
return rows; | ||
} | ||
exports.InMemoryDB = InMemoryDB; | ||
exports.queryInMemory = queryInMemory; | ||
//# sourceMappingURL=inMemory.db.js.map |
@@ -38,3 +38,3 @@ { | ||
}, | ||
"version": "1.5.4", | ||
"version": "1.6.0", | ||
"description": "Lowest Common Denominator API to supported Databases", | ||
@@ -41,0 +41,0 @@ "keywords": [ |
@@ -23,2 +23,8 @@ import { CommonDao, CommonDaoCfg, CommonDaoLogLevel } from './common.dao' | ||
import { | ||
CacheCommonDBOptions, | ||
CacheCommonDBSaveOptions, | ||
InMemoryCacheDB, | ||
InMemoryCacheDBCfg, | ||
} from './inMemoryCache.db' | ||
import { | ||
createdUpdatedFields, | ||
@@ -61,2 +67,6 @@ createdUpdatedIdFields, | ||
deserializeJsonField, | ||
InMemoryCacheDBCfg, | ||
CacheCommonDBOptions, | ||
CacheCommonDBSaveOptions, | ||
InMemoryCacheDB, | ||
} |
import { _pick, StringMap } from '@naturalcycles/js-lib' | ||
import { Debug } from '@naturalcycles/nodejs-lib' | ||
import { Observable, Subject } from 'rxjs' | ||
import { Observable, of } from 'rxjs' | ||
import { BaseDBEntity, CommonDB, CommonDBOptions, CommonDBSaveOptions } from './db.model' | ||
@@ -82,65 +82,55 @@ import { DBQuery } from './dbQuery' | ||
async runQuery<DBM = any> (q: DBQuery<DBM>, opts?: CommonDBOptions): Promise<DBM[]> { | ||
const { table } = q | ||
return queryInMemory(q, this.data[q.table]) | ||
} | ||
let rows: any[] = Object.values(this.data[table] || []) | ||
async runQueryCount<DBM = any> (q: DBQuery<DBM>, opts?: CommonDBOptions): Promise<number> { | ||
return queryInMemory(q, this.data[q.table]).length | ||
} | ||
// .filter | ||
rows = q._filters.reduce((rows, filter) => { | ||
return rows.filter(row => { | ||
const fn = FILTER_FNS[filter.op] | ||
if (!fn) throw new Error(`InMemoryDB query filter op not supported: ${filter.op}`) | ||
return fn(row[filter.name], filter.val) | ||
}) | ||
}, rows) | ||
streamQuery<DBM = any> (q: DBQuery<DBM>, opts?: CommonDBOptions): Observable<DBM> { | ||
return of(...queryInMemory(q, this.data[q.table])) | ||
} | ||
} | ||
// .select(fieldNames) | ||
if (q._selectedFieldNames) { | ||
rows = rows.map(row => | ||
_pick(row, q._selectedFieldNames!.length ? q._selectedFieldNames : ['id']), | ||
) | ||
} | ||
export function queryInMemory<DBM> (q: DBQuery<DBM>, tableCache?: StringMap<DBM>): DBM[] { | ||
let rows: any[] = Object.values(tableCache || []) | ||
// todo: only one order is supported (first) | ||
const [order] = q._orders | ||
if (order) { | ||
const { name, descending } = order | ||
rows = rows.sort((a, b) => { | ||
// tslint:disable-next-line:triple-equals | ||
if (a[name] == b[name]) return 0 | ||
// .filter | ||
rows = q._filters.reduce((rows, filter) => { | ||
return rows.filter(row => { | ||
const fn = FILTER_FNS[filter.op] | ||
if (!fn) throw new Error(`InMemoryDB query filter op not supported: ${filter.op}`) | ||
return fn(row[filter.name], filter.val) | ||
}) | ||
}, rows) | ||
if (descending) { | ||
return a[name] < b[name] ? 1 : -1 | ||
} else { | ||
return a[name] > b[name] ? 1 : -1 | ||
} | ||
}) | ||
} | ||
// .select(fieldNames) | ||
if (q._selectedFieldNames) { | ||
rows = rows.map(row => | ||
_pick(row, q._selectedFieldNames!.length ? q._selectedFieldNames : ['id']), | ||
) | ||
} | ||
// .limit() | ||
if (q._limitValue) { | ||
rows = rows.slice(0, Math.min(q._limitValue, rows.length)) | ||
} | ||
// todo: only one order is supported (first) | ||
const [order] = q._orders | ||
if (order) { | ||
const { name, descending } = order | ||
rows = rows.sort((a, b) => { | ||
// tslint:disable-next-line:triple-equals | ||
if (a[name] == b[name]) return 0 | ||
return rows as DBM[] | ||
if (descending) { | ||
return a[name] < b[name] ? 1 : -1 | ||
} else { | ||
return a[name] > b[name] ? 1 : -1 | ||
} | ||
}) | ||
} | ||
async runQueryCount<DBM = any> (q: DBQuery<DBM>, opts?: CommonDBOptions): Promise<number> { | ||
const rows = await this.runQuery(q) | ||
return rows.length | ||
// .limit() | ||
if (q._limitValue) { | ||
rows = rows.slice(0, Math.min(q._limitValue, rows.length)) | ||
} | ||
streamQuery<DBM = any> (q: DBQuery<DBM>, opts?: CommonDBOptions): Observable<DBM> { | ||
const subj = new Subject<DBM>() | ||
this.runQuery<DBM>(q) | ||
.then(rows => { | ||
rows.forEach(row => subj.next(row)) | ||
subj.complete() | ||
}) | ||
.catch(err => { | ||
subj.error(err) | ||
}) | ||
return subj | ||
} | ||
return rows as DBM[] | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
111693
31
2230