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

@yowza/db-handler

v0.2.4

Published

A mysql handler for nodejs

Downloads

88

Readme

dbHandler

환경변수

.env 파일에 다음과 같은 값을 작성하면 DB에 요청을 보낼 준비가 됩니다.

process.env.DB_HOST = "host"
process.env.DB_USER = "user"
process.env.DB_PASSWORD = "password"
process.env.DB_PORT = "port"
process.env.DB_DATABASE = "database"

defineDBHandler

interface Student{
    order: number;
    id: number;
    name: string;
    age: number;
}

const getAllStudents = defineDBHandler<[], Student[]>(() => { //제네릭으로 파라미터 타입과 리턴 타입을 설정할 수 있습니다.
    return async(run) => {
        return await run("SELECT * FROM `student`"); //run은 sql 서버에 요청을 보내 sql문을 실행합니다.
    }
})

//파라미터 타입은 제네릭의 배열 안에 순서대로 넣어서 설정합니다. 
//여기서 id는 number가 됩니다.
const getStudentById = defineDBHandler<[number], Student | null>((id) => {
    return async(run) => {
        //run의 두번째 파라미터를 사용하여 sql문에 escape된 값을 넣을 수 있습니다.
        const result = await run("SELECT * FROM `student` WHERE `id` = ?", [id]);
        if(result.length === 0){
            return null;
        }
        else{
            return result[0];
        }
    }
});

const updateStudent = defineDBHandler<[Student], void>((student) => {
    return async(run) => {
        //커넥션을 여러번 생성하는 것을 방지하기 위해 getCallback 메소드를 사용합니다.
        //getCallback 메소드는 run을 파라미터로 받는 함수를 반환합니다.
        const student = await getStudentById.getCallback(student.id)(run);

        if(student){
            await run("UPDATE `student` SET `name` = ?, `age` = ? WHERE `id` = ?", [student.name, student.age, student.id]);
        }
        else{
            await run("INSERT INTO `student` (`id`, `name`, `age`) VALUES (?, ?, ?)", [student.id, student.name, student.age]);
        }
    }
})

runQuery

// runQuery를 사용하여 DBHandler를 사용하지 않고 DB에 요청을 보낼 수 있습니다.
// 제네릭을 사용하여 리턴 값의 타입을 정할 수 있습니다.
const student = await runQuery<Student | null>(async(run) => {
    const result = await run("SELECT * FROM `student` WHERE `id` = ?", [id]);
    if(result.length === 0){
        return null;
    }
    else{
        return result[0];
    }
})

// 그러나 runQuery는 한번의 DB 연결에서 많은 DBHandler들을 실행해야 할 때 더 유용합니다.
const getStudents = defineDBHandler<[], Student[]>(() => {
    return async(run) => {
        return await run("SELECT * FROM `student`");
    }
});
const getClassrooms = defineDBHandler<[], Classroom[]>(() => {
    return async(run) => {
        return await run("SELECT * FROM `classroom`");
    }
});
const studentsAndClassrooms = await runQuery<{students: Student[], classrooms:Classroom[]}>(async(run) => {
    const students = await getStudents.getCallback()(run);
    const classrooms = await getStudents.getCallback()(run);

    return {
        students,
        classrooms
    }
})

DBConnector

기본적으로 defineDBHandlerrunQuery.env 파일에 적힌 DB로 요청을 보냅니다. 다른 DB로 요청을 보내려면 DBConnector 클래스로 인스턴스를 만들어 요청을 보낼 수 있습니다.

const anotherDBConnector = new DBConnector({
    host: 'another host',
    user: 'another user',
    password: 'another password',
    port: 3306 // or any,
    database: 'another database'
});

const handler = anotherDBConnector.defineDBHandler(() => {
    return async(run) => {
        ...
    }
});

const result = await anotherDBConnector.runQuery(async(run) => {
    ...
})