Feedback: Bookshelf must initialize with a DB
See original GitHub issuevar MySql = Bookshelf.initialize({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test',
charset : 'utf8'
}
});
var User = MySql.Model.extend({
tableName: 'users'
});
It’s odd that Bookshelf.Model is not immediately accessible and requires a DB to be specified. We’ve found ourselves in the past using models whether or not they have a database attached or we’ve wanted to attach different databases to different models. However, this becomes weird with the syntax provided.
A suggestion would be to instead allow Bookshelf.Model to be directly extended and the DB to be used be part of the extension.
var MySql = Bookshelf.Database.initialize({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test',
charset : 'utf8'
}
});
var User = Bookshelf.Model.extend({
tableName: 'users',
db: MySql // database connection
});
This would give you the ability to dynamically change the database used on the model or more easily use different databases without having the models be dependent upon which database is being run. It would also let us run Bookshelf.Model and extend a different save for mongodb or something. It also scales better when sharding – should you need multiple databases you can handle them.
Let me know if you already have a way of handling those situations though.
Issue Analytics
- State:
- Created 10 years ago
- Comments:8 (5 by maintainers)
Yeah… Actually, in thinking about it a bit more, it would be pretty cool to come up with a way to do this. It’s the API I was going for originally, but it was dependent on caching the initialized databases inside the library with
Bookshelf.Instances
andKnex.Instances
, which wasn’t a great way to do it.Let’s see, so if you had:
a few different api options come to mind:
or with a static method, returning an empty instance:
or the way you mentioned, sub-classing the model with the database instance.
I think it’d be neat to be able to pass it in the options as well as sub-classing the model… not sure if the static method would be worth it.
The project leadership of Bookshelf recently changed. In an effort to advance the project we close all issues older than one year.
If you think this issue needs to be re-evaluated please post a comment on why this is still important and we will re-open it.
We also started an open discussion about the future of Bookshelf.js here https://github.com/bookshelf/bookshelf/issues/1600. Feel free to drop by and give us your opinion. Let’s make Bookshelf great again