Skip to content

Commit 548abfe

Browse files
authored
feat(local-storage): add local storage as separate package (#18)
1 parent 78119d1 commit 548abfe

File tree

6 files changed

+164
-1
lines changed

6 files changed

+164
-1
lines changed

CONTRIBUTING.md

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ The following is the list of supported scopes:
137137

138138
* **loki**: The LokiJS database.
139139
* **partitioning-adapter**: The partitioning adapter.
140+
* **local-storage**: The local storage adapter.
140141
* **fts**: A full text search for the database.
141142

142143
There are currently a few exceptions to the "use package name" rule:

config/build.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const conventionalChangelog = require("conventional-changelog");
99

1010
const PACKAGES = [
1111
"loki",
12-
"partitioning-adapter"
12+
"partitioning-adapter",
13+
"local-storage"
1314
];
1415

1516
const ROOT_DIR = process.cwd();

packages/local-storage/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@lokijs/local-storage",
3+
"description": "A persistence adapter which persists to web browser's local storage.",
4+
"author": "Various authors",
5+
"license": "MIT",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/LokiJS-Forge/LokiJS2.git"
9+
},
10+
"main": "lokijs.local-storage.js",
11+
"dependencies": {
12+
"@lokijs/loki": "0"
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* global describe, it, expect */
2+
import {Loki} from "../../../loki/src/loki";
3+
import {LokiLocalStorage} from "../../src/local_storage";
4+
5+
describe("testing persistence adapter", function () {
6+
it("LokiLocalStorage", function (done) {
7+
const db = new Loki("myTestApp");
8+
9+
const adapter = {adapter: new LokiLocalStorage()};
10+
11+
db.initializePersistence(adapter)
12+
.then(() => {
13+
db.addCollection("myColl").insert({name: "Hello World"});
14+
return db.saveDatabase().then(() => {
15+
const db2 = new Loki("myTestApp");
16+
return db2.initializePersistence(adapter)
17+
.then(() => {
18+
return db2.loadDatabase()
19+
.then(() => {
20+
expect(db2.getCollection("myColl").find()[0].name).toEqual("Hello World");
21+
});
22+
});
23+
});
24+
})
25+
.then(() => {
26+
const db3 = new Loki("other");
27+
return db3.initializePersistence(adapter)
28+
.then(() => {
29+
return db3.loadDatabase()
30+
.then(() => {
31+
expect(false).toEqual(true);
32+
}, () => {
33+
expect(true).toEqual(true);
34+
});
35+
});
36+
})
37+
.then(() => {
38+
return db.deleteDatabase();
39+
})
40+
.then(() => {
41+
return db.loadDatabase()
42+
.then(() => {
43+
expect(db.getCollection("myColl").find()[0].name).toEqual("Hello World");
44+
expect(false).toEqual(true);
45+
done();
46+
}, () => {
47+
expect(true).toEqual(true);
48+
done();
49+
});
50+
});
51+
});
52+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import {Loki} from "../../loki/src/loki";
2+
3+
function localStorageAvailable() {
4+
try {
5+
return (window && window.localStorage !== undefined && window.localStorage !== null);
6+
} catch (e) {
7+
return false;
8+
}
9+
}
10+
11+
/**
12+
* A loki persistence adapter which persists to web browser's local storage object
13+
* @constructor LokiLocalStorageAdapter
14+
*/
15+
export class LokiLocalStorage {
16+
/**
17+
* loadDatabase() - Load data from localstorage
18+
* @param {string} dbname - the name of the database to load
19+
* @returns {Promise} a Promise that resolves after the database was loaded
20+
* @memberof LokiLocalStorageAdapter
21+
*/
22+
loadDatabase(dbname) {
23+
if (localStorageAvailable()) {
24+
return Promise.resolve(localStorage.getItem(dbname));
25+
}
26+
27+
return Promise.reject(new Error("localStorage is not available"));
28+
}
29+
30+
/**
31+
* saveDatabase() - save data to localstorage, will throw an error if the file can't be saved
32+
* might want to expand this to avoid dataloss on partial save
33+
* @param {string} dbname - the filename of the database to load
34+
* @returns {Promise} a Promise that resolves after the database was saved
35+
* @memberof LokiLocalStorageAdapter
36+
*/
37+
saveDatabase(dbname, dbstring) {
38+
if (localStorageAvailable()) {
39+
localStorage.setItem(dbname, dbstring);
40+
41+
return Promise.resolve();
42+
}
43+
44+
return Promise.reject(new Error("localStorage is not available"));
45+
}
46+
47+
/**
48+
* deleteDatabase() - delete the database from localstorage, will throw an error if it
49+
* can't be deleted
50+
* @param {string} dbname - the filename of the database to delete
51+
* @returns {Promise} a Promise that resolves after the database was deleted
52+
* @memberof LokiLocalStorageAdapter
53+
*/
54+
deleteDatabase(dbname) {
55+
if (localStorageAvailable()) {
56+
localStorage.removeItem(dbname);
57+
58+
return Promise.resolve();
59+
}
60+
61+
return Promise.reject(new Error("localStorage is not available"));
62+
}
63+
}
64+
65+
Loki.LokiLocalStorage = LokiLocalStorage;
66+
67+
export default LokiLocalStorage;
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* global __dirname, module, require */
2+
const path = require("path");
3+
4+
module.exports = {
5+
devtool: "source-map",
6+
entry: path.join(__dirname, "src", "local_storage.js"),
7+
output: {
8+
filename: "lokijs.local-storage.js",
9+
library: "@lokijs/local-storage",
10+
libraryTarget: "umd2",
11+
umdNamedDefine: false
12+
},
13+
externals: {
14+
"../../loki/src/loki": "@lokijs/loki",
15+
},
16+
module: {
17+
loaders: [
18+
{
19+
test: /(\.js)$/,
20+
loader: "eslint-loader",
21+
exclude: /(node_modules|bower_components)/,
22+
options: {
23+
configFile: path.join("config", "eslintrc.js")
24+
}
25+
}
26+
]
27+
}
28+
};

0 commit comments

Comments
 (0)