Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to specify location and iosDatabaseLocation #567

Merged
merged 2 commits into from Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/rx-database.js
Expand Up @@ -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);

Expand All @@ -57,13 +58,13 @@ export class RxDatabase {

get _adminPouch() {
if (!this.__adminPouch)
this.__adminPouch = _internalAdminPouch(this.name, this.adapter);
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.__collectionsPouch = _internalCollectionsPouch(this.name, this.adapter, this.pouchSettings);
return this.__collectionsPouch;
}

Expand Down Expand Up @@ -127,7 +128,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.pouchSettings);
}

get isLeader() {
Expand Down Expand Up @@ -485,7 +486,8 @@ export async function create({
password,
multiInstance = true,
ignoreDuplicate = false,
options = {}
options = {},
pouchSettings = {}
}) {
util.validateCouchDBString(name);

Expand Down Expand Up @@ -518,41 +520,42 @@ 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);
return db;
}


function _spawnPouchDB(dbName, adapter, collectionName, schemaVersion, pouchSettings = {}) {
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, pouchSettingsFromRxDatabaseCreator);
runPluginHooks('preCreatePouchDb', pouchDbParameters);
return new PouchDB(
pouchDbParameters.location,
pouchDbParameters.adapter,
pouchDBOptions,
pouchDbParameters.settings
);
}

function _internalAdminPouch(name, adapter) {
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
});
}, pouchSettingsFromRxDatabaseCreator);
}

function _internalCollectionsPouch(name, adapter) {
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
});
}, pouchSettingsFromRxDatabaseCreator);
}

export async function removeDatabase(databaseName, adapter) {
Expand Down
4 changes: 3 additions & 1 deletion src/typings/pouch.d.ts
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/typings/rx-database.d.ts
Expand Up @@ -10,6 +10,9 @@ import {
import {
RxChangeEvent
} from './rx-change-event';
import {
PouchSettings
} from "./pouch";

export interface RxDatabaseCreator {
name: string;
Expand All @@ -18,6 +21,7 @@ export interface RxDatabaseCreator {
multiInstance?: boolean;
ignoreDuplicate?: boolean;
options?: any;
pouchSettings?: PouchSettings;
}

export declare class RxDatabase {
Expand All @@ -27,6 +31,7 @@ export declare class RxDatabase {
readonly password: string;
readonly collections: any;
options?: any;
pouchSettings?: PouchSettings;

readonly $: Observable<RxChangeEvent>;

Expand Down
15 changes: 15 additions & 0 deletions test/unit/rx-database.test.js
Expand Up @@ -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 () => {
Expand Down