How to use the @nozbe/watermelondb/decorators.relation function in @nozbe/watermelondb

To help you get started, we’ve selected a few @nozbe/watermelondb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github status-im / liquid-funding / src / model / pledge.js View on Github external
export default class Pledge extends LiquidModel {
  static table = 'pledges'
  static associations = {
    profiles: { type: 'belongs_to', key: 'profile_id' },
  }

  @field('id_pledge') idPledge
  @field('owner_id') owner
  @field('amount') amount
  @field('token') token
  @field('commit_time') commitTime
  @field('n_delegates') nDelegates
  @field('intended_project') intendedProject
  @field('pledge_state') pledgeState
  @field('block_number') blockNumber
  @relation('profiles', 'profile_id') profile
  @json('delegates', sanitizeValues) delegates

  @action async transferTo(to, amount, projectId) {
    const toPledgeQuery = await this.collections.get('pledges').query(
      Q.where('pledge_id', to)
    ).fetch()
    const pledgesCollection = await this.collections.get('pledges')
    const toPledge = toPledgeQuery[0]
    const args = [
      this.prepareUpdate(pledge => {
        pledge.amount = (BigInt(pledge.amount) - BigInt(amount)).toString()
      })
    ]
    if (toPledge) {
      args.push(
        toPledge.prepareUpdate(pledge => {
github status-im / liquid-funding / src / model / delegate.js View on Github external
import { field, relation } from '@nozbe/watermelondb/decorators'
import { LiquidModel } from '../utils/models'


export default class Delegate extends LiquidModel {
  static table = 'delegates'
  static associations = {
    profiles:{ type: 'belongs_to', key: 'profile_id' },
    pledges: { type: 'belongs_to', key: 'pledge_id' }
  }

  @field('delegate_index') delegateIndex
  @field('id_pledge') idPledge
  @relation('profiles', 'profile_id') profile
  @relation('pledges', 'pledge_id') pledge
}
github status-im / liquid-funding / src / model / delegate.js View on Github external
import { field, relation } from '@nozbe/watermelondb/decorators'
import { LiquidModel } from '../utils/models'


export default class Delegate extends LiquidModel {
  static table = 'delegates'
  static associations = {
    profiles:{ type: 'belongs_to', key: 'profile_id' },
    pledges: { type: 'belongs_to', key: 'pledge_id' }
  }

  @field('delegate_index') delegateIndex
  @field('id_pledge') idPledge
  @relation('profiles', 'profile_id') profile
  @relation('pledges', 'pledge_id') pledge
}
github RocketChat / Rocket.Chat.ReactNative / app / lib / database / model / Message.js View on Github external
export default class Message extends Model {
	static table = 'messages';

	static associations = {
		subscriptions: { type: 'belongs_to', key: 'rid' }
	}

	@field('msg') msg;

	@field('t') t;

	@date('ts') ts;

	@json('u', sanitizer) u;

	@relation('subscriptions', 'rid') subscription;

	@field('alias') alias;

	@json('parse_urls', sanitizer) parseUrls;

	@field('groupable') groupable;

	@field('avatar') avatar;

	@json('attachments', sanitizer) attachments;

	@json('urls', sanitizer) urls;

	@date('_updated_at') _updatedAt;

	@field('status') status;
github Nozbe / WatermelonDB / examples / web / src / models / Post.js View on Github external
static associations = {
    blogs: { type: 'belongs_to', key: 'blog_id' },
    comments: { type: 'has_many', foreignKey: 'post_id' },
  }

  @field('title')
  title

  @field('subtitle')
  subtitle

  @field('body')
  body

  @relation('blogs', 'blog_id')
  blog

  @children('comments')
  comments

  @action async addComment(body) {
    return this.collections.get('comments').create(comment => {
      comment.post.set(this)
      comment.body = body
    })
  }
}