From e7a6a445c27ab20817e07f4447671c315df31c3b Mon Sep 17 00:00:00 2001 From: Ievgeniia Maltseva Date: Thu, 15 Mar 2018 13:01:15 +0200 Subject: [PATCH 1/2] Add support to specify location and iosDatabaseLocation required when using cordova-sqlite adapter --- src/rx-database.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/rx-database.js b/src/rx-database.js index e3e44831b44..9e26b87b24b 100644 --- a/src/rx-database.js +++ b/src/rx-database.js @@ -57,13 +57,13 @@ export class RxDatabase { get _adminPouch() { if (!this.__adminPouch) - this.__adminPouch = _internalAdminPouch(this.name, this.adapter); + this.__adminPouch = _internalAdminPouch(this.name, this.adapter, this.options); return this.__adminPouch; } get _collectionsPouch() { if (!this.__collectionsPouch) - this.__collectionsPouch = _internalCollectionsPouch(this.name, this.adapter); + this.__collectionsPouch = _internalCollectionsPouch(this.name, this.adapter, this.options); return this.__collectionsPouch; } @@ -127,7 +127,7 @@ export class RxDatabase { * @type {Object} */ _spawnPouchDB(collectionName, schemaVersion, pouchSettings = {}) { - return _spawnPouchDB(this.name, this.adapter, collectionName, schemaVersion, pouchSettings); + return _spawnPouchDB(this.name, this.adapter, collectionName, schemaVersion, pouchSettings, this.options); } get isLeader() { @@ -526,33 +526,34 @@ export async function create({ } -function _spawnPouchDB(dbName, adapter, collectionName, schemaVersion, pouchSettings = {}) { +function _spawnPouchDB(dbName, adapter, collectionName, schemaVersion, pouchSettings = {}, options = {}) { const pouchLocation = dbName + '-rxdb-' + schemaVersion + '-' + collectionName; const pouchDbParameters = { location: pouchLocation, adapter: util.adapterObject(adapter), settings: pouchSettings }; + const pouchDBOptions = Object.assign({}, pouchDbParameters.adapter, options); runPluginHooks('preCreatePouchDb', pouchDbParameters); return new PouchDB( pouchDbParameters.location, - pouchDbParameters.adapter, + pouchDBOptions, pouchDbParameters.settings ); } -function _internalAdminPouch(name, adapter) { +function _internalAdminPouch(name, adapter, options = {}) { return _spawnPouchDB(name, adapter, '_admin', 0, { auto_compaction: false, // no compaction because this only stores local documents revs_limit: 1 - }); + }, options); } -function _internalCollectionsPouch(name, adapter) { +function _internalCollectionsPouch(name, adapter, options = {}) { return _spawnPouchDB(name, adapter, '_collections', 0, { auto_compaction: false, // no compaction because this only stores local documents revs_limit: 1 - }); + }, options); } export async function removeDatabase(databaseName, adapter) { From 717f2c3f0ec0f3876ece31738119ab4a1f2a32b3 Mon Sep 17 00:00:00 2001 From: Ievgeniia Maltseva Date: Mon, 19 Mar 2018 15:47:05 +0200 Subject: [PATCH 2/2] Change the way to pass params from options to pouchSettings --- src/rx-database.js | 26 ++++++++++++++------------ src/typings/pouch.d.ts | 4 +++- src/typings/rx-database.d.ts | 5 +++++ test/unit/rx-database.test.js | 15 +++++++++++++++ 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/rx-database.js b/src/rx-database.js index 9e26b87b24b..0d14d2e0664 100644 --- a/src/rx-database.js +++ b/src/rx-database.js @@ -31,13 +31,14 @@ const USED_COMBINATIONS = {}; let DB_COUNT = 0; export class RxDatabase { - constructor(name, adapter, password, multiInstance, options) { + constructor(name, adapter, password, multiInstance, options, pouchSettings) { if (typeof name !== 'undefined') DB_COUNT++; this.name = name; this.adapter = adapter; this.password = password; this.multiInstance = multiInstance; this.options = options; + this.pouchSettings = pouchSettings; this.idleQueue = new IdleQueue(); this.token = randomToken(10); @@ -57,13 +58,13 @@ export class RxDatabase { get _adminPouch() { if (!this.__adminPouch) - this.__adminPouch = _internalAdminPouch(this.name, this.adapter, this.options); + this.__adminPouch = _internalAdminPouch(this.name, this.adapter, this.pouchSettings); return this.__adminPouch; } get _collectionsPouch() { if (!this.__collectionsPouch) - this.__collectionsPouch = _internalCollectionsPouch(this.name, this.adapter, this.options); + this.__collectionsPouch = _internalCollectionsPouch(this.name, this.adapter, this.pouchSettings); return this.__collectionsPouch; } @@ -127,7 +128,7 @@ export class RxDatabase { * @type {Object} */ _spawnPouchDB(collectionName, schemaVersion, pouchSettings = {}) { - return _spawnPouchDB(this.name, this.adapter, collectionName, schemaVersion, pouchSettings, this.options); + return _spawnPouchDB(this.name, this.adapter, collectionName, schemaVersion, pouchSettings, this.pouchSettings); } get isLeader() { @@ -485,7 +486,8 @@ export async function create({ password, multiInstance = true, ignoreDuplicate = false, - options = {} + options = {}, + pouchSettings = {} }) { util.validateCouchDBString(name); @@ -518,7 +520,7 @@ export async function create({ USED_COMBINATIONS[name].push(adapter); - const db = new RxDatabase(name, adapter, password, multiInstance, options); + const db = new RxDatabase(name, adapter, password, multiInstance, options, pouchSettings); await db.prepare(); runPluginHooks('createRxDatabase', db); @@ -526,14 +528,14 @@ export async function create({ } -function _spawnPouchDB(dbName, adapter, collectionName, schemaVersion, pouchSettings = {}, options = {}) { +function _spawnPouchDB(dbName, adapter, collectionName, schemaVersion, pouchSettings = {}, pouchSettingsFromRxDatabaseCreator = {}) { const pouchLocation = dbName + '-rxdb-' + schemaVersion + '-' + collectionName; const pouchDbParameters = { location: pouchLocation, adapter: util.adapterObject(adapter), settings: pouchSettings }; - const pouchDBOptions = Object.assign({}, pouchDbParameters.adapter, options); + const pouchDBOptions = Object.assign({}, pouchDbParameters.adapter, pouchSettingsFromRxDatabaseCreator); runPluginHooks('preCreatePouchDb', pouchDbParameters); return new PouchDB( pouchDbParameters.location, @@ -542,18 +544,18 @@ function _spawnPouchDB(dbName, adapter, collectionName, schemaVersion, pouchSett ); } -function _internalAdminPouch(name, adapter, options = {}) { +function _internalAdminPouch(name, adapter, pouchSettingsFromRxDatabaseCreator = {}) { return _spawnPouchDB(name, adapter, '_admin', 0, { auto_compaction: false, // no compaction because this only stores local documents revs_limit: 1 - }, options); + }, pouchSettingsFromRxDatabaseCreator); } -function _internalCollectionsPouch(name, adapter, options = {}) { +function _internalCollectionsPouch(name, adapter, pouchSettingsFromRxDatabaseCreator = {}) { return _spawnPouchDB(name, adapter, '_collections', 0, { auto_compaction: false, // no compaction because this only stores local documents revs_limit: 1 - }, options); + }, pouchSettingsFromRxDatabaseCreator); } export async function removeDatabase(databaseName, adapter) { diff --git a/src/typings/pouch.d.ts b/src/typings/pouch.d.ts index 7260686beb5..a4853a58d7e 100644 --- a/src/typings/pouch.d.ts +++ b/src/typings/pouch.d.ts @@ -31,7 +31,9 @@ export interface PouchSettings { auth?: any, skip_setup?: boolean, storage?: any, - size?: number + size?: number, + location?: string, + iosDatabaseLocation?: string } export declare class PouchDB { diff --git a/src/typings/rx-database.d.ts b/src/typings/rx-database.d.ts index e7d0f10f23c..412c0256835 100644 --- a/src/typings/rx-database.d.ts +++ b/src/typings/rx-database.d.ts @@ -10,6 +10,9 @@ import { import { RxChangeEvent } from './rx-change-event'; +import { + PouchSettings +} from "./pouch"; export interface RxDatabaseCreator { name: string; @@ -18,6 +21,7 @@ export interface RxDatabaseCreator { multiInstance?: boolean; ignoreDuplicate?: boolean; options?: any; + pouchSettings?: PouchSettings; } export declare class RxDatabase { @@ -27,6 +31,7 @@ export declare class RxDatabase { readonly password: string; readonly collections: any; options?: any; + pouchSettings?: PouchSettings; readonly $: Observable; diff --git a/test/unit/rx-database.test.js b/test/unit/rx-database.test.js index a3a9815e1c4..4485795f3a4 100644 --- a/test/unit/rx-database.test.js +++ b/test/unit/rx-database.test.js @@ -109,6 +109,21 @@ config.parallel('rx-database.test.js', () => { assert.equal(db.options.foo, 'bar'); db.destroy(); }); + it('should not forget the pouchSettings', async () => { + const name = util.randomCouchString(10); + const password = util.randomCouchString(12); + const db = await RxDatabase.create({ + name, + adapter: 'memory', + password, + ignoreDuplicate: true, + pouchSettings: { + foo: 'bar' + } + }); + assert.equal(db.pouchSettings.foo, 'bar'); + db.destroy(); + }); }); describe('negative', () => { it('should crash with invalid token', async () => {