Skip to content

Commit

Permalink
feat(loki): rename collection (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viatorus committed Oct 5, 2017
1 parent 9b426a4 commit 745e025
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/loki/spec/generic/collection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ describe("collection", () => {
expect(coll.data.length).toEqual(1);
});

it("collection rename works", function () {
const db = new loki("test.db");
db.addCollection("coll1");

let result = db.getCollection("coll1");
expect(result.name).toEqual("coll1");

db.renameCollection("coll1", "coll2");
result = db.getCollection("coll1");
expect(result).toBeNull();
result = db.getCollection("coll2");
expect(result.name).toEqual("coll2");
});

it("findAndUpdate works", () => {
const db = new loki("test.db");
const coll = db.addCollection("testcoll");
Expand Down
15 changes: 15 additions & 0 deletions packages/loki/src/loki.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ export class Loki extends LokiEventEmitter {
return null;
}

/**
* Renames an existing loki collection
* @param {string} oldName - name of collection to rename
* @param {string} newName - new name of collection
* @returns {Collection} reference to the newly renamed collection
*/
renameCollection(oldName, newName) {
const c = this.getCollection(oldName);
if (c) {
c.name = newName;
}

return c;
}

listCollections() {
let i = this.collections.length;
const colls = [];
Expand Down

0 comments on commit 745e025

Please sign in to comment.