@bowldevs/database

1.0.1 • Public • Published

README

This module is a wrapper for sequelize used in our projects to handle database connection, versioning and auditory.

Instalation

npm install @bowldevs/database

API

open

Open a database conexion

Param Description
config Object with the database config check sequelize documentation https://sequelize.org/api/v6/class/src/sequelize.js~sequelize#instance-constructor-constructor

Returns a sequalize object https://sequelize.org/api/v6/class/src/sequelize.js~sequelize

import {
    open,
} from "@bowldes/database";

const dbConfig = {
    database: "test",
    user: "user",
    password: "password",
    options: {
      dialect: "sqlite",
      storage: ":memory:",
      logging: true,
    },
};

const db = await open(dbConfig);

defineModel

Define a database model (will represent a table (SQL) or a document (non-sql))

Param Description
model_name Name of the model, will generate a schema with the pluralize name in the database
model_definition Object with the model definition https://sequelize.org/api/v6/class/src/sequelize.js~sequelize#instance-method-define
model_options Object with the extra options

Returns a Model object https://sequelize.org/api/v6/class/src/model.js~model

import {
    DataTypes,
    open,
    defineModel
} from "@bowldes/database";

const dbConfig = {
    database: "test",
    user: "user",
    password: "password",
    options: {
      dialect: "sqlite",
      storage: ":memory:",
      logging: true,
    },
};

const db = await open(dbConfig);

const Stuff = defineModel("stuff", {
    name: DataTypes.STRING,
});

defineAuditedModel

Define a database model with created_by and updated_by auditory owner (will represent a table (SQL) or a document (non-sql)) Needs a base user model to be associated with

Param Description
user_model_name Name of the user model, will add the created_by and updated_by attributes to the model and associated with the user model
model_name Name of the model, will generate a schema with the pluralize name in the database
model_definition Object with the model definition https://sequelize.org/api/v6/class/src/sequelize.js~sequelize#instance-method-define
model_options Object with the extra options

Returns a Model object https://sequelize.org/api/v6/class/src/model.js~model

import {
    DataTypes,
    open,
    defineModel,
    defineAuditedModel,
} from "@bowldes/database";

const dbConfig = {
    database: "test",
    user: "user",
    password: "password",
    options: {
      dialect: "sqlite",
      storage: ":memory:",
      logging: true,
    },
};

const db = await open(dbConfig);

const User = defineModel("user", {
    "name"
});

const Stuff = defineAuditedModel("user","stuff", {
    name: DataTypes.STRING,
});

sync

Will generate or update the database schema

import {
    DataTypes,
    open,
    defineModel,
    defineAuditedModel,
    sync
} from "@bowldes/database";

const dbConfig = {
    database: "test",
    user: "user",
    password: "password",
    options: {
      dialect: "sqlite",
      storage: ":memory:",
      logging: true,
    },
};

const db = await open(dbConfig);

const User = defineModel("user", {
    "name"
});

const Stuff = defineAuditedModel("user", "stuff", {
    name: DataTypes.STRING,
});

await sync();

Readme

Keywords

Package Sidebar

Install

npm i @bowldevs/database

Weekly Downloads

1

Version

1.0.1

License

none

Unpacked Size

106 kB

Total Files

10

Last publish

Collaborators

  • helmac