Skip to content

Commit

Permalink
feat: Updated attachment database adapter and repo
Browse files Browse the repository at this point in the history
Also updated Querybuilder.
  • Loading branch information
Nino van Galen committed Jun 26, 2020
1 parent 3bbcf99 commit ff03b92
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
12 changes: 7 additions & 5 deletions lib/database/adapters/AttachmentAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ class AttachmentAdapter {
* @param {Object} databaseObject Object to convert.
* @returns {Object} Converted entity object.
*/
static toEntity({ fileName, size, mimeType, originalName, path, encoding, log, createdAt, updatedAt }) {
static toEntity({ id, fileName, size, mimeType, originalName, path, encoding, logId, createdAt, updatedAt }) {
const entityObject = Object.assign(new Attachment(), {
id: id,
fileName: fileName,
size: size,
mimeType: mimeType,
Expand All @@ -36,10 +37,10 @@ class AttachmentAdapter {
updatedAt: new Date(updatedAt).getTime(),
});

if (log) {
entityObject.log = LogAdapter.toEntity(log);
if (logId) {
entityObject.logId = LogAdapter.toEntity(logId).id;
} else {
entityObject.log = undefined;
entityObject.logId = undefined;
}

return entityObject;
Expand All @@ -53,13 +54,14 @@ class AttachmentAdapter {
*/
static toDatabase(entityObject) {
return {
id: entityObject.id,
size: entityObject.size,
path: entityObject.path,
fileName: entityObject.filename,
originalName: entityObject.originalname,
mimeType: entityObject.mimetype,
encoding: entityObject.encoding,
logId: entityObject.log,
logId: entityObject.logId,
};
}
}
Expand Down
15 changes: 14 additions & 1 deletion lib/database/repositories/AttachmentRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AttachmentRepository {
* @returns {Promise<number>} Promise object representing the total number of entities.
*/
async count() {
return AttachmentRepository.count();
return Attachment.count();
}

/**
Expand Down Expand Up @@ -84,6 +84,19 @@ class AttachmentRepository {
const result = await Attachment.create(AttachmentAdapter.toDatabase(entity));
return AttachmentAdapter.toEntity(result);
}

/**
* Bulk insert entities.
*
* @param {Array} entities List of entities to insert.
* @returns {Promise} Promise object represents the just inserted Attachments.
*/
async bulkInsert(entities) {
entities = Array.isArray(entities) ? entities : [entities];

const results = await Attachment.bulkCreate(entities.map(AttachmentAdapter.toDatabase), { returning: true });
return results.map(AttachmentAdapter.toEntity);
}
}

module.exports = new AttachmentRepository();
1 change: 0 additions & 1 deletion lib/database/utilities/QueryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ module.exports = (sequelize) => {
},
},
});

return this.queryBuilder;
}
}
Expand Down

0 comments on commit ff03b92

Please sign in to comment.