Skip to content

Commit fa82ba0

Browse files
author
ryanholte
committed
[CHORE] starting before event functions
1 parent e972491 commit fa82ba0

File tree

6 files changed

+66
-16
lines changed

6 files changed

+66
-16
lines changed

lib/db/baseModel.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474

7575

7676
module.exports = {
77-
find(id) {
77+
find(id, doFetch = true) {
7878
let obj = {}
7979

8080
if (typeof id === 'object') {
@@ -92,11 +92,15 @@ module.exports = {
9292
};
9393
}
9494

95-
return this.forge()
95+
const query = this.forge()
9696
.query({
9797
where: obj,
98-
})
99-
.fetch(fetchOptions);
98+
});
99+
100+
if (doFetch) {
101+
return query.fetch(fetchOptions);
102+
}
103+
return query;
100104
},
101105

102106
findBy() {
@@ -169,7 +173,15 @@ module.exports = {
169173
},
170174

171175
update(id, record) {
172-
return this.forge({id: id})
173-
.save(record, {patch: true, require: false});
176+
// return this.forge({id: id})
177+
// .save(record, {patch: true, require: false});
178+
179+
return this
180+
.forge()
181+
.query(function (qb) {
182+
qb.where({id});
183+
qb.update(record, ['*']);
184+
});
185+
174186
},
175187
};

lib/db/modelFactory.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ModelFactory {
3535
this.on('updating', (model, attrs, options) => {
3636
model.set('updated_at', Math.floor(Date.now() / 1000));
3737
});
38-
this.on('saving', (model, attrs, options) => {
38+
this.on('creating', (model, attrs, options) => {
3939
model.set('created_at', Math.floor(Date.now() / 1000));
4040
});
4141

@@ -51,6 +51,10 @@ class ModelFactory {
5151
if (self.modelEvents.saved) {
5252
this.on('saved', self.modelEvents.saved);
5353
}
54+
55+
this.on('fetching', (model, columns, options, a, b, c) => {
56+
console.log('fetching event!');
57+
})
5458
}
5559
}
5660
};

lib/route/methods/get.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
const Response = require('../response');
22

33
function Get(options) {
4-
const {path, name, Model, validate, beforeCreate, pre = [], auth, strippedColumns} = options;
4+
const {
5+
path,
6+
name,
7+
Model,
8+
validate,
9+
beforeCreate,
10+
beforeRead,
11+
pre = [],
12+
auth,
13+
strippedColumns,
14+
} = options;
515
return {
616
method: 'GET',
717
path: `${path}/{id}`,
@@ -10,10 +20,13 @@ function Get(options) {
1020
auth,
1121
handler: async function (request) {
1222
const id = request.params.id;
13-
14-
console.log('id', id);
1523
try {
16-
const model = await Model.find(id);
24+
const query = await Model.find(id, false);
25+
if (beforeRead) {
26+
const valid = await beforeRead('get', request, query);
27+
}
28+
29+
const model = await query.fetch();
1730
return Response.success(name, model, strippedColumns);
1831
} catch (ex) {
1932
console.log('get error', ex);

lib/route/methods/post.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function Post(options) {
2222
method,
2323
path: `${path}`,
2424
config: {
25-
auth: 'jwt',
25+
auth,
2626
pre: preHandlers,
2727
handler: async function (request) {
2828
const record = request.payload[Tenses.getSingular(name)];

lib/route/methods/put.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@ const Response = require('../response');
44
const method = 'put';
55

66
function Put(options) {
7-
const {path, name, Model, validate, beforeCreate, pre = [], auth, strippedColumns} = options;
7+
const {
8+
path,
9+
name,
10+
Model,
11+
validate,
12+
beforeCreate,
13+
beforeUpdate,
14+
pre = [],
15+
auth,
16+
strippedColumns
17+
} = options;
818
const validateSutff = {
919
method: async function (request) {
1020
try {
@@ -33,8 +43,19 @@ function Put(options) {
3343
const id = request.params.id;
3444

3545
try {
36-
const updatedRecord = await Model.update(id, record);
37-
return Response.success(name, updatedRecord, strippedColumns);
46+
47+
const query = Model.update(id, record);
48+
if (beforeUpdate) {
49+
beforeUpdate('put', request, query._knex);
50+
}
51+
const responseArr = await query.query();
52+
let model = null;
53+
if (responseArr.length) {
54+
model = Model.forge(responseArr.pop());
55+
}
56+
57+
58+
return Response.success(name, model, strippedColumns);
3859
} catch (ex) {
3960
return Response.badRequest(ex);
4061
}

lib/route/route.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ class Plugin {
6060
const routeDefaults = {
6161
path: this.config.route,
6262
name: this.config.name,
63-
strippedColumns: getStripedColumns(this.stripColumns),
6463
Model: Model,
6564
validate: this.validate,
6665
};
@@ -69,6 +68,7 @@ class Plugin {
6968
pre: getPre(this.pre.post, this.pre.default),
7069
auth: getAuth(this.auth.post, this.auth.default),
7170
beforeCreate: this.modelHooks.beforeCreate,
71+
beforeRead: this.modelHooks.beforeRead,
7272
});
7373
const getOptions = Object.assign({}, routeDefaults, {
7474
pre: getPre(this.pre.get, this.pre.default),

0 commit comments

Comments
 (0)