Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support string uidType #84

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b468dba
Allow uidType('string')
derekwsgray Sep 21, 2018
d5f2e0d
Another spot to allow 'string'
derekwsgray Sep 21, 2018
3a28d57
Update README and Typescript file
derekwsgray Sep 21, 2018
d5de9b8
Oops, that should have been "!"
derekwsgray Sep 21, 2018
f5ad566
lock file
derekwsgray Sep 21, 2018
6104de0
Using eslint, not jshint
derekwsgray Apr 12, 2019
0bf6bc5
appending to a hasMany should append the given data[] elements, not t…
derekwsgray Apr 12, 2019
2357c07
Merge branch 'bugfix/post-add-relation'
derekwsgray Apr 12, 2019
789f202
Merge remote-tracking branch 'jagql/master'
derekwsgray Apr 12, 2019
f4a28a2
Merge remote-tracking branch 'jagql/master'
derekwsgray Apr 12, 2019
51cda7a
Merge remote-tracking branch 'origin/master'
derekwsgray Apr 12, 2019
e5cd240
re-run npm install
derekwsgray Apr 12, 2019
0f30cc1
Merge remote-tracking branch 'origin/master' into bugfix/post-add-rel…
derekwsgray Apr 12, 2019
ff2c2d6
Remove strange array containing a null instead of being empty.
derekwsgray Apr 13, 2019
0af7852
Merge remote-tracking branch 'origin/bugfix/post-add-relation'
derekwsgray Apr 13, 2019
852b9dd
Fix test failures re: includes[] when data is null.
derekwsgray Apr 15, 2019
0b8a0b7
Merge remote-tracking branch 'origin/bugfix/post-add-relation'
derekwsgray Apr 15, 2019
1652eeb
Removed default pagination vals, updated if condition for memoryhandler
aarce-uncharted Jan 11, 2021
ffec281
Reverted previous change, made pagination configurable
aarce-uncharted Jan 12, 2021
7752a15
Added tests for configurable pagination
aarce-uncharted Jan 13, 2021
2056def
Removed unused function
aarce-uncharted Jan 13, 2021
e152536
Merge pull request #1 from aarce-uncharted/Remove-default-pagination
derekwsgray Jan 14, 2021
b59fa90
Create node.js.yml
derekwsgray Jan 14, 2021
d98d48b
Update node.js.yml
derekwsgray Jan 14, 2021
046bf25
Create fortify-analysis.yml
derekwsgray Sep 8, 2021
32bae0e
Delete fortify-analysis.yml
derekwsgray Sep 8, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [8.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
3 changes: 0 additions & 3 deletions .jshintrc

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ We instead use a different property `primaryKey`, whose possible values are
\*In future there might be other types of primaryKeys if required.

#### relationship key type is configurable
When creating a field, you can state how to relate it
When creating a field, you can state how to relate it. Supported are `uuid`, `autoincrement` and `string`

```javascript
jsonApi.define({
Expand Down
7 changes: 5 additions & 2 deletions example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const fs = require('fs')
const path = require('path')
const debug = require('debug')

jsonApi.setConfig({
const config = {
graphiql: true,
swagger: {
title: 'Example JSON:API Server',
Expand Down Expand Up @@ -43,7 +43,9 @@ jsonApi.setConfig({
meta: {
description: 'This block shows up in the root node of every payload'
}
})
}

jsonApi.setConfig(config)

jsonApi.authenticate((request, callback) => {
// If a "blockMe" header is provided, block access.
Expand Down Expand Up @@ -79,4 +81,5 @@ if (typeof describe === 'undefined') {
}
server.start = jsonApi.start
server.close = jsonApi.close
server.setupTestServer = opts => (jsonApi.setConfig({...config, ...opts}), jsonApi.start())
server.getExpressServer = jsonApi.getExpressServer
10 changes: 6 additions & 4 deletions lib/ourJoi.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const Joi = require('joi')
const JoiDateExtensions = require('joi-date-extensions')
Joi.extend(JoiDateExtensions)

const ALLOWABLE_RELATED_UID_TYPES = ['uuid', 'autoincrement', 'string']

ourJoi._joiBase = resourceName => {
const relationType = Joi.object().keys({
id: Joi.string().required(),
Expand All @@ -26,8 +28,8 @@ Joi.one = function () {
}
obj._settings._uidType = 'string'
obj.uidType = function (keyType) {
if (keyType !== 'uuid' && keyType !== 'autoincrement') {
throw new Error('Resources can be related only via UUID or AUTOINCREMENT keys')
if (!ALLOWABLE_RELATED_UID_TYPES.includes(keyType)) {
throw new Error('Resources can be related only via UUID, STRING or AUTOINCREMENT keys')
}
obj._settings._uidType = keyType
return obj
Expand All @@ -45,8 +47,8 @@ Joi.many = function () {
}
obj._settings._uidType = 'string'
obj.uidType = function (keyType) {
if (keyType !== 'uuid' && keyType !== 'autoincrement') {
throw new Error('Resources can be related only via UUID or AUTOINCREMENT keys')
if (!ALLOWABLE_RELATED_UID_TYPES.includes(keyType)) {
throw new Error('Resources can be related only via UUID, STRING or AUTOINCREMENT keys')
}
obj._settings._uidType = keyType
return obj
Expand Down
6 changes: 4 additions & 2 deletions lib/pagination.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
const pagination = module.exports = { }

const jsonApi = require('./jsonApi.js')
const ourJoi = require('./ourJoi.js')
const url = require('url')

Expand All @@ -27,8 +28,9 @@ pagination.validatePaginationParams = request => {
}
const page = request.params.page

page.offset = parseInt(page.offset, 10) || 0
page.limit = parseInt(page.limit, 10) || 50
// Set your own pagination defaults through setConfig()
page.offset = parseInt(page.offset, 10) || (jsonApi._apiConfig.page && jsonApi._apiConfig.page.offset) || 0
page.limit = parseInt(page.limit, 10) || (jsonApi._apiConfig.page && jsonApi._apiConfig.page.limit) || 50
}

pagination.enforcePagination = (request, results) => results.slice(0, request.params.page.size)
Expand Down
2 changes: 1 addition & 1 deletion lib/postProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ postProcess._fetchRelatedResources = (request, mainResource, callback) => {
// Fetch the other objects
let dataItems = mainResource[request.params.relation]

if (!dataItems) return callback(null, [ null ], null)
if (!dataItems) return callback(null, [], null)

if (!(Array.isArray(dataItems))) dataItems = [ dataItems ]

Expand Down
2 changes: 1 addition & 1 deletion lib/responseHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ responseHelper._generateResponse = (request, resourceConfig, sanitisedData, hand
self: responseHelper._baseUrl + request.route.path + (request.route.query ? ('?' + request.route.query) : '')
}, pagination.generatePageLinks(request, handlerTotal)),

data: sanitisedData
data: (sanitisedData === undefined ? null : sanitisedData)
})

responseHelper._generateMeta = (request, handlerTotal) => {
Expand Down
3 changes: 2 additions & 1 deletion lib/routes/addRelation.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ addRelationRoute.register = () => {

if (resourceConfig.attributes[request.params.relation]._settings.__many) {
theirResource[request.params.relation] = theirResource[request.params.relation] || [ ]
theirResource[request.params.relation].push(theirs)
const elements = [].concat(theirs || [])
theirResource[request.params.relation].push(...elements)
} else {
theirResource[request.params.relation] = theirs
}
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/related.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ relatedRoute.register = () => {
if (relation._settings.__one) {
// if this is a hasOne, then disable pagination meta data.
total = null
relatedResources = relatedResources[0]
relatedResources = relatedResources[0] ? relatedResources[0] : null
}
request.resourceConfig = (relation._settings.__one || relation._settings.__many).map(resourceName => {
return jsonApi._resources[resourceName]
})

response = responseHelper._generateResponse(request, resourceConfig, relatedResources, total)
if (relatedResources !== null) {
response.included = [ ]
response.included = []
}
postProcess.handle(request, response, callback)
}
Expand Down
Loading