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

gimlet

v1.18.0

Published

NodeJS module for database access built on top of commonly used database drivers

Downloads

287

Readme

Gimlet

Build Status Coverage Status Published Version

Gimlet is a NodeJS module for database access built on top of commonly used database drivers. It gives some high level validations and helper methods on top of returned rows. This is not an ORM/ODM, queries must still be made by hand (more or less..).

Installation

Gimlet is available on NPM.

npm install gimlet

Since gimlet uses other drivers you must install the ones you need separately. For mysql just do:

npm install mysql

Drivers

Driver | Package -------|-------- mysql | mysql

Driver Registration

If you want to use another package to access the same database type, you can register the package.

const gimlet = require("gimlet");

gimlet.register("mysql2", "mysql"); // will use mysql2 package with mysql integration

gimlet.connect("mysql2://...");

Usage

const gimlet = require("gimlet");
const db     = gimlet.connect("mysql://....");

db.handler().query("SELECT * FROM users", (err, users) => {
    // this is where the differences from the driver appear
    console.log(users);
});

API

Connection

When you use gimlet.connect() you get a Connection instance.

handler()

Get a context isolation for possible transactions. This call is synchronous and returns an API to access the database using the connection pool.

handler().query(...[, cb])

Query the database, just like you do with the low level driver. Returned rows should be doped with features.

handler().queryRow(...[, cb])

Similar to query but returns only the first row of the results.

handler().queryOne(...[, cb])

Similar to query but returns only the first column of the first row of the results.

handler().create(table, data[, cb])

Just a shortcut to an INSERT query. data should be an object with the properties and values you want.

handler().remove(table, conditions[, cb])

Just a shortcut to a DELETE query. conditions should be an object.

handler().close([cb])

Close connection.

open([cb])

Open connection to database. Some drivers do not connect immediately so you need to call this if you want an immediate connection. MySQL for example connects on first query.

Record

When calling connection.query(), returned rows should be instances of Record instead of plain objects. Records usually are extended with some base plugins (and perhaps external plugins). By default, a Record will be extended with record-base, record-changes and record-freeze that will give you the methods below.

save([changes[, cb]])

Save record modifications. You can pass a changes object with a few more changes before saving.

remove([cb])

Remove record from database.

changes()

Returns an object with the changes detected on the record.

changed()

Returns a boolean indicating if the record has been changed or not.

Special Types

To simplify the use of POINT and POLYGON database types, there are special classes to help you. Here's an example.

const Gimlet = require("gimlet");
const db     = Gimlet.connect("mysql://username:password@hostname/database");

db.handler().query("INSERT INTO locations SET ?", {
	name     : "Aveiro, Portugal",
	position : new Gimlet.types.Point(-8.653602, 40.641271),
})

Extensions

Some extensions are loaded by default, you can create and load others if you need. The syntax is similar to Express and others.

const Gimlet = require("gimlet");
const db     = Gimlet.connect("test://");

db.use("cache"); // use built-in cache extension
db.cease("record-freeze"); // stop using built-in record freezing

cache

This is an extension that gives a Connection the ability to create simple asynchronous caches.

const Gimlet = require("gimlet");
const db     = Gimlet.connect("mysql://username:password@hostname/database");

db.use("cache");

let userCache = db.cache((id, next) => {
    db.handler().queryRow("SELECT * FROM users WHERE id = ?", [ id ], next);
});

/**
 * This will not trigger 2 queries, only one. The second will queue
 * and wait for the first to return (because the `id` requested is
 * the same).
 **/
userCache.get(1, (err, user) => {
    console.log(err, user);
});
userCache.get(1, (err, user) => {
    console.log(err, user);
});

record-base

This extensions is the one responsible for creating the Record.save and Record.remove methods.

record-changes

This extension is the one responsible for creating the Record.changes and Record.changed methods.

record-freeze

This extension just freezes the object. It just calls Object.freeze. This is a special case since, if detected in the extensions list, it will be moved to the end of the load process to avoid freezing objects before all the necessary changes.