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.

Understanding custom collections

See original GitHub issue

In my experience, collections are highly random. Even simple usage makes no sense to me.

class OtherModel extends bookshelf.Model {
  get tableName () { return 'other_models' }
}

class MyModel extends bookshelf.Model {
  get tableName () { return 'my_models' }

  otherRelation () {
    return this.hasMany(OtherModel)
  }
}

class MyOtherCollection extends bookshelf.Collection {
  get model () { return OtherModel }
}

MyModel
  .forge({ id: 1 })
  .otherRelation()
  .fetch()
  .then((myOtherModels) => {
    console.log(myOtherModels instanceof bookshelf.Collection) // true
    console.log(myOtherModels instanceof MyOtherCollection) // false
  })

My question is, is this behaviour intended? If yes, I’m quite lost on how I should reason about collections in general.

Am I perhaps going about this wrong and completely missed a core principle?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ricardogracacommented, Apr 26, 2018

Interesting. With #1806 the registry plugin will be promoted to core, so this feature will be built-in. It would be a good idea to add these instruction on how to create a custom reusable collection to a guide explaining the whole process.

0reactions
fl0wcommented, Apr 26, 2018

If anyone else stumbles upon this, here’s the correct way to register a custom collection when using the registry plugin:

class MyModels extends bookshelf.Collection {
  get model () {
    return MyModel
  }
}
class MyModel extends bookshelf.Model {
  static collection (...args) {
    return new MyModels(...args)
  }
}

bookshelf.collection('MyModels', MyModels) // This is what I previously forgot
bookshelf.model('User', User)

After this bootstrap everything runs as expected currently (except that Collection.fetched event isn’t fired on a relation call as I would have expected - but Collection.fetching does).

Read more comments on GitHub >

github_iconTop Results From Across the Web

What Are Custom Collections? - Virtuous Support
In Virtuous, you can track data and values specific to your organization by using Custom Fields, and even group them together for clear......
Read more >
4. Custom Collections for a Custom Library - Understanding System ...
In this chapter, you will learn the fundamental aspects of implementing custom collections using features and the organization required for any .
Read more >
4 Reasons a Custom Collections Software benefit Your ...
Both out-of-the-box software and custom software have their own benefits. ... As a collections professional, you know and understand the collections process ...
Read more >
CMS Collections - Webflow University
CMS Collections are content types with different structures in the Webflow ... structure by either choosing a template or adding custom collection fields....
Read more >
Collections in Java - Everything You MUST Know - DigitalOcean
Collections are used in every programming language and when Java arrived, it also came with few Collection classes – Vector, Stack, Hashtable, ...
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