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

glhera-db-utils

v1.0.0-next.3

Published

Biblioteca simples para auxiliar o uso de `Raw SQL`, provendo tipagem para métodos simples de `insert` e `update`, mas essa biblioteca é muito simples, não foi desenvolvida para ser um completo `query builder`, só para `insert` e `update` de uma linha

Downloads

309

Readme

glhera-db-utils

Biblioteca simples para auxiliar o uso de Raw SQL, provendo tipagem para métodos simples de insert e update, mas essa biblioteca é muito simples, não foi desenvolvida para ser um completo query builder, só para insert e update de uma linha

Possui wrappers para node-postgres (pg), @electric-sql/pglite (pglite) e better-sqlite3

Exemplo

import { PGDBInstance } from 'glhera-db-utils/pg';
import { pg } from 'pg';

//
// A tipagem do banco
type DBTypes = {
  users: {
    id: number;
    name: string;
  };
};

//
// Cria o driver de acesso do postgres
const client = new pg.Client({ connectionString });
await client.connect();

//
// Cria a instancia para acesso
const pgDB = new PGDBInstance<DBTypes>(client);

//
// Acessando o client no pgDB
console.log(pgDB.driver);

Banco de dados suportados no momento

import { PGDBInstance } from 'glhera-db-utils/pg';
import { PGLiteDBInstance } from 'glhera-db-utils/pglite';
import { BetterSqlite3DBInstance } from 'glhera-db-utils/better-sqlite3';

Integrando com alguma biblioteca como o Zod

import { PGDBInstance } from 'glhera-db-utils/pg';
import { pg } from 'pg';
import { z } from 'zod';

//
// A tipagem do banco
const dbSchemas = {
  users: z.object({
    id: z.number(),
    name: z.string(),
  }),
};

//
// Cria o driver de acesso do postgres
const client = new pg.Client({ connectionString });
await client.connect();

//
// Tipo utilitário para inferir a tipagem de um objeto Zod
type InferTypes<T extends Record<string, z.ZodType<any, any, any>>> = {
  [K in keyof T]: z.infer<T[K]>;
};

//
// Cria a instancia para acesso
const pgDB = new PGDBInstance<InferTypes<typeof dbSchemas>>(client);

E com o exemplo abaixo, podemos validar com o Zod (pode ser outro como o yup) o nosso campo antes de salvá-lo, útil para campos json ou salvar no SQLite

const dbSchemasInsert = {
  users: z.object({
    id: z.number(),
    name: z.string(),
  }),
};

const dbSchemasUpdate = {
  users: dbSchemasInsert.users.partial(),
};

const pgDB = new PGDBInstance<InferTypes<typeof dbSchemasInsert>>(
  client,
  (table, values) => (dbSchemasInsert as any)[table].parse(values), // Insert, unfortunately typing here does not work well
  (table, values) => (dbSchemasUpdate as any)[table].parse(values), // Update, unfortunately typing here does not work well
);

Notas

É necessário criar um dbSchemasUpdate que seja partial para essa operação

A tipagem para o Zod (ou outra lib) para insert é que o id se é autoincrements precisa ser .nullish() para que o TypeScript não fique reclamando em cada insert por não inserir o id

ROADMAP

  • wrappers para
    • mysql2

Tests

Para poder testar essa aplicação, é necessário definir as variáveis de ambiente para o postgres

PG_USER=
PG_PASSWORD=
PG_HOST=
PG_DATABASE=