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

osql

v0.4.1

Published

A simple object based interface to SQL data sources.

Downloads

7

Readme

osql

A simple object based interface to SQL data sources. Maybe this will evolve into an ORM in the future, but probably not as there are some great ORMs for none-mssql databases available already, and I'm not going to be doing enough with mssql to justify creating one.

The other nice thing this exposes is a wrapper for restify, to enable you to create REST routes for your given tables.

This project is simply designed to make interaction with SQL database tables a little simpler, in a nice when promise based library.

Currently supported drivers are:

  • sqlite3
  • mssql

The language syntax used in the "many" statements below, that is used to generate the sql required is https://github.com/hiddentao/squel.

Getting Started

Install the module with: npm install osql and then one of the following drivers:

  • npm install sqlite3
  • npm install mssql
var osql = require('osql');
var db = new osql.Database('sqlite3', ':memory:');

or

var osql = require('osql');
var db = new osql.Database('mssql', {
  user: 'example_user',
  password: 'example_user_password',
  server: '127.0.0.1',
  database: 'example_database'
});

Examples

Once you've got you db object and driver, define a table and start manipulating it:

var table = new osql.Table({
  table: 'example_table',
  identity: 'id',  // Optional
  database: db
});

Inserting a Single Row, presuming the identity is auto generated

table.insert({
  column1: 'value'
}).then(function(model) {
  // model.id will be set to the inserted identity
});

Inserting a Single Row where you set the identity

table.insert({
  id: id,
  column1: 'value'
}).then(function(model) {
  // done
});

Inserting a bunch of records, with a single insert statement

table.insert([
  { column1: 'val1'},
  { column1: 'val2'}
]).then(function() {
  // done
});

Selecting a single row from the table

table.select(rowId)
.then(function(model) {
  // done
});

Selecting mutliple rows from the table using squel syntax

table.selectMany()
.where('id > ?', 0)
.execute()
.then(function(models) {
  // done
});

Updating a single row from the table

table.update(rowId, {
  column1: 'updated value'
})
.then(function(model) {
  // done
});

Updating mutliple rows from the table using squel syntax

table.updateMany({
  column1: 'updated value'
})
.where('id > ?', 0)
.execute()
.then(function() {
  // done
});

Deleting a single row from the table

table.delete(rowId)
.then(function() {
  // done
});

Deleting mutliple rows from the table using squel syntax

table.deleteMany()
.where('id > ?', 0)
.execute()
.then(function() {
  // done
});

REST

To make life even easier, you can wrap a restify server with a table object, which will in turn expose the rest routes.

NOTE: This is NOT a complete solution, i've done a quick pragmatic implementation to enable an on going project.

// Create your restify server
var server = require('restify').createServer({
  name: 'osql-rest',
  version: '1.0.0'
});
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());

// Define your table
var table = new osql.Table({
  table: 'example_table',
  identity: 'id',
  database: db
});

// Wrap the server
table.wrap('/api', server);

// Profit
server.listen(port, done);

From there, you will get a bunch of URLs exposed on the server:

POST /api/example_table         // Create an entity
GET /api/example_table          // Get all entities
GET /api/example_table/id       // Get a single entity by id
GET /api/example_table/id/field // Get a field from a single entity by id
PUT /api/example_table/id       // Update an entity by id
DELETE /api/example_table/id    // Delete an entity by id

Have a look in test/rest.test.js to see how to query it using Restler

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • 0.1.0 Initial release
  • 0.1.1-2 Bug fixes
  • 0.1.3 Encapsulation bug fix, inserting of an object returns new instance of object with id
  • 0.1.4 Bug fix
  • 0.1.5 underlying db is now exposed via the dal
  • 0.1.6 Will now escape single quotes by default
  • 0.1.7 Concurrency fix for insert + get ID statements
  • 0.1.8 Dependency Updates
  • 0.1.9 Fixed concurrency issues on INSERT + get ID
  • 0.2.0 Fixed concurrency issues on MSSQL driver, as well as fixing an issue around IDENTITY_INSERT
  • 0.2.1 Updated MSSQL to 1.2.1
  • 0.2.2 table.options.identity is no longer a required paramter as tables dont always have identities
  • 0.2.4 Version Bump
  • 0.2.5 Changed a console warn to a log, so as not to break jenkins pipelines
  • 0.3.0 REST wrapping for Restify
  • 0.3.1 Updated dependencies
  • 0.3.2 Stored proc work
  • 0.4.0 Added callback support as well as promises
  • 0.4.1 Fixed an unhandled rejection

License

Copyright (c) 2014 Karl Stoney
Licensed under the MIT license.