Skip to content

Commit

Permalink
Update to work with Deno 1.3.1
Browse files Browse the repository at this point in the history
Also add a migration for an Admin user
Also added the env file
  • Loading branch information
giggio committed Aug 23, 2020
1 parent 043ed21 commit a66ba95
Show file tree
Hide file tree
Showing 20 changed files with 67 additions and 45 deletions.
4 changes: 2 additions & 2 deletions .env.example → .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DB_NAME=deno_api_db
DB_HOST=db
DB_HOST=localhost
DB_PASS=example
DB_USER=root
ENV=dev
Expand All @@ -8,5 +8,5 @@ ENV=dev
JWT_ACCESS_TOKEN_EXP=600000
# Refresh token validity in ms
JWT_REFRESH_TOKEN_EXP=3600000
# Secret secuirity string
# Secret security string
JWT_TOKEN_SECRET=HEGbulKGDblAFYskBLml
13 changes: 13 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"debug.javascript.usePreview": false,
"deno.enable": true,
"deno.unstable": true,
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
},
"vsicons.presets.angular": false,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ We can run the project **with/ without Docker**.
- docker-compose installed.
- To run API server without Docker we need
- MySQL server running &
- You can run it with Docker by running: `docker run --name deno-sample-mysql -e MYSQL_ROOT_PASSWORD=example -e MYSQL_DATABASE=deno_api_db -d -p 3306:3306 mysql --default-authentication-plugin=mysql_native_password`
- Deno run time installed
- **Configuration**
- In application root, rename example env file `env.example` to `.env`.
Expand All @@ -62,26 +63,26 @@ We use [nessie](https://deno.land/x/nessie) to manage database migration.
- In the application root, we have `nessie.config.ts`. Make sure to update this with the DB credentials.
- Run the following command to run the migration. Migration should create necessary tables and dump the data.
```
$ deno run --allow-net --allow-read --allow-write https://deno.land/x/nessie@v1.0.0-rc3/cli.ts migrate
$ deno run --allow-net --allow-read --allow-write https://deno.land/x/nessie@v1.0.4/cli.ts migrate
```

With this, the user table would be created and the table would be seeded with fake data

- Further, to add new migration, for example, to create new product table run
```
deno run --allow-net --allow-read --allow-write https://deno.land/x/nessie@v1.0.0-rc3/cli.ts make create_product
deno run --allow-net --allow-read --allow-write https://deno.land/x/nessie@v1.0.4/cli.ts make create_product
```

## Modules


| Package | Purpose |
| ---------|---------|
|[oak@v5.0.0](https://deno.land/x/oak@v5.0.0)| Deno middleware framework|
|[oak@v6.0.2](https://deno.land/x/oak@v6.0.2)| Deno middleware framework|
|[dotenv@v0.4.2](https://deno.land/x/dotenv@v0.4.2)| Read env variables|
|[mysql@2.2.0](https://deno.land/x/mysql@2.2.0)|MySQL driver for Deno|
|[nessie@v1.0.0-rc3](https://deno.land/x/nessie@v1.0.0-rc3)| DB migration tool for Deno|
|[validasaur@v0.7.0](https://deno.land/x/validasaur@v0.7.0)| validation library|
|[nessie@v1.0.4](https://deno.land/x/nessie@v1.0.4)| DB migration tool for Deno|
|[validasaur@v0.14.0](https://deno.land/x/validasaur@v0.14.0)| validation library|
|[djwt@v0.9.0](https://deno.land/x/djwt@v0.9.0)| JWT token encoding|
|[bcrypt@v0.2.1](https://deno.land/x/bcrypt@v0.2.1)| bcrypt encription lib|

Expand Down Expand Up @@ -143,14 +144,14 @@ deno run --allow-net --allow-read --allow-write https://deno.land/x/nessie@v1.0.
```

## How to validate request body
- Here we used [validasaur@v0.7.0](https://deno.land/x/validasaur@v0.7.0) module for validating forms or request body. List of available rules can be found [here](https://deno.land/x/validasaur@v0.7.0/#available-rules)
- Here we used [validasaur@v0.14.0](https://deno.land/x/validasaur@v0.14.0) module for validating forms or request body. List of available rules can be found [here](https://deno.land/x/validasaur@v0.14.0/#available-rules)
- [requestValidator](./middlewares/request-validator.middleware.ts) middleware added to validate the request body.
```
//auth.routes.ts
import {
required,
isEmail,
} from "https://deno.land/x/validasaur@v0.7.0/src/rules.ts";
} from "https://deno.land/x/validasaur@v0.14.0/src/rules.ts";
import { requestValidator } from "./../middlewares/request-validator.middleware.ts";
Expand Down
2 changes: 1 addition & 1 deletion app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Application } from "https://deno.land/x/oak@v5.0.0/mod.ts";
import { Application } from "https://deno.land/x/oak@v6.0.2/mod.ts";
import * as middlewares from "./middlewares/middlewares.ts";
import { oakCors } from "https://deno.land/x/cors/mod.ts";
import { router } from "./routes/routes.ts";
Expand Down
2 changes: 1 addition & 1 deletion db/db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client } from "https://deno.land/x/mysql@2.2.0/mod.ts";
import { Client } from "https://deno.land/x/mysql@v2.4.0/mod.ts";
import { config } from "./../config/config.ts";

const port = config.DB_PORT ? parseInt(config.DB_PORT || "") : undefined;
Expand Down
4 changes: 2 additions & 2 deletions db/migrations/1591637345959-create-user.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Migration,
} from "https://deno.land/x/nessie@v1.0.0-rc3/mod.ts";
import { Schema } from "https://deno.land/x/nessie@v1.0.0-rc3/qb.ts";
} from "https://deno.land/x/nessie@v1.0.4/mod.ts";
import { Schema } from "https://deno.land/x/nessie@v1.0.4/qb.ts";

/** Runs on migrate */
export const up: Migration<Schema> = ({ queryBuilder }) => {
Expand Down
3 changes: 1 addition & 2 deletions db/migrations/1591638037132-update-user-email.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Migration } from "https://deno.land/x/nessie@v1.0.0-rc3/mod.ts";
import { Schema } from "https://deno.land/x/nessie@v1.0.0-rc3/qb.ts";
import { Migration } from "https://deno.land/x/nessie@v1.0.4/mod.ts";

/** Runs on migrate */
export const up: Migration = () => {
Expand Down
2 changes: 1 addition & 1 deletion db/migrations/1591773649203-update-user-password.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Migration } from "https://deno.land/x/nessie@v1.0.0-rc3/mod.ts";
import { Migration } from "https://deno.land/x/nessie@v1.0.4/mod.ts";

/** Runs on migrate */
export const up: Migration = () => {
Expand Down
2 changes: 1 addition & 1 deletion db/migrations/1592058407065-update-user-roles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Migration } from "https://deno.land/x/nessie@v1.0.0-rc3/mod.ts";
import { Migration } from "https://deno.land/x/nessie@v1.0.4/mod.ts";

/** Runs on migrate */
export const up: Migration = () => {
Expand Down
8 changes: 8 additions & 0 deletions db/seeds/create-admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Seed } from "https://deno.land/x/nessie@v1.0.4/mod.ts";
import * as encription from "../../helpers/encription.ts";

export const run: Seed = async () => {
const password = await encription.encript("123456");
return `INSERT into users VALUES
(DEFAULT, 'João da Silva', 'joaodasilva@example.com', 'Admin', '${password}', 1, DEFAULT, DEFAULT)`;
};
2 changes: 1 addition & 1 deletion middlewares/error.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
isHttpError,
Status,
} from "https://deno.land/x/oak@v5.0.0/mod.ts";
} from "https://deno.land/x/oak@v6.0.2/mod.ts";
import { config } from "./../config/config.ts";
import { Context } from "./../types.ts";

Expand Down
23 changes: 12 additions & 11 deletions middlewares/request-validator.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
validate,
ValidationErrors,
ValidationRules,
} from "https://deno.land/x/validasaur@v0.7.0/src/mod.ts";
import { httpErrors } from "https://deno.land/x/oak@v5.0.0/mod.ts";
} from "https://deno.land/x/validasaur@v0.14.0/src/interfaces.ts";
import { validate } from "https://deno.land/x/validasaur@v0.14.0/src/validate.ts";
import { httpErrors } from "https://deno.land/x/oak@v6.0.2/mod.ts";
import { Context } from "./../types.ts";

/**
Expand All @@ -28,14 +28,15 @@ const requestValidator = ({ bodyRules }: { bodyRules: ValidationRules }) => {
return async (ctx: Context, next: () => Promise<void>) => {
/** get request body */
const request = ctx.request;
const body = (await request.body()).value;

/** check rules */
const [isValid, errors] = await validate(body, bodyRules);
if (!isValid) {
/** if error found, throw bad request error */
const message = getErrorMessage(errors);
throw new httpErrors.BadRequest(message);
const body = await request.body().value;
if (body != null) {
/** check rules */
const [isValid, errors] = await validate(body, bodyRules);
if (!isValid) {
/** if error found, throw bad request error */
const message = getErrorMessage(errors);
throw new httpErrors.BadRequest(message);
}
}

await next();
Expand Down
2 changes: 1 addition & 1 deletion middlewares/user-guard.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { httpErrors } from "https://deno.land/x/oak@v5.0.0/mod.ts";
import { httpErrors } from "https://deno.land/x/oak@v6.0.2/mod.ts";
import { Context, UserRole } from "./../types.ts";
import { hasUserRole } from "../helpers/roles.ts";

Expand Down
8 changes: 4 additions & 4 deletions nessie.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ClientPostgreSQL } from "https://deno.land/x/nessie@v1.0.0-rc3/mod.ts";
import { ClientMySQL } from "https://deno.land/x/nessie@v1.0.0-rc3/mod.ts";
import { ClientSQLite } from "https://deno.land/x/nessie@v1.0.0-rc3/mod.ts";
import { ClientPostgreSQL } from "https://deno.land/x/nessie@v1.0.4/mod.ts";
import { ClientMySQL } from "https://deno.land/x/nessie@v1.0.4/mod.ts";
import { ClientSQLite } from "https://deno.land/x/nessie@v1.0.4/mod.ts";

/** These are the default config options. */
const clientOptions = {
Expand All @@ -12,7 +12,7 @@ const clientMySql = new ClientMySQL(clientOptions, {
hostname: "localhost",
port: 3306,
username: "root",
password: "example", // uncomment this line for <8
password: "example",
db: "deno_api_db",
});

Expand Down
8 changes: 4 additions & 4 deletions routes/auth.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
required,
isEmail,
lengthBetween,
} from "https://deno.land/x/validasaur@v0.7.0/src/rules.ts";
} from "https://deno.land/x/validasaur@v0.14.0/src/rules.ts";

import * as authService from "./../services/auth.service.ts";
import { requestValidator } from "./../middlewares/request-validator.middleware.ts";
Expand All @@ -34,7 +34,7 @@ const register = [
/** router handler */
async (ctx: Context) => {
const request = ctx.request;
const userData = (await request.body()).value as CreateUser;
const userData = await request.body().value as CreateUser;
const user = await authService.registerUser(userData);
ctx.response.body = user;
},
Expand All @@ -55,7 +55,7 @@ const login = [
/** router handler */
async (ctx: Context) => {
const request = ctx.request;
const credential = (await request.body()).value as LoginCredential;
const credential = await request.body().value as LoginCredential;
const token = await authService.loginUser(credential);
ctx.response.body = token;
},
Expand All @@ -70,7 +70,7 @@ const refreshToken = [
/** router handler */
async (ctx: Context) => {
const request = ctx.request;
const data = (await request.body()).value as RefreshToken;
const data = await request.body().value as RefreshToken;

const token = await authService.refreshToken(
data["refresh_token"],
Expand Down
2 changes: 1 addition & 1 deletion routes/routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Router } from "https://deno.land/x/oak@v5.0.0/mod.ts";
import { Router } from "https://deno.land/x/oak@v6.0.2/mod.ts";
import { Context } from "./../types.ts";

import * as authRoutes from "./auth.routes.ts";
Expand Down
6 changes: 3 additions & 3 deletions routes/user.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import {
helpers,
Status,
httpErrors,
} from "https://deno.land/x/oak@v5.0.0/mod.ts";
} from "https://deno.land/x/oak@v6.0.2/mod.ts";
import {
required,
isEmail,
} from "https://deno.land/x/validasaur@v0.7.0/src/rules.ts";
} from "https://deno.land/x/validasaur@v0.14.0/src/rules.ts";
import * as userService from "./../services/user.service.ts";
import { requestValidator, userGuard } from "./../middlewares/middlewares.ts";
import { Context, UserRole } from "./../types.ts";
Expand Down Expand Up @@ -63,7 +63,7 @@ const updateUser = [
if (authUser) {
if (id === authUser.id || hasUserRole(authUser, UserRole.ADMIN)) {
const request = ctx.request;
const userData = (await request.body()).value;
const userData = await request.body().value;
const user = await userService.updateUser(+id, userData);
ctx.response.body = user;
return;
Expand Down
2 changes: 1 addition & 1 deletion services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as userRepo from "./../repositories/user.repository.ts";
import { httpErrors } from "https://deno.land/x/oak@v5.0.0/mod.ts";
import { httpErrors } from "https://deno.land/x/oak@v6.0.2/mod.ts";
import * as encription from "../helpers/encription.ts";
import * as jwt from "../helpers/jwt.ts";
import {
Expand Down
2 changes: 1 addition & 1 deletion services/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as userRepo from "./../repositories/user.repository.ts";
import { httpErrors } from "https://deno.land/x/oak@v5.0.0/mod.ts";
import { httpErrors } from "https://deno.land/x/oak@v6.0.2/mod.ts";
import { encript } from "../helpers/encription.ts";

/**
Expand Down
2 changes: 1 addition & 1 deletion types/core/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context as OakContext } from "https://deno.land/x/oak@v5.0.0/mod.ts";
import { Context as OakContext } from "https://deno.land/x/oak@v6.0.2/mod.ts";
import { AuthUser } from "./../auth/auth-user.ts";

/**
Expand Down

0 comments on commit a66ba95

Please sign in to comment.