Commit 79c5f91b authored by Bertrand PINEL's avatar Bertrand PINEL

add some Faker evoluted rules + Patch method in lambda

parent 090b3079
......@@ -4,6 +4,7 @@ ember-aws-ehipster
This addon simplify the generation of code for model build with JDL Studio https://start.jhipster.tech/jdl-studio/
It also guide you to set up a JSON-API server on top of AWS API Gateway, Lambda and DynamoDB.
It could also be seen as a starter for setting up Mirage, allowing the developper to build a full working application (using mock data) entirely running on the client. Once the development is over, the persistance can be delegated to an AWS API Gateway using a lambda function storing objects in DynamoDB.
Installation
------------------------------------------------------------------------------
......
......@@ -239,7 +239,7 @@ function templateEntityForm(name, options) {
let constraintsString = options[prop].split(':')[1];
switch (type) {
case "string":
attributes.push("\t" + prop + "() {\n\t\treturn faker.name.findName();\n\t}");
attributes.push("\t" + prop + "() {\n\t\treturn "+fakerizeString(prop)+";\n\t}");
break;
case "date":
......@@ -495,25 +495,39 @@ function templateEntityForm(name, options) {
}
};
const fakerize = (type) => {
let typeFake
switch (type) {
case 'boolean':
typeFake = 'faker.random.boolean()'
break
case 'date':
typeFake = 'faker.date.past()'
break
case 'number':
typeFake = 'faker.random.number({min: 0,max: 10000})'
break
case 'string':
typeFake = 'faker.name.findName()'
break
default:
return null
const fakerizeString = (name) => {
let lowerCaseName = name.toLowerCase();
if (lowerCaseName.endsWith('zipcode')) {
return 'faker.address.zipCode()';
} else if (lowerCaseName.endsWith('city')) {
return 'faker.address.city()';
} else if (lowerCaseName.endsWith('streetName')) {
return 'faker.address.streetName()';
} else if (lowerCaseName.endsWith('country')) {
return 'faker.address.country()';
} else if (lowerCaseName.endsWith('countrycode')) {
return 'faker.address.countryCode()';
} else if (lowerCaseName.endsWith('state')) {
return 'faker.address.state()';
} else if (lowerCaseName.endsWith('iban')) {
return 'faker.finance.iban()';
} else if (lowerCaseName.endsWith('firstname')) {
return 'faker.name.firstName()';
} else if (lowerCaseName.endsWith('lastname')) {
return 'faker.name.lastName()';
} else if (lowerCaseName.endsWith('phone')) {
return 'faker.phone.phoneNumber()';
} else if (lowerCaseName.endsWith('email')) {
return 'faker.internet.email()';
} else if (lowerCaseName.endsWith('description')) {
return 'faker.lorem.sentence()';
} else if (lowerCaseName.endsWith('filename')) {
return 'faker.system.fileName()';
} else {
return 'faker.lorem.word()';
}
return ' () {\n\t\treturn ' + typeFake + '\n\t}'
};
const relFunction = (str) => {
......
......@@ -55,7 +55,7 @@ module.exports = {
break;
case 'Boolean':
propertyType = ':number';
propertyType = ':boolean';
break;
default:
......@@ -83,7 +83,7 @@ module.exports = {
relationships.filter(relation => relation.from.name == entity.name).forEach(relation => {
let toObjectName = relation.to.name.toLowerCase();
// Process injectedField to better handle the relationship
let injectedField = relation.from.injectedField;
let injectedField = relation.to.injectedField;
let field = null;
if (injectedField != null && injectedField.indexOf('(') != -1) {
field = injectedField.substring(injectedField.indexOf('(')+1, injectedField.indexOf(')'));
......@@ -113,15 +113,26 @@ module.exports = {
// Do the same for to relationships
relationships.filter(relation => relation.to.name == entity.name).forEach(relation => {
let toObjectName = relation.from.name.toLowerCase();
let injectedField = relation.to.injectedField;
let field = null;
if (injectedField != null && injectedField.indexOf('(') != -1) {
field = injectedField.substring(injectedField.indexOf('(')+1, injectedField.indexOf(')'));
}
switch (relation.cardinality) {
case "one-to-one":
case "one-to-many":
entity.options.push(toObjectName + ':has-many:' + toObjectName);
if (field)
entity.options.push(toObjectName + ':has-many:' + toObjectName+ ':'+field);
else
entity.options.push(toObjectName + ':has-many:' + toObjectName);
break;
case "many-to-one":
case "many-to-many":
entity.options.push(inflection.pluralize(toObjectName) + ':has-many:' + toObjectName);
if (field)
entity.options.push(inflection.pluralize(toObjectName) + ':has-many:' + toObjectName+ ':'+field);
else
entity.options.push(inflection.pluralize(toObjectName) + ':has-many:' + toObjectName);
break;
default:
......
......@@ -26,50 +26,50 @@ function generateRowId(subid) {
}
const createObject = (obj) => {
let objout = { "type": obj.ObjectType,
"id": obj.Id,
"attributes": {},
"relationships": {}
let objout = { "type": obj.ObjectType,
"id": obj.Id,
"attributes": {},
"relationships": {}
};
for (var attr in obj) {
if (attr !== 'ObjectType' && attr !== 'Id') {
if(!attr.endsWith('_id')) {
objout.attributes[attr] = obj[attr];
} else {
let relationDetails = attr.split('_');
if (obj[attr].startsWith('[')) {
// hasMany relationship
let idsArray = obj[attr].substring(1, obj[attr].length-1).split(',');
let relData = [];
for (let l=0;l<idsArray.length;l++) {
let relObject = {"type": relationDetails[0], "id": Number(idsArray[l]) };
relData.push(relObject); }
objout.relationships[relationDetails[0]] = {
"links": {
"self": "/"+obj.ObjectType+"/"+obj.Id+"/relationships/"+relationDetails[0],
"related": "/"+obj.ObjectType+"/"+obj.Id+"/"+relationDetails[0]
},
"data": relData
};
for (var attr in obj) {
if (attr !== 'ObjectType' && attr !== 'Id') {
if(!attr.endsWith('_id')) {
objout.attributes[attr] = obj[attr];
} else {
let relationDetails = attr.split('_');
if (obj[attr].startsWith('[')) {
// hasMany relationship
let idsArray = obj[attr].substring(1, obj[attr].length-1).split(',');
let relData = [];
for (let l=0;l<idsArray.length;l++) {
let relObject = {"type": relationDetails[0], "id": Number(idsArray[l]) };
relData.push(relObject); }
} else {
// belongsTo relationship
objout.relationships[relationDetails[0]] = {
"links": {
"self": "/"+obj.ObjectType+"/"+obj.Id+"/relationships/"+relationDetails[0],
"related": "/"+obj.ObjectType+"/"+obj.Id+"/"+relationDetails[0]
},
"data": relData
};
} else {
// belongsTo relationship
objout.relationships[relationDetails[0]] = {
"links": {
"self": "/"+obj.ObjectType+"/"+obj.Id+"/relationships/"+relationDetails[0],
"related": "/"+obj.ObjectType+"/"+obj.Id+"/"+relationDetails[0]
},
"data": {"type": relationDetails[1], "id": Number(obj[attr])}
};
}
}
} else {
objout[(attr==='ObjectType')?'type':'id'] = obj[attr];
}
}
console.log('Return object is '+JSON.stringify(objout));
return objout;
}
"links": {
"self": "/"+obj.ObjectType+"/"+obj.Id+"/relationships/"+relationDetails[0],
"related": "/"+obj.ObjectType+"/"+obj.Id+"/"+relationDetails[0]
},
"data": {"type": relationDetails[1], "id": Number(obj[attr])}
};
}
}
} else {
objout[(attr==='ObjectType')?'type':'id'] = obj[attr];
}
}
console.log('Return object is '+JSON.stringify(objout));
return objout;
}
const handlingData = (data) => {
if (Array.isArray(data)) {
let outdata = [];
......@@ -120,7 +120,6 @@ function generateRowId(subid) {
} else {
params.KeyConditionExpression = 'ObjectType = :objectType';
params.ExpressionAttributeValues = { ':objectType': type };
params.Limit = MAX_OBJECTS;
dbGet = (params) => { return dynamo.query(params).promise() };
console.log('EmberDataServerless lambda GET multiple values with params: ', params);
}
......@@ -165,9 +164,9 @@ function generateRowId(subid) {
};
// Adding attributes as column in dynamoDb
for (var prop in attrs) {
if (attrs[prop] != null && attrs[prop].length>0)
if (attrs[prop] != null && attrs[prop].length>0)
// Empty field cannot be persisted
content[prop] = attrs[prop];
content[prop] = attrs[prop];
}
// Dealing with relationships if any
if (relations){
......@@ -206,6 +205,32 @@ function generateRowId(subid) {
});
};
const patchMethod = (event, context, callback) => {
let type = event.params.path.type,
id = Number(event.params.path.id),
params = {
'TableName': tableName,
'Key': {
'ObjectType': type,
'Id': id
},
'ReturnValues': 'ALL_OLD'
};
const body=event['body-json'];
let dbUpdate = (params) => { return dynamo.update(params).promise() };
dbUpdate(params).then( (data) => {
if (!data.Attributes) {
callback(null, createResponse(404, "ITEM NOT FOUND FOR UPDATE"));
return;
}
console.log(`UPDATE ITEM OF TYPE ${type} SUCCESSFULLY WITH id = ${id}`);
callback(null, body);
}).catch( (err) => {
console.log(`UPDATE ITEM OF TYPE ${type} FAILED FOR id = ${id}, WITH ERROR: ${err}`);
callback(null, createResponse(500, err));
});
};
const deleteMethod = (event, context, callback) => {
let type = event.params.path.type,
id = Number(event.params.path.id),
......
......@@ -197,6 +197,43 @@ function generateRowId(subid) {
});
};
const patchMethod = (event, context, callback) => {
const body=event['body-json'];
const attrs = body.data.attributes;
let updateExpressionList = [];
let expressionAttributeList = [];
for (var attr in attrs) {
updateExpressionList.push(attr+' = :'+attr);
expressionAttributeList[':'+attr] = attrs[attr];
}
let updateExpression = 'set '+updateExpressionList.join(',');
let type = event.params.path.type,
id = Number(event.params.path.id),
params = {
'TableName': tableName,
'Key': {
'ObjectType': type,
'Id': id
},
'UpdateExpression': updateExpression,
'ExpressionAttributeValues': expressionAttributeList,
'ReturnValues': 'UPDATED_NEW'
};
let dbUpdate = (params) => { return dynamo.update(params).promise() };
dbUpdate(params).then( (data) => {
if (!data.Attributes) {
callback(null, createResponse(404, "ITEM NOT FOUND FOR UPDATE"));
return;
}
console.log(`UPDATE ITEM OF TYPE ${type} SUCCESSFULLY WITH id = ${id}`);
callback(null, body);
}).catch( (err) => {
console.log(`UPDATE ITEM OF TYPE ${type} FAILED FOR id = ${id}, WITH ERROR: ${err}`);
callback(null, createResponse(500, err));
});
};
const deleteMethod = (event, context, callback) => {
let type = event.params.path.type,
id = Number(event.params.path.id),
......@@ -236,6 +273,9 @@ exports.handler = (event, context, callback) => {
case 'POST':
putMethod(event,context,callback);
break;
case 'PATCH':
patchMethod(event,context,callback);
break;
case 'DELETE':
deleteMethod(event,context,callback);
break;
......
{
"name": "ember-aws-ehipster",
"version": "0.3.23",
"version": "0.3.26",
"description": "Attempt to build a complete web application using serverless architecture on AWS",
"keywords": [
"ember-addon",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment