Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cmbf-hapi-restmodel

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cmbf-hapi-restmodel - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

101

lib/base-model.js
var Joi = require('joi'),
P = require('bluebird'),
_ = require('lodash');

@@ -7,3 +8,3 @@

var dbName = config.get('plugins:restmod:dbname', 'MAIN');
var dbName = config.get('plugins:restmod:dbname','MAIN');

@@ -17,21 +18,19 @@ class BaseModel {

static get validation() {
var params = {};
params[this.constructor.idField] = Joi.string().required();
return {
create: {
payload: BaseModel.Schema
payload: this.constructor.Schema
},
update: {
payload: BaseModel.Schema
payload: this.constructor.Schema
},
upsert: {
payload: BaseModel.Schema
payload: this.constructor.Schema
},
remove: {
params: {
id: Joi.string().required()
}
params: params
},
show: {
params: {
id: Joi.string().required()
}
params: params
},

@@ -47,2 +46,6 @@ list: {

static get auth() {
return "token";
}
static get db() {

@@ -52,4 +55,4 @@ return server.plugins['covistra-mongodb'][dbName];

static get auth() {
return "token";
static get idField() {
return "id";
}

@@ -60,11 +63,11 @@

static get collection() {
throw new Error("must be overriden");
throw new Error("collection must be overriden for model "+this);
}
static get endpoint() {
throw new Error("must be overriden");
throw new Error("endpoint must be overriden for model "+this);
}
static get name() {
throw new Error("must be overriden");
throw new Error("name must be overriden for model "+this);
}

@@ -77,9 +80,8 @@

static list(options) {
log.debug("list %s", BaseModel.name);
log.debug("list %s", this.constructor.name);
options = options || {};
var coll = BaseModel.collection;
var cursor = coll.find(options.filter);
return P.promisify(cursor.toArray, cursor)().then(function(results) {
var coll = this.constructor.db.collection(this.constructor.collection);
return coll.find(options.filter).toArray().then((results) => {
if(options.wrap) {
return P.map(results, function(r){ return BaseModel.wrap(r)});
return P.map(results, function(r){ return this.constructor.wrap(r)});
}

@@ -93,7 +95,9 @@ else {

static show(id) {
log.debug("show %s ", BaseModel.name, id);
log.debug("show %s ", this.constructor.name, id);
var _this = this;
var coll = BaseModel.collection;
return P.promisify(coll.findOne, coll)({id: id}).then(function(data) {
return _this.wrap(data);
var coll = this.constructor.db.collection(this.constructor.collection);
var q = {};
q[this.constructor.idField] = id;
return coll.findOne(q).then(function(data) {
return _this.constructor.wrap(data);
});

@@ -103,4 +107,5 @@ }

static wrap(data) {
if(data instanceof BaseModel)
return BaseModel.create(data);
if(!(data instanceof this.constructor)) {
return new this(data);
}
else

@@ -110,8 +115,15 @@ return data;

/**
* Helpful wrapper to create instances we know for sure doesn't exists
* @returns {*}
*/
create() {
return this.save(null,{upsert: true});
}
save(data, options) {
data = data || this;
log.debug("Save", data.id || this.id);
log.debug("Save", data[this.constructor.idField] || this[this.constructor.idField]);
options = options || {upsert: false};
var coll = BaseModel.collection;
var val = Joi.validate(data, BaseModel.Schema);
var val = Joi.validate(data, this.constructor.Schema);
if(val.error) {

@@ -122,4 +134,9 @@ throw val.error;

// Update our internal values
_.assign(this, val.value);
return P.promisify(coll.update, coll)({id: this.id}, {$set: _.omit(val.value, 'id') }, { multi: false, upsert: options.upsert});
var fields = _.omit(this.toJSON(), this.constructor.idField);
var q = {};
q[this.constructor.idField] = this[this.constructor.idField];
log.debug("Retrieving collection %s from db", this.constructor.collection, this.constructor.db);
var coll = this.constructor.db.collection(this.constructor.collection);
return coll.updateOne(q, {$set: fields }, { upsert: options.upsert});
}

@@ -130,14 +147,18 @@ }

log.debug("Remove", id);
var coll = BaseModel.collection;
return P.promisify(coll.removeOne, coll)({id: id});
var coll = this.constructor.db.collection(this.constructor.collection);
var q= {};
q[this.constructor.idField] = id;
return coll.removeOne(q);
}
remove() {
log.debug("Remove", this.id);
var coll = BaseModel.collection;
return P.promisify(coll.removeOne, coll)({id: this.id});
log.debug("Remove", this[this.constructor.idField]);
var coll = this.constructor.db.collection(this.constructor.collection);
var q= {};
q[this.constructor.idField] = this[this.constructor.idField];
return coll.removeOne(q);
}
toJSON() {
var val = Joi.validate(this, BaseModel.Schema);
var val = Joi.validate(this, this.constructor.Schema);
if(val.error) {

@@ -152,4 +173,6 @@ throw val.error;

isUnique() {
var coll = BaseModel.collection;
return P.promisify(coll.findOne, coll)({id: this.id}).then(function(existing) {
var coll = this.constructor.db.collection(this.constructor.collection);
var q= {};
q[this.constructor.idField] = this[this.constructor.idField];
return coll.findOne(q).then(function(existing) {
return !existing;

@@ -156,0 +179,0 @@ });

@@ -14,3 +14,4 @@ var Calibrate = require('calibrate'),

return model.isUnique().then(function() {
return model.save().then(function() {
log.trace("Model %s is unique", model.id);
return model.create().then(function() {
log.debug("Model %s instance %s was successfully created", Model.name, model.id);

@@ -17,0 +18,0 @@ return model.toJSON();

{
"name": "cmbf-hapi-restmodel",
"version": "0.3.0",
"version": "0.4.0",
"dependencies": {

@@ -5,0 +5,0 @@ "bluebird": "^3.0.6",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc