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

modli-mysql

v3.1.0

Published

Modli adapter for MySQL

Downloads

7

Readme

wercker status Code Climate Test Coverage

Modli - MySQL Adapter

This module provides adapter for the MySQL datasource for integration with Modli.

Installation

npm install modli-mysql --save

Config and Usage

When defining a property which will utilize the adapter it is required that a tableName be supplied:

import { model, adapter, Joi, use } from 'modli';
import mysql from 'modli-mysql';

model.add({
  name: 'foo',
  version: 1,
  tableName: 'tblFoo'
  schema: {
    id: Joi.number().integer(),
    fname: Joi.string().min(3).max(30),
    lname: Joi.string().min(3).max(30),
    email: Joi.string().email().min(3).max(254).required()
  }
});

Then add the adapter as per usual with the following config object structure:

adapter.add({
  name: 'mysqlFoo',
  source: mysql
  config: {
    host: {HOST_IP},
    username: {USERNAME},
    password: {PASSWORD},
    database: {DATABASE}
  }
});

You can then use the adapter with a model via:

// Use(MODEL, ADAPTER)
const mysqlTest = use('foo', 'mysqlFoo');

Methods

The following methods exist natively on the MySQL adapter:

query

Allows for passing standard MySQL queries:

mysqlTest.query('SELECT * FROM tblFoo')
  .then(/*...*/)
  .catch(/*...*/);

createTable

Creates (IF NOT EXISTS) a table based on params:

mysqlTest.createTable({
    'id': [ 'INT', 'NOT NULL', 'AUTO_INCREMENT', 'PRIMARY KEY'],
    'fname': [ 'VARCHAR(255)' ],
    'lname': [ 'VARCHAR(255)' ],
    'email': [ 'VARCHAR(255)' ]
  })
  .then(/*...*/)
  .catch(/*...*/);

create

Creates a new record based on object passed:

mysqlTest.create({
    fname: 'John',
    lname: 'Smith',
    email: '[email protected]'
  })
  .then(/*...*/)
  .catch(/*...*/);

read

Runs a SELECT with optional WHERE:

mysqlTest.read('fname="John"')
  .then(/*...*/)
  .catch(/*...*/);

update

Updates record(s) based on query and body:

mysqlTest.update('fname="John"', {
    fname: 'Bob',
    email: '[email protected]'
  })
  .then(/*...*/)
  .catch(/*...*/);

patch

Patches (partial update) based on query and body:

mysqlTest.patch('fname="John"', {
    fname: 'Bob',
  })
  .then(/*...*/)
  .catch(/*...*/);

delete

Deletes record(s) based on query:

mysqlTest.delete('fname="Bob"')
  .then(/*...*/)
  .catch(/*...*/);

extend

Extends the adapter to allow for custom methods:

mysqlTest.extend('myMethod', () => {
  /*...*/
});

Development

The MySQL adapter requires the following enviroment variables to be set for running the tests. These should be associated with the MySQL instance running locally.

MODLI_MYSQL_HOST,
MODLI_MYSQL_USERNAME,
MODLI_MYSQL_PASSWORD,
MODLI_MYSQL_DATABASE

This repository includes a base container config for running locally which is located in the /docker directory.

Makefile and Scripts

A Makefile is included for managing build and install tasks. The commands are then referenced in the package.json scripts if that is the preferred task method:

  • all (default) will run all build tasks
  • start will run the main script
  • clean will remove the /node_modules directories
  • build will transpile ES2015 code in /src to /build
  • test will run all spec files in /test/src
  • test-cover will run code coverage on all tests
  • lint will lint all files in /src

Testing

Running make test will run the full test suite. Since adapters require a data source if one is not configured the tests will fail. To counter this tests are able to be broken up.

Test Inidividual File

An individual spec can be run by specifying the FILE:

make test FILE=some.spec.js

The FILE is relative to the test/src/ directory.

Deploys

For deploying releases, the deploy TAG={VERSION} can be used where VERSION can be:

<newversion> | major | minor | patch | premajor

Both make {COMMAND} and npm run {COMMAND} work for any of the above commands.

License

Modli-MySQL is licensed under the MIT license. Please see LICENSE.txt for full details.

Credits

Modli-MySQL was designed and created at TechnologyAdvice.