Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add skip load and save option #124

Closed
Closed
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
14 changes: 10 additions & 4 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ class Database {
this.options = Object.assign({
version: 0,
onUpgrade() {},

onDowngrade() {}
onDowngrade() {},
dump: true
}, options);

this._models = {};
Expand Down Expand Up @@ -110,9 +110,12 @@ class Database {
* @return {Promise}
*/
load(callback) {
const { path, onUpgrade, onDowngrade, version: newVersion } = this.options;
const { path, onUpgrade, onDowngrade, version: newVersion, dump } = this.options;

if (!path) throw new WarehouseError('options.path is required');
if (!dump) {
return Promise.resolve().asCallback(callback);
}

let oldVersion = 0;

Expand Down Expand Up @@ -150,9 +153,12 @@ class Database {
* @return {Promise}
*/
save(callback) {
const { path } = this.options;
const { path, dump } = this.options;

if (!path) throw new WarehouseError('options.path is required');
if (!dump) {
return Promise.resolve().asCallback(callback);
}
return Promise.resolve(exportAsync(this, path)).asCallback(callback);
}

Expand Down
18 changes: 18 additions & 0 deletions test/scripts/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ describe('Database', () => {
});
});

it('load() - without dump', () => {
const db = new Database({path: DB_PATH, dump: false});

return db.load().then(() => {
const Test = db.model('Test');

Test.toArray().should.eql([]);
});
});

it('save()', () => db.save().then(() => fs.readFileAsync(DB_PATH, 'utf8')).then(data => {
const json = JSON.parse(data);

Expand All @@ -101,4 +111,12 @@ describe('Database', () => {
]
});
}));

it('save() - without dump', () => {
const DB_PATH = path.join(path.dirname(__dirname), 'fixtures', 'db-without-dump.json');
const db = new Database({path: DB_PATH, dump: false});

db.save();
fs.existsSync(DB_PATH).should.eql(false);
});
});