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

@bringittocode/fastify-mop

v1.0.6

Published

This is a fastify plugin to create long live connection between your application and mysql or postgresql, this is not mysql pool.

Downloads

8

Readme

fastify-mop

create a long live connection between your application and mysql or postgresql in fastify. this is not sql pool.

this package helps you to manage auto reconnection.

you still have the complete features of mysql or postgresql in nodejs except for pool

INSTALLATION

npm i @bringittocode/fastify-mop@latest

FEATURES

  • One library rule them all... i.e you can use any mysql module with this library to manage connection also support postgresql.
  • Auto reconnection if connection timeout or an error occured
  • You still have all the method of MYSQL2 and postgresql except for pool.
  • You have an event to listen on if connection failed or successful

USAGE

import the module

import mop from "@bringittocode/fastify-mop";
// Import any mysql module you wish to use
import mysql from "mysql";
//OR
import pg from "pg";

//OR
const mop = require('@bringittocode/fastify-mop');
// Import any mysql module you wish to use
const mysql2 = require("mysql2");
const pg = require("pg");

Register the module

fastify.register(mop,{
    DATABASE_INFO: {
        host: "",
        user: "",
        password: "",
        database: "",
    },
    WAIT_TIME: 2000, //optional
    MODULE: mysql, //mysql2 or pg //optional
    USE_NAME: "DB", //optional
    USE_EVENT_NAME: "DB_INFO" //optional
});

DATABASE_INFO => your database detailes... you can also put any valid MYSQL initialization option.

WAIT_TIME => how many miliseconds to wait before reconnecting if there is any disconnection.. DEFAULT 5 seconds

MODULE => Any MYSQL module you wish to use.. DEFAULT MYSQL2

USE_NAME => Any name you would love to use, this will be the name of the database decoration.. DEFAULT ( DB ).... so it will be fastify.DB or fastify.anyname

USE_EVENT_NAME => Any name you would love to use, this will be the name of the database event decoration.. DEFAULT ( DB_INFO ).... so it will be fastify.DB_INFO or fastify.anyname

After registering you then have mysql instance in all of your application

//mysql instance
fastify.DB //or fastify.anyname

//use to listen on events
fastify.DB_INFO //or fastify.anyname

what you have to do is to focus on your query as you would if no package is used

CURRENTLY SUPPORTED MODULE.

    const query = "SELECT * FROM your_table where ?";

    // If you are using postgre your query function might be different
    // Check there doc on how to query
    fastify.DB.query(query, ["user"], function (err, result) {
        try {
            if (err) throw err;
            //manage result
        }
        catch(err)
        {
            //manage error
        }
    });

NOTE :: do not disconnect after you finish... your connection is meant to stay alive so you can query your database at anytime

Event

event must be inside fastify ready function to avoid error

fastify.ready()
    .then(() => {

        // listen for connection and reconnection
        // status => boolean
        fastify.DB_INFO.on("connect",(status)=>{
            console.log('database connected');
        });

        // listen for reconnection error
        // code => error code
        // message => error message
        fastify.DB_INFO.on("reconnect_error", (code,message) => {
            console.log(code, message);
        });

        // listen for disconnection ...this can be for many reason
        fastify.DB_INFO.on("disconnect", (code) => {
            console.log(code);
        });
    })

That all you need. We are finding who can add typescript to this package

feel free to pull request and contribute....

any issue feel free as well.