Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ const LocalStorage = function (options) {
LocalStorage.prototype = {

init: async function (options) {
if (this.initialised) {
throw new Error("LocalStorage has already been initialised, did you call init or initSync twice?")
}
this.initialised = true

if (options) {
this.setOptions(options);
}
Expand All @@ -92,6 +97,11 @@ LocalStorage.prototype = {
},

initSync: function (options) {
if (this.initialised) {
throw new Error("LocalStorage has already been initialised, did you call init or initSync twice?")
}
this.initialised = true

if (options) {
this.setOptions(options);
}
Expand Down
3 changes: 3 additions & 0 deletions src/node-persist.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const LocalStorage = require('./local-storage');
* An options hash can be optionally passed.
*/
nodePersist.init = async function (userOptions) {
if (nodePersist.defaultInstance) {
throw new Error('node-persist has already been initialized')
}
const localStorage = nodePersist.defaultInstance = nodePersist.create(userOptions);
let ret = await localStorage.init(userOptions);
mixin(nodePersist, localStorage, {skip: ['init', 'create']});
Expand Down
103 changes: 80 additions & 23 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,39 @@ describe('node-persist ' + pkg.version + ' tests:', async function() {
rmdir(TEST_BASE_DIR, done);
});

describe('default instance', function() {
this.beforeEach(() => {
// Reset global state
nodePersist.defaultInstance = null;
})

it('should create the default instance of LocalStorage sync and use it', async function() {
await nodePersist.init({dir: randDir()});
assert.ok(nodePersist.defaultInstance instanceof LocalStorage);
await nodePersist.setItem('item8877', 'hello');
assert.equal(await nodePersist.getItem('item8877'), 'hello', `write/read didn't work`);
});

it('should create a default instance', async function() {
let dir = randDir();
let options = await nodePersist.init({dir: dir});
assert.equal(options.dir, dir, `Options don't match`);
});

it('should not allow init to be called more than once', async function() {
await nodePersist.init();

let initError;
try {
await nodePersist.init();
} catch(err) {
initError = err
}

assert.include(initError != null ? initError.message : '', 'node-persist has already been initialized');
});
})

describe('instances', function() {
let dir1, dir2, storage1, storage2, storage11, storage22, storageSync;

Expand Down Expand Up @@ -85,20 +118,50 @@ describe('node-persist ' + pkg.version + ' tests:', async function() {
await storageSync.setItem('item9977', 'hello');
assert.equal(await storageSync.getItem('item9977'), 'hello', `write/read didn't work`);
});
});

it('should create the default instance of LocalStorage sync and use it', async function() {
await nodePersist.init({dir: randDir()});
assert.ok(nodePersist.defaultInstance instanceof LocalStorage);
await nodePersist.setItem('item8877', 'hello');
assert.equal(await nodePersist.getItem('item8877'), 'hello', `write/read didn't work`);
describe('initialisation', function() {
let options = {
dir: randDir(),
// logging: true,
writeQueue: true,
writeQueueWriteOnlyLast: true
};

let storage;
beforeEach(async () => {
storage = nodePersist.create();
})

it('should init()', async function() {
await storage.init(options);
assert.equal(storage.options.dir, options.dir);
assert.ok(fs.existsSync(options.dir));
});

it('should create a default instance', async function() {
let dir = randDir();
let options = await nodePersist.init({dir: dir});
assert.equal(options.dir, dir, `Options don't match`);
it('should initSync()', async function() {
storage.initSync(options);
assert.equal(storage.options.dir, options.dir);
assert.ok(fs.existsSync(options.dir));
});
});

it('should fail if init() or initAsync() are called more than once per instance', async function() {
await storage.init(options);

let initError;
try {
await storage.init(options);
} catch(err) {
initError = err
}

assert.include(initError != null ? initError.message : '', 'LocalStorage has already been initialised');

assert.throws(() => {
storage.initSync(options)
}, 'LocalStorage has already been initialised', undefined, 'initSync() should throw an error when called after init()');
});
})

describe('operations', function() {
let options = {
Expand All @@ -107,7 +170,11 @@ describe('node-persist ' + pkg.version + ' tests:', async function() {
writeQueue: true,
writeQueueWriteOnlyLast: true
};
let storage = nodePersist.create();
let storage;
beforeEach(async () => {
storage = nodePersist.create();
await storage.init(options);
})

let items = {
'item1': 1,
Expand All @@ -126,17 +193,6 @@ describe('node-persist ' + pkg.version + ' tests:', async function() {
let generatedItemsKeys = Object.keys(generatedItems);

describe('general items operations', function() {
it('should init()', async function() {
await storage.init(options);
assert.equal(storage.options.dir, options.dir);
assert.ok(fs.existsSync(options.dir));
});

it('should initSync()', function() {
storage.initSync(options);
assert.equal(storage.options.dir, options.dir);
assert.ok(fs.existsSync(options.dir));
});

it('should setItem()', async function() {
await storage.setItem('item1', items.item1);
Expand Down Expand Up @@ -232,9 +288,10 @@ describe('node-persist ' + pkg.version + ' tests:', async function() {
let options = {
dir: randDir()
};
let storage = nodePersist.create();
let storage;

beforeEach(async function() {
storage = nodePersist.create();
await storage.init(options);
await storage.setItem('item1', items.item1);
await storage.setItem('item2', items.item2);
Expand Down