Skip to content

Commit

Permalink
Further tweaks to README
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamill34 committed Mar 25, 2017
1 parent ed6808f commit 694f6a3
Showing 1 changed file with 39 additions and 37 deletions.
76 changes: 39 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ User.create({ firstName: 'Grayson' })
* @return {Promise(bookshelf.Model)}
*/
create: function (data, options) {
return this.forge(data)
.save(null, options);
return this.forge(data).save(null, options);
}
```

Expand All @@ -93,8 +92,9 @@ create: function (data, options) {
* @return {Promise(bookshelf.Model)} empty model
*/
destroy: function (options) {
options = extend({ require: true }, options);
return this.forge({ [this.prototype.idAttribute]: options.id })
.destroy(options);
.destroy(options);
}
```

Expand All @@ -107,9 +107,9 @@ destroy: function (options) {
* @param {Object} [options] Options used of model.fetchAll
* @return {Promise(bookshelf.Collection)} Bookshelf Collection of Models
*/
findAll: function (options) {
return bookshelf.Collection.forge([], { model: this }).fetch(options);
}
findAll: function (filter, options) {
return this.forge().where(extend({}, filter)).fetchAll(options);
}}
```

#### model.findById
Expand All @@ -122,7 +122,7 @@ findAll: function (options) {
* @return {Promise(bookshelf.Model)}
*/
findById: function (id, options) {
return this.findOne({ [this.prototype.idAttribute]: id }, options)
return this.findOne({ [this.prototype.idAttribute]: id }, options);
}
```

Expand All @@ -136,8 +136,9 @@ findById: function (id, options) {
* @param {Boolean} [options.require=false]
* @return {Promise(bookshelf.Model)}
*/
findOne: function (data, options) {
return this.forge(data).fetch(options);
findOne: function (query, options) {
options = extend({ require: true }, options);
return this.forge(query).fetch(options);
}
```

Expand All @@ -151,12 +152,12 @@ findOne: function (data, 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);
})
return this.findOne(data, extend(options, { require: false }))
.bind(this)
.then(function (model) {
var defaults = options && options.defaults;
return model || this.create(extend(defaults, data), options);
});
}
```

Expand All @@ -171,36 +172,37 @@ findOrCreate: function (data, options) {
* @param {Boolean} [options.patch=true]
* @param {Boolean} [options.require=true]
* @return {Promise(bookshelf.Model)}
*//
*/
update: function (data, options) {
_.defaults(options, {
patch: true
});
return this.forge({ [this.prototype.idAttribute]: options.id }).fetch(options)
.then(function (model) {
if (model) {
return model.save(data, options);
}
})
options = extend({ patch: true, require: true }, options);
return this.forge({ [this.prototype.idAttribute]: options.id }).fetch(options);
.then(function (model) {
return model ? model.save(data, options) : undefined;
});
}
```

### model.upsert
```js
/**
* Upsert - select a model based on data and update if found, insert if not found
* @param {Object} selectData Data for select
* @param {Object} updateData Data for update
* @param {Object} [options] Options for model.save
* @return {Promise(bookshelf.Model)} edited Model
*/
* Select a model based on data and update if found, insert if not found
* @param {Object} selectData Data for select
* @param {Object} updateData Data for update
* @param {Object} [options] Options for model.save
*/
upsert: function (selectData, updateData, options) {
return this.findOne(selectData, extend(options, { require: false }))
.bind(this)
.then(function (model) {
return model
? model.save(updateData, extend({ patch: true }, options))
: this.create(extend(selectData, updateData), options)
})
.bind(this)
.then(function (model) {
return model
? model.save(
updateData,
extend({ patch: true, method: 'update' }, options)
)
: this.create(
extend(selectData, updateData),
extend(options, { method: 'insert' })
)
});
}
```

0 comments on commit 694f6a3

Please sign in to comment.