Gathering detailed insights and metrics for seneca-entity-crud
Gathering detailed insights and metrics for seneca-entity-crud
npm install seneca-entity-crud
Typescript
Module System
Min. Node Version
Node Version
NPM Version
75.1
Supply Chain
99.1
Quality
75.8
Maintenance
100
Vulnerability
100
License
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Total Downloads
8,458
Last Day
1
Last Week
2
Last Month
29
Last Year
369
Minified
Minified + Gzipped
Latest Version
1.7.3
Package Id
seneca-entity-crud@1.7.3
Unpacked Size
372.64 kB
Size
114.50 kB
File Count
80
NPM Version
6.1.0
Node Version
9.3.0
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
0%
2
Compared to previous week
Last Month
-56.1%
29
Compared to previous month
Last Year
1.7%
369
Compared to previous year
1
7
A seneca.js entity CRUD plugin.
Last update: 06/14/2018
This module is a plugin for the Seneca framework. It provides basic CRUD persistent actions for entities, and some extras.
The seneca-entity plugin already provides simple persistent functions: save$
, load$
, remove$
, list$
and data$
. The seneca-entity-crud plugin encapsulate these functions in an efficient way.
When we develop real applications, we often have to manage a lot of entities. For example: customer, product, catalog, address, order, sell, relations between them and so one. Working with the seneca-entity plugin, the same kind of code can be duplicated a lot of time. For example, here is a code used to simply read an entity:
1// Database entity creation 2var entityFactory = seneca.make(myZone, myBase, myName) 3// Reads the entity in the database 4entityFactory.load$(anId, (err, result) => { 5 if ( err ) { throw err } 6 // ... do some stuff with result ... 7})
The seneca-entity-crud plugin do the same work in a simplest manner. It defines a read
command and can use the promises power. Let's see it.
Once for all, the application promisify the act
function:
1const promise = require('bluebird') 2const seneca = require('seneca')() 3var act = promise.promisify(seneca.act, {context: seneca})
And here is our new code used to read an entity:
1// Reads the entity in the database 2act({role: myRole, cmd: 'read', id: anId}) 3.then(function(result) { 4// ... do some stuff with result ... 5});
Less code. CRUD names. And the code is easier to understand.
One very nice thing: in addition to CRUD, this plugin offers additional commands.
check
command verify that the store works. It performs a create-then-delete operation. This is useful when the microservice using this plugin has a health process.count
command encapsulate the list$
function, but return only the count for network optimization.deleterelationships
command extends the deletion of an entity to that of all its relations. See this readme file.query
command encapsulate the list$
function with new features.truncate
command works as traditional SQL TRUNCATE TABLE my_table
.And we even lie on the floor:
last_update
date value can be automatically added to each entity when created or updated.nonamespace: true
argument, the namespace of the resulting entities is automatically removed.Enjoy it!
Your application must declare the seneca-entity plugin:
1const seneca = require('seneca')() 2seneca.use('entity') 3// For Seneca >= 3.x 4seneca.use('basic').use('entity')
See the seneca-entity project for installation and more information.
Your application must declare this plugin:
1seneca.use('seneca-entity-crud', { 2 ... options ... 3})
The options are:
entity
. The primary name must not contain hyphen (-).last_update
) when created or updated. Default: false
.entity
.For more information on zone, base and name, see the entity namespace tutorial. For more information on role, see the seneca patterns guide.
Proceed each seneca-entity-crud command as any seneca command. Here is a full example for the create
command, using the seneca-mem-store persistence plugin:
1'use strict' 2 3/* Prerequisites */ 4const promise = require('bluebird') 5const seneca = require('seneca')() 6 7/* Plugin declaration */ 8seneca 9 .use('basic') // For Seneca >= 3.x 10 .use('entity') 11 .use('mem-store') 12 .use('seneca-entity-crud', { 13 name: 'post', 14 role: 'my-role' 15 }) 16 17/* Promisify seneca actions */ 18var act = promise.promisify(seneca.act, {context: seneca}) 19 20/* Starts seneca */ 21seneca.ready(function (err) { 22 if (err) { throw err } 23 24 /* The create example */ 25 var myPost = {title: 'A great post', content: 'Hello World'} 26 act({role: 'my-role', cmd: 'create', entity: myPost}) 27 .then(function (result) { 28 console.log('My great post ID is: ' + result.entity.id) 29 return result 30 }) 31 32 /* Ends seneca */ 33 seneca.close((err) => { 34 if (err) { console.log(err) } 35 }) 36}) 37
Try it! The console shows:
My great post ID is: <an id like 5a4732ef4049cfcb07d992007e003932>
For the list of the commands and their arguments, see the chapter below: API commands specifications.
Note: the ID generated for the entity is provided by the store plugin used in your application.
In most cases, it's a best practice to validate input data before insert it in the database. The seneca-entity-crud plugin cannot validate input data by itself: it highly depends on your application data types. However, if you need to proceed input data validation, this plugin can use your favorite function.
For more information and examples, please see the input data validation documentation.
By default, the entity$ field of the resulting entities contains the entity namespace zone-base-name
. For security reasons, sensitive applications may not need this data. To automatically remove the resulting entity namespace, use the nonamespace: true
argument in the command.
Note: for convenience, the
nonamespace
argument value can be thetrue
boolean or the'true'
string.
1var myId = '5a4732ef4049cfcb07d992007e003932' 2// Read 3act({role: 'my-role', cmd: 'read', id: myId, nonamespace: true}) 4.then(function (result) { 5 // No entity$ namespace zone-base-name in the result entity 6 console.log('My entity is: ' + JSON.stringify(result.entity)) 7 return result 8})
For more information on zone, base and name, see the entity namespace tutorial.
This plugin can automatically add defaults to the resulting entities of a read or query command.
Note: this feature is optional.
The defaults argument is an object. Each object key/value pair is a default field name/value pair:
1{'a field name': <a default value>, 'another field name': <another default value>, ...}
If the resulting entity does not contain the field a field name
, it is added with the default value.
If the resulting entity already contains the field a field name
, nothing is changed.
Add the defaults object to the read or query command pattern:
1{role: 'my-role', cmd: 'read', id: anId, defaults: { ... }} 2{role: 'my-role', cmd: 'query', defaults: { ... }}
1var myId = '5a4732ef4049cfcb07d992007e003932' 2// Read 3act({role: 'my-role', cmd: 'read', id: myId, defaults: {country: 'Belgium', currency: 'Euro'}) 4.then(function (result) { 5 console.log('My entity is: ' + JSON.stringify(result.entity)) 6 return result 7})
The console output looks like:
1{id: '5a4732ef4049cfcb07d992007e003932', ... , country: 'Belgium', currency: 'Euro'}
To install, simply use npm:
1npm install seneca-entity-crud
To run tests, simply use npm:
1npm test
The Senecajs org encourages open participation. If you feel you can help in any way, be it with documentation, examples, extra testing, or new features please get in touch.
Copyright (c) 2016-2018, Richard Rodger and other contributors. Licensed under MIT.
No vulnerabilities found.
No security vulnerabilities found.