Skip to content

Commit 745e025

Browse files
committed
feat(loki): rename collection (#32)
(from techfort/LokiJS#611)
1 parent 9b426a4 commit 745e025

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

packages/loki/spec/generic/collection.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ describe("collection", () => {
3131
expect(coll.data.length).toEqual(1);
3232
});
3333

34+
it("collection rename works", function () {
35+
const db = new loki("test.db");
36+
db.addCollection("coll1");
37+
38+
let result = db.getCollection("coll1");
39+
expect(result.name).toEqual("coll1");
40+
41+
db.renameCollection("coll1", "coll2");
42+
result = db.getCollection("coll1");
43+
expect(result).toBeNull();
44+
result = db.getCollection("coll2");
45+
expect(result.name).toEqual("coll2");
46+
});
47+
3448
it("findAndUpdate works", () => {
3549
const db = new loki("test.db");
3650
const coll = db.addCollection("testcoll");

packages/loki/src/loki.js

+15
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,21 @@ export class Loki extends LokiEventEmitter {
283283
return null;
284284
}
285285

286+
/**
287+
* Renames an existing loki collection
288+
* @param {string} oldName - name of collection to rename
289+
* @param {string} newName - new name of collection
290+
* @returns {Collection} reference to the newly renamed collection
291+
*/
292+
renameCollection(oldName, newName) {
293+
const c = this.getCollection(oldName);
294+
if (c) {
295+
c.name = newName;
296+
}
297+
298+
return c;
299+
}
300+
286301
listCollections() {
287302
let i = this.collections.length;
288303
const colls = [];

0 commit comments

Comments
 (0)