Skip to content

artis-auxilium/realm-orm

Repository files navigation

Realm-orm

Codacy Badge Codacy Badge

Simple orm for realm-js with query builder

Installation

npm

npm install realm-orm realm --save

yarn

yarn add realm-orm realm

Usage example

create your models

import Model from 'realm-orm/Model'

export default class Person extends Model {
  // realm schema
  static schema = {
    name: 'Person',
    primaryKey: 'id',
    properties: {
      id: 'int',
      name: 'string',
      hobbies: {
        type: 'string',
        optional: true
      },
      age: 'int',
      createdAt: 'date'
    }
  }
  // for model.searchText()
  static stringFields = ['name']
}

create a new db instance with realm option (path, schema, migration, etc...) and open it

import DB from 'realm-orm'
import Person from './models/Person'
realm = new DB({
  schema: [
    Person
  ]
});
realm.open().then(() => {
  // realm is ready
})

insert data in database (data can be array or object). If you insert object with same primary key realm update existing object

    Person.insert([{
      id: 1,
      name: 'second person',
      age: 16,
      createdAt: new Date(),
    }, {
      id: 2,
      name: 'third person',
      age: 96,
      createdAt: new Date(),
    }]);

get a record

person = Person.find(1)

update person

person.update({ age: 10 })

delete person

person.delete()
// or
Person.delete(person)

query model

let oldPeople = Person.query()
                  .greaterThan('age', 90)
                  .findAll();
// results can use query
oldPeople.query()

For more examples and usage, please refer to the doc.

Typescript

see person definition in test file

Stuff used to make this

Release History

  • 1.0.0
    • initial version

Meta

Distributed under the MIT license. See LICENSE for more information.

https://github.com/artis-auxilium/realm-orm

Contributing