Package Exports
- @chrisbrocklesby/mysql
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@chrisbrocklesby/mysql) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
MySQL Connector (with built CRUD Functions)
This is a MySQL Connector with built in quick CRUD and RAW functions for faster development.
Install
npm i @chrisbrocklesby/mysqlConnect to MySQL (.env)
Create a .env file with the following params
MYSQL_HOST=localhost
MYSQL_USER=username
MYSQL_PASSWORD=password
MYSQL_DATABASE=databasenameRequire
const { Model, query } = require('@chrisbrocklesby/mysql');Create a Model
const Post = new Model('posts');
// const modelName = new Model('tableName');CRUD Functions
Please note these CRUD Functions are designed to be used within a async function.
Create
await Post.create({
title: 'Hello',
body: 'World'
});
// => Returns Object { inserted: true, insertId: 1 }Read
await Post.read({ id: 1 });
// => Returns Single Object [0]
// or
await Post.read({ pk: '12345-abcde-09876-54321' });
// => Returns Single Object [0]
// or
await Post.read({ email: 'user@email.ext' });
// => Returns Single Object [0]
// or
await Post.read({ anyKey: 'anyValue' });
// => Returns Single Object [0]
await Post.readAll();
// => Returns Array of Objects []
// or
await Post.readAll({ anyKey: 'anyValue' });
// => Returns Array of Objects []Update
await Post.update({ id: 1 }, { title: 'Hello World' });
// => Returns Object { updated: true, where: {id: 1} }
// or
await Post.update({ pk: '12345-abcde-09876-54321' }, { title: 'Hello World' }));
// => Returns Object { updated: true, where: {pk: '12345-abcde-09876-54321'} }Delete
await Post.delete({ id: 1 });
// => Returns Object { deleted: true, where: {id: 1} }
//or
await Post.delete({ pk: '12345-abcde-09876-54321' });
// => Returns Object { deleted: true, where: {pk: '12345-abcde-09876-54321'} }RAW MySQL Query
Quick Raw Query
await query('SELECT * FROM posts');
// => Returns Object or Array depending on queryEscaped Placeholder Raw Query
await query('SELECT * FROM posts WHERE title LIKE ?', ['%hello%']);
// => Returns Object or Array depending on queryExample Usage
const { Model, query } = require('@chrisbrocklesby/mysql');
const Post = new Model('posts');
async function exampleFunction() {
try {
const posts = await Post.read();
console.log(posts);
}
catch (error) {
console.log(error);
}
}
exampleFunction();Contributing
Contributions, issues and feature requests are welcome.
Authors
Chris Brocklesby
- Twitter: @ChrisBrocklesby
- Github: @ChrisBrocklesby
Show Your Support
Please ⭐️ this repository if this project helped you!
License
Copyright © 2020 Chris Brocklesby.
This project is licensed under the MIT License - see the LICENSE file for details.