Skip to content

Commit ef202a9

Browse files
@jotadevelopersergiohgz
authored andcommitted
feat: update database method with callbacks
1 parent b73e497 commit ef202a9

File tree

4 files changed

+52
-26
lines changed

4 files changed

+52
-26
lines changed

plugins/local-storage/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"devDependencies": {
2626
"@commitlint/cli": "^6.0.2",
2727
"@commitlint/config-conventional": "^6.0.2",
28-
"@verdaccio/types": "^2.0.4",
28+
"@verdaccio/types": "2.1.0",
2929
"babel-cli": "6.26.0",
3030
"babel-core": "6.26.0",
3131
"babel-eslint": "8.2.2",

plugins/local-storage/src/___tests___/local-database.test.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,40 @@ describe('Local Database', () => {
3636
});
3737

3838
describe('Database CRUD', () => {
39-
test('should add an item to database', () => {
39+
test('should add an item to database', done => {
4040
const pgkName = 'jquery';
41-
expect(locaDatabase.get()).toHaveLength(0);
41+
locaDatabase.get((err, data) => {
42+
expect(err).toBeNull();
43+
expect(data).toHaveLength(0);
4244

43-
locaDatabase.add(pgkName);
44-
expect(locaDatabase.get()).toHaveLength(1);
45+
locaDatabase.add(pgkName, err => {
46+
expect(err).toBeNull();
47+
locaDatabase.get((err, data) => {
48+
expect(err).toBeNull();
49+
expect(data).toHaveLength(1);
50+
done();
51+
});
52+
});
53+
});
4554
});
4655

47-
test('should remove an item to database', () => {
56+
test('should remove an item to database', done => {
4857
const pgkName = 'jquery';
49-
expect(locaDatabase.get()).toHaveLength(0);
50-
locaDatabase.add(pgkName);
51-
locaDatabase.remove(pgkName);
52-
53-
expect(locaDatabase.get()).toHaveLength(0);
58+
locaDatabase.get((err, data) => {
59+
expect(err).toBeNull();
60+
expect(data).toHaveLength(0);
61+
locaDatabase.add(pgkName, err => {
62+
expect(err).toBeNull();
63+
locaDatabase.remove(pgkName, err => {
64+
expect(err).toBeNull();
65+
locaDatabase.get((err, data) => {
66+
expect(err).toBeNull();
67+
expect(data).toHaveLength(0);
68+
done();
69+
});
70+
});
71+
});
72+
});
5473
});
5574
});
5675
});

plugins/local-storage/src/local-database.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import _ from 'lodash';
55
import Path from 'path';
66
import LocalFS from './local-fs';
77
import mkdirp from 'mkdirp';
8-
import type { StorageList, LocalStorage, Logger, Config } from '@verdaccio/types';
8+
import type { StorageList, LocalStorage, Logger, Config, Callback } from '@verdaccio/types';
99
import type { IPackageStorage, ILocalData } from '@verdaccio/local-storage';
1010

1111
/**
@@ -45,10 +45,10 @@ class LocalDatabase implements ILocalData {
4545
* @param {*} name
4646
* @return {Error|*}
4747
*/
48-
add(name: string) {
48+
add(name: string, cb: Callback) {
4949
if (this.data.list.indexOf(name) === -1) {
5050
this.data.list.push(name);
51-
return this._sync();
51+
cb(this._sync());
5252
}
5353
}
5454

@@ -57,21 +57,27 @@ class LocalDatabase implements ILocalData {
5757
* @param {*} name
5858
* @return {Error|*}
5959
*/
60-
remove(name: string) {
61-
const pkgName = this.get().indexOf(name);
62-
if (pkgName !== -1) {
63-
this.data.list.splice(pkgName, 1);
64-
}
60+
remove(name: string, cb: Callback) {
61+
this.get((err, data) => {
62+
if (err) {
63+
cb(new Error('error on get'));
64+
}
65+
66+
const pkgName = data.indexOf(name);
67+
if (pkgName !== -1) {
68+
this.data.list.splice(pkgName, 1);
69+
}
6570

66-
return this._sync();
71+
cb(this._sync());
72+
});
6773
}
6874

6975
/**
7076
* Return all database elements.
7177
* @return {Array}
7278
*/
73-
get() {
74-
return this.data.list;
79+
get(cb: Callback) {
80+
cb(null, this.data.list);
7581
}
7682

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

94100
try {
95101
fs.writeFileSync(this.path, JSON.stringify(this.data));
102+
return null;
96103
} catch (err) {
97104
return err;
98105
}

plugins/local-storage/yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@
212212
version "1.0.0"
213213
resolved "https://registry.npmjs.org/@verdaccio/streams/-/streams-1.0.0.tgz#d5d24c6747208728b9fd16b908e3932c3fb1f864"
214214

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

219219
JSONStream@^1.0.4:
220220
version "1.3.2"

0 commit comments

Comments
 (0)