Giter Club home page Giter Club logo

synth-db's Introduction

synth-db

An ORM for Node and SQL databases. Heavily inspired by ActiveRecord with a goal of being compatible with the same db that a Rails app would use.

Requires Node 0.12 or IO.js so it can make use of ES6/ES2015 features of JavaScript. Requires classes and arrow functions.

Being developed readme first (tests second). There's still lots of missing implementation.

Build Status

Install

npm install --save synth-db
npm install --save knex      # needed too

API

init

Create a connection to your db using knex, then pass it to synth-db.

let knex = require('knex')('postgres://localhost/mydb');
let sdb = require('synth-db');
sdb.knex = knex;

synth-db is a base class that you models will extend.

Declaring Models

Create a model and have it extend the Base class.

// Assuming you called setKnex previously in the app
var sdb = require('synth-db');

class User extends sdb.Base {
  constructor () {
    super();
  }
}

/* init will populate the attributes with fields from the `users` table */
User.init().then(function () {
  return User.first;
}).then(function (user) {
  console.log(user.name);
})

The above will look to the database for a users table, and add setters and getters for each column detected.

What can you do with a model? You can use it to find records from the table is represents.

Querying (not yet implemented)

.find(id:string|number):Record

.find takes in the primary key used to look up a record. The id field will be used for lookups. A promise to the record is returned, if no record is found, the promise will be rejected.

var userId = 1235;
User.find(userId).then(function (currentUser) {
  console.log(`Email: ${currentUser.email}`);
}, function () {
  console.log("Sorry, no record was found");
});

.where([equalities:object]|[searchString:string][, ...vars]):Relation

.where can either take an equalities object or a search string. The equality object is a set of keys and values that need to all be true for a record to be included.

Alternatively, you can pass in a search string that will be passed through to the db. Don't put user provided data into the search string, instead insert a ? into the string, and pass the data as an extra argument, this will avoid SQL injection attacks.

var query = User.where({ confirmed: true });
// or
query = User.where('confirmed = ?', true);
query.toString(); // "select * from users where confirmed = true;"
// Only once .then is called on a relation is the db hit
query.then(function (users) {
  return user.email;
});

synth-db's People

Contributors

jonabrams avatar bradvogel avatar

Watchers

 avatar James Cloos 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.