From 559df105b6fdfd4fecc789b53145cee0f407fa4c Mon Sep 17 00:00:00 2001 From: geek Date: Thu, 11 May 2017 15:28:32 -0500 Subject: [PATCH] Use portal-data module --- portal-api/lib/data/index.js | 152 ----------------------------------- portal-api/lib/index.js | 4 +- portal-api/package.json | 4 +- 3 files changed, 4 insertions(+), 156 deletions(-) delete mode 100644 portal-api/lib/data/index.js diff --git a/portal-api/lib/data/index.js b/portal-api/lib/data/index.js deleted file mode 100644 index 780ac4dd..00000000 --- a/portal-api/lib/data/index.js +++ /dev/null @@ -1,152 +0,0 @@ -'use strict'; - -const Hoek = require('hoek'); -const Penseur = require('penseur'); - - -const internals = { - defaults: { - name: 'portal' - } -}; - - -module.exports = class Data { - constructor (options) { - const settings = Hoek.applyToDefaults(options || {}, internals.defaults); - const name = settings.name; - delete settings.name; - - // Penseur will assert that the options are correct - this._db = new Penseur.Db(name, settings); - } - - connect (cb) { - this._db.establish(['activities', 'datacenters', 'deployments', 'manifests', 'metrics'], cb); - } - - createDeployment (deployment) { - return new Promise((resolve, reject) => { - deployment.services = []; - deployment.state = { current: 'stopped' }; - - this._db.deployments.insert(deployment, (err, key) => { - if (err) { - return reject(err); - } - - deployment.id = key; - - resolve(deployment); - }); - }); - } - - getDeployment (id) { - return new Promise((resolve, reject) => { - this._db.deployments.get(id, (err, deployment) => { - return err ? reject(err) : resolve(deployment); - }); - }); - } - - updateDeployment (deployment) { - return new Promise((resolve, reject) => { - this._db.deployments.update(deployment.id, deployment, (err) => { - return err ? reject(err) : resolve(deployment); - }); - }); - } - - deleteDeployment (id) { - return new Promise((resolve, reject) => { - this._db.deployments.remove(id, (err) => { - return err ? reject(err) : resolve(); - }); - }); - } - - getDeployments () { - return new Promise((resolve, reject) => { - this._db.deployments.all((err, deployments) => { - return err ? reject(err) : resolve(deployments); - }); - }); - } - - getDatacenters () { - return new Promise((resolve, reject) => { - this._db.datacenters.all((err, datacenters) => { - return err ? reject(err) : resolve(datacenters || []); - }); - }); - } - - createManifest (deploymentId, manifest) { - return new Promise((resolve, reject) => { - manifest.deploymentId = deploymentId; - manifest.created = Date.now(); - - this._db.manifests.insert(manifest, (err, id) => { - if (err) { - return reject(err); - } - - manifest.id = id; - resolve(manifest); - }); - }); - } - getManifest (id) { - return new Promise((resolve, reject) => { - this._db.manifests.get(id, (err, manifest) => { - return err ? reject(err) : resolve(manifest); - }); - }); - } - - getActivities (deploymentId) { - return new Promise((resolve, reject) => { - this._db.activities.query({ deploymentId }, (err, activities) => { - return err ? reject(err) : resolve(activities || []); - }); - }); - } - - getMetrics (deploymentId) { - return new Promise((resolve, reject) => { - this._db.metrics.query({ deploymentId }, (err, metrics) => { - return err ? reject(err) : resolve(metrics || []); - }); - }); - } - - getServices (deploymentId) { - return new Promise((resolve, reject) => { - this._db.deployments.get(deploymentId, { filter: 'services' }, (err, deployment) => { - return err ? reject(err) : resolve(deployment.services); - }); - }); - } - updateService (deploymentId, service) { - return new Promise((resolve, reject) => { - this._db.deployments.get(deploymentId, { filter: 'services' }, (err, deployment) => { - if (err) { - return reject(err); - } - - const services = deployment.services.map((currentService) => { - if (currentService.name === service.name) { - currentService.count = service.count; - } - - return currentService; - }); - - this._db.deployments.update(deploymentId, { services }, (err, keys) => { - return err ? reject(err) : resolve(service); - }); - }); - }); - } -}; diff --git a/portal-api/lib/index.js b/portal-api/lib/index.js index 4948a393..2ff671b5 100644 --- a/portal-api/lib/index.js +++ b/portal-api/lib/index.js @@ -1,14 +1,14 @@ 'use strict'; const GraphqlHapi = require('graphql-server-hapi'); -const Data = require('./data'); +const PortalData = require('portal-data'); const Graphql = require('./models/graphql'); const Pack = require('../package.json'); const Routes = require('./routes'); module.exports = function (server, options, next) { - const data = new Data(options.data); + const data = new PortalData(options.data); data.connect((err) => { if (err) { return next(err); diff --git a/portal-api/package.json b/portal-api/package.json index 6dc0f885..b7b20ce4 100644 --- a/portal-api/package.json +++ b/portal-api/package.json @@ -8,7 +8,7 @@ "lint": "belly-button", "rethinkdb-up": "docker run -d -p 8080:8080 -p 28015:28015 -p 29015:29015 --name rethinkdb rethinkdb", "rethinkdb-down": "docker rm -f rethinkdb", - "test": "npm run lint && lab -t 94" + "test": "npm run lint && lab -t 97" }, "keywords": [], "author": "wyatt", @@ -28,6 +28,6 @@ "graphql-server-hapi": "^0.7.2", "hoek": "^4.1.1", "joi": "^10.4.1", - "penseur": "^7.8.1" + "portal-data": "^1.0.0" } }