2.7.0 • Published 9 months ago

bookshelf-relations v2.7.0

Weekly downloads
10,212
License
MIT
Repository
github
Last release
9 months ago

Bookshelf Relations

Insert, update and remove relationships on your Bookshelf models. This plugin supports all relationship types: belongs-to, belongs-to-many has-one and has-many.

Install

npm install bookshelf-relations --save

or

yarn add bookshelf-relations

Usage

Pre-word

  • It's highly recommended to insert/update/delete your models within transactions when using this plugin, because updating nested relationships requires additional queries to the database. Otherwise if an error occurs during any query, you can't expect data to be rolled back fully.

Options

hooktypedefaultdescription
autoHookBooleantrueThe plugin takes over everything for you and hooks into the Bookshelf workflow.
allowedOptionsArray-An array of allowed model options the plugin passes on when executing Bookshelf queries.
unsetRelationsBooleantrueThe plugin will unset the relations after they are detected (e.g. model.unset('tags')). If you are disabling "autoHook", you manually need to unset the relations.
editRelationsBooleantrueIf false value is passed in the plugin will not edit the properties of related models unless specified otherwise on model-level relationshipConfig through editable flag.
extendChangedString-Define a variable name and Bookshelf-relations will store the information which relations were changed.
attachPreviousRelationsBooleanfalseAn option to attach previous relations. Bookshelf-relations attaches this information as _previousRelations on the target parent model.
hooksObject-belongsToMany: Hook into the process of updating belongsToMany relationships. Example: hooks: {belongsToMany: {after: Function, before: Function}}

Take a look at the plugin configuration in Ghost.

Hooks

Hooks can be defined globally on the plugin options as described above, or they can be defined on a model by model basis. A model hook will replace a global hook if present - only one of them will run.

Hook should have a structure like so:

hooks: {
    belongsToMany: {
        before() {},
        after() {}
    }
}

The hooks we support are:

  • belongsToMany
    • before / beforeRelationCreated
    • after / afterRelationCreated

Either name can be used but the shorter name will be preferred if both exist.

Automatic

The plugin will automatically deal with relationships upserts and cascading deletions through hasMany relationships. It's required to register your relationships in Bookshelf before you can use bookshelf-relations, see this example.

  1. Register the plugin.
    bookshelf.plugin('bookshelf-relations', {options});
  1. Define your relationships on each model.
    bookshelf.Model.extend({
        relationships: ['tags', 'news']
    }, {...});

To opt-out of automatic child record deletion for hasMany relationships it's possible to define per-relationship config:

    bookshelf.Model.extend({
        relationships: ['tags', 'news', 'events'],
        relationshipConfig: {
            events: {
                destroyRelated: false
            }
        }
    });

To opt-in for automatic relation editing pass in editable flag in per-relationship config:

    bookshelf.Model.extend({
        relationships: ['tags', 'news', 'events'],
        relationshipConfig: {
            tags: {
                editable: true
            }
        }
    });

Manual

You manually need to call the plugin to update relationships. It's required to register your relationships in Bookshelf before you can use bookshelf-relations, see this example.

  1. Register the plugin.
    bookshelf.plugin('bookshelf-relations', {options});
  1. Manually call bookshelf-relations.
    bookshelf.manager.updateRelations({
        model: model,
        relations: {tags: [...]},
        pluginOptions: {options}
    })

Notations

// will detach & remove all existing relations
model.set('tags', []);

// will check if "test" exists and if not, it will insert the target tag
// will remove all previous relations if exist
model.set('tags', [{slug: 'test'}]);

Test

  • yarn test to run tests & eslint
  • yarn lint to run eslint only
  • NODE_ENV=testing-mysql yarn test to run tests with mysql db
  • yarn perf to run a performance test
  • yarn coverage to run test coverage

Publish

  • yarn ship

Copyright & License

Copyright (c) 2013-2023 Ghost Foundation - Released under the MIT license.

2.7.0

9 months ago

2.6.0

1 year ago

2.5.1

2 years ago

2.5.0

2 years ago

2.4.0

3 years ago

2.3.0

3 years ago

2.2.0

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

2.1.0

4 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.2

5 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.13

7 years ago

0.1.12

7 years ago

0.1.11

7 years ago

0.1.10

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago