@n1ru4l/slonik-utilities
TypeScript icon, indicating that this package has built-in type declarations

0.0.1-alpha.4 • Public • Published

@n1ru4l/slonik-utilities

Utilities for the PostgreSQL Client Slonik.

Motivation

Slonik removed some convenient methods in version 19.x.x. The new approach is a bit "too" verbose for my taste. This is why I wrote these helper methods to do common stuff more easily and quicker.

API

valuesSingle - Insert a single record

import { sql } from "slonik";
import { valuesSingle } from "@n1ru4l/slonik-utilities";

const user = {
  id: 1,
  login: "peter",
  email: "peter@localhost"
};

const query = sql`
  INSERT INTO "users" (
    "id",
    "login",
    "email"
  ) VALUES (${valuesSingle([user.id, user.login, user.email])});
`;

valuesList - Insert multiple records

import { sql } from "slonik";
import { valuesList } from "@n1ru4l/slonik-utilities";

const users = [
  {
    id: 1,
    login: "peter",
    email: "peter@localhost"
  },
  {
    id: 2,
    login: "lukas",
    email: "lukas@localhost"
  }
];

const query = sql`
  INSERT INTO "users" (
    "id",
    "login",
    "email"
  ) VALUES (${valuesList(users, user => [user.id, user.login, user.email])});
`;

assignmentList - Update multiple columns

import { sql } from "slonik";
import { assignmentList } from "@n1ru4l/slonik-utilities";

const query = sql`
  UPDATE "users"
  SET ${assignmentList({
    email: "newemail@localhost",
    otherValue: 10
  })}
  WHERE "id" = 1;
`;

identifierList - Generate a list of identifiers

import { sql } from "slonik";
import { identifierList } from "@n1ru4l/slonik-utilities";

const query = sql`
  SELECT ${identifierList([
    ["users", "id"],
    ["users", "someOtheColumn"]
  ])}
  FROM "users";
`;

Readme

Keywords

Package Sidebar

Install

npm i @n1ru4l/slonik-utilities

Weekly Downloads

2

Version

0.0.1-alpha.4

License

MIT

Unpacked Size

11.7 kB

Total Files

27

Last publish

Collaborators

  • n1ru4l