Skip to content

Commit 712756e

Browse files
authored
Merge pull request #29 from hapipal/skipUndef-dependencies
Update dependencies, remove deprecated skipUndefined() objection functionality
2 parents 73cbf4b + 7202497 commit 712756e

5 files changed

Lines changed: 81 additions & 33 deletions

File tree

.eslintrc.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
2-
"extends": "@hapi/eslint-config-hapi",
2+
"extends": "plugin:@hapi/module",
33
"parserOptions": {
4-
"ecmaVersion": 9
4+
"ecmaVersion": 2020,
5+
"sourceType": "script"
56
}
67
}

lib/actions/find.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@ module.exports = function findRecords(route, options) {
3939
const rangeEnd = rangeStart ? rangeStart + limit : limit;
4040
const where = actionUtil.parseWhere();
4141

42-
const foundModels = await Model.query()
43-
.skipUndefined()
44-
.range(rangeStart, rangeEnd)
45-
.where(where)
46-
.limit(limit)
47-
.orderByRaw(sort);
42+
const foundModelsQuery = Model.query().range(rangeStart, rangeEnd).limit(limit);
43+
44+
if (where) {
45+
foundModelsQuery.where(where);
46+
}
47+
48+
if (sort) {
49+
foundModelsQuery.orderByRaw(sort);
50+
}
51+
52+
const foundModels = await foundModelsQuery;
53+
4854
return foundModels.results;
55+
4956
};
5057
};

lib/actions/populate.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,28 @@ module.exports = function expand(route, options) {
3737
const rangeStart = actionUtil.parseSkip();
3838
const rangeEnd = rangeStart + limit;
3939

40-
const modelFull = await Model.query().skipUndefined().findById(keys.parent.value).withGraphFetched(relation)
40+
const modelFullQuery = Model.query().findById(keys.parent.value)
41+
.withGraphFetched(relation)
4142
.modifyGraph(relation, (builder) => {
4243

43-
builder.skipUndefined()
44-
.where(Ref(RelationModel.tableName + '.' + keys.child.key), '=', keys.child.value)
45-
.range(rangeStart, rangeEnd)
46-
.limit(limit)
47-
.orderByRaw(sort);
44+
if (keys.child?.value) {
45+
builder.where(Ref(RelationModel.tableName + '.' + keys.child.key), '=', keys.child.value);
46+
}
47+
48+
builder.range(rangeStart, rangeEnd).limit(limit);
49+
50+
if (sort) {
51+
builder.orderByRaw(sort);
52+
}
4853
});
4954

55+
const modelFull = await modelFullQuery;
5056

5157
if (!modelFull) {
5258
return Boom.notFound('No record found with the specified id.');
5359
}
5460

55-
if (keys.child.value) {
61+
if (keys.child?.value) {
5662
if (modelFull[options.associationAttr].length) {
5763
return h.response().code(204);
5864
}

package.json

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hapipal/tandy",
3-
"version": "3.0.0",
3+
"version": "3.1.0",
44
"description": "Auto-generated, RESTful, CRUDdy route handlers for Schwifty models in hapi",
55
"main": "lib/index.js",
66
"scripts": {
@@ -28,28 +28,27 @@
2828
"url": "https://github.com/hapipal/tandy/issues"
2929
},
3030
"dependencies": {
31-
"@hapi/boom": "9.x.x",
32-
"@hapi/call": "8.x.x",
33-
"@hapi/hoek": "9.x.x",
34-
"joi": "17.x.x",
31+
"@hapi/boom": "^10.0.1",
32+
"@hapi/call": "^9.0.1",
33+
"@hapi/hoek": "^11.0.2",
34+
"joi": "^17.7.1",
3535
"lodash": "^4.17.21"
3636
},
3737
"peerDependencies": {
38-
"@hapi/hapi": ">=19 <21",
39-
"@hapipal/schwifty": "6.x.x"
38+
"@hapi/hapi": ">=19 <22",
39+
"@hapipal/schwifty": "^6.2.0"
4040
},
4141
"devDependencies": {
42-
"@hapi/code": "8.x.x",
43-
"@hapi/eslint-config-hapi": "13.x.x",
44-
"@hapi/eslint-plugin-hapi": "4.x.x",
45-
"@hapi/hapi": ">=20",
46-
"@hapi/lab": "24.x.x",
47-
"coveralls": "3.x.x",
48-
"eslint": "7.x.x",
49-
"knex": "0.95.4",
50-
"objection": "2.x.x",
51-
"@hapipal/schwifty": "6.x.x",
52-
"sqlite3": "5.x.x"
42+
"@hapi/code": "^9.0.3",
43+
"@hapi/eslint-plugin": "^6.0.0",
44+
"@hapi/hapi": ">=19 <22",
45+
"@hapi/lab": "^25.1.2",
46+
"coveralls": "^3.1.1",
47+
"eslint": "^8.34.0",
48+
"knex": "^2.4.2",
49+
"objection": "^3.0.1",
50+
"@hapipal/schwifty": "^6.2.0",
51+
"sqlite3": "^5.1.4"
5352
},
5453
"homepage": "https://github.com/hapipal/tandy"
5554
}

test/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,41 @@ describe('Tandy', () => {
13881388
expect(result.tokens).to.be.an.array();
13891389
expect(result.tokens.length).to.equal(1);
13901390
});
1391+
1392+
it('Fetches tokens for a user, sorted by token id', async () => {
1393+
1394+
const server = await getServer(getOptions());
1395+
server.registerModel([
1396+
TestModels.Users,
1397+
TestModels.Tokens
1398+
]);
1399+
1400+
await server.initialize();
1401+
1402+
const knex = server.knex();
1403+
await knex.seed.run({ directory: 'test/seeds' });
1404+
1405+
server.route({
1406+
method: 'GET',
1407+
path: '/users/{id}/tokens',
1408+
handler: { tandy: { sort: 'Tokens.temp DESC' } }
1409+
});
1410+
1411+
const options = {
1412+
method: 'GET',
1413+
url: '/users/1/tokens'
1414+
};
1415+
1416+
const response = await server.inject(options);
1417+
1418+
const result = response.result;
1419+
1420+
expect(response.statusCode).to.equal(200);
1421+
expect(result.tokens).to.be.an.array();
1422+
expect(result.tokens.length).to.equal(2);
1423+
expect(result.tokens[0].temp).to.equal('text');
1424+
});
1425+
13911426
it('Fetches limited number of tokens for a user using query param', async () => {
13921427

13931428
const server = await getServer(getOptions());

0 commit comments

Comments
 (0)