@chrisbrocklesby/mysql

    1.0.4 • Public • Published

    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/mysql
    

    Connect to MySQL (.env)

    Create a .env file with the following params

    MYSQL_HOST=localhost
    MYSQL_USER=username
    MYSQL_PASSWORD=password
    MYSQL_DATABASE=databasename
    

    Require

    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 query

    Escaped Placeholder Raw Query

    await query('SELECT * FROM posts WHERE title LIKE ?', ['%hello%']);
    // => Returns Object or Array depending on query

    Example 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

    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.

    Install

    npm i @chrisbrocklesby/mysql

    DownloadsWeekly Downloads

    1

    Version

    1.0.4

    License

    MIT

    Unpacked Size

    7.34 kB

    Total Files

    4

    Last publish

    Collaborators

    • chrisbrocklesby