0.55.8 • Published 1 month ago

@startupjs/orm v0.55.8

Weekly downloads
687
License
MIT
Repository
-
Last release
1 month ago

startupjs racer-orm

ORM system for Racer.js and ShareDB

What it does

Lets you automatically override your scope models (created with .at() and .scope()) with the additional methods.

Usage

Add the plugin:

import Racer from 'racer'
import racerOrm from 'racer-orm'
Racer.use(racerOrm)

Then start adding the ORM entities to your model. Each ORM Entity must be inherited from Model.ChildModel.

import { Model } from 'racer'
import { promisifyAll } from 'bluebird'

// Promisify the default model methods like subscribe, fetch, set, push, etc.
promisifyAll(Model.prototype)


class PlayerModel extends Model.ChildModel {
  alert (message) {
    this.set('alert', this.get('name') + ', ' + message)
    this.setDiff('showAlert', true)
  }
}

class GamesModel extends Model.ChildModel {
  async addNew (userId = 'system', params = {}) {
    let gameId = this.id()
    await this.addAsync('games', {      
      name: 'Dummy Game',
      ...params,
      id: gameId,      
      userId,
      playerIds: [],
      createdAt: Date.now()      
    })
    return gameId    
  }
}

class GameModel extends Model.ChildModel {
  async alertPlayers (message) {
    let playerIds = this.get('playerIds')
    let playersQuery = this.root.query('players', { _id: { $in: playerIds } })
    await this.subscribeAsync(playersQuery)
    for (let playerId of playersQuery.getIds()) {
      this.scope('players.' + playerId).alert(message)
    }
  }

  async addPlayer (userId, params = {}) {
    if (!userId) throw new Error('userId required')
    var playerId = this.id()
    await this.root.addAsync('players', {      
      ...params,
      id: playerId,      
      userId,
      createdAt: Date.now()
    })
    await this.pushAsync('playerIds', playerId)
    return playerId
  }
}

racer.orm('games', GamesModel)
racer.orm('games.*', GameModel)
racer.orm('players.*', PlayerModel)

// ...

async function main ($root) {
  let $games = $root.scope('games')
  let gameId = await $games.addNew('userId1', { name: 'Cool game' })
  let $game = $games.at(gameId)
  for (let userIds of ['userId1', 'userId2', 'userId3']) {
    await $game.addPlayer(userIds)
  }
  $game.alertPlayers('please join the game!')
}

// ...

Factory

Sometimes you want to dynamically decide which ORM to use based on the document's data. Factory let you do that.

Example:

class BasePlayerModel extends Model.ChildModel {  
  getColor () {
    throw new Error('Player color is unknown')
  }     
}

class AlliedPlayerModel extends BasePlayerModel {
  getColor () {
    return 'blue'
  }
}

class RivalPlayerModel extends BasePlayerModel {
  getColor () {
    return 'red'
  }
}

function PlayerFactory ($player, $parent) {
  // $player here is going to be just a pure scoped model  
  let playerTeamId = $player.get('teamId')
  let $root = $player.root
  let myTeamId = $root.get('_session.myTeamId')

  // you have to always pass `$parent` when manually
  // instantiating the ORM Entity
  if (!playerTeamId || !myTeamId) return new BasePlayerModel($parent)

  if (playerTeamId === myTeamId) {
    return new AlliedPlayerModel($parent)
  } else {
    return new RivalPlayerModel($parent)
  }
}
PlayerFactory.factory = true

racer.orm('players.*', PlayerFactory)

Alias

You can optionally specify an alias for the ORM Entity:

racer.orm('players.*', PlayerModel, 'Player')

This will allow you to explicitly specify in .at() and .scope() which ORM Entity to use even for the unknown path patters:

let playerId = 'playerId1'
// will create the PlayerModel, since it matches the specified path pattern:
model.scope('players.' + playerId).alert('please join the game!')

// The following will also create the PlayerModel
// even though '_session.myPlayer' wasn't specified in the orm path patterns:
model.scope('_session.myPlayer', 'Player').alert('please join the game!')

IMPORTANT: Note, that this is a bad practice and must only be used in the edge cases.

It's always better to list all your path patterns explicitly and don't use aliases at all:

racer.orm('players.*', PlayerModel)
racer.orm('_session.myPlayer', PlayerModel)
racer.orm('_session.rivalPlayer', PlayerModel)

Licence

MIT

(c) Decision Mapper - http://decisionmapper.com

0.56.0-alpha.64

1 month ago

0.56.0-alpha.61

2 months ago

0.56.0-alpha.57

2 months ago

0.56.0-alpha.53

2 months ago

0.56.0-alpha.43

3 months ago

0.56.0-alpha.44

3 months ago

0.56.0-alpha.41

3 months ago

0.56.0-alpha.40

3 months ago

0.56.0-alpha.39

3 months ago

0.56.0-alpha.37

3 months ago

0.56.0-alpha.0

4 months ago

0.55.8

5 months ago

0.55.0-alpha.1

7 months ago

0.55.0

6 months ago

0.53.0

9 months ago

0.55.0-alpha.12

6 months ago

0.54.1

7 months ago

0.54.2

7 months ago

0.54.0

7 months ago

0.51.0

12 months ago

0.52.1

12 months ago

0.52.0

12 months ago

0.50.13

1 year ago

0.50.12

1 year ago

0.50.5

1 year ago

0.48.0

2 years ago

0.49.0

1 year ago

0.46.4

2 years ago

0.47.0

2 years ago

0.45.0

2 years ago

0.46.0

2 years ago

0.44.23

2 years ago

0.44.24

2 years ago

0.44.0

2 years ago

0.43.0

2 years ago

0.41.2

2 years ago

0.41.0

2 years ago

0.42.6

2 years ago

0.42.0

2 years ago

0.43.1

2 years ago

0.39.11

3 years ago

0.40.0

3 years ago

0.39.0

3 years ago

0.38.0

3 years ago

0.37.3

3 years ago

0.37.0

3 years ago

0.36.0

3 years ago

0.35.10

3 years ago

0.35.2

3 years ago

0.35.0

3 years ago

0.34.0

3 years ago

0.33.0

3 years ago

0.33.0-alpha.4

3 years ago

0.33.0-alpha.0

3 years ago

0.32.1

3 years ago

0.32.0

3 years ago

0.31.28

3 years ago

0.31.29

3 years ago

0.31.27

3 years ago

0.31.26

3 years ago

0.31.22

3 years ago

0.31.21

3 years ago

0.31.18

3 years ago

0.31.19

3 years ago

0.31.0

3 years ago

0.30.9

3 years ago

0.30.7

3 years ago

0.30.6

3 years ago

0.30.5

3 years ago

0.30.4

3 years ago

0.30.3

3 years ago

0.30.2

3 years ago

0.30.1

3 years ago

0.30.0

3 years ago

0.29.11

3 years ago

0.29.2

3 years ago

0.29.0

3 years ago

0.28.0

4 years ago

0.27.0

4 years ago

0.26.0

4 years ago

0.25.0

4 years ago

0.24.0

4 years ago

0.23.45

4 years ago

0.23.44

4 years ago

0.23.32

4 years ago

0.23.0

4 years ago

0.22.0-canary.0

4 years ago

0.22.0

4 years ago

0.22.0-alpha.34

4 years ago

0.22.0-alpha.24

4 years ago

0.22.0-alpha.23

4 years ago

0.22.0-alpha.22

4 years ago

0.22.0-alpha.20

4 years ago

0.22.0-alpha.11

4 years ago

0.22.0-alpha.0

4 years ago

0.21.1

4 years ago

0.21.0

4 years ago

0.20.0

4 years ago

0.19.2

4 years ago

0.19.1

4 years ago

0.19.0

4 years ago

0.18.4

4 years ago

0.18.3

4 years ago

0.18.2

4 years ago

0.18.1

4 years ago

0.18.0

4 years ago

0.17.2

4 years ago

0.17.3

4 years ago

0.17.0

4 years ago

0.16.2

4 years ago

0.16.0

4 years ago

0.15.5

4 years ago

0.15.0

4 years ago

0.14.1

4 years ago

0.13.0

4 years ago

0.12.0

4 years ago

0.11.0

4 years ago

0.10.0

4 years ago

0.9.6

4 years ago

0.10.0-alpha.0

4 years ago

0.9.0

5 years ago

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.0

5 years ago

0.5.2

5 years ago

0.5.0

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.5

5 years ago