Comments (10)
Maybe you should be able to declaratively set the where
clause on the table.
class UserCollection extends Bookshelf.Collection
tableName: 'users'
where: {id: 1}
users = new UserCollection
users.fetch().then (models) -> ...
The where
is then like the url
in Backbone.
At the moment fetch
gets the entire table which is not what you want most of the time.
Or perhaps just adding a where
method onto the collection directly?
from bookshelf.
You can use the query
method on both the Model or Collection to do something like this:
new UserCollection().query('where', {active: true}).fetch().then(function(collection) {
Where the first param will be the Knex
method you want to call, and the rest of the arguments will be apply'ed on that function, and ultimately returns the model or collection you're working with...
from bookshelf.
Ok. I can just extend Bookshelf.Collection
if need be.
from bookshelf.
I think ultimately I may add some class finder class methods, I just wanted to start off without them.
from bookshelf.
Going to close this for now, will reopen down the line if/when static finder methods are something that make sense to add (probably after the core api is nailed down a bit further).
from bookshelf.
Alright, so I'm starting a branch called active-record which will extend the base of Bookshelf to provide an almost 1-to-1 map with Rails' Active-record.
Feel free to take a look and see the direction that's being taken over there
from bookshelf.
Great to see this reopened :) I'll take a look.
from bookshelf.
I've got some cool ideas for how this could be done with the bluebird promise lib's error specific catch
, and by creating a deferred chain for the model calls, until a then
coerces it to a promise:
Err = require 'bookshelf/errors'
Model.find(1).destroy().then ->
console.log('model destroyed');
.catch Err.ModelNotFound, (err) ->
console.log('model not found' + err.stack);
.catch Err.ModelNotDestroyed, (err) ->
console.log('model could not be destroyed' + err.stack);
which would expand to:
Err = require 'bookshelf/errors'
Model.find(1).then ->
return model.destroy()
.then ->
console.log('model destroyed');
.catch Err.ModelNotFound, (err) ->
console.log('model not found' + err.stack);
.catch Err.ModelNotDestroyed, (err) ->
console.log('model could not be destroyed' + err.stack);
In that instance it only saves a line, but I feel like this idea could make for a really cool looking chaining api here.
/cc @rstacruz
from bookshelf.
That looks great.
p.s. Ive setup a simple temp solution using simple class property wrappers
like these for now -
https://github.com/TryGhost/Ghost/blob/master/core/server/models/base.js
On Monday, November 25, 2013, Tim Griesser wrote:
I've got some cool ideas for how this could be done with the bluebird
promise lib's error specific catch, and by creating a deferred chain for
the model calls, until a then coerces it to a promise:Err = require 'bookshelf/errors'Model.find(1).destroy().then ->
console.log('model destroyed');.catch Err.ModelNotFound, (err) ->
console.log('model not found' + err.stack); .catch Err.ModelNotDestroyed, (err) ->
console.log('model could not be destroyed' + err.stack);which would expand to:
Err = require 'bookshelf/errors'Model.find(1).then ->
return model.destroy().then ->
console.log('model destroyed');.catch Err.ModelNotFound, (err) ->
console.log('model not found' + err.stack); .catch Err.ModelNotDestroyed, (err) ->
console.log('model could not be destroyed' + err.stack);In that instance it only saves a line, but I feel like this idea could
make for a really cool looking chaining api here./cc @rstacruz https://github.com/rstacruz
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/27#issuecomment-29226238
.
from bookshelf.
Added model.fetchAll
for fetching a collection, as well as .where
, and .query
as both static and prototype methods to the model.
So with that, you can now do
User.where({id: 1}).fetch().then(function(model) {
User.where('active', true).fetchAll().then(function(collection) { ...
User.query({where: {active: true}, orWhere: {admin: true}})
.fetchAll().then(function(collection) {...
which should provide a nice syntax for dealing with the most common use cases until the more robust lib which extends bookshelf with scopes/relations/validations etc is done.
from bookshelf.
Related Issues (20)
- Change primary key value
- withRelated TypeScript error HOT 1
- Bookshelf.js update query not returning expected data
- Bookshelf.js update query not returning expected data
- Enhancement: ignore extra keys in withRelated option?
- ErrorCtor [CustomError]: EmptyResponse HOT 1
- How to save a object with hasMany/hasOne relations ?
- Make has_one relation with another model with composite id, Is it possible?
- [email protected]" has incorrect peer dependency "knex@>=0.15.0 <0.22.0" HOT 23
- Unable to use method named as attributes( ) in a model.
- Doc: MSSQL support
- How to know if a transaction has been committed? HOT 1
- Typescript models do not have relationship functions HOT 1
- Loop of transactions or transactions + promise pool
- Remove freenode references HOT 1
- Bookshelf using vulnerable version of lodash
- Property 'fetchPage' does not exist on type.
- Is Bookshelf actively maintained? HOT 1
- Limited SQL Injection Vulnerability in Bookshelf.js
- Specify sub-ordinate database name to connect to through Bookshelf model
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bookshelf.