Skip to content

Commit

Permalink
update examples to use objection 3
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Nov 9, 2021
1 parent 4c80295 commit 2dda083
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ node_modules
scratchpad.js
testCoverage
examples/*/*.db
examples/express-es7/build
examples/*/dist
examples/**/package-lock.json
*.iml
*.DS_Store
Expand Down
6 changes: 5 additions & 1 deletion examples/koa-ts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default (router: KoaRouter) => {
}

if (ctx.query.orderBy) {
query.orderBy(ctx.query.orderBy)
query.orderBy(takeFirst(ctx.query.orderBy))
}

if (ctx.query.withPetCount) {
Expand Down Expand Up @@ -239,3 +239,7 @@ export default (router: KoaRouter) => {
ctx.body = actors
})
}

function takeFirst<T>(item: T | ReadonlyArray<T>): T {
return Array.isArray(item) ? item[0] : item
}
2 changes: 1 addition & 1 deletion examples/koa-ts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ app.listen(port, () => {
async function errorHandler(ctx: Context, next: () => Promise<any>) {
try {
await next()
} catch (err) {
} catch (err: any) {
if (err instanceof ValidationError) {
ctx.status = 400
ctx.body = {
Expand Down
6 changes: 5 additions & 1 deletion examples/koa-ts/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ const req = axios.create({
await addPersonToMovieAsActor(departed, matt)
await removePersonFromMovie(departed, matt)
})().catch((err) => {
console.error('error:', err.response.status, err.response.data)
if (err.response) {
console.error('error:', err.response.status, err.response.data)
} else {
console.error('error:', err)
}
})

async function inserPersonWithRelations() {
Expand Down
17 changes: 8 additions & 9 deletions examples/koa-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "objection-example-koa",
"version": "2.0.0",
"description": "Objection.js koa example",
"name": "objection-example-koa-ts",
"version": "3.0.0",
"description": "Objection.js koa typescript example",
"main": "app.js",
"scripts": {
"migrate": "knex migrate:latest",
"start": "npm run migrate && ts-node app"
"start": "npm run migrate && rm -rf dist && tsc && node dist/app.js"
},
"engines": {
"node": ">=8.0.0"
Expand All @@ -14,18 +14,17 @@
"license": "MIT",
"dependencies": {
"axios": "^0.19.0",
"knex": "^0.20.3",
"knex": "^0.95.13",
"koa": "^2.11.0",
"koa-bodyparser": "^4.2.1",
"koa-router": "^7.4.0",
"objection": "^2.1.0",
"sqlite3": "^4.1.0"
"objection": "^3.0.0-rc.4",
"sqlite3": "^5.0.2"
},
"devDependencies": {
"@types/koa": "^2.11.0",
"@types/koa-bodyparser": "^4.3.0",
"@types/koa-router": "^7.0.42",
"ts-node": "^8.5.4",
"typescript": "3.7.2"
"typescript": "4.4.4"
}
}
2 changes: 1 addition & 1 deletion examples/koa-ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"allowJs": true,
"target": "esnext",
"rootDir": "./",
"outDir": "lib"
"outDir": "dist"
}
}
8 changes: 4 additions & 4 deletions examples/koa/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "objection-example-koa",
"version": "2.0.0",
"version": "3.0.0",
"description": "Objection.js koa example",
"main": "app.js",
"scripts": {
Expand All @@ -14,11 +14,11 @@
"license": "MIT",
"dependencies": {
"axios": "^0.19.0",
"knex": "^0.20.3",
"knex": "^0.95.13",
"koa": "^2.11.0",
"koa-bodyparser": "^4.2.1",
"koa-router": "^7.4.0",
"objection": "^2.1.0",
"sqlite3": "^4.1.0"
"objection": "3.0.0-rc.4",
"sqlite3": "^5.0.2"
}
}

0 comments on commit 2dda083

Please sign in to comment.