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

my-db-utils

v1.2.2

Published

pg utils

Downloads

37

Readme

my-db-utils

npm install my-db-utils

Define a simple Db Model

const { dbModel, table, column } = require("my-db-utils");

const myDbModel = dbModel("MyModelName", table("my_table_name"), {
    id: column("id_column").pk().integer(),
    name: column("name_column").string(),
    description: column("description_column").string().nullable()
});

const MyModel = myDbModel.constructor;

Using model for factory object

const myItem1 = MyModel({
    id: 1,
    name: "name",
});

const myItem2 = MyModel({
    id: 2,
    name: "name 2",
    description: "My description 2"
});

Using model Crud

const { Pool } = require('pg');
const { crud } = require('my-db-utils');

const dbPool = new Pool();
const myModelCrud = crud(myDbModel, dbPool);

Create

myModelCrud.create([myItem1, myItem2])
    .then(items => console.log(items))
    .catch(error => concole.error(error));

Read

myModelCrud.read({id: 1})
    .then(items => console.log(items)) // items Array
    .catch(error => concole.error(error));

Update

myItem1.description = "My description";
myItem2.name = "My new name 2";

myModelCrud.update([myItem1, myItem2])
    .then(() => console.log("success"))
    .catch(error => concole.error(error))

Delete

myModelCrud.delete({id: 1})
    .then(() => console.log("success"))
    .catch(error => concole.error(error))

Advanced Model types

const { dbModel, table, column } = require("my-db-utils");

const myAdvancedDbModel = dbModel("MyAdvancedModel", table("my_advanced_table_name"), {
    // Use default Db definition if value is null, Ej: Sequence generation for PK
    id: column("id_column").pk().integer().default(), 
    // Array of strings
    names: column("names_column").array().string(), 
    // JSON column
    address: column("address_column").json().nullable(),
    // TIMESTAMP WITHOUT TIME ZONE column 
    createdAt: column("created_at_column").timeStamp(), 
    // TIMESTAMP WITH TIME ZONE column
    scheduleTime: column("schedule_time_column").timeStampTZ(), 
});

const MyAdvancedModel = myAdvancedDbModel.constructor;

const myAdvancedItem1 = MyAdvancedModel({
    names: ["name 1.1", "name 1.2"],
    address: {city: "NewYork", address: "Calle 34 # 16", intern: "Apto 920"},
    createdAt: new Date(),
    scheduleTime: new Date(2021, 05, 13, 19, 50),
})

Advanced Search (For read and delete)

IN

myModelCrud.read({ id: [1, 2]})    // WHERE id IN (1,2)
    .then(items => console.log(items))
    .catch(error => concole.error(error));

Searh by text

// WHERE name LIKE "%name%"
myModelCrud.delete({ name: "%name%" })    
    .then(() => console.log("success"))
    .catch(error => concole.error(error));

AND

// WHERE name LIKE '%name%' AND description LIKE '%pretty%'
myModelCrud.read({ name: "%name%", description: "%pretty%" }) 
    .then(items => console.log(items))
    .catch(error => concole.error(error));

Relational operators

const { condition } = require("my-db-utils");

const { equal, gretter, gretterEqual, less, lessEqual, different } = condition;

// WHERE id > 5
myModelCrud.read({ id:  gretter(5)}) 
    .then(items => console.log(items))
    .catch(error => concole.error(error));

// WHERE id <= 10
myModelCrud.read({ id:  lessEqual(10)}) 
    .then(items => console.log(items))
    .catch(error => concole.error(error));

// WHERE createdAt < '2020-01-15'
myAdvancedModelCrud.delete({createdAt: less(new Date(2020, 0, 15))}) 
    .then(() => console.log("success"))
    .catch(error => concole.error(error));