Comparing version 0.0.5 to 0.0.6
18
index.js
@@ -5,8 +5,9 @@ exports.attack = function(root, pathToConfig){ | ||
// prepare attack | ||
var express = require('express') | ||
, srv = express() | ||
, config = require(root + pathToConfig) | ||
var express = require('express') | ||
, srv = express() | ||
, config = require(root + pathToConfig) | ||
fs = require('fs') | ||
mustache = require('mustache') | ||
log = console.log | ||
@@ -70,6 +71,10 @@ require('sugar') | ||
// model | ||
var pathToModels = (config.pathToModels) ? root+config.pathToModels : root+'/models' | ||
, models = fs.readdirSync(pathToModels) | ||
, modelExt = (process.argv.find('mock')) ? require('./lib/model/modelExtMock') : require('./lib/model/modelExt') | ||
var pathToModels = (config.pathToModels) ? root+config.pathToModels : root+'/models' | ||
var pathToValidator = (config.pathToValidator) ? root+config.pathToValidator : './lib/model/modelValidator' | ||
, models = fs.readdirSync(pathToModels) | ||
, modelExt = (process.argv.find('mock')) ? require('./lib/model/modelExtMock') : require('./lib/model/modelExt') | ||
, modelValidation = require(pathToValidator)() | ||
srv.m = {} | ||
@@ -86,2 +91,3 @@ models.forEach(function(file){ | ||
srv.m[model.name].merge(modelExt) | ||
srv.m[model.name].merge(modelValidation) | ||
srv.m[model.name].srv = srv | ||
@@ -88,0 +94,0 @@ srv.m[model.name].start() |
@@ -30,9 +30,14 @@ module.exports = { | ||
insert: function(data, callback){ | ||
insert: function(record, callback){ | ||
var err = this.check(record) | ||
if (err.size()){ | ||
callback(err) | ||
return | ||
} | ||
var self = this | ||
, newItem = data | ||
, newItem = record | ||
delete newItem._id | ||
this.srv.db[this.name].insert(newItem, function(err, items){ | ||
@@ -50,6 +55,11 @@ if (!err) { | ||
update: function(data, callback){ | ||
update: function(record, callback){ | ||
var err = this.check(record) | ||
if (err.size()){ | ||
callback(err) | ||
return | ||
} | ||
var self = this | ||
, item = this.find({_id: data._id}) | ||
, item = this.find({_id: record._id}) | ||
@@ -64,8 +74,7 @@ if (this.keepVersions) { | ||
item.merge(data) | ||
item.merge(record) | ||
var _id = item._id.toString() | ||
, options = {safe: true} | ||
var clone = item.clone() | ||
, clone = item.clone() | ||
delete clone._id | ||
@@ -110,4 +119,29 @@ | ||
return options; | ||
}, | ||
check: function(record){ | ||
var self = this | ||
, err = {} | ||
this.fields.each(function(field){ | ||
if (record[field.name]){ | ||
if (self.validate.byType[field.type]){ | ||
try{ self.validate.byType[field.type](field, record[field.name]) } | ||
catch(e){ err[field.name] = e.message } | ||
} | ||
} | ||
if (field.checks){ | ||
field.checks.each(function(check){ | ||
if (self.validate[check]){ | ||
try{ self.validate[check](field, record[field.name]) } | ||
catch(e){ err[field.name] = e.message } | ||
} | ||
}) | ||
} | ||
}) | ||
return err | ||
} | ||
} |
@@ -28,2 +28,7 @@ module.exports = { | ||
insert: function(record, callback){ | ||
var err = this.check(record) | ||
if (err.keys().length){ | ||
callback(err) | ||
return | ||
} | ||
record._id = ''+Number.random(100000000) | ||
@@ -35,3 +40,8 @@ this.add(record) | ||
update: function(record, callback){ | ||
var err = this.check(record) | ||
if (err.keys().length){ | ||
callback(err) | ||
return | ||
} | ||
var item = this.find({_id: record._id}) | ||
@@ -46,3 +56,3 @@ | ||
} | ||
item.merge(record) | ||
@@ -81,4 +91,29 @@ | ||
return options; | ||
}, | ||
check: function(record){ | ||
var self = this | ||
, err = {} | ||
this.fields.each(function(field){ | ||
if (record[field.name]){ | ||
if (self.validate.byType[field.type]){ | ||
try{ self.validate.byType[field.type](field, record[field.name]) } | ||
catch(e){ err[field.name] = e.message } | ||
} | ||
} | ||
if (field.checks){ | ||
field.checks.each(function(check){ | ||
if (self.validate[check]){ | ||
try{ self.validate[check](field, record[field.name]) } | ||
catch(e){ err[field.name] = e.message } | ||
} | ||
}) | ||
} | ||
}) | ||
return err | ||
} | ||
} |
@@ -22,2 +22,3 @@ (function($){ | ||
$.view.applyTemplates() | ||
alertify.success('Registro incluido.') | ||
if (cb) cb() | ||
@@ -39,2 +40,3 @@ } | ||
$.view.applyTemplates() | ||
alertify.success('Registro alterado.') | ||
if (cb) cb() | ||
@@ -82,3 +84,3 @@ } | ||
talkToServer: function(method, par, success){ | ||
talkToServer: function(method, par, success, error){ | ||
@@ -88,3 +90,3 @@ var contentType = (method === 'GET') ? '' : 'application/json' | ||
, data = (par.isObject()) ? JSON.stringify( par ) : undefined | ||
, error = this.errorHandler | ||
, error = error || this.errorHandler | ||
@@ -104,3 +106,8 @@ $.ajax({ | ||
errorHandler: function(jqXHR, txt, err){ | ||
console.log(txt) | ||
var errs = JSON.parse(jqXHR.responseText) | ||
errs.each(function(field, err){ | ||
$('#ctrl-group-'+field).addClass('error') | ||
alertify.error(err) | ||
}) | ||
}, | ||
@@ -107,0 +114,0 @@ |
@@ -10,2 +10,3 @@ (function($){ | ||
this.startShortcuts() | ||
alertify.delay=15000 | ||
@@ -44,2 +45,3 @@ $.model.watch('pageNr', function(prop, oldVal, newVal){ | ||
$('#crud-form').hide('fast') | ||
$('.control-group').removeClass('error') | ||
}, | ||
@@ -51,2 +53,3 @@ | ||
$.view.applyTemplates() | ||
$('.control-group').removeClass('error') | ||
}) | ||
@@ -53,0 +56,0 @@ }, |
{ | ||
"name": "10tcl", | ||
"description": "CRUD over express and mongodb", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"author": { | ||
@@ -15,2 +15,3 @@ "name": "Fernando Becker", | ||
"sugar": "1.3.x", | ||
"validator": "0.4.x", | ||
"less": "*", | ||
@@ -17,0 +18,0 @@ "less-middleware": "*" |
@@ -35,3 +35,4 @@ ## What is it? | ||
pathToModels: '/app/models', | ||
pathToViews: '/app/views' | ||
pathToViews: '/app/views', | ||
pathToValidator: 'app/models/validator' | ||
} | ||
@@ -107,7 +108,22 @@ | ||
Each will result in a different html element and layout. | ||
A field type different from the above will result in a regular input field. | ||
### Validation | ||
"validator" package provides type associated checks, that are automatically executed on POST/PUT transactions. | ||
Other than that, a field can contain a property validates: ['mandatory', ...] | ||
Each check in checks array will trigger the correspondent function in the 10tcl modelValidator return object. | ||
Defining pathToValidator, you can implement your own validator that will be required instead of the 10tcl one. | ||
### i18n | ||
Nop... sorry. | ||
And I'm outputting static texts in PT-BR. | ||
Not happy with any package I've tried. | ||
Want to keep texts in a cached db for online editing and translation. | ||
### CRUD | ||
The views are made with bootstrap, jquery, and some self-made binding (Angular was just making me angry). | ||
The views are made with bootstrap, jquery, and some home-made binding (Angular was just making me angry). | ||
In mobile they are much simpler, made with jquerymobile and jquery. | ||
The decision between mobile and desktop is made by reading request headers. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1458200
113
30660
127
8
5
+ Addedvalidator@0.4.x
+ Addedmongodb@6.11.0(transitive)
+ Addedvalidator@0.4.28(transitive)
- Removedmongodb@6.10.0(transitive)