Skip to content

Latest commit

 

History

History
87 lines (73 loc) · 2.52 KB

README.md

File metadata and controls

87 lines (73 loc) · 2.52 KB

JavaScript Database

A secure light-weight and dependency free database wrapper for the web.

Documentation

This package provides everything u need to use RIDB on react easily

Install

npm i @trust0/ridb-react

Usage

import React from 'react'
import { Database, useDatabase, DatabaseProvider } from '@trust0/ridb-react'

Create your schemas and type them for better inference.

const users = {
  version: 0 as const,
  primaryKey: 'id',
  type: SchemaFieldType.object,
  properties: {
      id: {
          type: SchemaFieldType.string,
          maxLength: 60
      }
  }
} as const
const schemas = {
  users: users
}
type DatabaseSchemas = typeof schemas;

Now just create your component and use the useDatabase hook to get the database instance.

const MyComponent: React.FC = () => {
    const db = useDatabase<DatabaseSchemas>();
    const [isDbReady, setIsDbReady] = React.useState(false);

    React.useEffect(() => {
        const startDb = async () => {
            if (db) {
                await db.start();
                setIsDbReady(true);
            }
        };
        startDb();
    }, [db]);

    if (!db) {
        return <div>No database available</div>;
    }

    if (!isDbReady) {
        return <div>Loading...</div>;
    }

    return (
        <div> <h1>My Component</h1> </div>
    );
};

Wrap your component with the DatabaseProvider component to provide the database instance to your component.

<DatabaseProvider>
    <MyComponent />
</DatabaseProvider>

All the database methods and operations from RIDB are supported, for more details check the RIDB documentation