Git Product home page Git Product logo

realm-ts-class-decorators's Introduction

Realm TS Class Decorators

NPM Version License Issues

This is a utility library for โ— ONLY โ— react-native applications. If you use classes to define your models and/or use TypeScript as well, this library includes 2 main utilities:

  • Better type support for model classes
  • Decorators for models and properties

This library also works with with plain JavaScript, providing similar typing benefits, along with the decorators

Basic Usage

The Realm JavaScript docs suggest defining classes this way:

class Person {
  get fullName(): string {
    return this.firstName + ' ' + this.lastName;
  }
}

Person.schema = {
  name: 'Person',
  properties: {
    firstName: 'string',
    lastName: 'string'
  }
};

However, there are several things wrong with this for TypeScript and class usage in general:

  1. The class needs to extend Realm.Object
    • Functions like addListener or isValid on models from realm.objectForPrimaryKey will NOT be present (See realm-js #2430 for more info)
  2. The class can't extend Realm.Object because of bad typing...
  3. The firstName and lastName properties are not defined in the class, so they cannot be accessed like person.firstName.

To work around all these issues and make defining the schema easier, the same "Person" class can be defined as:

import { RealmModel, model, property } from 'realm-ts-class-decorators';

@model("Person")
class Person extends RealmModel {
  @property("string") firstName!: string;
  @property("string") lastName!: string;

  get fullName(): string {
      return this.firstName + ' ' + this.lastName;
  }
}

The important parts of the code above are that:

  1. The class has the @model() decorator before it
  2. The class itself extends RealmModel
  3. Every property stored in Realm has the @property() decorator before them

To add this model to Realm, nothing has changed! Simply include the class like Realm suggests:

Realm.open({ schema: [Person] })
  .then( /* ... */ );

Advanced Usage

For more advanced usage, checkout the example Star Wars App!

Since this library is written in TypeScript, all editors with some form of intellisense should also be able to provide strongly types suggestions for the decorators as well!

realm-ts-class-decorators's People

Contributors

aklinker1 avatar apklinker avatar bdlindsay avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.