Skip to content

Commit

Permalink
Merge pull request #111 from Kinto/db-prefix
Browse files Browse the repository at this point in the history
Introduce local DB prefix
  • Loading branch information
n1k0 committed Sep 1, 2015
2 parents 5afbe41 + 428cc67 commit 387ebee
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 48 deletions.
48 changes: 33 additions & 15 deletions demo/kinto.dev.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions demo/kinto.min.js

Large diffs are not rendered by default.

48 changes: 33 additions & 15 deletions dist/kinto.dev.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/kinto.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const db = new Kinto(options);
- `headers`: The default headers to pass for every HTTP request performed to the Cliquet server (eg. `{"Authorization": "Basic bWF0Og=="}`);
- `adapter`: The persistence layer adapter to use for saving data locally (default: `Kinto.adapters.IDB`); alternatively, a `Kinto.adapters.LocalStorage` adapter is also provided; last, if you plan on writing your own adapter, you can read more about how to do so in the [Extending Kinto.js](extending.md) section.
- `requestMode`: The HTTP [CORS](https://fetch.spec.whatwg.org/#concept-request-mode) mode. Default: `cors`.
- `dbPrefix`: The prefix for the local database name (default: `""`). Use this option to isolate different specific databases, eg. for storing distinct users data.

## Collections

Expand Down
4 changes: 2 additions & 2 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ export default class Api {
* @param {String} remote The remote URL.
* @param {Object} options The options object.
*/
constructor(remote, options={headers: {}}) {
constructor(remote, options={}) {
if (typeof(remote) !== "string" || !remote.length)
throw new Error("Invalid remote URL: " + remote);
if (remote[remote.length-1] === "/")
remote = remote.slice(0, -1);
this._backoffReleaseTime = null;
// public properties
this.remote = remote;
this.optionHeaders = options.headers;
this.optionHeaders = options.headers || {};
this.serverSettings = null;
this.events = options.events || new EventEmitter();
try {
Expand Down
6 changes: 4 additions & 2 deletions src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export default class Collection {
* Constructor.
*
* Options:
* - {BaseAdapter} adapter: The DB adapter (default: IDB)
* - {BaseAdapter} adapter The DB adapter (default: IDB)
* - {String} dbPrefix The DB name prefix (default: "")
*
* @param {String} bucket The bucket identifier.
* @param {String} name The collection name.
Expand All @@ -60,7 +61,8 @@ export default class Collection {
this._lastModified = null;
this._transformers = {remote: []};
const DBAdapter = options.adapter || IDB;
const db = new DBAdapter(`${bucket}/${name}`);
const dbPrefix = options.dbPrefix || "";
const db = new DBAdapter(`${dbPrefix}${bucket}/${name}`);
if (!(db instanceof BaseAdapter))
throw new Error("Unsupported adapter.");
// public properties
Expand Down
28 changes: 20 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import IDB from "./adapters/IDB";
import RemoteTransformer from "./transformers/remote";

const DEFAULT_BUCKET_NAME = "default";
const DEFAULT_REMOTE = "http://localhost:8888/v1";

/**
* Kinto class.
Expand Down Expand Up @@ -70,18 +71,27 @@ export default class Kinto {
* Constructor.
*
* Options:
* - {String} remote The server URL to use.
* - {String} bucket The collection bucket name.
* - {EventEmitter} events Events handler.
* - {BaseAdapter} adapter The base DB adapter class.
* - {String} dbPrefix The DB name prefix.
* - {Object} headers The HTTP headers to use.
* - {String} requestMode The HTTP CORS mode to use.
*
* @param {Object} options The options object.
*/
constructor(options={}) {
this._options = options;
const defaults = {
adapter: Kinto.adapters.IDB,
bucket: DEFAULT_BUCKET_NAME,
events: new EventEmitter(),
remote: DEFAULT_REMOTE,
};
this._options = Object.assign(defaults, options);
this._collections = {};
// public properties
this.events = options.events || new EventEmitter();
this.events = this._options.events;
}

/**
Expand All @@ -94,17 +104,19 @@ export default class Kinto {
if (!collName)
throw new Error("missing collection name");

const bucket = this._options.bucket || DEFAULT_BUCKET_NAME;
const api = new Api(this._options.remote || "http://localhost:8888/v1", {
headers: this._options.headers || {},
events: this.events,
const remote = this._options.remote;
const api = new Api(remote, {
headers: this._options.headers,
events: this._options.events,
requestMode: this._options.requestMode,
});

if (!this._collections.hasOwnProperty(collName)) {
const bucket = this._options.bucket;
this._collections[collName] = new Collection(bucket, collName, api, {
events: this.events,
adapter: this._options.adapter || Kinto.adapters.IDB,
events: this._options.events,
adapter: this._options.adapter,
dbPrefix: this._options.dbPrefix,
});
}

Expand Down
7 changes: 7 additions & 0 deletions test/collection_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ describe("Collection", () => {
expect(collection.api.http.events).eql(collection.events);
});

it("should allow providing a prefix for the db name", () => {
const collection = new Collection(TEST_BUCKET_NAME, TEST_COLLECTION_NAME, api, {
dbPrefix: "user-x/"
});
expect(collection.db.dbname).eql("user-x/kinto-test/kinto-test");
});

it("should create and expose an IDB database by default", () => {
const events = new EventEmitter();
const api = new Api(FAKE_SERVER_URL, {events});
Expand Down
5 changes: 5 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ describe("Kinto", () => {
expect(new Kinto({requestMode}).collection("x").api.http.requestMode)
.eql(requestMode);
});

it("should propagate the dbPrefix option to child dependencies", () => {
expect(new Kinto({dbPrefix: "app--"}).collection("x").db.dbname)
.eql("app--default/x");
});
});

describe("#collection()", () => {
Expand Down

0 comments on commit 387ebee

Please sign in to comment.