CRUD for mono
####Example config with flow:
config: {
myMiid: {
myEvent: ['read', 'update', 'create', 'delete']
}
}
All these events must have two parameters:
- the CRUD object defined by the CRUD module. See example request data.
- the callback to be called with the results when the operation completes.
####Fetch templates
If an array is send to self.emit('read')
as data CRUD will fetch the templates inside the array.
Normal queries for templates are working also.
Templates are always initialized before returned.
t
(client) ortemplateId
(server): the stringified ObjectID id od this templateq
(client) orquery
(server): a query object in MongoDB query format used forread
,update
,delete
operationsd
(client) ordata
(server): a data object forupdate
orcreate
operations. Theupdate
operation requires the MongoDB update document format, while for thecreate
operations this will be interpreted as the document to be created.o
(client) oroptions
(server): an options object in the MongoDB NodeJs Driver format that can contain:fields
,sort
,skip
,limit
, etc. optionsrole
(server): the role in the name of which this request should be made. This usually comes fromlink.session.crudRole
. From the client this fiels is not necessary because it will be automatically added on the server side depending on the role of the user making this request.
{
// the template that this CRUD object will be validated against
t: 'the stringified template id',
// the query object in MongoDB format
q: {/*query object*/},
// the document object (updates) in MongoDB format
d: {/*update document*/},
// the CRUD operation options in node-monogdb-native (NodeJs MongoDb driver) format
o: {/*options*/},
// don't make joins
noJoins: true,
// don't merge template
noMerge: true,
// don't return cursors for read operations
noCursor: true,
// perform only a count using the given query
onlyCount: true
}
Build the CRUD request object:
{
templateId: 'the stringified template id',
role: ObjectId('the crud role'), // link.session.crudRole
query: {
/* query data */
},
data: {
/* data to insert/update */
},
noCursor: true // don't return cursors for read operations,
onlyCount: true // perform only a count using the given query
}
and emit a crud.<operation>
server-side event:
M.emit("crud.create", crudObject, callback);
myTemplate = {
_id: myTemplateItemId,
_tp: [_templateTemplateId],
db: 'dbName',
collection: 'collectionName',
name: 'template_name',
roles: {
// set any combination of c, r, u or d in access
'roleId': {access: 'crud'},
// optional template configuration overwriting
// the only supported template properties are: options, links, and schema
'config': {
'options': {
'html': 'another/html/file.html'
},
'links': {
// ...
},
'schema': {
...
}
}
},
// add a role with access rights to every item
itemAccess: 'crud',
options: {
label: {
de: 'Template Label'
},
order: 5,
html: '/myTemplate.html',
sort: [['sort.field', 1]],
// a hidden fixed filter to display only the customers that are HB
filters: [
{
field: 'filterFIeld',
operator: 'exists',
value: true,
hidden: true,
fixed: true
}
]
},
// plug custom code
on: {
create: {
myCustomEventA: [arg1, argN]
},
read: {
myCustomEventB: [arg1, argN]
},
update: {
myCustomEventC: [arg1, argN]
},
delete: {
myCustomEventD: [arg1, argN]
}
},
links: [
// see crud links module
],
schema: {
// modm schema
}
}
- Configurable core template initialization. A
init
operation must be configured in the crud application.json miid before the module can be used. The operation params must contain atemplateConfig
object with adb
property (the db name where the templates are located)
- Implemented a
templatesNotFound
event
- Added
findAndModify
template option for theupdate
operation
- Transferred the module to the new jxMono organization
- Updated Flow to
v0.2.0
- Template role handling: always return at least current role in a template document.
- Added
onlyCount
option to find requests - Count is no longer performed for every find request
- Fixed template merging for templates that are not fetched by id
- Fixed
templateId
handling to a uniform behavior: alwaysObjectID
- Fixed
read
,update
anddelete
operations with empty query string that were not adding the_tp
to the query before executing it.
- Added
init
operation to expose the server side initialization if no other crud operations are needed.
- Added missing sort argument to
findAndRemove
db request.
- Cache
modm
instances. Solves a critical memory leak.
- Fixed a bug from v0.3.1 (undefined is not a function)
- Added public access feature
-
added the
noCursor
crud read request option to automatically convert all the cursors into array. Defaultfalse
-
fixed the linked field filtering
-
sort linked fields using JavaScript sort methods after we have the full result array
-
added
noJoins: true
template option to disable linked template joining -
get template requests filter out all roles except the user role
-
callback the data after running non-read operations and server events
-
emit
request.template.callback
event if this is provided (also, don't call the built-in callback) -
dates are converted using moment.js if this is installed. Dates in valid ISO date format (
YYYY-MM-DDThh:mm:ss.MMM
) will be parsed with theDate
constructor. -
Introduced
before
andafter
configuration:{ before: { appId: { create: "createEventB", read: "readEventB", update: "updateEventB", delete: "deleteEventB" } }, after: { appId: { create: "createEventA", read: "readEventA", update: "updateEventA", delete: "deleteEventA" } } }
or
{ before: { appId: "catchAllB" }, after: { appId: "catchAllA" } }
In server custom scripts we can do:
// before event M.on("crud:createEventB", function (request, callback) { // do another db request foo(request, function (err, data) { if (err) { return callback(err); } request.query.something = data.something; callback(null, request); }); }); // after event M.on("crud:createEventA", function (request, err, data, callback) { // handle error message // then send the callback callback(err, data); });
- Fixed wrong behavior when sorting numbers and strings. Related to #29.
- Fixed the wrong behavior when sorting dates
- Improved the sort method.
- fixed item link bug that was nullifying itms with no linked objects
v0.2.11
had to be skipped because of an unprotected object reference and a server installation was already performed
- fixed MongoDB connection leak due to template cache bug
- fixed typo in mongo server options when initializing modm for each template
- fixed fetch template requests that were not converting
$in
string
arrays intoObjectId
arrays - fixed template cache retrieval problem
- fixed the cursor constructor name:
Cursor
instead ofObject
- TODO
- fixed client bug when merging templates: links were polluting the template cache
- fixed bug in recursive query conversion
- added date conversion
- set data as request.result
- overwrite request.method. Return error in createError function
- add fixed
cloneJSON
bug when handlingObjectID
's
- added role template configuration overwriting
- fixed wrong
M.on
server configuration for thecrud_read
event that was usingcreate
as the model operation to call
- initial release