Skip to content

Commit

Permalink
add initial files for save data file relation
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro-Gonzalez committed Dec 11, 2019
1 parent 7a7f548 commit 2a7fe75
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 9 deletions.
27 changes: 27 additions & 0 deletions lib/sls-api-file-relation-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

class SlsApiUploadError extends Error {

static get messages() {

return {
BUCKET_NOT_DEFINED: 'Bucket not defined',
BUCKET_NOT_STRING: 'Bucket should be return a string',
ENTITY_ID_FIELD_NOT_DEFINED: 'EntityIdField not defined',
ENTITY_ID_FIELD_NOT_STRING: 'EntityIdField should be return a string',
TABLE_NOT_STRING: 'Table should be return a string',
CUSTOM_FIELDS_NOT_ARRAY_OF_STRINGS: 'CustomFields should be an array of strings.',
CUSTOM_FIELDS_STRUCT_NOT_OBJECT: 'CustomFieldsStruct should be a object',
DATABASEKEY_NOT_STRING: 'DatabaseKey should be return a string'
};

}

constructor(err) {
super(err);
this.message = err.message || err;
this.name = 'SlsApiFileRelationError';
}
}

module.exports = SlsApiUploadError;
96 changes: 92 additions & 4 deletions lib/sls-api-file-relation.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,102 @@
'use strict';

const { struct } = require('superstruct');
const { API } = require('@janiscommerce/api');
const Model = require('@janiscommerce/model');
const SlsApiFileRelationError = require('./sls-api-file-relation-error');

const buildModel = (table, dataBaseKey) => class FileModel extends Model {
get databaseKey() {
return dataBaseKey;
}

static get table() {
return table;
}
};

class SlsApiFileRelation extends API {
get struct() {
return {
get table() {
return 'files';
}

get databaseKey() {
return undefined;
}

get customFields() {
return [];
}

get customFieldsStruct() {
return undefined;
}

validate() {
if(this.customFields.length) {
if(!this.customFields.every(field => field === 'string'))
throw new SlsApiFileRelationError(SlsApiFileRelationError.messages.CUSTOM_FIELDS_NOT_ARRAY_OF_STRINGS);
}

if(this.customFieldsStruct && (typeof this.customFieldsStruct !== 'object' || Array.isArray(this.customFieldsStruct)))
throw new SlsApiFileRelationError(SlsApiFileRelationError.messages.CUSTOM_FIELDS_STRUCT_NOT_OBJECT);

const CustomStruct = struct({
fileName: 'string',
fileSource: 'string'
};
fileSource: 'string',
...this.customFieldsStruct
});

CustomStruct(this.data);

if(!this.entityIdField)
throw new SlsApiFileRelationError(SlsApiFileRelationError.messages.ENTITY_ID_FIELD_NOT_DEFINED);

if(typeof this.entityIdField !== 'string')
throw new SlsApiFileRelationError(SlsApiFileRelationError.messages.ENTITY_ID_FIELD_NOT_STRING);

if(!this.bucket)
throw new SlsApiFileRelationError(SlsApiFileRelationError.messages.BUCKET_NOT_DEFINED);

if(typeof this.bucket !== 'string')
throw new SlsApiFileRelationError(SlsApiFileRelationError.messages.BUCKET_NOT_STRING);

if(typeof this.table !== 'string')
throw new SlsApiFileRelationError(SlsApiFileRelationError.messages.TABLE_NOT_STRING);

if(this.databaseKey && typeof this.databaseKey !== 'string')
throw new SlsApiFileRelationError(SlsApiFileRelationError.messages.DATABASEKEY_NOT_STRING);

}

async process() {
const [id] = this.pathParameters;

const {
fileName: name,
fileSource: path,
...data
} = this.data;

const model = buildModel(this.table, this.databaseKey);

const modelInstance = this.session.getInstance(model);

const insertedId = await modelInstance.insert({
[this.entityIdField]: id,
name,
path,
...data
});

console.log(insertedId);

this
.setCode(201)
.setBody({ id: 'test' });
}


}


Expand Down
24 changes: 20 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
"dependencies": {
"@janiscommerce/api": "^4.1.1",
"@janiscommerce/api-test": "^2.1.0",
"@janiscommerce/model": "^3.3.3",
"aws-sdk": "^2.562.0",
"mime": "^2.4.4",
"superstruct": "^0.6.2",
"uuid": "^3.3.3"
}
}
3 changes: 2 additions & 1 deletion tests/sls-api-file-relation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
/* 'use strict';
const APITest = require('@janiscommerce/api-test');
const { SlsApiFileRelation } = require('../lib/index');
Expand Down Expand Up @@ -65,3 +65,4 @@ describe('SlsApiRelation', () => {
]);
});
*/

0 comments on commit 2a7fe75

Please sign in to comment.