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

mserv-pgentity

v0.2.21

Published

ORM using mserv microservices.

Downloads

8

Readme

Introduction

mserv-pgentity is mserv extension that simplifies creating CRUD like actions for postgres-backed data entities. The middleware assumes that mserv-validate is present and the postgres client is pg-promise.

Installation

$ npm i --save mserv-pgentity

Usage


var entity  = require('mserv-pgentity'),
	service = require('mserv')(),

service.extend('enitity', entity, {
	postgres: 'postgres://localhost/mydatabase'
})

service.ext.entity('todo', {
	table: 'todos',
	scope: 'userId',
	keys: {id:'uuid'}
	model: {
		userId: Joi.string().guid(),
		id:     Joi.string().guid(),
		seq:    Joi.number(),
		title:  Joi.string(),
		done:   Joi.boolean(),
	},
	create: true,
	read:   true,
	update: true,
	delete: true
})


service.invoke('todo.fetch.all', {ownerId:'12345678-1234-1234-1234-123456789012'})

Options

The extension accepts only one option:

  • postgres: URI to the postgres database. If the context middleware is installed the postgres property will be added to the context.

Entities

When creating entities there are more options:

  • name : prefix for the action names that are created.
  • table : postgres table name with schema prefix if needed.
  • scope : multi-tenant support throw scoping.
  • keys : object with key names and their types.
  • model : object with column names (camel-cased) and their Joi model.
  • create : if true the <name>.create action will be created. Can also be a generator function, which acts as middleware around the create action. The generator function should have the following signature: function*(array[Object], next) and yield next(array) where array is the possibly modified array of objects that was initially passed in.
  • read : if true the <name>.find.by<Key> actions will be created for each key. Can also be a generator function, which acts as middleware around the read action. See create for more details.
  • update : if true the <name>.update action will be created. Can also be a generator function, which acts as middleware around the update action. See create for more details.
  • delete : if true the <name>.delete action will be created. Can also be a generator function, which acts as middleware around the delete action. See create for more details.
  • merge : if true the <name>.merge action will be created. Can also be a generator function, which acts as middleware around the merge action. Merge attempts perform update and will insert if the update failed. See create for more details.

Actions

The following actions are generated for you when using mserv-pgentity. Note that all functions support both a single and batch modes.

  • <name>.create : In single mode the argument should be an object that satisfies model. The result will be the created object or an exception will be thrown. In batch mode, the argument should be an object with a batch key whose value is an array of objects satisfying model. Returns an array of objects. Objects that could not be created will return {error$} with some details.

  • <name>.fetch.by<Key> : There will be one such action for each specified key. The argument would be an object with the <key> field that is either a single value or an array of values (single or batch). The result is either null, a single object or an array of objects.

  • <name>.fetch : Takes no arguments (or a scope argument) and returns all of the records.

  • <name>.update : In single mode the argument should be an object that satisfies model. The result will be the updated object or an exception will be thrown. In batch mode, the argument should be an object with a batch key whose value is an array of objects satisfying model. Returns an array of objects. Objects that could not be created will return {error$} with some details.

  • <name>.delete.by<Key> : There will be one such action for each specified key. The argument would be an object with the <key> field that is either a single value or an array of values (single or batch). The result is always the number of deleted records.

  • <name>.delete.all<Key> : Only if scoped, Takes no arguments and deletes all of the records within the scope. The result is the number of deleted records.

Scoping

Scoping was introduced to facilitate multi-tenancy. When scoped, all actions will require a key whose name is what was specified in the scope. For example if scope: 'tenantId' then all actions will require a tenantId. If the scope key is not present then a missingScope exception is thrown.