Skip to content

Commit 07f0f97

Browse files
committed
refactor(grind-example-api): updated api starter to work with monorepo
1 parent f6e620e commit 07f0f97

34 files changed

+232
-291
lines changed

.babelrc

Lines changed: 0 additions & 13 deletions
This file was deleted.

.editorconfig

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ root = true
33
[*]
44
charset = "utf-8"
55
end_of_line = lf
6-
indent_style = tab
7-
insert_final_newline = true
8-
trim_trailing_whitespace = true
9-
10-
[{package.json}]
116
indent_size = 2
127
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
1310

14-
[*.yml]
15-
indent_size = 2
16-
indent_style = space
11+
[*.{js,cjs}]
12+
indent_size = 4
13+
indent_style = tab

.eslintrc.json

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
{
2-
"parser": "babel-eslint",
3-
"plugins": [
4-
"import-auto-name"
5-
],
6-
"env": {
7-
"node": true
8-
},
9-
"extends": "grind",
10-
"parserOptions": {
11-
"sourceType": "module"
12-
},
13-
"globals": {
14-
"ValidationError": false
15-
}
2+
"plugins": ["eslint-plugin-import-auto-name", "prettier"],
3+
"parser": "babel-eslint",
4+
"rules": {
5+
"prettier/prettier": "error"
6+
},
7+
"env": {
8+
"node": true
9+
},
10+
"parserOptions": {
11+
"ecmaVersion": 2020,
12+
"sourceType": "module"
13+
},
14+
"globals": {
15+
"ValidationError": false
16+
},
17+
"extends": ["plugin:prettier/recommended"]
1618
}

.travis.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2016-2017 Shaun Harrison
3+
Copyright (c) 2016-2020 Shaun Harrison
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.markdown

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
# Grind API Template
1111

12-
The Grind API Template is an example project for building API’s on [Grind](https://github.com/grindjs/framework). It’s also used as a template in [Grind Installer](https://github.com/grindjs/installer) so you can quickly setup a new API project.
12+
The Grind API Template is an example project for building API’s on [Grind](https://github.com/grindjs/framework). It’s also used as a template in [Grind Installer](https://github.com/grindjs/installer) so you can quickly setup a new API project.
1313

1414
## Installation
1515

@@ -40,10 +40,10 @@ You should now be able to visit [localhost:3000/states](http://localhost:3000/st
4040

4141
Other URLs:
4242

43-
* Paged: [localhost:3000/states?limit=10](http://localhost:3000/states?limit=10)
44-
* Individual: [localhost:3000/states/ny](http://localhost:3000/states/ny)
45-
* Search: [localhost:3000/states/search?term=new](http://localhost:3000/states/search?term=new)
46-
* Swagger: [petstore.swagger.io/?url=http://localhost:3000/swagger.json](http://petstore.swagger.io/?url=http://localhost:3000/swagger.json)
43+
- Paged: [localhost:3000/states?limit=10](http://localhost:3000/states?limit=10)
44+
- Individual: [localhost:3000/states/ny](http://localhost:3000/states/ny)
45+
- Search: [localhost:3000/states/search?term=new](http://localhost:3000/states/search?term=new)
46+
- Swagger: [petstore.swagger.io/?url=http://localhost:3000/swagger.json](http://petstore.swagger.io/?url=http://localhost:3000/swagger.json)
4747

4848
## Documentation
4949

app/Commands/StatesListCommand.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,34 @@ import { Command, InputArgument, InputOption } from 'grind-cli'
33
import 'App/Models/StateModel'
44

55
export class StatesListCommand extends Command {
6-
76
// Name of the command
87
name = 'states:list'
98

109
// Description of the command to show in help
1110
description = 'List all states in the database'
1211

1312
// Arguments available for this command
14-
arguments = [
15-
new InputArgument('term', InputArgument.VALUE_OPTIONAL, 'Search term'),
16-
]
13+
arguments = [new InputArgument('term', InputArgument.VALUE_OPTIONAL, 'Search term')]
1714

1815
// Options for this command
1916
options = [
20-
new InputOption('limit', InputOption.VALUE_OPTIONAL, 'Limit the number of states', '100')
17+
new InputOption('limit', InputOption.VALUE_OPTIONAL, 'Limit the number of states', '100'),
2118
]
2219

2320
run() {
2421
const limit = Number.parseInt(this.option('limit', 100))
2522
let query = null
2623

27-
if(this.containsArgument('term')) {
24+
if (this.containsArgument('term')) {
2825
query = StateModel.find(this.argument('term'))
2926
} else {
3027
query = StateModel.query()
3128
}
3229

3330
return query.limit(limit).then(rows => {
34-
for(const row of rows) {
31+
for (const row of rows) {
3532
this.comment(row.name)
3633
}
3734
})
3835
}
39-
4036
}

app/Controllers/BaseController.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Controller } from 'grind-http'
22

33
export class BaseController extends Controller {
4-
54
db = null
65

76
constructor(app) {
@@ -19,7 +18,6 @@ export class BaseController extends Controller {
1918

2019
paginationRange(req, limit = 100) {
2120
const pagination = this.pagination(req, limit)
22-
return { start: pagination.offset, end: (pagination.limit + pagination.offset) - 1 }
21+
return { start: pagination.offset, end: pagination.limit + pagination.offset - 1 }
2322
}
24-
2523
}

app/Controllers/CompaniesController.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ import 'App/Controllers/BaseController'
33
import 'App/Models/CompanyModel'
44

55
export class CompaniesController extends BaseController {
6-
76
model = CompanyModel
87

98
index(req, res) {
109
const { start, end } = this.paginationRange(req)
1110

12-
return this.model.query().range(start, end).then(rows => {
13-
if(rows.isNil) {
14-
throw new NotFoundError('No companies found')
15-
}
11+
return this.model
12+
.query()
13+
.range(start, end)
14+
.then(rows => {
15+
if (rows.isNil) {
16+
throw new NotFoundError('No companies found')
17+
}
1618

17-
res.send(rows)
18-
})
19+
res.send(rows)
20+
})
1921
}
20-
2122
}

app/Controllers/CountriesController.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@ import 'App/Controllers/BaseController'
33
import 'App/Models/CountryModel'
44

55
export class CountriesController extends BaseController {
6-
76
model = CountryModel
87

98
index(req, res) {
109
const { start, end } = this.paginationRange(req)
1110

12-
return this.model.query().range(start, end).then(rows => {
13-
if(rows.isNil) {
14-
throw new NotFoundError('No countries found')
15-
}
11+
return this.model
12+
.query()
13+
.range(start, end)
14+
.then(rows => {
15+
if (rows.isNil) {
16+
throw new NotFoundError('No countries found')
17+
}
1618

17-
res.send(rows)
18-
})
19+
res.send(rows)
20+
})
1921
}
2022

2123
show(req, res) {
@@ -25,19 +27,21 @@ export class CountriesController extends BaseController {
2527
}
2628

2729
search(req, res) {
28-
if(req.query.term.isNil || req.query.term.length === 0) {
30+
if (req.query.term.isNil || req.query.term.length === 0) {
2931
throw new ValidationError({ term: 'term is required' })
3032
}
3133

3234
const { start, end } = this.paginationRange(req)
3335

34-
return this.model.find(req.query.term).range(start, end).then(rows => {
35-
if(rows.isNil) {
36-
throw new NotFoundError('No countries found, try a different term')
37-
}
36+
return this.model
37+
.find(req.query.term)
38+
.range(start, end)
39+
.then(rows => {
40+
if (rows.isNil) {
41+
throw new NotFoundError('No countries found, try a different term')
42+
}
3843

39-
res.send(rows)
40-
})
44+
res.send(rows)
45+
})
4146
}
42-
4347
}

app/Controllers/StatesController.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@ import 'App/Controllers/BaseController'
33
import 'App/Models/StateModel'
44

55
export class StatesController extends BaseController {
6-
76
model = StateModel
87

98
index(req, res) {
109
const { start, end } = this.paginationRange(req)
1110

12-
return this.model.query().range(start, end).then(rows => {
13-
if(rows.isNil) {
14-
throw new NotFoundError('No states found')
15-
}
11+
return this.model
12+
.query()
13+
.range(start, end)
14+
.then(rows => {
15+
if (rows.isNil) {
16+
throw new NotFoundError('No states found')
17+
}
1618

17-
res.send(rows)
18-
})
19+
res.send(rows)
20+
})
1921
}
2022

2123
show(req, res) {
@@ -25,19 +27,21 @@ export class StatesController extends BaseController {
2527
}
2628

2729
search(req, res) {
28-
if(req.query.term.isNil || req.query.term.length === 0) {
30+
if (req.query.term.isNil || req.query.term.length === 0) {
2931
throw new BadRequestError('`term` is required')
3032
}
3133

3234
const { start, end } = this.paginationRange(req)
3335

34-
return this.model.find(req.query.term).range(start, end).then(rows => {
35-
if(rows.isNil) {
36-
throw new NotFoundError('No states found, try a different term')
37-
}
36+
return this.model
37+
.find(req.query.term)
38+
.range(start, end)
39+
.then(rows => {
40+
if (rows.isNil) {
41+
throw new NotFoundError('No states found, try a different term')
42+
}
3843

39-
res.send(rows)
40-
})
44+
res.send(rows)
45+
})
4146
}
42-
4347
}

app/Models/CompanyModel.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,24 @@ import { Model } from 'grind-orm'
33
import 'App/Models/StateModel'
44

55
export class CompanyModel extends Model {
6-
76
static tableName = 'companies'
87
static eager = 'locations'
98

109
static jsonSchema = {
1110
type: 'object',
12-
required: [ 'name' ],
11+
required: ['name'],
1312

1413
properties: {
1514
id: { type: 'integer' },
1615
name: { type: 'string', maxLength: 255 },
1716
created_at: { type: 'datetime', format: 'date-time' },
18-
updated_at: { type: 'datetime', format: 'date-time' }
19-
}
17+
updated_at: { type: 'datetime', format: 'date-time' },
18+
},
2019
}
2120

2221
static buildRelations() {
2322
return {
24-
locations: this.belongsToMany(StateModel)
23+
locations: this.belongsToMany(StateModel),
2524
}
2625
}
27-
2826
}

app/Models/CountryModel.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@ import { Model } from 'grind-orm'
33
import 'App/Models/StateModel'
44

55
export class CountryModel extends Model {
6-
76
static tableName = 'countries'
87

98
static jsonSchema = {
109
type: 'object',
11-
required: [ 'name', 'abbreviation' ],
10+
required: ['name', 'abbreviation'],
1211

1312
properties: {
1413
id: { type: 'integer' },
1514
name: { type: 'string', maxLength: 64 },
16-
abbreviation: { type: 'string', maxLength: 2 }
17-
}
15+
abbreviation: { type: 'string', maxLength: 2 },
16+
},
1817
}
1918

2019
static find(term) {
21-
return this.query().where('name', 'like', `%${term}%`).orWhere('abbreviation', 'like', `%${term}%`)
20+
return this.query()
21+
.where('name', 'like', `%${term}%`)
22+
.orWhere('abbreviation', 'like', `%${term}%`)
2223
}
2324

2425
static findByAbbreviation(abbreviation) {
@@ -31,8 +32,7 @@ export class CountryModel extends Model {
3132

3233
static buildRelations() {
3334
return {
34-
states: this.hasMany(StateModel)
35+
states: this.hasMany(StateModel),
3536
}
3637
}
37-
3838
}

0 commit comments

Comments
 (0)