@naturalcycles/db-lib
Advanced tools
Comparing version 1.6.0 to 1.7.0
@@ -0,1 +1,8 @@ | ||
# [1.7.0](https://github.com/NaturalCycles/db-lib/compare/v1.6.0...v1.7.0) (2019-08-18) | ||
### Features | ||
* InMemoryCacheDB log, global settings ([995802f](https://github.com/NaturalCycles/db-lib/commit/995802f)) | ||
# [1.6.0](https://github.com/NaturalCycles/db-lib/compare/v1.5.4...v1.6.0) (2019-08-18) | ||
@@ -2,0 +9,0 @@ |
@@ -7,2 +7,20 @@ import { StringMap } from '@naturalcycles/js-lib'; | ||
downstreamDB: CommonDB; | ||
/** | ||
* Global default. | ||
* @default false | ||
*/ | ||
skipCache?: boolean; | ||
/** | ||
* Global default. | ||
* @default false | ||
*/ | ||
onlyCache?: boolean; | ||
/** | ||
* @default false | ||
*/ | ||
logCached?: boolean; | ||
/** | ||
* @default false | ||
*/ | ||
logDownstream?: boolean; | ||
} | ||
@@ -9,0 +27,0 @@ export interface CacheCommonDBOptions extends CommonDBOptions { |
@@ -34,3 +34,3 @@ "use strict"; | ||
const missingIds = []; | ||
if (!opts.skipCache) { | ||
if (!opts.skipCache && !this.cfg.skipCache) { | ||
ids.forEach(id => { | ||
@@ -45,4 +45,7 @@ const r = (this.cache[table] || {})[id]; | ||
}); | ||
if (this.cfg.logCached) { | ||
log(`getByIds ${ids.length} rows from cache: [${ids.join(', ')}]`); | ||
} | ||
} | ||
if (missingIds.length && !opts.onlyCache) { | ||
if (missingIds.length && !opts.onlyCache && !this.cfg.onlyCache) { | ||
const results = await this.cfg.downstreamDB.getByIds(table, ids, opts); | ||
@@ -55,2 +58,5 @@ results.forEach(r => { | ||
}); | ||
if (this.cfg.logDownstream) { | ||
log(`getByIds ${results.length} rows from downstream: [${results.map(r => r.id).join(', ')}]`); | ||
} | ||
} | ||
@@ -62,12 +68,20 @@ // return in right order | ||
const deletedIds = []; | ||
if (!opts.onlyCache) { | ||
if (!opts.onlyCache && !this.cfg.onlyCache) { | ||
deletedIds.push(...(await this.cfg.downstreamDB.deleteByIds(table, ids, opts))); | ||
if (this.cfg.logDownstream) { | ||
log(`deleteByIds ${deletedIds.length} rows from downstream: [${deletedIds.join(', ')}]`); | ||
} | ||
} | ||
if (!opts.skipCache) { | ||
if (!opts.skipCache && !this.cfg.skipCache) { | ||
const deletedFromCache = []; | ||
ids.forEach(id => { | ||
if (this.cache[table][id]) { | ||
deletedIds.push(id); | ||
deletedFromCache.push(id); | ||
delete this.cache[table][id]; | ||
} | ||
delete this.cache[table][id]; | ||
}); | ||
if (this.cfg.logCached) { | ||
log(`deleteByIds ${deletedFromCache.length} rows from cache: [${deletedFromCache.join(', ')}]`); | ||
} | ||
} | ||
@@ -78,6 +92,11 @@ return deletedIds; | ||
let savedDBMs = dbms; | ||
if (!opts.onlyCache) { | ||
if (!opts.onlyCache && !this.cfg.onlyCache) { | ||
savedDBMs = await this.cfg.downstreamDB.saveBatch(table, dbms, opts); | ||
if (this.cfg.logDownstream) { | ||
log(`saveBatch ${savedDBMs.length} rows to downstream: [${savedDBMs | ||
.map(r => r.id) | ||
.join(', ')}]`); | ||
} | ||
} | ||
if (!opts.skipCache) { | ||
if (!opts.skipCache && !this.cfg.skipCache) { | ||
this.cache[table] = this.cache[table] || {}; | ||
@@ -87,2 +106,5 @@ dbms.forEach(dbm => { | ||
}); | ||
if (this.cfg.logCached) { | ||
log(`saveBatch ${savedDBMs.length} rows to cache: [${savedDBMs.map(r => r.id).join(', ')}]`); | ||
} | ||
} | ||
@@ -92,5 +114,8 @@ return savedDBMs; | ||
async runQuery(q, opts = {}) { | ||
if (!opts.onlyCache) { | ||
if (!opts.onlyCache && !this.cfg.onlyCache) { | ||
const dbms = await this.cfg.downstreamDB.runQuery(q, opts); | ||
if (!opts.skipCache) { | ||
if (this.cfg.logDownstream) { | ||
log(`runQuery ${dbms.length} rows from downstream`); | ||
} | ||
if (!opts.skipCache && !opts.skipCache) { | ||
dbms.forEach((dbm) => { | ||
@@ -102,17 +127,24 @@ this.cache[q.table][dbm.id] = dbm; | ||
} | ||
if (opts.skipCache) | ||
if (opts.skipCache || this.cfg.skipCache) | ||
return []; | ||
return inMemory_db_1.queryInMemory(q, this.cache[q.table]); | ||
const dbms = inMemory_db_1.queryInMemory(q, this.cache[q.table]); | ||
if (this.cfg.logCached) { | ||
log(`runQuery ${dbms.length} rows from cache`); | ||
} | ||
return dbms; | ||
} | ||
async runQueryCount(q, opts = {}) { | ||
if (!opts.onlyCache) { | ||
if (!opts.onlyCache && !this.cfg.onlyCache) { | ||
return this.cfg.downstreamDB.runQueryCount(q, opts); | ||
} | ||
const rows = await this.runQuery(q, opts); | ||
if (this.cfg.logCached) { | ||
log(`runQueryCount ${rows.length} rows from cache`); | ||
} | ||
return rows.length; | ||
} | ||
streamQuery(q, opts = {}) { | ||
if (!opts.onlyCache) { | ||
if (!opts.onlyCache && !this.cfg.onlyCache) { | ||
return this.cfg.downstreamDB.streamQuery(q, opts).pipe(operators_1.tap((dbm) => { | ||
if (!opts.skipCache) { | ||
if (!opts.skipCache && !this.cfg.skipCache) { | ||
this.cache[q.table][dbm.id] = dbm; | ||
@@ -122,10 +154,17 @@ } | ||
} | ||
if (opts.skipCache) | ||
if (opts.skipCache || this.cfg.skipCache) | ||
return rxjs_1.EMPTY; | ||
return rxjs_1.of(...inMemory_db_1.queryInMemory(q, this.cache[q.table])); | ||
const rows = inMemory_db_1.queryInMemory(q, this.cache[q.table]); | ||
if (this.cfg.logCached) { | ||
log(`runQueryCount ${rows.length} rows from cache`); | ||
} | ||
return rxjs_1.of(...rows); | ||
} | ||
async deleteBy(table, by, value, limit, opts = {}) { | ||
if (!opts.onlyCache) { | ||
if (!opts.onlyCache && !this.cfg.onlyCache) { | ||
const deletedIds = await this.cfg.downstreamDB.deleteBy(table, by, value, limit, opts); | ||
if (!opts.skipCache) { | ||
if (this.cfg.logDownstream) { | ||
log(`deleteBy ${deletedIds.length} rows from downstream and cache: [${deletedIds.join(', ')}]`); | ||
} | ||
if (!opts.skipCache && !this.cfg.skipCache) { | ||
deletedIds.forEach(id => { | ||
@@ -137,5 +176,8 @@ delete this.cache[table][id]; | ||
} | ||
if (opts.skipCache) | ||
if (opts.skipCache || this.cfg.skipCache) | ||
return []; | ||
const deletedIds = (await this.runQuery(new dbQuery_1.DBQuery(table).filter(by, '=', value), opts)).map(row => row.id); | ||
if (this.cfg.logCached) { | ||
log(`deleteBy ${deletedIds.length} rows from cache: [${deletedIds.join(', ')}]`); | ||
} | ||
if (this.cache[table]) { | ||
@@ -142,0 +184,0 @@ deletedIds.forEach(id => { |
@@ -38,3 +38,3 @@ { | ||
}, | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "Lowest Common Denominator API to supported Databases", | ||
@@ -41,0 +41,0 @@ "keywords": [ |
@@ -17,2 +17,24 @@ import { StringMap } from '@naturalcycles/js-lib' | ||
downstreamDB: CommonDB | ||
/** | ||
* Global default. | ||
* @default false | ||
*/ | ||
skipCache?: boolean | ||
/** | ||
* Global default. | ||
* @default false | ||
*/ | ||
onlyCache?: boolean | ||
/** | ||
* @default false | ||
*/ | ||
logCached?: boolean | ||
/** | ||
* @default false | ||
*/ | ||
logDownstream?: boolean | ||
} | ||
@@ -77,3 +99,3 @@ | ||
if (!opts.skipCache) { | ||
if (!opts.skipCache && !this.cfg.skipCache) { | ||
ids.forEach(id => { | ||
@@ -87,5 +109,9 @@ const r = (this.cache[table] || {})[id] | ||
}) | ||
if (this.cfg.logCached) { | ||
log(`getByIds ${ids.length} rows from cache: [${ids.join(', ')}]`) | ||
} | ||
} | ||
if (missingIds.length && !opts.onlyCache) { | ||
if (missingIds.length && !opts.onlyCache && !this.cfg.onlyCache) { | ||
const results = await this.cfg.downstreamDB.getByIds<ObjectWithId>(table, ids, opts) | ||
@@ -98,2 +124,8 @@ results.forEach(r => { | ||
}) | ||
if (this.cfg.logDownstream) { | ||
log( | ||
`getByIds ${results.length} rows from downstream: [${results.map(r => r.id).join(', ')}]`, | ||
) | ||
} | ||
} | ||
@@ -112,13 +144,27 @@ | ||
if (!opts.onlyCache) { | ||
if (!opts.onlyCache && !this.cfg.onlyCache) { | ||
deletedIds.push(...(await this.cfg.downstreamDB.deleteByIds(table, ids, opts))) | ||
if (this.cfg.logDownstream) { | ||
log(`deleteByIds ${deletedIds.length} rows from downstream: [${deletedIds.join(', ')}]`) | ||
} | ||
} | ||
if (!opts.skipCache) { | ||
if (!opts.skipCache && !this.cfg.skipCache) { | ||
const deletedFromCache: string[] = [] | ||
ids.forEach(id => { | ||
if (this.cache[table][id]) { | ||
deletedIds.push(id) | ||
deletedFromCache.push(id) | ||
delete this.cache[table][id] | ||
} | ||
delete this.cache[table][id] | ||
}) | ||
if (this.cfg.logCached) { | ||
log( | ||
`deleteByIds ${deletedFromCache.length} rows from cache: [${deletedFromCache.join( | ||
', ', | ||
)}]`, | ||
) | ||
} | ||
} | ||
@@ -135,7 +181,15 @@ | ||
let savedDBMs = dbms | ||
if (!opts.onlyCache) { | ||
if (!opts.onlyCache && !this.cfg.onlyCache) { | ||
savedDBMs = await this.cfg.downstreamDB.saveBatch(table, dbms, opts) | ||
if (this.cfg.logDownstream) { | ||
log( | ||
`saveBatch ${savedDBMs.length} rows to downstream: [${savedDBMs | ||
.map(r => r.id) | ||
.join(', ')}]`, | ||
) | ||
} | ||
} | ||
if (!opts.skipCache) { | ||
if (!opts.skipCache && !this.cfg.skipCache) { | ||
this.cache[table] = this.cache[table] || {} | ||
@@ -146,2 +200,6 @@ | ||
}) | ||
if (this.cfg.logCached) { | ||
log(`saveBatch ${savedDBMs.length} rows to cache: [${savedDBMs.map(r => r.id).join(', ')}]`) | ||
} | ||
} | ||
@@ -153,6 +211,10 @@ | ||
async runQuery<DBM = any> (q: DBQuery<DBM>, opts: CacheCommonDBOptions = {}): Promise<DBM[]> { | ||
if (!opts.onlyCache) { | ||
if (!opts.onlyCache && !this.cfg.onlyCache) { | ||
const dbms = await this.cfg.downstreamDB.runQuery(q, opts) | ||
if (!opts.skipCache) { | ||
if (this.cfg.logDownstream) { | ||
log(`runQuery ${dbms.length} rows from downstream`) | ||
} | ||
if (!opts.skipCache && !opts.skipCache) { | ||
dbms.forEach((dbm: any) => { | ||
@@ -165,5 +227,11 @@ this.cache[q.table][dbm.id] = dbm | ||
if (opts.skipCache) return [] | ||
if (opts.skipCache || this.cfg.skipCache) return [] | ||
return queryInMemory(q, this.cache[q.table]) | ||
const dbms = queryInMemory(q, this.cache[q.table]) | ||
if (this.cfg.logCached) { | ||
log(`runQuery ${dbms.length} rows from cache`) | ||
} | ||
return dbms | ||
} | ||
@@ -175,3 +243,3 @@ | ||
): Promise<number> { | ||
if (!opts.onlyCache) { | ||
if (!opts.onlyCache && !this.cfg.onlyCache) { | ||
return this.cfg.downstreamDB.runQueryCount(q, opts) | ||
@@ -181,2 +249,7 @@ } | ||
const rows = await this.runQuery(q, opts) | ||
if (this.cfg.logCached) { | ||
log(`runQueryCount ${rows.length} rows from cache`) | ||
} | ||
return rows.length | ||
@@ -186,6 +259,6 @@ } | ||
streamQuery<DBM = any> (q: DBQuery<DBM>, opts: CacheCommonDBSaveOptions = {}): Observable<DBM> { | ||
if (!opts.onlyCache) { | ||
if (!opts.onlyCache && !this.cfg.onlyCache) { | ||
return this.cfg.downstreamDB.streamQuery(q, opts).pipe( | ||
tap((dbm: any) => { | ||
if (!opts.skipCache) { | ||
if (!opts.skipCache && !this.cfg.skipCache) { | ||
this.cache[q.table][dbm.id] = dbm | ||
@@ -197,5 +270,11 @@ } | ||
if (opts.skipCache) return EMPTY | ||
if (opts.skipCache || this.cfg.skipCache) return EMPTY | ||
return of(...queryInMemory(q, this.cache[q.table])) | ||
const rows = queryInMemory(q, this.cache[q.table]) | ||
if (this.cfg.logCached) { | ||
log(`runQueryCount ${rows.length} rows from cache`) | ||
} | ||
return of(...rows) | ||
} | ||
@@ -210,6 +289,14 @@ | ||
): Promise<string[]> { | ||
if (!opts.onlyCache) { | ||
if (!opts.onlyCache && !this.cfg.onlyCache) { | ||
const deletedIds = await this.cfg.downstreamDB.deleteBy(table, by, value, limit, opts) | ||
if (!opts.skipCache) { | ||
if (this.cfg.logDownstream) { | ||
log( | ||
`deleteBy ${deletedIds.length} rows from downstream and cache: [${deletedIds.join( | ||
', ', | ||
)}]`, | ||
) | ||
} | ||
if (!opts.skipCache && !this.cfg.skipCache) { | ||
deletedIds.forEach(id => { | ||
@@ -223,3 +310,3 @@ delete this.cache[table][id] | ||
if (opts.skipCache) return [] | ||
if (opts.skipCache || this.cfg.skipCache) return [] | ||
@@ -230,2 +317,6 @@ const deletedIds = (await this.runQuery(new DBQuery(table).filter(by, '=', value), opts)).map( | ||
if (this.cfg.logCached) { | ||
log(`deleteBy ${deletedIds.length} rows from cache: [${deletedIds.join(', ')}]`) | ||
} | ||
if (this.cache[table]) { | ||
@@ -232,0 +323,0 @@ deletedIds.forEach(id => { |
Sorry, the diff of this file is not supported yet
119217
2362