Comments (9)
So this is probably what you want:
class MyModel extends Bookshelf.Model
items: (id) ->
@belongsToMany(Item)
new MyModel(id: 1).items().query('where', 'id', id).fetch().then (c) ->
c.at(0)
The reason I didn't want to put finder methods in is because there's a million permutations of them, and once you start adding direct support for them the API can quickly become a bit of a mess.
By using query
the way described above you can add them wherever you need... and by not doing query()
but instead query('method', args*)
you stay on the context of the current model/collection you're accessing rather than dropping down into the Knex
builder.
Though I agree some way to fetch/return a single model from a collection could a good addition in this case (hadn't considered relations), I'll take a look into supporting it...
And yeah, a BaseModel
is exactly what I'm thinking would be a good way to go... so you can build out the API you'd find most intuitive... it's a little more verbose the first time but it makes the library easier to wrap your head around.
Also, a get raw json as the result when what I would like is a Collection of Models.
Sorry... I'm not sure I follow... you do or do not want that?
from bookshelf.
Sorry... I'm not sure I follow... you do or do not want that?
query().where()
produces the json from the db. When I wanted a Model or a Collection of Models.
But you have covered this above:
you stay on the context of the current model/collection you're accessing rather than dropping down into the Knex builder.
from bookshelf.
Yeah, query('where', ...
will do it.
from bookshelf.
And yeah, a BaseModel is exactly what I'm thinking would be a good way to go... so you can build out the API you'd find most intuitive... it's a little more verbose the first time but it makes the library easier to wrap your head around.
I agree. The thing that drew me to Bookshelf was the fact that it was layered really nicely. In Sequelize and most ORMs if you find something you can't do you have to drop straight down to SQL, and extensibility is difficult.
from bookshelf.
Yeah, query('where', ... will do it.
Would be nice if you could pass in a hash.
from bookshelf.
Would be nice if you could pass in a hash.
Interesting, so like query({where: {id: 1}, orWhere: {id: 2})
from bookshelf.
Yep. That would be great.
Also, if a query is called on a collection I think it should always return the models as a collection of models.
If the user wants to run a raw query they should use Bookshelf.Knex
.
from bookshelf.
Yeah... that's the idea, unless you call query()
with no arguments which returns the underlying Knex
builder, which I think I'll keep in there.
Take a look at the last commit for the object support.
from bookshelf.
So I think that I'm going to revisit the idea of fetching a single object from a collection at some point down the road... for now I'm going to have the object support for the query
chain be the solution for this for now.
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.