GithubHelp home page GithubHelp logo

origamih / safedb Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 91 KB

Typescript high level class to typesafe your database query, accepting knex as query-builder

License: MIT License

TypeScript 100.00%

safedb's Introduction

safedb

Typescript high level class to typesafe your database query, requiring knex as middle-layer query-builder. Type-check your column name, column type, column attributes. Distinguish different db operations input:

  • select, update, delete (where db generated columns like id, created_at exist)
  • insert (where id is not there yet)

Typescript

This package is written in Typescipt and utilize Typescript to type-check table/model and column/property. Although it's compiled into Javascript but there's no use in using pure Javascript, since its core purpose is to type-safe database queries.

Getting Started

Installation

npm i safedb --save

or

yarn add safedb

Basic Usage

Your database declaration file. Here you will use the classFactory to inject db instance into Base class. Then export it to be used later with your models.

import * as Knex from 'knex';
import { classFactory } from './baseClassFactory';

// example config
const db = Knex({
  client: 'pg',
  connection: process.env.DATABASE_URL,
  pool: {
    min: 0,
    max: 1,
  },
  asyncStackTraces: true, // this line will safe you a whole lot of time
});

const Base = classFactory(db);
export Base;

Model declaration. This might look verbose and cumbersome, but you'll end up declare a model sooner or later, under the form of type, interface, or class if you're using Typescipt. Just extends your Model from Base class. It will ensure you to put in your tableName. image

import { Base } from './db';

class User extends Base {
  tableName: 'users';

  public name: string;
  public address: string;
  public age: number;
}

Just instantiate a model instance when you need to use it:

const user = new User();

user.find({})

Knex query-builder

There will be circumstances where you need a query-builder, just call queryBuilder() method.

Transaction

Available methods: find findOne findByIds findAll insert update delete

safedb's People

Contributors

origamih avatar origamitest avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.