min
minidb
npm i minidb
min

minidb

Tiny ORM for PostgreSQL and SQLite

by Zac McCormick

0.15.1 (see all)License:BSDTypeScript:Not FoundCategories:Vanilla JavaScript SQLite ORM
npm i minidb
Readme

minidb Build Status

A small object wrapper for PostgreSQL and SQLite. It has very little sugar and is intentionally low-level. It does not do migrations, relationships, identity management, or any other magic.

Installation

npm install minidb

Usage

import {PersistentObject, SQLite} from 'minidb';

export default class User extends PersistentObject {
  static get tableName() {
    return 'users';
  }

  static get columns() {
    return [
      { name: 'name', column: 'name', type: 'string', null: false },
      { name: 'email', column: 'email', type: 'string', null: false },
      { name: 'age', column: 'age', type: 'integer', null: false },
      { name: 'height', column: 'height', type: 'double' },
      { name: 'birthDate', column: 'birth_date', type: 'date' }
    ];
  }
}

PersistentObject.register(User);

// use the API
const db = new SQLite({file: 'users.db'});
//const db = new Postgres({db: 'dbname = minidb'});

await db.open();

const setupScript = `
DROP TABLE IF EXISTS users;

CREATE TABLE users (
  id bigserial NOT NULL,
  name text NOT NULL,
  email text NOT NULL,
  age bigint NOT NULL,
  height double precision,
  birth_date date,
  created_at double precision,
  updated_at double precision
);

CREATE UNIQUE INDEX idx_user_email ON users (email);
`;

await db.execute(setupScript);

const user = await User.findOrCreate(db, {name: 'John', email: 'john@example.com', age: 30});

user.height = 72.3;

await user.save();

Downloads/wk

6

GitHub Stars

4

LAST COMMIT

10mos ago

MAINTAINERS

1

CONTRIBUTORS

0

OPEN ISSUES

0

OPEN PRs

3
VersionTagPublished
0.15.1
latest
10mos ago
No tutorials found
Add a tutorial