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

dbrest

v0.0.17

Published

DBRest simplifies the publishing of rest API from (very) simple SQL Models.

Downloads

19

Readme

DBRest

Install

$ npm install --save dbrest

Connection

Supported databases: Postgresql and MSSQL.

const {DBRest, Model} = require('dbrest');
const dbrest = new DBRest({
    dialect: 'postgresql',
    connection: {
	    server: 'localhost',
	    port: '5432',
	    database: 'dbrest',
	    user: 'postgres',
	    password: 'postgres'
	}
});
await dbrest.connect();

Database table example

Task

Define your Model

For a basic example, just create a class that extends Model.

class Task extends Model {}

dbrest.loadModel(Task);
const router = dbrest.publish();

//attach dbrest routes to your express app
app.use('/', router);

Result

It creates a REST API for CRUD operations.

HTTP Verb | Operation | Route ------------ | ------------- | ------------- GET | get tasks from database. | /task POST | insert a task | /task PUT | update a task | /task DELETE | delete a task | /task

Aditional methods

HTTP Verb | Operation | Route ------------ | ------------- | ------------- GET | get task schema | /task/define GET | get task schema and data | /task/fetch

Customize Model methods (find, insert, update, delete)

let's see how to re-define the default methods

find
class Task extends Model {

	//re-define find to add a calculated column `foo`.
	async find (params) {

		//`addWhereExpress` helps to generate `where` expression based on the request params 
		const statement = this.addWhereExpress(
			`select id, title, status, 'foo' as calculated from Task`,
			params
		);
		
		return await this.database.query(statement);
	}
}
insert
class Task extends Model {

	//re-define insert to validate params
	async insert (params) {

		if (params.status != 'backlog') {
			throw new ModelError("a new task must be created with status 'backlog'.")
		}

		super.insert(param);
	}
}
update
class Task extends Model {

	//re-define update to log changes in console
	async update (params) {

		console.log('[UPDATED] - ' + params);
		super.update(params);
	}
}
delete
class Task extends Model {

	//re-define delete to validate references
	async delete (params) {
		const id = params.id;
		const reference = await this.database.query(`select id from event where taskId = ${id}`);
		if (reference) {
			throw new ModelError('this record is referenced by ' + reference.id);
		}

		super.delete(params);
	}
}

MSSQL connection example

const dbrest = new DBRest({
	dialect: 'mssql',
	connection: {
		userName: 'admin',
		password: 'admin',
		server: 'localhost',
		options: {
		  database: 'MyDB',
		  instanceName: 'SQLSERVEREXPRESS',
		  rowCollectionOnRequestCompletion: true
		}
	}
});

API

connect()

Connect to database and create a connections pool.

loadModel(model)

model

Required
Type: Object

Javascript class that extends Model.

loadFrom(modelsDir)

modelsDir

Required Type: String

Directory path containing the Models.

publish(middleware)

middleware

Optional Type: function

Express middleware function.

Warning

This project is in development, so be careful and don't use it in production yet.

License

MIT © Danilo Sampaio