Minimal, database agnostic Models for Node.js (and the Browser)
Minimal, database agnostic Models for Node.js and the Browser.
The idea is to implement a Domain Model System that is persitence agnostic, to be used only for validation, type casting, transormations, and business logic. By using
minimodel, persintence (if relevant) has to be delegated to an external component (e.g. DAO/Services).
var minimodel = require'minimodel';var Post = minimodeldefineid://the field is a Stringtype: String//the field is requiredrequired: truecreatedDate:type: Date//set the default (can be a value or a function)//the execution context is the field itselfthissetRaw;visits:type: Numberdefault: 0//do not include during object export using toJson()includeInJson: false//other options include//includeInObject: false//includeInDb: false//a nested objectauthor://shortcut to define a fieldname: Stringsurname: Stringfullname://Virtuals are by default not exported (e.g. using toObject())//Virtuals do not have any default getter/setter, neet to define them explicitlytype: minimodelTypesVirtual//a custom getterreturn thismodelauthorname + " " + thismodelauthorsurname;//a custom settervar parts = valsplit' ';thismodelsetRawauthor:name: parts0surname: parts1;;//create a postvar post =id: Datenowauthor:fullname: "John Doe";//set a field after creationpostset'author.name' 'Joe';//orpostauthorname = 'Johnny'//get a fieldconsole.logpostget'author.fullname';//orconsole.logpostauthorname;
1 - Experimental
Please try it out and provide feedback.
validate()is now an async function. A callback could be given as argument otherwise a promise will be returned.