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

mysql-admin-sql-gen

v0.1.1

Published

A library for generating MySQL administrative SQL statements.

Downloads

31

Readme

MySQL Administrative SQL Generator Library

A library for generating MySQL administrative SQL statements. Similar to Squel but for CREATE DATABASE, CREATE ROLE, CREATE USER, GRANT, and other administrative statements.

(If anyone knows of a JavaScript/Node.js library that performs the same or nearly the same functionality as this library, please let me know. I searched for an hour or two looking for a similar library before I decided to create this library.)

Note: This library does NOT provide the ability to connect to a MySQL database and therefore also does NOT execute generated SQL. There are several packages that can be used for executing the generated SQL: bookshelf, knex, mysql, mysql2, sequelize, etc.

GitHub Repo NPM Version Minimum Node Version Build Status Test Coverage License Lines of Code Gzip Size NPM Downloads GitHub Stars

Why?

Almost all, if not all, ORM's expect that the role(s), user(s), database(s), et al already exist before using the ORM to generate tables and other database objects. Some will provide you some documentation on how to execute raw SQL which can be used to execute SQL statements that create role(s), user(s), database(s), et al but they don't have an API to generate these types of statements. That's where this library comes into play.

For many projects, these administrative SQL statements are not configuration-driven, and hard coding them is perfectly valid. But for those projects that are configuration-driven, an API provides some benefit. For example, this library will properly escape special characters for the arguments that need it.

This library works in the browser too.

Have online documentation instructing the user to execute some administrative SQL? Use this library to provide data-driven documentation.

Examples

// database-config.js
const dbConfig = {
  database: 'example',
  users: {
    readonly: { name: 'readonly', host: 'localhost' },
  },
}
export default dbConfig

Creating a Database

import { createDatabase } from 'mysql-admin-sql-gen'

import dbConfig from './database-config'

const sql = createDatabase(dbConfig.database, { ifNotExists: true })
// 'CREATE DATABASE IF NOT EXISTS `example`;'

Creating a User

import { createUser } from 'mysql-admin-sql-gen'

import dbConfig from './database-config'

const sql = createUser(dbConfig.users.readonly, { ifNotExists: true })
// "CREATE USER IF NOT EXISTS 'readonly'@'localhost'"

Grant Privileges

import { grant } from 'mysql-admin-sql-gen'

import dbConfig from './database-config'

const sql = grant({
  privileges: ['EXECUTE', 'SELECT'],
  on: {
    level: { database: dbConfig.database },
  },
  to: dbConfig.users.readonly,
})
// "GRANT EXECUTE, SELECT ON `example`.* TO 'readonly'@'localhost'"

These examples are just the tip of the iceberg. See the unit tests for a complete set of usage examples.

Maintainers

License

ISC