1.0.2 • Published 3 months ago

postgres-crud v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
3 months ago

connect to postgres,

    const {Pool} = require("pg");

    const pool = new Pool({
    user: 'database-username',
    host: 'localhost',
    database: 'database-name',
    password: 'your-password',
    port: 5432
    });

Import postgres-crud package

    const crudPostgres = require("postgres-crud");

Create class instance of the package

    const user = new crudPostgres(pool, "studentsRecord", 
    {name: "name", type: "string", isUnique: true, isNull: false}, 
    {name: "age", type: "number", isNull: false}, 
    {name: "gender", type: "enum", isNull: false, values: ["male", "female"]}, 
    {name: "in_school", type: "boolean", isNull: false, defaultBool: false});

first argument: pool connection variable

second argument: table name

rest arguments: contain various columns structure.

data using in structuring a column are:

    name: column name
    type: column type // can be string, number, enum and boolean
    isUnique: set column variables to be unique, can be either true or false
    isNull: set column variable to be null or require, can be either true or false
    defaultBool: set column of type boolean default value of true or false
    values: can only be use when column type is enum. should be in array which state the enum variables

Insert Data

    await user.create({name: "Turner", age: 40, gender: "male", in_school: false})

Retrieve Data

    await user.find({name: "Turner"})
    
    response 
        [
            {
                id: 1,
                name: 'Turner',
                age: '40',
                gender: 'male',
                in_school: false,
                created_at: 2024-03-07T15:19:47.367Z,
                updated_at: 2024-03-07T15:19:47.367Z
            }
        ]

Update Data

    await user.update({name: "Turner"}, {age: 55});

    response
        {
            id: 1,
            name: 'Turner',
            age: '55',
            gender: 'male',
            in_school: false,
            created_at: 2024-03-07T15:19:47.367Z,
            updated_at: 2024-03-07T15:19:47.367Z
        }

Delete Data

    await user.delete({name: "Turner"})
1.0.2

3 months ago

1.0.1

3 months ago

1.0.0

3 months ago