New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

sequelize-database-connector

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sequelize-database-connector

This will help to connect and register model for database like MySQL and PostgreSQL

latest
npmnpm
Version
1.0.1
Version published
Maintainers
0
Created
Source

Sequelize Connector

sequelize-connector is an npm package that simplifies the process of connecting to a Sequelize database like 'mysql' | 'postgres' | 'mariadb' | 'mssql' | 'db2' | 'oracle' | 'snowflake'. It provides an easy-to-use interface for setting up and managing database connections.

Installation

To install the package, use npm:

npm install sequelize-connector

Usage

Setting Up the Connection

First, import the sequelize-connector package and configure your database connection:

db/index.ts

import path from 'path';
import { SequelizeConnector } from 'sequelize-connector';
import { SequelizeConnectorOptions } from 'sequelize-connector/dist/types';

const sqConf: SequelizeConnectorOptions = {
    dbConfig: {
        dbDialect: 'mysql',
        dbHost: 'localhost',
        dbName: 'todo',
        dbUser: 'root',
        dbPassword: 'root',
        dbLogging: false,
    },
    modelsPath: path.join(__dirname, '../models'),
};
const connector = new SequelizeConnector(sqConf);
const dbInstance = connector.getSequelizeInstance();
const getModels = connector.getModels.bind(connector);
const getModel = connector.getModel.bind(connector);
const closeDBConnection = connector.closeDBConnection.bind(connector);

export { dbInstance, getModels, getModel, closeDBConnection };

Defining Models

Define your models using Sequelize:

import { Sequelize, DataTypes } from 'sequelize';

export default (sequelize: Sequelize, dataTypes: typeof DataTypes) => {
    const User = sequelize.define(
        'Users',
        {
            id: {
                type: dataTypes.INTEGER,
                primaryKey: true,
                autoIncrement: true,
            },
            name: {
                type: dataTypes.STRING,
                allowNull: false,
            },
            email: {
                type: dataTypes.STRING,
                allowNull: false,
                unique: true,
            },
        },
        {
            tableName: 'users',
            timestamps: false,
        },
    );

    return User;
};

Syncing the Database

Sync your models with the database:

sequelize.sync({ force: true }).then(() => {
    console.log('Database & tables created!');
});

Sample Code

Here's a sample code to create and retrieve data:

// Create a new user
import { getModel } from '../db/index';

await getModel('User')
    .create({
        name: 'Kartick Dey',
        email: 'kartick@kd.com',
    })
    .then((user) => {
        console.log('User created:', user.toJSON());
    });

// Retrieve all users
await getModel('User')
    .findAll()
    .then((users) => {
        console.log('All users:', JSON.stringify(users, null, 2));
    });

Available methods

    |-------------------|---|
    | getSequelizeInstance | |
    |-------------------|---|
    | Description | This function returns an instance of Sequelize, which is used to interact with the database. |
    | Parameters | None |
    | Returns | {Sequelize} An instance of Sequelize. |
    |-------------------|---|

    |-------------------|---|
    | getModels | |
    |-------------------|---|
    | Description | Retrieves all the models defined in the Sequelize instance. |
    | Parameters | None |
    | Returns | {Object} An object containing all the Sequelize models. |
    |-------------------|---|

    |-------------------|---|
    | closeDBConnection | |
    |-------------------|---|
    | Description | Closes the current database connection. |
    | Parameters | None |
    | Returns | Promise<void> - Resolves when the connection is successfully closed. |
    |-------------------|---|

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any changes.

Contact

For any questions or issues, please contact kartick.dey1995@gmail.com.

FAQs

Package last updated on 30 Jan 2025

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