@disco/has-one

1.0.0 • Public • Published

@disco/has-one

CI status Coverage Status npm package Dependencies MIT License

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

Install

npm install @disco/has-one

Usage

const disco = require('@disco/disco')
const hasOne = require('@disco/has-one')

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

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

User.hasOne({
  model: Profile,
  as: 'profile'
})

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

HasOne API

Model.hasOne(config : Object)

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

  • config {Object} config object
    • model {Model} Model this has one of
    • 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.hasOne({
  model: Profile,
  as: 'profile'
})

const user = User.findOne({})
user.posts // User.hasOne(...) 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 profile = await user.profile.get()

Mutating

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

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

Set an existing model to this relation.

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

await user.profile.set(profile)

relation.build(data : Object) : Model

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

const profile = user.profile.build({
  name: 'Stephen'
})
await profile.save()

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

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

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

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

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

const profile = await user.profile.getOrCreate({
  name: 'Stephen'
})

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 profile = await user.profile.createOrUpdate({
  name: 'Stephen'
})

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

Update the related record by applying the given changes.

const profile = await user.profile.update({
  name: 'Stephen'
})

relation.remove() : Promise<Model>

Remove the related record.

const removedProfile = await user.profile.remove({
  name: 'Stephen'
})

/@disco/has-one/

    Package Sidebar

    Install

    npm i @disco/has-one

    Weekly Downloads

    2

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    14.1 kB

    Total Files

    8

    Last publish

    Collaborators

    • qard