Skip to content

Instantly share code, notes, and snippets.

@Quixomatic
Last active February 10, 2016 18:17
Show Gist options
  • Save Quixomatic/a4e5fa9f974e456b31c1 to your computer and use it in GitHub Desktop.
Save Quixomatic/a4e5fa9f974e456b31c1 to your computer and use it in GitHub Desktop.
var function_array = {
// sets the dates for creation of a new document
"setDateTimes" : function (req, res, next) {
var tempDate = new Date().toISOString();
req.body.created_date = tempDate;
req.body.updated_date = tempDate;
next();
},
// sets the created by and updated by for creation of a new document
"setCreatedUpdatedBy" : function (req, res, next) {
req.body.created_by = "guest";
req.body.updated_by = "guest";
next();
},
// updates the updated date for an updated document
"setUpdatedDate" : function (req, res, next) {
var tempDate = new Date().toISOString();
req.body.updated_date = tempDate;
next();
},
// updates the updated by for an updated document
"setUpdatedBy" : function (req, res, next) {
req.body.updated_by = "guest";
next();
},
// updates the update count number for an updated document
"incrementUpdateCount" : function (req, res, next) {
var collection_name_string = req.path.substr(8, req.path.length - 1).toLowerCase();
var collection_name_array = collection_name_string.split('/');
var collection_name = collection_name_array[0];
findDocs(collection_name, {_id: req.params.id}, function(docs) {
// Grabs the document to parse through and access
var tempDocs = JSON.parse(JSON.stringify(docs));
var oldUpdateCount = tempDocs[0].update_count;
var data = { update_count : (parseInt(oldUpdateCount) + 1) };
updateDoc(collection_name, req.params.id, data, function(doc) {
console.log(doc);
});
});
next();
},
// Prevents the creation of a new document in core_field for a field that already exists
// This can be done using the unique identifier in the schema
"preventExistingFieldDuplication" : function (req, res, next) {
findDocs('core_field', {table_name: req.body.table_name, field_name: req.body.field_name}, function(docs) {
if (docs.length > 0) {
next(new Error('Field already exists'));
} else {
next();
}
});
},
// Intercepts the creation of a new field and adds it to the schema to be applied to the endpoints using restify.serve(..)
"newFieldAdded" : function (req, res, next) {
console.log(req.body.table_name);
findDocs('core_table', {table_name: req.body.table_name}, function(docs){
// Grabs the document to parse through and access
var tempDocs = JSON.parse(JSON.stringify(docs));
// Initializes the schema data to be whatever the db has stored
var temp_schema_data = tempDocs[0].schema_data;
// Instantiates new variable for insertion of a new field
var newFieldEntry = {
type : req.body.field_type,
required : req.body.required
};
// Create the schema json object to add to the schema
var newSchemaEntry = {};
// Create a new option inside that object with the same name as the field and apply newFieldEntry
newSchemaEntry[req.body.field_name] = newFieldEntry;
// Adds the new field entry into the schema
temp_schema_data[req.body.field_name] = newFieldEntry;
var data = {"schema_data" : temp_schema_data};
// Adds the new field to the schema and enforces the data policy
mongoose.model(tempDocs[0].table_name).schema.add(newSchemaEntry);
// Updates the core_table record with the new schema so that if the server restarts it will still reflect the updates
updateDoc('core_table', tempDocs[0]._id, data, function(document){
console.log(document);
});
next();
});
},
"fieldRemovedFromCoreField" : function (req, res, next) {
var collection_name_string = req.path.substr(8, req.path.length - 1).toLowerCase();
var collection_name_array = collection_name_string.split('/');
var collection_name = collection_name_array[0];
findDocs('core_table', {table_name: collection_name}, function(docs) {
// Grabs the document to parse through and access
var tempDocs = JSON.parse(JSON.stringify(docs));
// Initializes the schema data to be whatever the db has stored
var temp_schema_data = tempDocs[0].schema_data;
});
next(new Error('Sorry Trying to Debug'));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment