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

modella-mysql

v0.13.5

Published

MySQL storage plugin and persistence layer for Modella.

Downloads

9

Readme

modella-mysql

Build Status Dependency Status

MySQL persistence layer for Modella.

Installation

npm install modella-mysql

Example

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

var User = modella('User');

User.use(mysql({
  database: 'mydb',
  user: 'root'
});

API

exports(settings, options)

Create a new Modella 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.

Model.all(query, callback)

Get all models using given query.

User.find({ where: { city: 'San Francisco' }}, function(err, result) {
  console.log(result);
  // => { data: [...], limit: 50, offset: 0, total: 43834 }
});

The callback result is an object with the following properties:

data array of found models
limit maximum number of models returned (page size)
offset offset for pagination
total total number of models found with query

The limit, offset, and total properties are used in building pagination.

Model.find(id|query, callback)

Find a model by given id or query.

User.find(5, function(err, user) {
  user.name(); // => Alex
});

User.find({ where: { name: 'Alex' }}, function(err, user) {
  user.name(); // => Alex
});

Model.all(query, callback)

Find models using given query.

User.all({ $gt: { created_at: lastWeek} }, function(err, users) {
  // ...
});

The users object looks like:

{
  "total": 32443,
  "limit": 50,
  "offset": 0,
  "data": [user, user, ...]
}

Model.hasMany(name, params)

Define a "has many" relationship with given name and params.

User.hasMany('posts', { model: Post, foreignKey: 'user_id' });

// Creates methods:

user.posts(function(err, posts) {
  // ...
})

var post = user.posts.create();

Model.belongsTo(Model, params)

Define a "belongs to" relationship with given Model.

User.belongsTo(Post, { as: 'author', foreignKey: 'user_id' });

// Creates method:

post.author(function(err, user) {
  // ...
});

Model.hasAndBelongsToMany(name, params)

Define a "has and belongs to many" relationship with given name and params.

User.hasAndBelongsToMany(Post, {
  as: 'posts',
  through: PostUser,
  fromKey: 'user_id',
  toKey: 'post_id'
});

// Creates methods:

user.posts(function(err, posts) {
  // ...
})

var post = user.posts.create();

post.author(function(err, user) {
  // ...
});

Querying for relations

Any model with a "has many" relationship can be included in the results for Model.all() and Model.get() by specifying the name of the relationship in the req.query.include parameter. Related models will be added to model.related.

User.find({ id: 1, include: 'posts' }, function(err, user) {
  console.log(user.related.posts);
  // => will contain the post models related to this user.
});

Events

mysql before save

User.on('mysql before save', function(model, attrs) {
  // ...
});

user.on('mysql before save', function(attrs) {
  // ...
});

mysql after save

User.on('mysql after save', function(model) {
  // ...
});

user.on('mysql after save', function() {
  // ...
});

mysql before update

User.on('mysql before update', function(model) {
  // ...
});

user.on('mysql before update', function() {
  // ...
});

mysql after update

User.on('mysql after update', function(model) {
  // ...
});

user.on('mysql after update', function() {
  // ...
});

mysql before remove

User.on('mysql before remove', function(model) {
  // ...
});

user.on('mysql before remove', function() {
  // ...
});

mysql after remove

User.on('mysql after remove', function(model) {
  // ...
});

user.on('mysql after remove', function() {
  // ...
});

exports.adapter

MySQL module.

Connection pooling

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

Queries

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

Custom table / field names

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

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

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.

Data formatters

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

var Event = modella('Event');

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

Tests

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

Tests require an accessible MySQL server. Configure the database using the following set of environment variables:

export NODE_TEST_MYSQL_HOST="127.0.0.1"
export NODE_TEST_MYSQL_USER="root"
export NODE_TEST_MYSQL_PASSWORD=""
export NODE_TEST_MYSQL_PORT=3306

Run the tests with npm:

npm test

MIT Licensed