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

icesql

v1.1.5

Published

query SQL databases with strict typing in Node.js

Downloads

41

Readme

icesql

(icicle)

A library to easily query and manipulate SQL databases.

Currently supports only MySQL, but is a work in progress to support all major types of SQL database.

Installation

Add package

npm i icesql

yarn add icesql

icesql has typescript typings out of the box.

Usage

const databaseConfig = {
  host: "localhost",
  port: 3306,
  database: "database",
  user: "root",
  password: "root"
};
const connector = connectToDatabase(databaseConfig);

If you use only one (or a primary) database connection, instead call the following:

registerDefaultConnection(databaseConfig);

You can then omit connector from any function call in the following.

Query

import { query } from 'icesql';

interface Person {
  id: number;
  name: string;
}

...

async function findPerson(id: number): Promise<Person | undefined> {
  const person = await queryOne<Person>({ query: { id: 1 }, table: 'people' }, connector)
  return person;
}

async function findPeople(): Promise<Person[]> {
  const people = await query<Person>({ query: {}, table: 'people' }, connector)
  return people;
}

If you registered a default connection, use:

const people = await query<Person>({ query: {}, table: 'people' }) // omit `connector`
return people;

Update

update returns ResultSetHeader from mysql2, which is implemented by icesql.

import { update } from 'icesql';

...

async function updatePerson(id: number, updated: Partial<Person>) {
  const result = await update(
    {
      query: { id },
      table: 'people',
      updated
    },
    connector
  );
  return result;
}

...
await updatePerson(1, { name: 'John' });

Insert

insert returns ResultSetHeader from mysql2, which is implemented by icesql.

import { insert } from 'icesql';

...

async function insertPerson(person: Person) {
  const result = await insert(
    { object: person, table: 'people' },
    connector
  );
  return result;
}

async function insertPeople(people: Person[]) {
  const result = await insert(
    { object: people, table: 'people' },
    connector
  );
  return result;
}

Delete

note: should be named 'delete', but that is a reserved word in javascript. remove returns ResultSetHeader from mysql2, which is implemented by icesql.

import { remove } from 'icesql';

...

async function deletePerson(id: number) {
  const result = await remove({ query: { id }, table: 'people' }, connector)
  return result;
}

Execute

All executions return ResultSetHeader from mysql2, which is implemented by icesql.

import { execute } from 'icesql';

...

const result = await execute({ sql: `MODIFY TABLE person DROP COLUMN name;` }, connector)

Transaction

import { transaction } from 'icesql';

...

async function doLotsOfStuff(): Promise<Person[]> {
  return transaction((connection) => {
    await connection.remove({ query: { id }, table: 'people' });

    const people = await connection.query<Person>(`SELECT * FROM people`);
    return people;
  }, connector)
}

You can concatenate the connection of transaction with regular queries and executions by passing it as the second parameter:

// this version is equivalent to the above one
async function doLotsOfStuff(): Promise<Person[]> {
  return transaction((connection) => {
    await remove({ query: { id }, table: 'people' }, connection)

    const people = await query<Person>({ query: {}, table: 'people' }, connection);
    return people;
  }, connector)
}

...

// you can also chain a transaction by passing the connection to a separate function.

async function deletePerson(id: number, connection?: Connection) {
  const result = await remove({ query: { id }, table: 'people' }, connection)
  return result;
}