0.1.0 • Published 2 years ago

react-native-realm-schema v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-native-realm-schema

Well-defined schema syntax for React Native Realm

Installation

npm install react-native-realm-schema

or using yarn:

yarn add react-native-realm-schema

Config your project

  1. Enable decorator feature in tsconfig.json
{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  }
}
  1. Using @babel/plugin-proposal-decorators:

    module.exports = {
    // ...
    plugins: [
      [
        '@babel/plugin-proposal-decorators',
        {
          legacy: true,
        },
      ],
    ]
    }

Usage

Creating a schema

import { BaseSchema, RealmProperty, RealmSchema } from 'react-native-realm-schema';
import nameof from 'ts-nameof.macro';

@RealmSchema({
  // name of the schema
  name: nameof(DeviceSchema),
  // primary key
  primaryKey: nameof(DeviceSchema.prototype._id),
})
export class DeviceSchema extends BaseSchema {
  @RealmProperty(/* dataType */ 'uuid', /* isRequired */ true)
  public _id: string;

  @RealmProperty('string')
  public name: string;

  @RealmProperty('string')
  public status?: string;
}

Get schema object

const schema = DeviceSchema.schema;

Create new schema object

const deviceSchema = DeviceSchema.create();

Schema name

const schemaName = DeviceSchema.schemaName;

Initialize Realm

import { RealmDatabase, schemaListOf } from 'src/helpers';

class Database extends RealmDatabase {
  public get schemas() {
    return schemaListOf(
      DeviceSchema,
      // ...
    );
  }

  public get path(): string {
    return 'my_realm'
  }
}

const database = new Database();

/**
 * Initialize the database
 */
const realm = await database.init();

// or access using getter
database.realm;

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library