Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

realm-ts-class-decorators

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

realm-ts-class-decorators

Typescript support and class utils for realm-js

  • 1.0.2
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Realm TS Class Decorators

NPM Version License Issues

This is a utility library for :exclamation: ONLY :exclamation: 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!

Keywords

FAQs

Package last updated on 06 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc