1.0.1 • Published 11 months ago

@bowldevs/database v1.0.1

Weekly downloads
-
License
-
Repository
-
Last release
11 months ago

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

ParamDescription
configObject 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))

ParamDescription
model_nameName of the model, will generate a schema with the pluralize name in the database
model_definitionObject with the model definition https://sequelize.org/api/v6/class/src/sequelize.js~sequelize#instance-method-define
model_optionsObject 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

ParamDescription
user_model_nameName of the user model, will add the created_by and updated_by attributes to the model and associated with the user model
model_nameName of the model, will generate a schema with the pluralize name in the database
model_definitionObject with the model definition https://sequelize.org/api/v6/class/src/sequelize.js~sequelize#instance-method-define
model_optionsObject 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();
1.0.1

11 months ago