Skip to content

Commit

Permalink
Fixed #17
Browse files Browse the repository at this point in the history
  • Loading branch information
ozziest committed May 23, 2021
1 parent 8f4641b commit 6fa2fd3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "axe-api",
"version": "0.5.0",
"version": "0.6.0",
"description": "AXE API is a simple tool which has been created based on Express and Knex.js to create Rest APIs quickly.",
"main": "index.js",
"type": "module",
Expand Down
6 changes: 6 additions & 0 deletions src/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ const DEPENDECY_TYPES = {
SINGLETON: "SINGLETON",
};

const TIMESTAMP_COLUMNS = {
CREATED_AT: "createdAtColumn",
UPDATED_AT: "updatedAtColumn",
};

export {
LOG_LEVEL,
HOOK_FUNCTIONS,
Expand All @@ -134,4 +139,5 @@ export {
LOG_COLORS,
DEPENDECY_TYPES,
HANDLERS,
TIMESTAMP_COLUMNS,
};
8 changes: 8 additions & 0 deletions src/Controller/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,11 @@ export const filterHiddenFields = (itemArray, hiddens) => {
});
});
};

export const bindTimestampValues = (formData, columnTypes = [], model) => {
for (const columnType of columnTypes) {
if (model.instance[columnType]) {
formData[model.instance[columnType]] = new Date();
}
}
};
10 changes: 9 additions & 1 deletion src/Controller/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import {
callHooks,
getParentColumn,
filterHiddenFields,
bindTimestampValues,
} from "./helpers.js";
import Validator from "validatorjs";
import { HOOK_FUNCTIONS } from "./../Constants.js";
import { HOOK_FUNCTIONS, TIMESTAMP_COLUMNS } from "./../Constants.js";

export default async (pack) => {
const { request, response, model, database, relation, parentModel } = pack;
Expand All @@ -30,6 +31,13 @@ export default async (pack) => {
formData[relation.foreignKey] = request.params[parentColumn];
}

// We should bind the timestamp values
bindTimestampValues(
formData,
[TIMESTAMP_COLUMNS.CREATED_AT, TIMESTAMP_COLUMNS.UPDATED_AT],
model
);

await callHooks(model, HOOK_FUNCTIONS.onBeforeInsert, {
...pack,
formData,
Expand Down
6 changes: 5 additions & 1 deletion src/Controller/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import {
callHooks,
getParentColumn,
filterHiddenFields,
bindTimestampValues,
} from "./helpers.js";
import Validator from "validatorjs";
import { HOOK_FUNCTIONS } from "./../Constants.js";
import { HOOK_FUNCTIONS, TIMESTAMP_COLUMNS } from "./../Constants.js";
import ApiError from "./../Exceptions/ApiError.js";

export default async (pack) => {
Expand Down Expand Up @@ -50,6 +51,9 @@ export default async (pack) => {
}
}

// We should bind the timestamp values
bindTimestampValues(formData, [TIMESTAMP_COLUMNS.UPDATED_AT], model);

await callHooks(model, HOOK_FUNCTIONS.onBeforeUpdate, {
...pack,
item,
Expand Down
8 changes: 8 additions & 0 deletions src/Models/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ class Model {
return [];
}

get createdAtColumn() {
return "created_at";
}

get updatedAtColumn() {
return "updated_at";
}

hasMany(relatedModel, primaryKey = "id", foreignKey = null) {
if (!foreignKey) {
const currentModelName = pluralize.singular(
Expand Down

0 comments on commit 6fa2fd3

Please sign in to comment.