question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feedback: Bookshelf must initialize with a DB

See original GitHub issue
var 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:closed
  • Created 10 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tgriessercommented, Oct 23, 2013

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 and Knex.Instances, which wasn’t a great way to do it.

Let’s see, so if you had:

var User = Bookshelf.Model.extend({
  tableName: 'users'
});

a few different api options come to mind:

new User({...}, {db: mysql}); 

or with a static method, returning an empty instance:

user = User.db(mysql).set({...})

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.

0reactions
crunchtime-alicommented, Jul 19, 2017

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

process keeps running · Issue #64 · bookshelf/bookshelf · GitHub
Bookshelf = require('bookshelf'); db = Bookshelf.initialize({ client: 'sqlite' ... by calling: Bookshelf.knex.client.pool.destroy() , which should drain and ...
Read more >
API Reference - Bookshelf.js
The Bookshelf library is initialized by passing an initialized Knex client instance. ... The transaction object must be passed along in the options...
Read more >
Initialize SQL Server Mart - erwin, Inc.
You can initialize the database using a database authenticated user or a Windows ... After Mart is initialized, the user who initialized must...
Read more >
bookshelf-fields - npm Package Health Analysis - Snyk
Basic usage. First you need to require bookshelf-fields and apply exported plugin to an initialized database instance: Fields = require ' ...
Read more >
A quick starter for Bookshelf.js - DEV Community ‍ ‍
Bookshelf relies on Knex which is a database connection library, to well, ... for initializing knex, which in turn initializes bookshelf.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found