npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

synth-db

v1.0.1

Published

An ORM with support for the latest JavaScript features

Downloads

11

Readme

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 pg      # 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.setKnex(knex);

synth-db is a base class that your 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

.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;
});

.all:Array[Record]

Executes the current relation by turning it into a query, returning a promise to an array of records.

Note: Instead of using .all you can also use .then().

User.order('created_at').limit(10).all.then(function (users) {
  users.forEach(function (user) {
    console.log(user.name);
  });
});

// or

User.order('created_at').limit(10).then(function (users) {
  …
});