Skip to content

Commit

Permalink
feat: update database method with callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
@jotadeveloper authored and sergiohgz committed Aug 13, 2019
1 parent b73e497 commit ef202a9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
2 changes: 1 addition & 1 deletion plugins/local-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"devDependencies": {
"@commitlint/cli": "^6.0.2",
"@commitlint/config-conventional": "^6.0.2",
"@verdaccio/types": "^2.0.4",
"@verdaccio/types": "2.1.0",
"babel-cli": "6.26.0",
"babel-core": "6.26.0",
"babel-eslint": "8.2.2",
Expand Down
39 changes: 29 additions & 10 deletions plugins/local-storage/src/___tests___/local-database.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,40 @@ describe('Local Database', () => {
});

describe('Database CRUD', () => {
test('should add an item to database', () => {
test('should add an item to database', done => {
const pgkName = 'jquery';
expect(locaDatabase.get()).toHaveLength(0);
locaDatabase.get((err, data) => {
expect(err).toBeNull();
expect(data).toHaveLength(0);

locaDatabase.add(pgkName);
expect(locaDatabase.get()).toHaveLength(1);
locaDatabase.add(pgkName, err => {
expect(err).toBeNull();
locaDatabase.get((err, data) => {
expect(err).toBeNull();
expect(data).toHaveLength(1);
done();
});
});
});
});

test('should remove an item to database', () => {
test('should remove an item to database', done => {
const pgkName = 'jquery';
expect(locaDatabase.get()).toHaveLength(0);
locaDatabase.add(pgkName);
locaDatabase.remove(pgkName);

expect(locaDatabase.get()).toHaveLength(0);
locaDatabase.get((err, data) => {
expect(err).toBeNull();
expect(data).toHaveLength(0);
locaDatabase.add(pgkName, err => {
expect(err).toBeNull();
locaDatabase.remove(pgkName, err => {
expect(err).toBeNull();
locaDatabase.get((err, data) => {
expect(err).toBeNull();
expect(data).toHaveLength(0);
done();
});
});
});
});
});
});
});
31 changes: 19 additions & 12 deletions plugins/local-storage/src/local-database.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import _ from 'lodash';
import Path from 'path';
import LocalFS from './local-fs';
import mkdirp from 'mkdirp';
import type { StorageList, LocalStorage, Logger, Config } from '@verdaccio/types';
import type { StorageList, LocalStorage, Logger, Config, Callback } from '@verdaccio/types';
import type { IPackageStorage, ILocalData } from '@verdaccio/local-storage';

/**
Expand Down Expand Up @@ -45,10 +45,10 @@ class LocalDatabase implements ILocalData {
* @param {*} name
* @return {Error|*}
*/
add(name: string) {
add(name: string, cb: Callback) {
if (this.data.list.indexOf(name) === -1) {
this.data.list.push(name);
return this._sync();
cb(this._sync());
}
}

Expand All @@ -57,21 +57,27 @@ class LocalDatabase implements ILocalData {
* @param {*} name
* @return {Error|*}
*/
remove(name: string) {
const pkgName = this.get().indexOf(name);
if (pkgName !== -1) {
this.data.list.splice(pkgName, 1);
}
remove(name: string, cb: Callback) {
this.get((err, data) => {
if (err) {
cb(new Error('error on get'));
}

const pkgName = data.indexOf(name);
if (pkgName !== -1) {
this.data.list.splice(pkgName, 1);
}

return this._sync();
cb(this._sync());
});
}

/**
* Return all database elements.
* @return {Array}
*/
get() {
return this.data.list;
get(cb: Callback) {
cb(null, this.data.list);
}

/**
Expand All @@ -88,11 +94,12 @@ class LocalDatabase implements ILocalData {
mkdirp.sync(Path.dirname(this.path));
} catch (err) {
// perhaps a logger instance?
/* eslint no-empty:off */
return null;
}

try {
fs.writeFileSync(this.path, JSON.stringify(this.data));
return null;
} catch (err) {
return err;
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/local-storage/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@
version "1.0.0"
resolved "https://registry.npmjs.org/@verdaccio/streams/-/streams-1.0.0.tgz#d5d24c6747208728b9fd16b908e3932c3fb1f864"

"@verdaccio/types@^2.0.4":
version "2.0.4"
resolved "https://registry.npmjs.org/@verdaccio/types/-/types-2.0.4.tgz#a81566b00e305f3e25e1f2913299b3e57a303f11"
"@verdaccio/types@2.1.0":
version "2.1.0"
resolved "https://registry.npmjs.org/@verdaccio/types/-/types-2.1.0.tgz#1a0b330f96bc63fbc87391c2b5c625fd3be5da84"

JSONStream@^1.0.4:
version "1.3.2"
Expand Down

0 comments on commit ef202a9

Please sign in to comment.