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

mio-mysql

v0.9.0

Published

MySQL storage plugin for Mio.

Downloads

8

Readme

mio-mysql

Build Status Coverage Status NPM version Dependency Status

MySQL storage plugin for Mio.

Installation

npm install mio-mysql

API

See the Mio documentation for query methods like find, save, etc.

exports(settings, options)

Create a new Mio mysql plugin with the given database settings and options.

settings same as settings for node-mysql

options

  • tableName The table for this model. Defaults to singularized model name.
  • maxLimit The maximum number of records to select at once. Default is 200.
var User = require('mio').createModel('User');

User.server(require('mio-mysql', {
  database: 'mydb',
  user: 'root'
}));

Queries

The query syntax is a subset of mongo-sql. The type, columns, and table properties are handled by mio-mysql.

Pagination

Both offset and limit, and page and pageSize query parameters are supported.

The collection returned by Model.findAll() has pagination properties on the array:

User.findAll()
  .where({ created_at: { $lt: '2014-01-01' }})
  .sort({ created_at: 'desc' })
  .limit(25)
  .exec(function(err, users) {
    console.log(users);          // => [user1, user2, user3, ...]
    console.log(users.total);    // => 73
    console.log(users.pages);    // => 3
    console.log(users.page);     // => 1
    console.log(users.pageSize); // => 25
    console.log(users.offset);   // => 0
    console.log(users.limit);    // => 25
});

Custom table names

Custom table names are specified using the tableName option. For example:

User.use(require('mio-mysql')({
  database: 'mydb',
  user: 'root'
}, {
  tableName: 'users'
}));

Custom column names

Custom field names are provided by a columnName property in the attribute definition. For example:

User
  .attr('id')
  .attr('firstName', {
    type: 'string',
    length: 255,
    columnName: 'first_name'
  })
  .attr('lastName', {
    type: 'string',
    length: 255,
    columnName: 'last_name'
  });

Date types

Attributes with type: "date" will be handled based on the columnType property. This property can either be "datetime", "timestamp", or "integer", corresponding to MySQL column type. If not specified, mio-mysql will assume "integer".

Data formatters

If you need to control exactly how a data-type is determined, set the attribute definition's dataFormatter function:

var Event = mio.createModel('Event');

Event.attr('date', {
  dataFormatter: function(value, Event) {
    value = Math.floor(value.getTime() / 1000);
    return value;
  }
});

Query timeout

Queries have a default timeout of 60000 milliseconds. If the query takes longer, the connection will be destroyed and the callback executed with Error("Connection ended due to query time-out.").

To change the query timeout, set settings.queryTimeout to the desired value.

Database connection

mio-mysql utilizes node-mysql's connection pool.

Models that share a settings object will share a connection pool, exposed via settings.pool.

var mysql = require('mio-mysql');

var settings = {
  database: 'mydb',
  user: 'root'
};

// Both User and Post models will share the same connection.
User.server(mysql(settings));
Post.server(mysql(settings));

console.log(settings.pool);
// => node-mysql connection pool object...

exports.mysql

MySQL module.

Tests

Tests are written with mocha and should using BDD-style assertions.

Run the tests with npm:

npm test

MIT Licensed