bookshelf-modelbase
Advanced tools
Comparing version 1.5.1 to 2.0.0
@@ -16,4 +16,4 @@ var _ = require('lodash'); | ||
id: Joi.any().optional(), | ||
createdAt: Joi.date().optional(), | ||
updatedAt: Joi.date().optional() | ||
created_at: Joi.date().optional(), | ||
updated_at: Joi.date().optional() | ||
}) | ||
@@ -27,3 +27,3 @@ } else { | ||
hasTimestamps: ['createdAt', 'updatedAt'], | ||
hasTimestamps: ['created_at', 'updated_at'], | ||
@@ -37,18 +37,4 @@ validateSave: function () { | ||
} | ||
}, | ||
} | ||
parse: function (response) { | ||
return _.reduce(response, function (memo, val, key) { | ||
memo[_.str.camelize(key)] = val; | ||
return memo; | ||
}, {}); | ||
}, | ||
// camelCase attributes -> snake_case db columns | ||
format: function (attrs) { | ||
return _.reduce(attrs, function (memo, val, key) { | ||
memo[_.str.underscored(key)] = val; | ||
return memo; | ||
}, {}); | ||
}, | ||
}, { | ||
@@ -115,2 +101,17 @@ | ||
.destroy(options); | ||
}, | ||
/** | ||
* Find or create - try and find the model, create one if not found | ||
* @param {Object} data | ||
* @param {Object} options | ||
* @return {Promise(bookshelf.Model)} single Model | ||
*/ | ||
findOrCreate: function (data, options) { | ||
var self = this; | ||
return self.findOne(data, options) | ||
.then(function (model) { | ||
return model ? model : self.create(data, options); | ||
}) | ||
} | ||
@@ -117,0 +118,0 @@ |
{ | ||
"name": "bookshelf-modelbase", | ||
"version": "1.5.1", | ||
"version": "2.0.0", | ||
"description": "Extensible ModelBase for bookshelf-based model layers", | ||
@@ -11,10 +11,10 @@ "main": "./lib", | ||
"type": "git", | ||
"url": "git://github.com/enjoy/bookshelf-modelbase.git" | ||
"url": "git://github.com/bsiddiqui/bookshelf-modelbase.git" | ||
}, | ||
"author": "Enjoy", | ||
"author": "Basil Siddiqui <basil.siddiqui@gmail.com>, Grayson Chao <grayson.chao@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/enjoy/bookshelf-modelbase/issues" | ||
"url": "https://github.com/bsiddiqui/bookshelf-modelbase/issues" | ||
}, | ||
"homepage": "https://github.com/enjoy/bookshelf-modelbase", | ||
"homepage": "https://github.com/bsiddiqui/bookshelf-modelbase", | ||
"dependencies": { | ||
@@ -21,0 +21,0 @@ "bluebird": "^2.3.11", |
@@ -1,5 +0,5 @@ | ||
#bookshelf-modelbase | ||
[![Build Status](https://travis-ci.org/enjoy/bookshelf-modelbase.svg?branch=master)](https://travis-ci.org/enjoy/bookshelf-modelbase) [![Code Climate](https://codeclimate.com/github/enjoy/bookshelf-modelbase/badges/gpa.svg)](https://codeclimate.com/github/enjoy/bookshelf-modelbase) [![Test Coverage](https://codeclimate.com/github/enjoy/bookshelf-modelbase/badges/coverage.svg)](https://codeclimate.com/github/enjoy/bookshelf-modelbase) [![Version](https://badge.fury.io/js/bookshelf-modelbase.svg)](http://badge.fury.io/js/bookshelf-modelbase) [![Downloads](http://img.shields.io/npm/dm/bookshelf-modelbase.svg)](https://www.npmjs.com/package/bookshelf-modelbase) | ||
# bookshelf-modelbase | ||
[![Build Status](https://travis-ci.org/bsiddiqui/bookshelf-modelbase.svg?branch=master)](https://travis-ci.org/bsiddiqui/bookshelf-modelbase) [![Code Climate](https://codeclimate.com/github/bsiddiqui/bookshelf-modelbase/badges/gpa.svg)](https://codeclimate.com/github/bsiddiqui/bookshelf-modelbase) [![Test Coverage](https://codeclimate.com/github/bsiddiqui/bookshelf-modelbase/badges/coverage.svg)](https://codeclimate.com/github/bsiddiqui/bookshelf-modelbase) [![Version](https://badge.fury.io/js/bookshelf-modelbase.svg)](http://badge.fury.io/js/bookshelf-modelbase) [![Downloads](http://img.shields.io/npm/dm/bookshelf-modelbase.svg)](https://www.npmjs.com/package/bookshelf-modelbase) | ||
##Why | ||
## Why | ||
[Bookshelf.js](https://github.com/tgriesser/bookshelf) is awesome. However, | ||
@@ -22,3 +22,3 @@ we found ourselves extending `bookshelf.Model` for the same reasons over and | ||
### Features | ||
* Adds timestamps (`createdAt` and `updatedAt`) | ||
* Adds timestamps (`created_at` and `updated_at`) | ||
@@ -29,8 +29,5 @@ * Validate own attributes on save using [Joi](https://github.com/hapijs/joi). | ||
* Writes attributes to the db as `snake_case`, | ||
but exposes them in code as `camelCase`. | ||
* Naive CRUD methods - `findAll`, `findOne`, `findOrCreate`, `create`, `update`, and `destroy` | ||
* Naive CRUD methods - `findAll`, `findOne`, `create`, `update`, and `destroy` | ||
##Usage | ||
## Usage | ||
```javascript | ||
@@ -71,3 +68,16 @@ var db = require(knex)(require('./knexfile')); | ||
### CRUD | ||
### API | ||
#### model.create | ||
#### model.destroy | ||
#### model.findAll | ||
#### model.findOne | ||
#### model.findOrCreate | ||
#### model.update | ||
```javascript | ||
@@ -74,0 +84,0 @@ /** |
@@ -22,3 +22,5 @@ var Joi = require('joi'); | ||
tableName: 'test_table', | ||
validate: { name: Joi.string().valid('hello', 'goodbye') } | ||
validate: { | ||
name: Joi.string().valid('hello', 'goodbye') | ||
} | ||
}); | ||
@@ -47,16 +49,2 @@ | ||
describe('parse', function () { | ||
it('should convert snake case to camel case', function () { | ||
return expect(specimen.parse({ variable_name: 'snake_case' })) | ||
.to.eql({ variableName: 'snake_case' }) | ||
}); | ||
}); | ||
describe('format', function () { | ||
it('should convert camel case to snake case', function () { | ||
return expect(specimen.format({ variableName: 'snake_case' })) | ||
.to.eql({ variable_name: 'snake_case' }) | ||
}); | ||
}); | ||
describe('validateSave', function () { | ||
@@ -152,2 +140,20 @@ it('should validate own attributes', function () { | ||
}); | ||
describe('findOrCreate', function () { | ||
it('should find an existing model', function () { | ||
return specimenClass.findOrCreate() | ||
.then(function (model) { | ||
expect(model).to.be.instanceof(specimenClass); | ||
}); | ||
}); | ||
it('should create when model not found', function () { | ||
return specimenClass.findOrCreate({ | ||
name: 'goodbye' | ||
}) | ||
.then(function (model) { | ||
return expect(model.id).to.not.eql(specimen.id); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
271
139
15251
11