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

react-native-realm-wrapper

v1.2.0

Published

With this wrapper, you can write less code when using Realm.

Downloads

2

Readme

React Native Realm Wrapper

With this wrapper, you can write less code when using Realm.

Installation and usage

First, install Realm:

npm install realm

Then:

npm install react-native-realm-wrapper

Import it:

import { rnrw } from 'react-native-realm-wrapper';

First instructions

You don't need to worry about opening and closing realms because everything is done automatically. All operations can be performed without calling any additional function.

This wrapper supports only the functions listed here. If you need anything else, you can always use Realm directly.

All functions return a promise with a success or error message or the query results.

Schemas

Here, schemas are arrays, not objects, and they have the following elements: schema name, properties, and encryption key (optional). The id property is always required, and it must be an integer representing the primary key.

const schema = ["Cars", {id: "int", model: "string", color: "string", year: "int"}];

The id will automatically be used as the primary key.

Write operations

Create and edit objects

With the same function, you can create new objects or edit existing ones. If you pass a new id, it'll be a new object, and if you pass an existing id, the object properties will be updated with the new values provided.

The id property must be unique per object. Objects from different schemas can have the same id.

rnrw.write(schema, properties);

Example:

const schema = ["Cars", {id: "int", model: "string", color: "string", year: "int"}];

rnrw.write(schema, {id: 1, model: "Honda Pilot", color: "Blue Pearl", year: 2022});

Delete an object

To delete an object, you must pass its id.

rnrw.del(schema, id);

Example:

const schema = ["Cars", {id: "int", model: "string", color: "string", year: "int"}];

rnrw.del(schema, 1);

Delete all objects of a specific type

It deletes all objects in a schema.

rnrw.delT(schema);

Example:

const schema = ["Cars", {id: "int", model: "string", color: "string", year: "int"}];

rnrw.delT(schema);

Delete all objects

It deletes all objects from all schemas.

rnrw.delAll();

If you're using an encryption key, it must be passed as an argument.

rnrw.delAll(key);

Read operations

Find a specific object by primary key

rnrw.pk(schema, id);

Example:

const schema = ["Cars", {id: "int", model: "string", color: "string", year: "int"}];

rnrw.pk(schema, 1).then(obj => {});

Query an object type

It returns all objects in a schema.

rnrw.ot(schema).then(obj => {});

You can sort the objects by any property. In the example below, we're sorting them by model in ascending order:

rnrw.ot(schema, "model").then(obj => {});

To sort in descending order, pass true as the third argument.

Find the next available id

Since every object in a schema must have a unique id, you can use the following function to find the next available one:

rnrw.nextID(schema).then(id => {});

Add or remove properties (schema version)

To add or remove properties, use the following function passing the modified schema and the new schema version as arguments. The standard version is zero, so if you're making changes for the first time, you can set it to 1.

rnrw.schema(schema, newVersion);

Example:

// Original schema: ["Cars", {id: "int", model: "string", color: "string", year: "int"}];

// Modified schema:

const schema = ["Cars", {id: "int", brand: "string", model: "string", color: "string", year: "int"}];

rnrw.schema(schema, 1);

As you can see, when you add or remove properties, you need to set a new schema version greater than the previous one, and it comes with some changes too. Let's see what happens when the schema version changes:

The schema array will require two additional elements

The fourth element must always be the new schema version, and the third one will depend on whether you're using an encryption key. If you're using it, pass the key, and if not, pass null.

With encryption key:

const schema = [name, properties, key, schemaVersion];

Without encryption key:

const schema = [name, properties, null, schemaVersion];

All schemas in Realm use the same schema version. Even if you're changing the properties of only one schema, the other ones will follow the same rule.

The delAll() function will require two arguments

The second argument must always be the new schema version, and the first one will depend on whether you're using an encryption key. If you're using it, pass the key, and if not, pass null.

With encryption key:

rnrw.delAll(key, schemaVersion);

Without encryption key:

rnrw.delAll(null, schemaVersion);

Encryption

If you're starting a new project, you can opt for encryption. Unfortunately, there's no easy way of encrypting an existing Realm database. Bear in mind that, although you first pass the key in only one schema array, all future schemas will be encrypted too, and you must use the key with all of them.

To learn more about how encryption works with Realm, read their page https://docs.mongodb.com/realm/sdk/react-native/advanced/encrypt/.