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

@simplehealth/mysql-simulator

v1.9.3

Published

This MySQL simulator offers a super easy API that, at the highest level, looks like:

Downloads

70

Readme

Build Status

Usage

This MySQL simulator offers a super easy API that, at the highest level, looks like:

import simulate from '@simplehealth/mysql-simulator';

const db = simulate('./path/to/migrations');

The resulting DB instance is an in-memory representation of the final DB state after applying all the SQL migrations in order, exactly as it would be in MySQL. The DB object can now conveniently be introspected, for example:

for (const table of db.getTables()) {
  console.log(`- ${table.name}`);
}

const users = db.getTable('users');
console.log(users.getColumn('email').getTypeInfo().baseType); // varchar
console.log(users.getColumn('email').getTypeInfo().nullable); // true
console.log(users.getColumn('email').toString()); // `email` varchar(254) DEFAULT NULL

// Or just print out the whole table definition, conveniently
console.log(users.toString());
// CREATE TABLE `users` (
//   `id` int(11) NOT NULL AUTO_INCREMENT,
//   `partner_id` tinyint(3) unsigned NOT NULL,
//   `external_id` varchar(32) NOT NULL,
//   `date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
//   `name` varchar(128) DEFAULT NULL,
//   `email` varchar(254) DEFAULT NULL,
//   ...

These objects are fully Flow-typed.

Usage from the command line

There's a command line interface that might help you if you simply want to apply some migrations and output the resulting table definitions:

$ bin/mysql-simulate <path>

This assumes a directory of *.sql files containing SQL statements, which are numbered sequentially, for example: 0001-create-table.sql, 0002-change-field.sql, etc.

To run it on all *.sql migration files in a directory:

$ bin/mysql-simulate path/to/migrations

This will simulate running the migrations found in that directory sequentially. At the end, the DB's state is outputted as a single SQL table definition dump.

For developers

Regenerating the parser

After changing the grammar file (src/parser/mysql.pegjs), you need to recompile the parser:

$ yarn build:parser

Running the test suite

The test suite for this project is a little bit different from "normal" JavaScript project tests. It's invoked by

$ yarn test

And will basically do the following for all test files found in tests/*.sql:

  1. Run test files against a real, running, MySQL database. Output to tests/real/*.sql.
  2. Run test files against the simulator. Output to tests/simulated/*.sql.
  3. Diff the results. No diff means test suite passes.

This setup offers the level of confidence that the simulator is actually working as expected, and at the same time makes it really easy to add specific test cases later on: simply add a new *.sql file!