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

chill-sql

v1.1.5

Published

simple sql builder for mysql in typescript

Downloads

11

Readme

Introduction

Chill-SQL is a TypeScript-based MySQL library designed to offer a type-safe and efficient way to build and execute SQL queries. Leveraging the robust type system of TypeScript and seamlessly integrating with Jetbrains IDEs, it provides developers with convenient type hints and checks, significantly enhancing development speed and code quality.

Features

  • Type-Safe SQL Building: Utilize the power of TypeScript's type system to reduce runtime errors.
  • IDE Friendly: Seamless integration with Jetbrains IDEs for real-time type hints and code completion.
  • Simplified Query Building: An easy-to-use API to quickly construct complex SQL queries.
  • Flexible Query Operations: Supports a variety of operations including joins, sorting, aggregation, etc.
  • Transaction Support: Convenient transaction handling methods to ensure data consistency.

Installation

npm install chill-sql
# or
yarn add chill-sql

Quick Start

Here's a simple example to demonstrate how to use chill-sql for MySQL operations:

import { createMysqlClient } from 'chill-sql'

// Define table types
type User = {
    user_id: string
    user_name: string
    user_phone?: string
    user_company?: string
    user_height?: number
}

type UserGroup = {
    group_id: string
    group_name: string
    group_owner_id: string
    group_created_time: number
}

type MyTables = {
    user: User
    user_group: UserGroup
}

// Create MySQL client instance
const mysqlClient = createMysqlClient<MyTables>({
    host: `127.0.0.1`,
    user: `root`,
    port: 3306,
    password: `whosyourdaddy`,
    database: `test`,
    charset: 'utf8mb4_unicode_ci',
    timeout: 8000,
})

// Usage examples
// Query operation

// the type of users is User[]
const users = await mysqlClient.tables.user.select('*')
    .where(e => e.user_company.eq(`doge`))
    .and(e => e.user_height.gte(166))
    .exec()


// the type of result is Pick<User,'user_name'>[]
const result = await mysqlClient.tables.user.select('user_name')
    .where(e =>
        e.user_company.eq(`doge`)
            .or(e.user_company.eq('cate'))
    )
    .and(e => e.user_height.gte(166))
    .exec()


// the type of joinResult is Array<UserGroup & {ownerName: string}>
const joinResult = await mysqlClient.tables.user_group.as('g')
    .join('user as u')
    .on(it =>
        it.u.user_id.eq(
            it.g.group_owner_id
        )
    )
    .select(
        'g.*',
        'u.user_name as ownerName'
    )
    .orderBy('g.group_created_time asc')
    .exec()

// the type of countResult is Array<{ownerName: string, groupCount: number}>
const countResult = await mysqlClient.tables.user_group.as('g')
    .join('user')
    .on(it =>
        it.user.user_id.eq(
            it.g.group_owner_id
        )
    )
    .select(
        'user.user_name as ownerName',
        'count(g.group_id) as groupCount'
    )
    .orderBy('g.group_created_time asc')
    .having(it => it.groupCount.gte(3))
    .exec()

// Insert operation
await mysqlClient.tables.user.insert({
    user_id: 'Enoch',
    user_name: 'EnochL',
    user_company: 'doge'
})

// Update operation
await mysqlClient.tables.user
    .update({
        user_company: 'capsule'
    })
    .where(e => e.user_id.eq('Enoch'))
    .exec()

// Delete operation
await mysqlClient.tables.user
    .delete()
    .where(e => e.user_id.eq('xxx'))
    .exec()

// Transaction operation
await mysqlClient.runTransaction(async client => {
    await client.tables.user.insert({
        user_id: 'Enoch',
        user_name: 'EnochL',
        user_company: 'doge',
        user_height: 180
    })
    await client.tables.user.insert({
        user_id: 'somebody',
        user_name: 'whoknows',
        user_company: 'doge',
        user_height: 166
    })
})

License

Chill-SQL is open-sourced software licensed under the MIT license.

Issues and Support

If you encounter any issues or have questions regarding chill-sql, please feel free to open an issue here. Your feedback and questions are valuable to me, and I'll do my best to assist you.

Show Your Support

If you find chill-sql helpful or interesting, please consider giving it a star on GitHub! Your support means a lot and motivates me to continue improving this project. To star the project, just click on the ⭐️ button in the top right corner of the chill-sql repository page.

Thank you for using and supporting chill-sql!