Skip to content

Commit

Permalink
Merge pull request #29 from hapipal/skipUndef-dependencies
Browse files Browse the repository at this point in the history
Update dependencies, remove deprecated skipUndefined() objection functionality
  • Loading branch information
mattboutet authored Feb 18, 2023
2 parents 73cbf4b + 7202497 commit 712756e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 33 deletions.
5 changes: 3 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "@hapi/eslint-config-hapi",
"extends": "plugin:@hapi/module",
"parserOptions": {
"ecmaVersion": 9
"ecmaVersion": 2020,
"sourceType": "script"
}
}
19 changes: 13 additions & 6 deletions lib/actions/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,19 @@ module.exports = function findRecords(route, options) {
const rangeEnd = rangeStart ? rangeStart + limit : limit;
const where = actionUtil.parseWhere();

const foundModels = await Model.query()
.skipUndefined()
.range(rangeStart, rangeEnd)
.where(where)
.limit(limit)
.orderByRaw(sort);
const foundModelsQuery = Model.query().range(rangeStart, rangeEnd).limit(limit);

if (where) {
foundModelsQuery.where(where);
}

if (sort) {
foundModelsQuery.orderByRaw(sort);
}

const foundModels = await foundModelsQuery;

return foundModels.results;

};
};
20 changes: 13 additions & 7 deletions lib/actions/populate.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,28 @@ module.exports = function expand(route, options) {
const rangeStart = actionUtil.parseSkip();
const rangeEnd = rangeStart + limit;

const modelFull = await Model.query().skipUndefined().findById(keys.parent.value).withGraphFetched(relation)
const modelFullQuery = Model.query().findById(keys.parent.value)
.withGraphFetched(relation)
.modifyGraph(relation, (builder) => {

builder.skipUndefined()
.where(Ref(RelationModel.tableName + '.' + keys.child.key), '=', keys.child.value)
.range(rangeStart, rangeEnd)
.limit(limit)
.orderByRaw(sort);
if (keys.child?.value) {
builder.where(Ref(RelationModel.tableName + '.' + keys.child.key), '=', keys.child.value);
}

builder.range(rangeStart, rangeEnd).limit(limit);

if (sort) {
builder.orderByRaw(sort);
}
});

const modelFull = await modelFullQuery;

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

if (keys.child.value) {
if (keys.child?.value) {
if (modelFull[options.associationAttr].length) {
return h.response().code(204);
}
Expand Down
35 changes: 17 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hapipal/tandy",
"version": "3.0.0",
"version": "3.1.0",
"description": "Auto-generated, RESTful, CRUDdy route handlers for Schwifty models in hapi",
"main": "lib/index.js",
"scripts": {
Expand Down Expand Up @@ -28,28 +28,27 @@
"url": "https://github.com/hapipal/tandy/issues"
},
"dependencies": {
"@hapi/boom": "9.x.x",
"@hapi/call": "8.x.x",
"@hapi/hoek": "9.x.x",
"joi": "17.x.x",
"@hapi/boom": "^10.0.1",
"@hapi/call": "^9.0.1",
"@hapi/hoek": "^11.0.2",
"joi": "^17.7.1",
"lodash": "^4.17.21"
},
"peerDependencies": {
"@hapi/hapi": ">=19 <21",
"@hapipal/schwifty": "6.x.x"
"@hapi/hapi": ">=19 <22",
"@hapipal/schwifty": "^6.2.0"
},
"devDependencies": {
"@hapi/code": "8.x.x",
"@hapi/eslint-config-hapi": "13.x.x",
"@hapi/eslint-plugin-hapi": "4.x.x",
"@hapi/hapi": ">=20",
"@hapi/lab": "24.x.x",
"coveralls": "3.x.x",
"eslint": "7.x.x",
"knex": "0.95.4",
"objection": "2.x.x",
"@hapipal/schwifty": "6.x.x",
"sqlite3": "5.x.x"
"@hapi/code": "^9.0.3",
"@hapi/eslint-plugin": "^6.0.0",
"@hapi/hapi": ">=19 <22",
"@hapi/lab": "^25.1.2",
"coveralls": "^3.1.1",
"eslint": "^8.34.0",
"knex": "^2.4.2",
"objection": "^3.0.1",
"@hapipal/schwifty": "^6.2.0",
"sqlite3": "^5.1.4"
},
"homepage": "https://github.com/hapipal/tandy"
}
35 changes: 35 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,41 @@ describe('Tandy', () => {
expect(result.tokens).to.be.an.array();
expect(result.tokens.length).to.equal(1);
});

it('Fetches tokens for a user, sorted by token id', async () => {

const server = await getServer(getOptions());
server.registerModel([
TestModels.Users,
TestModels.Tokens
]);

await server.initialize();

const knex = server.knex();
await knex.seed.run({ directory: 'test/seeds' });

server.route({
method: 'GET',
path: '/users/{id}/tokens',
handler: { tandy: { sort: 'Tokens.temp DESC' } }
});

const options = {
method: 'GET',
url: '/users/1/tokens'
};

const response = await server.inject(options);

const result = response.result;

expect(response.statusCode).to.equal(200);
expect(result.tokens).to.be.an.array();
expect(result.tokens.length).to.equal(2);
expect(result.tokens[0].temp).to.equal('text');
});

it('Fetches limited number of tokens for a user using query param', async () => {

const server = await getServer(getOptions());
Expand Down

0 comments on commit 712756e

Please sign in to comment.