From 745e0257dc16b45e3a2d32326b1ae23a0f64ab06 Mon Sep 17 00:00:00 2001 From: Viatorus Date: Thu, 5 Oct 2017 19:41:05 +0200 Subject: [PATCH] feat(loki): rename collection (#32) (from techfort/LokiJS#611) --- packages/loki/spec/generic/collection.spec.js | 14 ++++++++++++++ packages/loki/src/loki.js | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/packages/loki/spec/generic/collection.spec.js b/packages/loki/spec/generic/collection.spec.js index 4fd1ba77..73246582 100644 --- a/packages/loki/spec/generic/collection.spec.js +++ b/packages/loki/spec/generic/collection.spec.js @@ -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"); diff --git a/packages/loki/src/loki.js b/packages/loki/src/loki.js index bb7a35f6..7d6a6545 100644 --- a/packages/loki/src/loki.js +++ b/packages/loki/src/loki.js @@ -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 = [];