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-database

v0.3.3

Published

A nicer interface for the react native flavour of realm

Downloads

63

Readme

react-native-database

Installation

npm install --save react-native-database

Requires that Realm is also installed, if not:
npm install --save realm

Usage

This library contains two components, which can be used independently or in parallel

  • Database - Provides a simple wrapper around Realm, adding fine-grained notifications
  • Settings - An interface for easily maintaining app settings in local storage, without any extra database clutter

Database

Store any data your app needs in local storage, using essentially the same interface as standard Realm, with custom fine grained notification functionality. Requires a realm format schema to be passed in on construction.

Methods

Essentially the same as Realm, but with different notifications.

  • write(callback) - Creates a transaction, and executes a function within that transaction. Essential to wrap any code making persistent changes in a write(). See the realm docs
  • addListener(callback) - The callback passed in will be executed whenever there is a change to the database, and be passed the change type, record type, the old version of the record, and any additional parameters passed into the function that caused the change (see the ...listenerArgs in create, update etc. below). addListener returns the id of the listener, which can be used to remove it once it is no longer needed (be sure to do this, or functions could be called with their context out of scope!)
  • removeListener(listenerId) - Detaches the listener function identified by listenerId from the database
  • create(type, properties, ...listenerArgs), update(type, properties, ...listenerArgs), delete(type, object, ...listenerArgs), deleteAll(...listenerArgs) - All work exactly as in Realm, but also send out notifications of the change, i.e. call any change listeners that are attached to this Database. Any additional parameters beyond the standard Realm ones are passed through to the listening function in the same order
  • objects(type) - Returns a ResultSet of all objects of the given type. The data is not immediately loaded into memory, meaning you don't have to worry about large queries exhausting memory capacity, or pagination to avoid it - Realm takes care of all of this, and loads the data as it is used. See the realm docs
import { Database } from 'react-native-database';
class User {};
User.schema = {
  name: 'User',
  primaryKey: 'id',
  properties: {
    id: 'string',
    username: { type: 'string', default: 'placeholderUsername' },
    lastLogin: { type: 'date', optional: true },
    firstName: { type: 'string', optional: true },
    lastName: { type: 'string', optional: true },
    email: { type: 'string', optional: true },
  },
};
const schema = { schema: [User], schemaVersion: 1 };
const database = new Database(schema);

Settings

Methods

  • set(key, value) - Takes in a (string) key that uniquely identifies the setting, and a (string) value to update against that key (or create if the setting with that key does not yet exist)
  • get(key) - Returns the (string) value associated with the given key

Standalone

Settings can be constructed with no parameters, if you only intend to store settings in the local Realm database, and no extra data.

const settings = new Settings(database);
settings.set('CurrentUserName', 'Edwin');
console.log(`Hello ${settings.get('CurrentUserName')}!`); // Will print 'Hello Edwin!'

Using in conjunction with a database

If you need to store extra data, pass through a realm database including a 'Setting' table on construction. Note that this could be a react-native-database Database rather than a raw realm database.

import { Database, Settings } from 'react-native-database';
class Setting {};
Setting.schema = {
  name: 'Setting',
  primaryKey: 'key',
  properties: {
    key: 'string',
    value: 'string',
  },
};
class User {};
User.schema = {
  name: 'User',
  primaryKey: 'id',
  properties: {
    id: 'string',
    username: { type: 'string', default: 'placeholderUsername' },
    lastLogin: { type: 'date', optional: true },
    firstName: { type: 'string', optional: true },
    lastName: { type: 'string', optional: true },
    email: { type: 'string', optional: true },
  },
};
const schema = { schema: [Setting, User], schemaVersion: 1 };
const database = new Database(schema);
const settings = new Settings(database);
database.write(() => { database.create('User', { id: '54321', username: 'edmofro', firstName: 'Edwin' }) });
settings.set('CurrentUserId', '54321');
const currentUserId = settings.get('CurrentUserId')```
console.log(`Hello ${database.objects('User').filter(`id = ${settings.get('CurrentUserId')}`)[0].firstName}!`); // Will print 'Hello Edwin!'