Skip to content

Commit 15c61a0

Browse files
committed
setting up with new sqlite database
1 parent 8472a23 commit 15c61a0

File tree

14 files changed

+300
-64
lines changed

14 files changed

+300
-64
lines changed

chinook.db

Whitespace-only changes.

database.db

8 KB
Binary file not shown.
-8 KB
Binary file not shown.

lessons/06-server/practice/db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path'
22
import sqlite3 from 'sqlite3'
33

4-
const db = new sqlite3.Database(path.resolve(__dirname, 'database.db'))
4+
const db = new sqlite3.Database(path.resolve(process.cwd(), 'database.db'))
55

66
export function query(sql: string) {
77
return new Promise((resolve, reject) => {

lessons/07-middleware/practice/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express from 'express'
22
import { getAuthUser } from './auth'
3+
// import './index.final'
34

45
const app = express()
56
const port = 3000
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
const data = [{ id: 3, name: 'Fake User', email: 'hello@reacttraining.com' }]
1+
import path from 'path'
2+
import sqlite3 from 'sqlite3'
23

3-
export const db = {
4-
query: (sql: string) => {
5-
if (
6-
sql === "SELECT * FROM user WHERE user.id = '3'" ||
7-
sql ===
8-
"SELECT * FROM user WHERE user.email = 'hello@reacttraining.com' AND user.password = 'abc123'"
9-
) {
10-
return Promise.resolve(data)
11-
} else {
12-
// Empty array signifies SQL found nothing. Usually databases won't
13-
// throw errors if you run an SQL statement that finds nothing, they
14-
// just give you no results
15-
return Promise.resolve([])
16-
}
17-
},
4+
const db = new sqlite3.Database(path.resolve(process.cwd(), 'database.db'))
5+
6+
export function query(sql: string) {
7+
return new Promise((resolve, reject) => {
8+
db.all(sql, (err, rows) => {
9+
if (err) {
10+
reject(err.message)
11+
} else {
12+
resolve(rows || [])
13+
}
14+
})
15+
})
1816
}

lessons/08-authentication/lecture/router.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from 'express'
22
import createError from 'http-errors'
3-
import { db } from './db'
3+
import { query } from './db'
44
import { jwtStartSession, jwtVerify, jwtLogout } from './jwt'
55
export const router = express.Router()
66

@@ -16,8 +16,8 @@ const requireAuthentication = (
1616
jwtVerify(req, res)
1717
.then((userId) => {
1818
if (userId) {
19-
db.query(`SELECT * FROM user WHERE user.id = '${userId}'`)
20-
.then((results) => {
19+
query(`SELECT id, name FROM user WHERE user.id = '${userId}'`)
20+
.then((results: any) => {
2121
if (results.length === 0) return Promise.reject()
2222
const user = results[0]
2323
req.user = user
@@ -49,8 +49,8 @@ router.get('/login', (req, res, next) => {
4949
const email = 'hello@reacttraining.com'
5050
const password = 'abc123'
5151

52-
db.query(`SELECT * FROM user WHERE user.email = '${email}' AND user.password = '${password}'`)
53-
.then((results) => {
52+
query(`SELECT id FROM user WHERE user.email = '${email}' AND user.password = '${password}'`)
53+
.then((results: any) => {
5454
if (!results || results.length === 0) {
5555
return Promise.reject(createError(401, '<h1>Login Failed</h1>'))
5656
}
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
const data = [{ id: 3, name: 'Fake User', email: 'hello@reacttraining.com' }]
1+
import path from 'path'
2+
import sqlite3 from 'sqlite3'
23

3-
export const db = {
4-
query: (sql: string) => {
5-
if (
6-
sql === "SELECT * FROM user WHERE user.id = '3'" ||
7-
sql ===
8-
"SELECT * FROM user WHERE user.email = 'hello@reacttraining.com' AND user.password = 'abc123'"
9-
) {
10-
return Promise.resolve(data)
11-
} else {
12-
// Empty array signifies SQL found nothing. Usually databases won't
13-
// throw errors if you run an SQL statement that finds nothing, they
14-
// just give you no results
15-
return Promise.resolve([])
16-
}
17-
},
4+
const db = new sqlite3.Database(path.resolve(process.cwd(), 'database.db'))
5+
6+
export function query(sql: string) {
7+
return new Promise((resolve, reject) => {
8+
db.all(sql, (err, rows) => {
9+
if (err) {
10+
reject(err.message)
11+
} else {
12+
resolve(rows || [])
13+
}
14+
})
15+
})
1816
}

lessons/08-authentication/practice/router.final.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from 'express'
22
import createError from 'http-errors'
3-
import { db } from './db'
3+
import { query } from './db'
44
import { jwtStartSession, jwtVerify, jwtLogout } from './jwt'
55
export const router = express.Router()
66

@@ -16,8 +16,8 @@ const requireAuthentication = (
1616
jwtVerify(req, res)
1717
.then((userId) => {
1818
if (userId) {
19-
db.query(`SELECT * FROM user WHERE user.id = '${userId}'`)
20-
.then((results) => {
19+
query(`SELECT * FROM user WHERE user.id = '${userId}'`)
20+
.then((results: any) => {
2121
if (results.length === 0) return Promise.reject()
2222
const user = results[0]
2323
req.user = user
@@ -53,8 +53,8 @@ router.get('/login', (req, res, next) => {
5353
const email = 'hello@reacttraining.com'
5454
const password = 'abc123'
5555

56-
db.query(`SELECT * FROM user WHERE user.email = '${email}' AND user.password = '${password}'`)
57-
.then((results) => {
56+
query(`SELECT * FROM user WHERE user.email = '${email}' AND user.password = '${password}'`)
57+
.then((results: any) => {
5858
if (results.length === 0) {
5959
return Promise.reject(createError(401, '<h1>Login Failed</h1>'))
6060
}

lessons/08-authentication/practice/router.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from 'express'
22
import createError from 'http-errors'
3-
import { db } from './db'
3+
import { query } from './db'
44
import { jwtStartSession, jwtVerify, jwtLogout } from './jwt'
55
export const router = express.Router()
66

@@ -16,8 +16,8 @@ const requireAuthentication = (
1616
jwtVerify(req, res)
1717
.then((userId) => {
1818
if (userId) {
19-
db.query(`SELECT * FROM user WHERE user.id = '${userId}'`)
20-
.then((results) => {
19+
query(`SELECT * FROM user WHERE user.id = '${userId}'`)
20+
.then((results: any) => {
2121
if (results.length === 0) return Promise.reject()
2222
const user = results[0]
2323
req.user = user
@@ -49,8 +49,8 @@ router.get('/login', (req, res, next) => {
4949
const email = 'hello@reacttraining.com'
5050
const password = 'abc123'
5151

52-
db.query(`SELECT * FROM user WHERE user.email = '${email}' AND user.password = '${password}'`)
53-
.then((results) => {
52+
query(`SELECT * FROM user WHERE user.email = '${email}' AND user.password = '${password}'`)
53+
.then((results: any) => {
5454
if (results.length === 0) {
5555
return Promise.reject(createError(401, '<h1>Login Failed</h1>'))
5656
}

0 commit comments

Comments
 (0)