Skip to content

mrphu3074/react-native-realm-model

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

React native Realm Model

RealmModel is a wrapper for realm.

CHANGELOG

  • 0.0.10
    • Fixed some bugs
    • Supported model events: beforeInsert, afterInsert, beforeUpdate, afterUpdate, beforeRemove, afterRemove
    class Product extends RealmModel {
        static realm = realm;
    
        static beforeInsert(doc) {
            doc.createdAt = doc.updatedAt = new Date();
            return doc;
        }
    }

How it works

import Realm from 'realm';
import RealmModel from 'react-native-realm-model';
const realm = new Realm({
  schema: [{
    name: 'Product',
    properties: {
      title: 'string',
      content: {
      	type: 'string',
      	optional: true
      },
      price: 'int'
    }
  }]
})

class Product extends RealmModel {
  static realm = realm;
  // custom methods
  priceLabel() {
  	return this.price + 'vnđ';
  }
}


Product.insert({title: 'Product 1', content: 'This is content', price: 22000});
var product = Product.findOne();

console.log(product.priceLabel());

product.update({price: 24000})

console.log(product.priceLabel());

product.remove();

API

find(selector: Object, option: Object) -> Array

  • selector is search criteria

    • Type 1: {field: value}
    • Type 2: logical operator {$or: [{field1: value1, field2: value2}]}
    • Type 3: realm operator ==, >=, <=, >, <, BEGINSWITH, ENDSWITH, CONTAINS and string comparisons can be made case insensitive by appending [c] to the operator: ==[c], BEGINSWITH[c] etc.

    Ex: Product.find({title: {'BEGINSWITH[c]': 'hello'}})

  • option is search option support: limit, offset, sort

findOne(selector: Object)

insert(data: Object)

update(selector: Object, modifier: Object)

remove(selector: Object)

About

Model wrapper for react native Realm

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published