npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

type-realm

v11.0.0

Published

Typescript support and class utils for realm-js

Downloads

79

Readme

TypeRealm

NPM Version License Issues

Create Realm schema with TypeScript, using classes and decorators!

Basic Usage

The Realm JavaScript docs suggest defining Realm object schema this way:

class Car {
  static schema = {
    name: "Car",
    properties: {
      _id: 'objectId',
      make: "string?",
      model: "string",
      miles: { type: "int", default: 0 },
    },
    primaryKey: '_id',
  };
  get carName() {
    return `${this.make} ${this.model}`;
  }
}

TypeRealm makes writing Realm Object Schema an enjoyable process, i.e. by defining the schema using only classes and a bit of decorator magic.

So, to create Realm object schema we use a kind of DTO classes. For example, to declare the previous Car type we simply create a class and annotate it with decorators:

import type { ObjectSchema } from 'type-realm';
import { Model, Property } from 'type-realm';

@Model('Car')
class Car {
  @Property({ type: 'objectId', primaryKey: true })
  _id!: number;

  @Property('string?')
  make?: string;

  @Property('string')
  model!: string;

  @Property({ type: 'int', default: 0 })
  miles = 0;

  static schema: ObjectSchema;

  get carName() {
    return `${this.make} ${this.model}`;
  }
}

And we get the corresponding part of the schema :

{
  properties: {
    _id: 'objectId',
    make: 'string?',
    model: 'string',
    miles: { type: 'int', default: 0 }
  },
  primaryKey: '_id',
  name: 'Car'
}

We can pass the class itself to the schema property of the Realm.Configuration object when opening a realm. You can then read and write data normally.

const realm = await Realm.open({
  path: "myrealm",
  schema: [Car],
});
let car1;
realm.write(() => {
  car1 = realm.create("Car", {
    make: "Nissan",
    model: "Sentra",
    miles: 1000,
  });
});

Advanced Usage

For more advanced usage, checkout the examples in!

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!

Documentation

The documentation for the Realm React Native SDK can be found at docs.mongodb.com/realm/sdk/react-native/. The documentation for Realm Node.js SDK can be found at docs.mongodb.com/realm/sdk/node.

The API reference is located at docs.mongodb.com/realm-sdks/js/latest/.

More Examples

Coming soon.