Skip to content

Latest commit

 

History

History
87 lines (70 loc) · 1.8 KB

File metadata and controls

87 lines (70 loc) · 1.8 KB

Object-Data-Relational-Mapping

Like mongoose, RxDB has ORM-capabilities which can be used to add specific behavior to documents and collections.

statics

Statics are defined collection-wide and can be called on the collection.

Add statics to a collection

To add static functions, pass a statics-object when you create your collection. The object contains functions, mapped to their function-names.

const heroes = await myDatabase.collection({
  name: 'heroes',
  schema: mySchema,
  statics: {
    scream: function(){
        return 'AAAH!!';
    }
  }
});

console.log(heroes.scream());
// 'AAAH!!'

You can also use the this-keyword which resolves to the collection:

const heroes = await myDatabase.collection({
  name: 'heroes',
  schema: mySchema,
  statics: {
    whoAmI: function(){
        this.name;
    }
  }
});
console.log(heroes.whoAmI());
// 'heroes'

instance-methods

Instance-methods are defined collection-wide. They can be called on the RxDocuments of the collection.

Add instance-methods to a collection

const heroes = await myDatabase.collection({
  name: 'heroes',
  schema: mySchema,
  methods: {
    scream: function(){
        return 'AAAH!!';
    }
  }
});
const doc = await heroes.findOne().exec();
console.log(doc.scream());
// 'AAAH!!'

Here you can also use the this-keyword:

const heroes = await myDatabase.collection({
  name: 'heroes',
  schema: mySchema,
  methods: {
    whoAmI: function(){
        return 'I am ' + this.name + '!!';
    }
  }
});
await heroes.insert({
  name: 'Skeletor'
});
const doc = await heroes.findOne().exec();
console.log(doc.whoAmI());
// 'I am Skeletor!!'

If you are new to RxDB, you should continue here