Quickstart
Getting started with @nhtio/web-re-active-record
is quick and easy. Just follow the steps below and you'll be up and running in no time.
Installation
You can install @nhtio/web-re-active-record
directly from your preferred package manager
npm i @nhtio/web-re-active-record
pnpm add @nhtio/web-re-active-record
yarn add @nhtio/web-re-active-record
Usage
Below you can find a simple example of how to use @nhtio/web-re-active-record
in your project. Once you understand the basics, read through the rest of the documentation to get into more details on more advanced configurations.
Setup your Object Map
Typescript Only
This functionality is only relevant for Typescript consumers. If you're using JavaScript, you can skip this step.
Create an object type where the keys are the names of your tables and the values are the shapes which represent the structure of the data in those tables.
interface ObjectMap {
example: {
id: number;
key: string;
name: string;
createdAt: Date;
updatedAt?: Date;
}
}
For more information, see the Typescript Documentation.
Configuration
Create a configuration
import type { ReactiveDatabaseOptions } from "@nhtio/web-re-active-record";
const config: ReactiveDatabaseOptions<ObjectMap> = {
namespace: 'example',
version: 1,
psk: 'whateverittakes!',
models: {
example: {
schema: '++id, &key, name, createdAt, updatedAt',
properties: ['id', 'key', 'name', 'createdAt', 'updatedAt'],
primaryKey: 'id',
},
}
}
See the Configuration Documentation for more information about the configuration options.
Initialization
Initialize an instance of the database client using the configuration you created.
import { ReactiveDatabase } from "@nhtio/web-re-active-record"
const db = new ReactiveDatabase<ObjectMap>(config)
Access your Models
Use the ReactiveDatabase#model
method to retrieve a model constructor.
const exampleModel = db.model('example')
See the Models Documentation for more information about working with models.