Skip to content

GustavoMaritan/sql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Package para manipular banco de dados SQL

  • Possibilita mochar retornos do banco de dados para teste em todos ambientes.
  • Use MSSQL

Install

npm i sql-mocha

Documentação

Configuração

const { Sql } = require('sql-mocha');

const sql = new Sql({
    server: '',
    database: '',
    user: '',
    password: '',
    //DEFAULTS
    connectTimeout: 60000,
    requestTimeout: 60000,
    pool: {
        max: 5,
        min: 1,
        idleTimeoutMillis: 3000
    }
});

Execute

const return = await sql.execute('procedureName', {
    input: any
});

const return = await sql.execute('procedureName', {
    input: sql.types.decimal(14,2, 1444.23),
    $output: sql.types.int()
});

const return = await sql.execute('procedureName', {
    input: any,
    $output: sql.types.int()
});

/*
return =
    {
        content: [],
        returnValue: Number|String,
        outputs: { output: any }
    }
*/

Execute/Model

const return = await sql.execute('procedureName', {
    input: any,
    $output: sql.types.type()
}, { dataset1: [], dataset2: {}, dataset3: [] });

/*
return =
    {
        content: {
            dataset1: [], 
            dataset2: {}, 
            dataset3: []
        },
        returnValue: Number|String,
        outputs: { output: any }
    }
*/

ExecuteOne

const return = await sql.executeOne('procedureName', {
    input: any
});

/*
return =
    {
        content: {},
        returnValue: Number|String
    }
*/

Query

const return = await sql.query('Select * From TABELA Where id = $id', {
    id: 'String|Number|Date|Boolean'
});

Transactions

await sql.openTransaction();

const result = await sql.execute('procedureName', {
    input: any,
    $output: sql.types.type()
});

if(!result.returnValue)
    await sql.rollback();

await sql.commit();

Mocha

  • Mocar retornos procedures
  • Criar arquivo 'mocha.json' no mesmo diretório onde sera executado a procedure
  • Setar process.env.DATABASE_MOCHA = true;

Estrutura de pastas

Api
└─ src
    └─ diretorio
        |─ controller.js
        |─ repository.js //sql.execute('procedureName')
        └─ mocha.json
mocha.js
  • Estrutura do json
    • Key = Nome da procedure
      • parameters = Nome dos parâmetros e seu devido valor
      • returns = [content, outputs, returnValue] // Definir valores conforme retorno da procedure
{
	"procedureName": {
        "parameters": {
            "param1": 'value'
        }
		"content": [],
		"outputs": {},
		"returnValue": null
	}
}
  • Erros
    • Code 1: Arquivo mocha não encontrado
    • Code 2: Parâmetros inválidos

Context

  • Instância de sql por requisição
const express = require('express');
const app = express();
const { Context } = require('sql-mocha');

Context.middleware(app, { /* Sql Config */ });

app.get('/', async (req, res, next)=> {
    await Context.sql.execute('procedureName');
});

SQLTypes(mssql)

  • numeric(precision, scale, value?)
  • decimal(precision, scale, value?)
  • varChar(length, value?)
  • nVarChar(length, value?)
  • char(length, value?)
  • nChar(length, value?)
  • varBinary(length, value?)
  • time(scale, value?)
  • dateTime2(scale, value?)
  • dateTimeOffset(scale, value?)
  • text(value?)
  • int(value?)
  • bigInt(value?)
  • tinyInt(value?)
  • smallInt(value?)
  • bit(value?)
  • float(value?)
  • real(value?)
  • date(value?)
  • dateTime(value?)
  • smallDateTime(value?)
  • uniqueIdentifier(value?)
  • smallMoney(value?)
  • money(value?)
  • binary(value?)
  • nText(value?)

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published