@disco/belongs-to

1.0.0 • Public • Published

@disco/belongs-to

CI status Coverage Status npm package Dependencies MIT License

This is a middleware for disco to add belongs-to relation support.

Install

npm install @disco/belongs-to

Usage

const disco = require('@disco/disco')
const belongsTo = require('@disco/belongs-to')

const modeller = disco(driver)
modeller.use(belongsTo)

const User = modeller.createModel('user')
const Profile = modeller.createModel('profile')

Profile.belongsTo({
  model: User,
  as: 'user'
})

const user = await User.create({
  email: 'me@example.com',
  password: 'badpassword'
})

const profile = await Profile.create({
  name: 'Stephen'
})

await profile.user.set(user)

belongsTo API

Model.belongsTo(config : Object)

This is the entrypoint to create a belongsTo relation on a given model.

  • config {Object} config object
    • model {Model} Model this belongs to
    • as {String} Name of relation property (default: model.tableName)
    • foreignKey {String} Column name of foreign key (default: Model.tableName)
    • immutable {Boolean} If it should exclude mutation APIs (default: false)
User.belongsTo({
  model: Profile,
  as: 'profile'
})

const user = User.findOne({})
user.posts // User.belongsTo(...) added this relation property

Note that while a relation can be set to immutable, this currently only makes the relation immutable and not the model returned by it.

Non-mutating

These APIs will always be included regardless of if immutable has been set to false.

relation.get() : Promise<Model>

Get the related record.

const user = await profile.user.get()

Mutating

If immutable has been set to false in Model.belongsTo(...), these APIs will not be included.

relation.set(model : Model) : Promise<Model>

Set an existing model to this relation.

const user = User.build({
  email: 'me@example.com',
  password: 'badpassword'
})

await profile.user.set(user)

relation.build(data : Object) : Model

Build a new related record. This will not persist until the returned model is saved.

const user = profile.user.build({
  email: 'me@example.com',
  password: 'badpassword'
})
await profile.save()

relation.create(data : Object) : Promise<Model>

Create a new related record. This will persist before returning the model.

const user = await profile.user.create({
  email: 'me@example.com',
  password: 'badpassword'
})

relation.getOrCreate(data : Object) : Promise<Model>

Attempt to get the related record, creating it with the given data if not found.

const user = await profile.user.getOrCreate({
  email: 'me@example.com',
  password: 'badpassword'
})

relation.createOrUpdate(changes : Object) : Promise<Model>

Attempt to update the related record by applying the given changes, creating it with the changes if not found.

const user = await profile.user.createOrUpdate({
  email: 'me@example.com',
  password: 'badpassword'
})

relation.update(changes : Object) : Promise<Model>

Update the related record by applying the given changes.

const user = await profile.user.update({
  email: 'me@example.com',
  password: 'badpassword'
})

relation.remove() : Promise<Model>

Remove the related record.

const removedUser = await profile.user.remove({
  email: 'me@example.com',
  password: 'badpassword'
})

Package Sidebar

Install

npm i @disco/belongs-to

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

15.6 kB

Total Files

7

Last publish

Collaborators

  • qard