Skip to content

Commit 8472a23

Browse files
committed
sqlite
1 parent 059ea5d commit 8472a23

File tree

13 files changed

+2549
-145
lines changed

13 files changed

+2549
-145
lines changed

chinook.db

Whitespace-only changes.

database.db

Whitespace-only changes.

lessons/06-server/lecture/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// import './app.ts'
1+
// import './app'
22
import * as http from 'http'
33

44
const hostname = '127.0.0.1'
8 KB
Binary file not shown.

lessons/06-server/practice/db.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
// Okay you caught us, it was all fake. But we can't really expect you
2-
// to setup local databases on your machine specifically for a workshop.
3-
// If you did use a third party database driver for an SQL-based
4-
// database, it would probably work a little like this:
5-
// db.query('SELECT ...').then(results => {})
1+
import path from 'path'
2+
import sqlite3 from 'sqlite3'
63

7-
const data = [
8-
{ id: 1, name: 'Michael Jackson' },
9-
{ id: 2, name: 'Ryan Florence' },
10-
{ id: 3, name: 'Brad Westfall' },
11-
{ id: 4, name: 'Chance Stickland' },
12-
]
4+
const db = new sqlite3.Database(path.resolve(__dirname, 'database.db'))
135

14-
export const db = {
15-
query: (sql: string) => {
16-
if (!sql.startsWith('SELECT')) return Promise.reject('Invalid SQL, must start with SELECT')
17-
18-
const results = data.filter((row) => {
19-
const formatted = sql.toLowerCase().replace(/(`|')/g, '')
20-
if (formatted === 'select * from user') return true
21-
const matches = formatted.match(/id = (\d+)/)
22-
return matches ? row.id === parseInt(matches[1], 10) : false
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+
}
2314
})
24-
25-
return Promise.resolve(results)
26-
},
15+
})
2716
}

lessons/06-server/practice/index.final.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express from 'express'
2-
import { db } from './db'
2+
import { query } from './db'
3+
34
const app = express()
45
const port = 3000
56

@@ -8,7 +9,7 @@ app.get('/', (req, res) => {
89
})
910

1011
app.get('/users', (req, res, next) => {
11-
db.query('SELECT * FROM user')
12+
query('SELECT * FROM user')
1213
.then((results) => {
1314
res.json(results)
1415
})
@@ -21,8 +22,8 @@ app.get('/users/:id', (req, res, next) => {
2122
next()
2223
return
2324
}
24-
db.query(`SELECT * FROM user WHERE user.id = '${id}'`)
25-
.then((results) => {
25+
query(`SELECT * FROM user WHERE user.id = '${id}'`)
26+
.then((results: any) => {
2627
if (results.length === 0) {
2728
next()
2829
return

lessons/06-server/practice/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express from 'express'
2-
import { db } from './db'
2+
import { query } from './db'
3+
// import './index.final'
34

45
const app = express()
56
const port = 3000

nodemon.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"watch": ["lessons"],
3+
"ext": "ts,tsx,json,csv"
4+
}

0 commit comments

Comments
 (0)