Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@ttoss/postgresdb

Package Overview
Dependencies
Maintainers
2
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ttoss/postgresdb

A library to handle PostgreSQL database connections and queries

Source
npmnpm
Version
0.1.2
Version published
Weekly downloads
1.1K
206.79%
Maintainers
2
Weekly downloads
 
Created
Source

@ttoss/postgresdb

This package uses Sequelize to provide a simple framework for working with PostgreSQL databases.

Installation

pnpm add @ttoss/postgresdb
pnpm add -D @ttoss/postgresdb-cli

Usage

Setup the database

If you already have a database, you can skip this step. If you don't, you can use the following Docker command to create a new PostgreSQL database on port 5432 using Docker:

docker run --name postgres-test -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres

Create a model

Create a folder called models and add a new file called User.ts with the following content:

import { Table, Column, Model } from '@ttoss/postgresdb';

@Table
export class User extends Model<User> {
  @Column
  declare name: string;

  @Column
  declare email: string;
}

_This packages exports all decorators from [sequelize-typescript](https://github.com/sequelize/sequelize-typescript), so you can use them to define your models._

Export the model in the models/index.ts file:

export { User } from './User';

Create a new instance of the database

Create a new file called src/db.ts with the following content:

import { initialize } from '@ttoss/postgresdb';
import * as models from './models';

export const db = initialize({ models });

Environment variables

You can set the database connection parameters in two ways:

  • Defining them in the src/db.ts file using the initialize function.

    export const db = initialize({
      database: '', // database name
      username: '', // database username
      password: '', // database password
      host: '', // database host
      port: 5432, // database port. Default: 5432
      models,
    });
    
  • Using environment variables:

    • DB_NAME: database name
    • DB_USERNAME: database username
    • DB_PASSWORD: database password
    • DB_HOST: database host
    • DB_PORT: database port. Default: 5432

    @ttoss/postgresdb will use them automatically if they are defined.

Sync the database schema

To sync the database schema with the models, use the sync command:

pnpm dlx @ttoss/postgresdb-cli sync

By now, you should have a working database with a User table.

CRUD operations

You can now use the db object to interact with the database. Check the Sequelize documentation for more information.

import { db } from './db';

const user = await db.User.create({
  name: 'John Doe',
  email: 'johndoe@email.com',
});

All models are available in the db object.

API

initialize(options: InitializeOptions): db

Initialize the database connection and load the models.

Options

All Sequelize options are available, expect models.

  • models: An object with all models to be loaded. The keys are the model names, and the values are the model classes. This way, you can access the models using the db object.

Sequelize decorators

This package exports all decorators from sequelize-typescript, i.e., @Table, @Column, @ForeignKey, etc.

Keywords

database

FAQs

Package last updated on 26 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts