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

weerm

v0.4.0

Published

Tiny SQL toolkit for PG

Downloads

2

Readme

Weerm

🧰 Tiny SQL toolkit for PG + Node (<200 LOC)

npm i weerm

Usage

Your database URL should be in DATABASE_URL env var, e.g.

export DATABASE_URL=postgres://user:password@host/database:5432

Define your model, for example models/user

// models/user.js
const db = require('weerm')

const TableName = 'users'

// model attribute to column mapping object
const Columns = {
  id: 'user_id',
  firstName: 'first_name',
  lastName: 'last_name',
  createdAt: 'created_at'
}

const findOne = async (condition) => {
  let conditionValues = db.mapToColumns(condition, columns)
  let row = await db.findOne(TableName, conditionValues)
  return db.mapFromColumns(row, columns)
}

const find = async (condition) => {
  let conditionValues = db.mapToColumns(condition, columns)
  let rows = await db.find(TableName, conditionValues)
  return rows.map((row) => db.mapFromColumns(row, columns))
}

// second optional argument is a current transaction
// transactions are optional and only required when 
// you need to execute multiple SQL statements as a single unit
const create = async (attrs, tr) => {
  let columnValues = db.mapToColumns(attrs, columns)
  return await db.insert(tr, TableName, columnValues)
}

const update = async (condition, attrs, tr) => {
  let conditionValues = db.mapToColumns(condition, columns)
  let columnValues = db.mapToColumns(attrs, columns)
  return await db.update(tr, TableName, columnValues, conditionValues)
}

const del = async (condition, tr) => {
  let conditionValues = db.mapToColumns(condition, columns)
  return await db.del(tr, TableName, conditionValues)
}

Use your model

const user = require('/models/user')

// find one (e.g. by ID)
let user = await User.findOne({id: 3956})
// if no user is found, null will be returned


// find multiple users
let users = await User.find({lastName: 'Smith'})
// if no users were found, empty array will be returned


// add a new user
let user = await User.create({firstName: 'John', lastName: 'Smith'})


// update existing user
// update a user by ID
await User.update({id: 3956}, {lastName: 'Bunyan'})


// delete user
// delete a user by ID
await User.del({id: 3956})

Using inline SQL

// models/user.js

const findByFirstNameWithLimit = async (firstName, limit) => {
  const sql = `SELECT * from ${TableName} WHERE firstName = $1 order by ID DESC LIMIT $2`
  let rows = await db.query(sql, [firstName, limit])
  return rows.map((row) => db.mapFromColumns(row, columns))
}

Using transactions

// using transaction requires wrapping everything in a transaction and 
// passing the current transaction as the last parameter
let userAudit = await db.withTransaction(async (tr) => {
  await User.update({id: 9363}, {lastName: 'Bunyan'}, tr)
  return await UserAudit.create({entity: 'User', op: 'update', args: [{lastName: 'Bunyan'}]}, tr)
})
// note that the return value from the callback will be returned by withTransaction function

If you want to execute certain actions after the transaction was rolled back, use the second function argument for this.

let onRollback = () => {
  // cleanup external resources
  // e.g. // payment gateway rollback etc
}
await db.withTransaction(tr => {/* do something in transaction.. */}, onRollback)

Unique index violation

  // checking error type will tell you if it's a unique index violation
  try {
    let user = await User.create({email: '[email protected]'})
  } catch(e) {
    if(e instanceof db.UniqueIndexError) {
      console.log('Unique index violation on table: users, columns:', e.columns)
    }
  }

MIT Licensed.

Copyright FingerprintJS Inc., 2020.