├── README.md ├── complete-project ├── .vscode │ ├── launch.json │ └── settings.json ├── db.sqlite ├── package-lock.json ├── package.json └── src │ ├── db.js │ └── index.js └── preclass ├── .nvmrc ├── .vscode └── launch.json ├── db.sqlite ├── package-lock.json ├── package.json └── src ├── db.js └── index.js /README.md: -------------------------------------------------------------------------------- 1 | Hi this is the repo containing code examples and challenges of my video on [Using SQLite natively on Node.js (pt-br)](https://youtu.be/-eklEr3Adr4?si=gl3Z82ZISGESRXld). 2 | 3 | **First leave your star in the repo 🌟** 4 | 5 | ![Node](https://github.com/user-attachments/assets/fc15792d-7087-486b-8ad3-7a969070e82c) 6 | -------------------------------------------------------------------------------- /complete-project/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Launch via NPM", 5 | "request": "launch", 6 | "runtimeArgs": [ 7 | "run", 8 | "dev" 9 | ], 10 | "runtimeExecutable": "npm", 11 | "skipFiles": [ 12 | "/**" 13 | ], 14 | "type": "node", 15 | "console": "integratedTerminal" 16 | } 17 | ] 18 | } -------------------------------------------------------------------------------- /complete-project/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "window.zoomLevel": 4 3 | } -------------------------------------------------------------------------------- /complete-project/db.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ErickWendel/nodejs-native-sqlite/5df6da5505aaa7d8497e28d8d9315d5e5441a7f7/complete-project/db.sqlite -------------------------------------------------------------------------------- /complete-project/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "complete-project", 3 | "version": "0.0.1", 4 | "lockfileVersion": 3, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "complete-project", 9 | "version": "0.0.1", 10 | "license": "ISC", 11 | "dependencies": { 12 | "sql-bricks": "^3.0.1" 13 | } 14 | }, 15 | "node_modules/sql-bricks": { 16 | "version": "3.0.1", 17 | "resolved": "https://registry.npmjs.org/sql-bricks/-/sql-bricks-3.0.1.tgz", 18 | "integrity": "sha512-ZkU/R+bwf7d9FxlwMJp/31P5bluVCjUuftutkqJjQKH1QMCE1iaEc0xeY0aVepc38fxC+ljUrqausGCzzcHzHQ==", 19 | "license": "MIT", 20 | "engines": { 21 | "node": "*" 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /complete-project/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "complete-project", 3 | "version": "0.0.1", 4 | "main": "index.js", 5 | "type": "module", 6 | "scripts": { 7 | "dev": "node --inspect --watch --experimental-sqlite src/index.js" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "description": "", 13 | "dependencies": { 14 | "sql-bricks": "^3.0.1" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /complete-project/src/db.js: -------------------------------------------------------------------------------- 1 | import { DatabaseSync } from 'node:sqlite' 2 | import sqlBricks from 'sql-bricks' 3 | const database = new DatabaseSync('./db.sqlite') 4 | 5 | function runSeed(items) { 6 | database.exec(` 7 | DROP TABLE IF EXISTS students 8 | `) 9 | database.exec(` 10 | CREATE TABLE students( 11 | id INTEGER PRIMARY KEY AUTOINCREMENT, 12 | name TEXT NOT NULL, 13 | phone TEXT NOT NULL 14 | ) STRICT 15 | `) 16 | insert({ table: 'students', items }) 17 | 18 | // const query = sqlBricks 19 | // .select('name,phone') 20 | // .orderBy('name') 21 | // .from('students') 22 | // .toString() 23 | // console.log(select(query)) 24 | 25 | } 26 | 27 | export function select(query) { 28 | return database.prepare(query).all() 29 | } 30 | 31 | export function insert({ table, items }) { 32 | const { text, values } = sqlBricks.insertInto(table, items) 33 | .toParams({ placeholder: '?' }) 34 | 35 | const insertStatement = database.prepare(text) 36 | insertStatement.run(...values) 37 | } 38 | 39 | runSeed([ 40 | { 41 | name: 'erickwendel', 42 | phone: '11231234' 43 | }, 44 | { 45 | name: 'ananeri', 46 | phone: '321231231' 47 | }, 48 | { 49 | name: 'xuxa da silva', 50 | phone: '00001' 51 | }, 52 | ]) -------------------------------------------------------------------------------- /complete-project/src/index.js: -------------------------------------------------------------------------------- 1 | import { createServer } from 'node:http' 2 | import { setTimeout } from 'node:timers/promises' 3 | import { select, insert } from "./db.js"; 4 | import sqlBricks from 'sql-bricks' 5 | import { once } from 'node:events' 6 | createServer(async (request, response) => { 7 | if (request.method === 'GET') { 8 | 9 | const query = sqlBricks 10 | .select('name,phone') 11 | .orderBy('name') 12 | .from('students') 13 | .toString() 14 | 15 | const items = select(query) 16 | return response.end(JSON.stringify(items)) 17 | 18 | } 19 | if (request.method === 'POST') { 20 | const item = JSON.parse(await once(request, 'data')) 21 | insert({ table: 'students', items: [item] }) 22 | 23 | response.end(JSON.stringify({ 24 | message: `Student: ${item.name} created with success!` 25 | })) 26 | } 27 | }) 28 | .listen(3000, () => console.log('server running at 3000')) 29 | 30 | await setTimeout(500) 31 | 32 | { 33 | const result = await (await fetch('http://localhost:3000', { 34 | method: 'POST', 35 | body: JSON.stringify({ 36 | name: 'joaozinho', 37 | phone: '0012312' 38 | }) 39 | 40 | })).json() 41 | console.log('POST', result) 42 | } 43 | { 44 | const result = await (await fetch('http://localhost:3000')).json() 45 | console.log('GET', result) 46 | } 47 | -------------------------------------------------------------------------------- /preclass/.nvmrc: -------------------------------------------------------------------------------- 1 | v22.9.0 2 | -------------------------------------------------------------------------------- /preclass/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "type": "node", 6 | "request": "launch", 7 | "name": "Run Test Debugger", 8 | "runtimeExecutable": "npm", 9 | "runtimeArgs": [ 10 | "run", 11 | "dev" 12 | ], 13 | "skipFiles": [ 14 | "/**/**" 15 | ], 16 | "console": "integratedTerminal" 17 | }, 18 | ], 19 | "compounds": [] 20 | } -------------------------------------------------------------------------------- /preclass/db.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ErickWendel/nodejs-native-sqlite/5df6da5505aaa7d8497e28d8d9315d5e5441a7f7/preclass/db.sqlite -------------------------------------------------------------------------------- /preclass/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "preclass", 3 | "version": "0.0.1", 4 | "lockfileVersion": 3, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "preclass", 9 | "version": "0.0.1", 10 | "license": "ISC", 11 | "dependencies": { 12 | "sql-bricks": "^3.0.1" 13 | }, 14 | "engines": { 15 | "node": "v22.9.0" 16 | } 17 | }, 18 | "node_modules/sql-bricks": { 19 | "version": "3.0.1", 20 | "resolved": "https://registry.npmjs.org/sql-bricks/-/sql-bricks-3.0.1.tgz", 21 | "integrity": "sha512-ZkU/R+bwf7d9FxlwMJp/31P5bluVCjUuftutkqJjQKH1QMCE1iaEc0xeY0aVepc38fxC+ljUrqausGCzzcHzHQ==", 22 | "license": "MIT", 23 | "engines": { 24 | "node": "*" 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /preclass/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "preclass", 3 | "version": "0.0.1", 4 | "main": "index.js", 5 | "scripts": { 6 | "dev": "node --inspect --watch --experimental-sqlite src/index.js" 7 | }, 8 | "keywords": [], 9 | "author": "erickwendel", 10 | "license": "ISC", 11 | "description": "", 12 | "engines": { 13 | "node": "v22.9.0" 14 | }, 15 | "type": "module", 16 | "dependencies": { 17 | "sql-bricks": "^3.0.1" 18 | } 19 | } -------------------------------------------------------------------------------- /preclass/src/db.js: -------------------------------------------------------------------------------- 1 | import { DatabaseSync } from 'node:sqlite'; 2 | import sqlBricks from 'sql-bricks' 3 | const database = new DatabaseSync('./db.sqlite'); 4 | 5 | export function insert({ table, items = [] }) { 6 | const { text, values } = sqlBricks 7 | .insertInto(table, items) 8 | .toParams({ placeholder: '?' }); 9 | 10 | const insertStatement = database.prepare(text); 11 | insertStatement.run(...values) 12 | } 13 | 14 | export function select(query) { 15 | return database.prepare(query).all() 16 | } 17 | 18 | function runSeed(items) { 19 | database.exec(` 20 | DROP TABLE IF EXISTS students 21 | `); 22 | 23 | database.exec(` 24 | CREATE TABLE students( 25 | id INTEGER PRIMARY KEY, 26 | name TEXT NOT NULL, 27 | phone TEXT NOT NULL 28 | ) STRICT 29 | `); 30 | 31 | insert({ 32 | table: 'students', 33 | items 34 | }) 35 | 36 | // console.log(select( 37 | // sqlBricks 38 | // .select('name,phone') 39 | // .orderBy('name') 40 | // .from('students') 41 | // .toString() 42 | // )) 43 | } 44 | 45 | runSeed([ 46 | { 47 | name: 'erickwendel', 48 | phone: '112222' 49 | }, 50 | { 51 | name: 'ana', 52 | phone: '323123' 53 | }, 54 | { 55 | name: 'developer', 56 | phone: '3333222' 57 | }, 58 | { 59 | name: 'xuxa da silva', 60 | phone: '000111' 61 | }, 62 | ]) 63 | -------------------------------------------------------------------------------- /preclass/src/index.js: -------------------------------------------------------------------------------- 1 | import { createServer } from 'node:http' 2 | import { once } from 'node:events'; 3 | import { setTimeout } from 'node:timers/promises'; 4 | 5 | import sqlBricks from 'sql-bricks' 6 | import { select, insert } from './db.js'; 7 | 8 | createServer(async (req, res) => { 9 | 10 | if (req.method === 'POST') { 11 | const data = JSON.parse(await once(req, 'data')) 12 | insert({ 13 | table: 'students', 14 | items: [data] 15 | }) 16 | 17 | return res.end(JSON.stringify({ 18 | message: `Student ${data.name} created with success` 19 | })) 20 | } 21 | 22 | if (req.method === 'GET') { 23 | const items = select( 24 | sqlBricks 25 | .select('name,phone') 26 | .orderBy('name') 27 | .from('students') 28 | .toString() 29 | ) 30 | 31 | return res.end(JSON.stringify(items)) 32 | } 33 | 34 | }) 35 | .listen(3000, () => console.log('running on 3000')) 36 | 37 | 38 | await setTimeout(500) 39 | 40 | const result = await (await fetch('http://localhost:3000', { 41 | method: 'POST', 42 | body: JSON.stringify({ 43 | name: 'erickwendel', 44 | phone: '112222' 45 | }) 46 | })).json() 47 | 48 | console.log('POST:', result) 49 | 50 | const result2 = await (await fetch('http://localhost:3000')).json() 51 | console.log('GET:', result2) 52 | 53 | --------------------------------------------------------------------------------