@scratchdb/database
TypeScript icon, indicating that this package has built-in type declarations

0.0.8 • Public • Published

Database

An easy to use database library.

Usage

  1. Install

    npm i @scratchdb/database
  2. Import in module

    import { createDatabase } from '@scratchdb/database';
  3. Setup driver

    interface Episode {
        title: string;
    }
    
    interface User {
        name: string;
        alive: boolean;
        password: string;
    }
    
    interface Tables {
        episodes: Episode
        users: User
    }
    type ExtendPrototype<Prototype, NewFields> = NewFields & Omit<Prototype, keyof NewFields>;
    // This is the actual data stored in the table
    // Note: Booleans need to be set to numbers
    type SerialisedTables = {
        episodes: ExtendPrototype<Episode, {}>;
        users: ExtendPrototype<User, {
            alive: number;
            // Dead users will have their passwords stripped
            password: string | null;
        }>;
    };
    
    const db = createDatabase<SerialisedTables, Tables>('SQLite', {
        connections: [{
            uri: '/tmp/my-database-file.db'
        }]
    });
  4. Profit?

    const episode = await db.findOne('episodes', 1);
    // {
    //     id: 1,
    //     lastUpdated: '2020-05-17T04:12:11.201Z',
    //     title: 'A happy day'
    // }
    
    const users = await db.find('users');
    // [{
    //     id: 1,
    //     lastUpdated: '2020-05-17T04:12:11.201Z',
    //     name: 'Alexis',
    //     alive: 1,
    //     password: 'a-secret-password'
    // }, {
    //     id: 2,
    //     lastUpdated: '2020-05-15T04:12:11.201Z',
    //     name: 'Ella',
    //     alive: 1,
    //     password: 'another-secret-password'
    // }, {
    //     id: 3,
    //     lastUpdated: '2020-05-15T04:12:11.201Z',
    //     name: 'Elvis',
    //     alive: 0,
    //     password: null
    // }]

Readme

Keywords

none

Package Sidebar

Install

npm i @scratchdb/database

Weekly Downloads

0

Version

0.0.8

License

MIT

Unpacked Size

20.7 kB

Total Files

5

Last publish

Collaborators

  • omgimalexis