-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added a express/knex example application
- Loading branch information
Showing
31 changed files
with
1,231 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2018, Rob McLarty (http://robmclarty.com) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included | ||
in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Knex/Express Example Application | ||
|
||
This is an example app that uses the [New Relic hybrid agent](https://github.com/newrelic/newrelic-node-opentelemetry-integration) to demonstrate how to create both New Relic and OpenTelemetry spans. | ||
|
||
|
||
## Setup | ||
**Note**: This is used to demonstrate behavior between the New Relic Node.js agent and hybrid agent. The hybrid agent is currently only available to New Relic employees. | ||
|
||
### Get hybrid agent | ||
|
||
```sh | ||
git clone [email protected]:newrelic/newrelic-node-opentelemetry-integration.git | ||
npm install | ||
npm link | ||
``` | ||
|
||
```sh | ||
npm install | ||
cp env.sample .env | ||
# Fill out `NEW_RELIC_LICENSE_KEY` | ||
npm link @newrelic/opentelemetry-integration | ||
docker compose up -d | ||
npm run db:migrate | ||
npm run db:seed | ||
npm start | ||
``` | ||
|
||
## Exploring Telemetry | ||
More to come later... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict' | ||
|
||
const env = process.env.NODE_ENV || 'development' | ||
const knexfile = require('../knexfile') | ||
debugger | ||
const knex = require('knex')(knexfile[env]) | ||
|
||
module.exports = knex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
exports.up = knex => { | ||
return knex.schema.createTable('users', t => { | ||
t.increments('id').primary().unsigned() | ||
t.string('username').unique().index() | ||
t.string('password') | ||
t.string('email').unique().index() | ||
t.timestamp('created_at').defaultTo(knex.fn.now()) | ||
t.timestamp('updated_at').defaultTo(knex.fn.now()) | ||
}) | ||
} | ||
|
||
exports.down = knex => { | ||
return knex.schema.dropTable('users') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
exports.up = knex => { | ||
return knex.schema.createTable('projects', t => { | ||
t.increments('id').primary().unsigned() | ||
t.integer('user_id').references('users.id').unsigned().index().onDelete('CASCADE') | ||
t.string('name') | ||
t.text('description') | ||
t.timestamp('completed_at') | ||
t.timestamp('created_at').defaultTo(knex.fn.now()) | ||
t.timestamp('updated_at').defaultTo(knex.fn.now()) | ||
}) | ||
} | ||
|
||
exports.down = knex => { | ||
return knex.schema.dropTable('projects') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict' | ||
|
||
const { User } = require('../../server/models') | ||
|
||
exports.seed = knex => knex(User.tableName).del() | ||
.then(() => [ | ||
{ | ||
username: 'admin', | ||
password: 'password', | ||
email: '[email protected]' | ||
}, | ||
{ | ||
username: 'first-user', | ||
password: 'another-password', | ||
email: '[email protected]' | ||
} | ||
]) | ||
.then(newUsers => Promise.all(newUsers.map(user => User.create(user)))) | ||
.catch(err => console.log('err: ', err)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict' | ||
|
||
const { User, Project } = require('../../server/models') | ||
|
||
exports.seed = knex => knex(Project.tableName).del() | ||
.then(() => User.findAll()) | ||
.then(users => { | ||
if (users.length <= 0) throw 'No users found' | ||
|
||
return users[0].id | ||
}) | ||
.then(userId => [ | ||
{ | ||
user_id: userId, | ||
name: 'Sample Project', | ||
description: 'This is just a sample project to create some data to look at.' | ||
}, | ||
{ | ||
user_id: userId, | ||
name: 'Another Project', | ||
description: 'This is another project of sample data.', | ||
completed_at: knex.fn.now() | ||
} | ||
]) | ||
.then(newProjects => Promise.all(newProjects.map(project => Project.create(project)))) | ||
.catch(err => console.log('err: ', err)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
version: '3' | ||
|
||
services: | ||
db: | ||
container_name: pg_knex | ||
image: postgres:15 | ||
ports: | ||
- "5436:5436" | ||
environment: | ||
PGPORT: 5436 | ||
POSTGRES_PASSWORD: newrelic | ||
POSTGRES_DB: knex | ||
healthcheck: | ||
test: ["CMD", "pg_isready"] | ||
interval: 1s | ||
timeout: 10s | ||
retries: 30 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Knex Express Project Sample Documentation | ||
|
||
## Usage | ||
|
||
- [Auth](./usage/auth.md) | ||
- [Users](./usage/users.md) | ||
- [Projects](./usage/projects.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Authentication | ||
|
||
This being a sample application, this is by no means a proper authentication | ||
implementation and is only meant to demonstrate some very basic concepts in | ||
the context of knex and express. | ||
|
||
## Login | ||
|
||
### Request | ||
|
||
```shell | ||
curl \ | ||
-X POST \ | ||
-H "Content-Type: application/json" \ | ||
-d '{"username":"admin", "password":"password"}' \ | ||
http://localhost:3000/login | ||
``` | ||
|
||
### Response | ||
|
||
```json | ||
{ | ||
"ok": true, | ||
"message": "Login successful", | ||
"user": { | ||
"id": 1, | ||
"username": "admin", | ||
"email": "[email protected]", | ||
"created_at": "2018-04-03 14:43:02.183277-04", | ||
"updated_at": "2018-04-03 14:43:02.183277-04" | ||
} | ||
} | ||
``` | ||
|
||
## Register | ||
|
||
```shell | ||
curl \ | ||
-X POST \ | ||
-H "Content-Type: application/json" \ | ||
-d '{"username":"new-user", "password":"my-password", "email":"[email protected]"}' \ | ||
http://localhost:3000/register | ||
``` | ||
|
||
### Response | ||
|
||
```json | ||
{ | ||
"ok": true, | ||
"message": "Registration successful", | ||
"user": { | ||
"id": 2, | ||
"username": "new-user", | ||
"email": "[email protected]", | ||
"created_at": "2018-04-03 14:43:02.183277-04", | ||
"updated_at": "2018-04-03 14:43:02.183277-04" | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# Projects | ||
|
||
Basic CRUD for interacting with the JSON API's project resources. | ||
|
||
## Create | ||
|
||
### Request | ||
|
||
```shell | ||
curl \ | ||
-X POST \ | ||
-H "Content-Type: application/json" \ | ||
-d '{"name":"my-project", "description":"This is my description of this project."}' \ | ||
http://localhost:3000/users/1/projects | ||
``` | ||
|
||
### Response | ||
|
||
```json | ||
{ | ||
"ok": true, | ||
"message": "Project created", | ||
"project": { | ||
"id": 1, | ||
"name": "my-project", | ||
"description": "This is my description of this project.", | ||
"completed_at": null, | ||
"created_at": "2018-04-03 14:43:02.183277-04", | ||
"updated_at": "2018-04-03 14:43:02.183277-04" | ||
} | ||
} | ||
``` | ||
|
||
## List | ||
|
||
### Request | ||
|
||
```shell | ||
curl \ | ||
-X GET \ | ||
http://localhost:3000/users/1/projects | ||
``` | ||
|
||
### Response | ||
|
||
```json | ||
{ | ||
"ok": true, | ||
"message": "Projects found", | ||
"projects": [ | ||
{ | ||
"id": 1, | ||
"name": "my-project", | ||
"description": "This is my description of this project.", | ||
"completed_at": null, | ||
"created_at": "2018-04-03 14:43:02.183277-04", | ||
"updated_at": "2018-04-03 14:43:02.183277-04" | ||
}, | ||
{ | ||
"id": 2, | ||
"name": "my-other-project", | ||
"description": "This is my other project description.", | ||
"completed_at": "2018-04-03 14:43:02.183277-04", | ||
"created_at": "2018-04-03 14:43:02.183277-04", | ||
"updated_at": "2018-04-03 14:43:02.183277-04" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## Get | ||
|
||
### Request | ||
|
||
```shell | ||
curl \ | ||
-X GET \ | ||
http://localhost:3000/projects/1 | ||
``` | ||
|
||
### Response | ||
|
||
```json | ||
{ | ||
"ok": true, | ||
"message": "Project found", | ||
"project": { | ||
"id": 1, | ||
"name": "my-project", | ||
"description": "This is my description of this project.", | ||
"completed_at": null, | ||
"created_at": "2018-04-03 14:43:02.183277-04", | ||
"updated_at": "2018-04-03 14:43:02.183277-04" | ||
} | ||
} | ||
``` | ||
|
||
## Update | ||
|
||
### Request | ||
|
||
```shell | ||
curl \ | ||
-X PUT \ | ||
-H "Content-Type: application/json" \ | ||
-d '{"name":"my-new-name", "completed_at":"2018-04-03 14:43:02"}' \ | ||
http://localhost:3000/projects/1 | ||
``` | ||
|
||
### Response | ||
|
||
```json | ||
{ | ||
"ok": true, | ||
"message": "Project updated", | ||
"project": { | ||
"id": 1, | ||
"name": "my-new-name", | ||
"description": "This is my description of this project.", | ||
"completed_at": "2018-04-03 14:43:02.183277-04", | ||
"created_at": "2018-04-03 14:43:02.183277-04", | ||
"updated_at": "2018-04-03 14:43:02.183277-04" | ||
} | ||
} | ||
``` | ||
|
||
## Delete | ||
|
||
### Request | ||
|
||
```shell | ||
curl \ | ||
-X DELETE \ | ||
http://localhost:3000/projects/2 | ||
``` | ||
|
||
### Response | ||
|
||
```json | ||
{ | ||
"ok": true, | ||
"message": "Project '2' deleted", | ||
"deleteCount": 1 | ||
} | ||
``` |
Oops, something went wrong.