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

mapper-pg

v0.1.0-pre

Published

A lightweight PostgreSQL data mapper that likes SQL.

Downloads

5

Readme

Mapper-pg

A lightweight PostgreSQL data mapper that likes SQL.

Install

To install

npm install mapper-pg --save

To test it

npm install -d
make test

To run Backbone example

make test             # creates the necessary database and table
node example/app.js   # runs the server, browse http://localhost:3000

TODO

  • Transaction support

Documentation

See comprehensive tests in test/integration/integrationTest.js.

Quickstart

Require it

var Mapper = require('mapper-pg');

Define Data Access Objects (DAO) for each table

// simple, only table name with optional primary key
var Comment = Mapper.map('Comments');
var Post = Mapper.map('Posts', 'id');

Define relationships beetween DAOS, see lib/relation.js

Post.hasMany('comments', Comment, 'postId');
Comment.belongsTo('post', Post, 'postId');

Initialize

// set `verbose`  to trace SQL, set `strict` for invalid column warnings
var config = { user: 'boo', password: 'secret', database: 'app_dev', verbose: true, strict: false };

Mapper.initialize(config, function(err) {
    // setup express, etc
});

CRUD Examples

Create

var insertId;

// insert a new post
Post.insert({title: 'First Post'}).exec(function(err, result) {
    insertId = result.id;
});

// OR sugar
Post.create({title: 'First Post'}, function(err, result) {
    insertId = result.id;
});

Retrieve

// select inserted post
Post.where({id: insertId}).one(function(err, post) {
    assert.equal(post.title, 'First Post,');
});

// OR sugar
Post.findById(insertId, function(err, post) {});

Update

// update inserted post
Post
  .update()                         // optional since set() is used
  .set({title: 'New Title'})
  .where({id: insertId})
  .exec(function (err, result) {
    assert.equal(result.rowCount, 1);
  });

// OR sugar, updates based on id
Post.save({title: 'New Title', id: insertId}, function(err, result) {});

Delete

// delete all posts with a specific title
Post.delete().where({title: 'New Title'}).exec(function(err, result) {
    assert.equal(result.rowCount, 1);
});

// OR sugar
Post.deleteById(insertId, function(err, result) {});

Gets the first page of posts and populate comments property with the second page of comments for each post retrieved.

Post
  .select('id', 'title', 'excerpt')
  .page(0, 25)
  .order('id DESC')
  .load('comments', function(c) {
    c.select('comment', 'created_at')
     .order('id DESC')
     .page(1, 50);
  })
  .all(function(err, posts) {
    // boo-yah!
  });

Or, mix SQL

var sql = ('SELECT id, title, excerpt FROM Posts ORDER BY id DESC LIMIT 25';

Post.all(sql, function(err, posts) {
  Post.load('comments', function(c) {
    c.sql('SELECT comment, createdAt FROM Comments ORDER BY id DESC LIMIT 50 OFFSET 50');
  }).in(posts, function(err) {
    // boo-yah!
  });
});

SQL goodness

Execute multiple statements in a series

Mapper.client.series([
  'SELECT * FROM posts WHERE author = ?;', [1],

  // use commas to break up SQL
  'SELECT * ',
  'FROM comments WHERE author = ?;', [1]
], function(err, results) {
    // posts are in results[0][0..n]
    // comments are in results[1][0..n]
});

Execute multiple statements in parallel

Mapper.client.parallel([
  'SELECT * FROM posts WHERE author = ?;', [1],
  'SELECT * FROM comments WHERE author = ?;', [1],
], function(err, results) {
    //...
});