SQLite - Node.JS API
Exercise 💪
You can find the codesandbox here.
Let’s build a CRUD API to manage users. Here are some of the most used SQLite commands with the corresponding sqlite3 functions:
| SQLite Command | sqlite3 npm Package Function |
|---|---|
| CREATE TABLE | db.run() |
| INSERT INTO | db.run() |
| SELECT | db.all() or db.each() |
| UPDATE | db.run() |
| DELETE | db.run() |
| WHERE | db.all() or db.each() |
| ORDER BY | db.all() or db.each() |
| LIMIT | db.all() or db.each() |
| GROUP BY | db.all() or db.each() |
| JOIN | db.all() or db.each() |
| IN | db.all() or db.each() |
| LIKE | db.all() or db.each() |
| COUNT | db.all() or db.each() |
| AVG | db.all() or db.each() |
| SUM | db.all() or db.each() |
| MAX | db.all() or db.each() |
| MIN | db.all() or db.each() |
| DISTINCT | db.all() or db.each() |
| BETWEEN | db.all() or db.each() |
Note that the db.all() function returns all rows as an array, while the db.each() function iterates over each row and calls a provided callback function. The db.run() function is used for SQL commands that don’t return data, such as creating or modifying tables.
Both functions take a sql query as first argument. Here are some examples:
- Create a table:
sqlCREATE TABLE users (id INTEGER PRIMARY KEY,name TEXT NOT NULL,email TEXT UNIQUE NOT NULL,password TEXT NOT NULL);
- Insert data into a table:
sqlINSERT INTO users (name, email, password)VALUES ('John Doe', 'john@example.com', 'password123');
- Select data from a table:
sqlSELECT name, email FROM users;
- Update data in a table:
sqlUPDATE users SET password = 'newpassword123' WHERE id = 1;
- Delete data from a table:
sqlDELETE FROM users WHERE id = 1;
- Join two tables:
sqlSELECT users.name, orders.order_date, orders.totalFROM usersINNER JOIN orders ON users.id = orders.user_id;
- Create an index:
sqlCREATE INDEX email_index ON users (email);
- Create a view:
sqlCREATE VIEW user_orders AS SELECT users.name, orders.order_date, orders.total FROM users INNER JOIN orders ON users.id = orders.user_id;