From ef202a9f840809cec49855b70880c73bc7a43f09 Mon Sep 17 00:00:00 2001 From: "@jotadeveloper" <@jotadeveloper> Date: Fri, 20 Apr 2018 08:34:37 +0200 Subject: [PATCH] feat: update database method with callbacks --- plugins/local-storage/package.json | 2 +- .../src/___tests___/local-database.test.js | 39 ++++++++++++++----- plugins/local-storage/src/local-database.js | 31 +++++++++------ plugins/local-storage/yarn.lock | 6 +-- 4 files changed, 52 insertions(+), 26 deletions(-) diff --git a/plugins/local-storage/package.json b/plugins/local-storage/package.json index 47d6628c..173c5907 100644 --- a/plugins/local-storage/package.json +++ b/plugins/local-storage/package.json @@ -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", diff --git a/plugins/local-storage/src/___tests___/local-database.test.js b/plugins/local-storage/src/___tests___/local-database.test.js index a5e8b853..4b1c017b 100644 --- a/plugins/local-storage/src/___tests___/local-database.test.js +++ b/plugins/local-storage/src/___tests___/local-database.test.js @@ -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(); + }); + }); + }); + }); }); }); }); diff --git a/plugins/local-storage/src/local-database.js b/plugins/local-storage/src/local-database.js index daa05775..3edcb1f8 100644 --- a/plugins/local-storage/src/local-database.js +++ b/plugins/local-storage/src/local-database.js @@ -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'; /** @@ -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()); } } @@ -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); } /** @@ -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; } diff --git a/plugins/local-storage/yarn.lock b/plugins/local-storage/yarn.lock index 2aaffae3..3043a6fb 100644 --- a/plugins/local-storage/yarn.lock +++ b/plugins/local-storage/yarn.lock @@ -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"