npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mr-mdb

v1.0.12

Published

Multi Database MDB [mssql/mysql]

Downloads

18

Readme

mr-mdb

Gerencia multiplas bases de dados MS-SQL e MySQL. Permite instanciar facilmente em um servidor http (express) servindo as bases de dados via metodologia REST.

Getting started

npm install mr-mdb

1. Definir as variaveis de ambiente

// caminho da rota raiz das requisições rest
process.env.DEFAULT_MDB_REST_ROOT_ROUTE = 'mdb-test'      // => default: 'mdb-rest'

2. Instanciando um servidor REST

Esta ferramenta pode ser facilmente configurada em um servidor http (express), sem a necessidade de manipulação dos dados.

import express from 'exprepss'
import mdb from 'mr-mdb'

const app = express()
app.use(mdb({
	defaultRootRoute: 'mdb-test',
	environments: [
		{
			name: 'my_database_mysql',
			plataform: 'mysql',
			dbConfig: {
				host: 'localhost',
				// port: 1234,
				database: 'my_database_name',
				user: 'my_user_name',
				password: 'my_password'
			},
			envDefaults: {
				idColumnName: "_id",
				deletedColumnName: "_deleted",
				deletedValue: 1,
				columnsNotShow: ["_date_insert", "_date_edit", "_date_delete"],
				updateAdd: ["_date_edit = now()"],
				insertAdd: [],
				deleteAdd: ["_date_delete = now()"],
				idAutoIncrement: true,
				relationshipConditionById: true
			}
		},
		{
			name: 'my_database_mssql',
			plataform: 'mssql',
			dbConfig: {
				server: 'localhost',
				database: 'my_database_name',
				user: 'my_user_name',
				password: 'my_password',
				options: {
					enableArithAbort: true
				}
			},
			envDefaults: {
				idColumnName: "R_E_C_N_O_",
				deletedColumnName: "D_E_L_E_T_",
				deletedValue: "*",
				columnsNotShow: ["D_E_L_E_T", "R_E_C_D_E_L_"],
				updateAdd: [],
				insertAdd: [],
				deleteAdd: ["R_E_C_D_E_L_ = R_E_C_N_O_"],
				idAutoIncrement: false,
				relationshipConditionById: false
			}
		}
	]
}))

app.listen(3000)

No exemplo foram definidos:

  1. a rota raiz como mdb-test (http://localhost:3000/mdb-test)

  2. dois ambientes que poderão ser requisitados utilizando a metodologia REST, sendo cada um para uma base de dados especifica:

Método | Caminho | Descrição -------- | ---------------------------------------------------------------- | -------------------------------------------------- GET | http://localhost:3000/mdb-test/:environment_name/:table_name | requisita todos os registros de uma tabela GET | http://localhost:3000/mdb-test/:environment_name/:table_name/:id | requisita um registro específico da tabela POST | http://localhost:3000/mdb-test/:environment_name/:table_name | requisita a inclusão de um registro em uma tabela PUT | http://localhost:3000/mdb-test/:environment_name/:table_name/:id | atualiza um registro específico em uma tabela DELETE | http://localhost:3000/mdb-test/:environment_name/:table_name/:id | remove um registro específico de uma tabela

3. Uso da base de dados em aplicações

2.1. Criar um ambiente

// 1. mdb.createConnection({ name: string, path?: string, logger?: boolean })
const environment = new mdb.Environment({
	name: 'my_database_mysql',
	plataform: 'mysql',
	dbConfig: {
		host: 'localhost',
		// port: 1234,
		database: 'my_database_name',
		user: 'my_user_name',
		password: 'my_password'
	},
	envDefaults: {
		idColumnName: "_id",
		deletedColumnName: "_deleted",
		deletedValue: 1,
		columnsNotShow: ["_date_insert", "_date_edit", "_date_delete"],
		updateAdd: ["_date_edit = now()"],
		insertAdd: [],
		deleteAdd: ["_date_delete = now()"],
		idAutoIncrement: true,
		relationshipConditionById: true
	}
})

// 2. Connections.createConnection({ name: string, path?: string, logger?: boolean })
const environment = new MdbEnvironment({
	name: 'my_database_mssql',
	plataform: 'mssql',
	dbConfig: {
		server: 'localhost',
		database: 'my_database_name',
		user: 'my_user_name',
		password: 'my_password',
		options: {
			enableArithAbort: true
		}
	},
	envDefaults: {
		idColumnName: "R_E_C_N_O_",
		deletedColumnName: "D_E_L_E_T_",
		deletedValue: "*",
		columnsNotShow: ["D_E_L_E_T", "R_E_C_D_E_L_"],
		updateAdd: [],
		insertAdd: [],
		deleteAdd: ["R_E_C_D_E_L_ = R_E_C_N_O_"],
		idAutoIncrement: false,
		relationshipConditionById: false
	}
})

2.2. Instanciar uma das classes MdbQuery enviando o nome da conexão desejada

QueryModel

// 1. mdb.QueryModel(tableName: string, environment: EnvironmentOpts | MdbEnvironment)
const model = new mdb.QueryModel('table_name', environment)

// 2. MdbQueryModel(tableName: string, environment: EnvironmentOpts | MdbEnvironment)
const model = new MdbQueryModel('table_name', environment)

QueryManager

// 1. mdb.QueryManager(tableName: string, environment: EnvironmentOpts | MdbEnvironment)
const qm = new mdb.QueryManager('table_name', environment)

// 2. MdbQueryManager(tableName: string, environment: EnvironmentOpts | MdbEnvironment)
const qm = new MdbQueryManager('table_name', environment)

2.3. Executar as consultas

QueryModel

// model.findAll(query?: IObjectQueryString): Promise<Record[]>
const recordset = await model.findAll({ $map: 'name,age' })

// model.findAndCountAll(query?: IObjectQueryString): Promise<QueryCollectionResponse>
const { affectedRows, recordset } = await model.findAndCountAll({ $map: 'name,age' })

// model.findById(id: number | string, query?: IObjectQueryString): Promise<Record>
const data = await model.findById(id: number | string, { $map: 'name,age' })

// model.findOne(query: IObjectQueryString): Promise<Record>
const data = await model.findOne({ name: 'joao' })

// model.create(data: Record): Promise<Record>
const data = await model.create({ name: 'joao', age: 52 })

// model.updateById(id: number | string, data: Record, overwrite?: boolean): Promise<Record>
const data = await model.updateById(1234567, { address: 'Rua 1, 15' })

// model.removeById(id): Promise<number>
const affected = await model.removeById(1234567)

QueryManager



// qm.select({ query, id }: QueryManagerOpts = {}): Promise<QueryConnectionResponse>
const { affectedRows, recordset } = await qm.select({ query: { $map: 'name,age' } })

// qm.insert(data: Record): Promise<QueryConnectionResponse>
const { affectedRows, recordset } = await qm.insert({ name: 'joao', age: 52 })

// qm.update(data: Record, { query, id }: QueryManagerOpts = {}): Promise<QueryConnectionResponse>
const { affectedRows, recordset } = await qm.update({ address: 'Rua 1, 15' }, { id: 1234567 })

// qm.delete({ query, id }: QueryManagerOpts = {}): Promise<QueryConnectionResponse>
const { affectedRows, recordset } = await qm.delete({ id: 1234567 })