Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for http oak middleware with crud and sqlite DB #1101

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
86 changes: 86 additions & 0 deletions examples/http_routes_oak_crud_middleware_with_sqlite3_db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* @title HTTP server: Performing CRUD operations using SQLite3
* @difficulty intermediate
* @tags web, cli, deploy
* @run -A <url>
* @group Network
*
* An example of a HTTP server for CRUD routes with oak middleware framework and SQLite3 database.
* It demonstrates the CRUD(Create, Read, Update and Delete) operations on file-based SQLite Database using HTTP methods (Get, Post, Put, Delete, Options)
*/

import { Application, Router } from "https://deno.land/x/[email protected]/mod.ts";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can simply use jsr:@oak/oak here, unless you're are doing this specifically to use v11.1.0.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have updated as requested, thanks for pointing out

import { Database } from "jsr:@db/[email protected]";

// Open a database from file, creates if doesn't exist. here the 'people.db' is a file-based database
const peopleDb = new Database("people.db");

// Create Table "people" if not exists with schema as shown below
peopleDb.exec(
"CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT not null,age INTEGER not null)",
);
// logs the DB creation status
console.log("People table created successfully ");

const app = new Application();
const router = new Router();
const PORT = "8369";
// Create person record into people database
router.post("/people", async (ctx) => {
const { name, age } = await (await ctx.request.body("json")).value;
const result = await peopleDb.prepare(
"INSERT INTO people (name, age) VALUES (?,?)",
)
.run(name, age);
ctx.response.status = 201;
ctx.response.body = { id: result.lastInsertRowId, name, age };
});

// Read all records from people database
router.get("/people", async (ctx) => {
const users = await peopleDb.prepare("SELECT * FROM people").all();
ctx.response.body = users;
});

// Updates person record in people database (name value is optional)
router.put("/people/:id", async (ctx) => {
const id = ctx.params.id;
const { name, age } = await (await ctx.request.body("json")).value;
let result = 0;
if (name) {
result = await peopleDb.prepare(
"UPDATE people SET name =?, age=? WHERE id = ?",
)
.run(name, age, id);
} else {
result = await peopleDb.prepare(
"UPDATE people SET age=? WHERE id = ?",
)
.run(age, id);
}
ctx.response.body = result > 0
? { message: "person updated successfully as requested" }
: { message: "Failure in update operation" };
});

// Delete person record from people database
router.delete("/people/:id", async (ctx) => {
const result = await peopleDb.prepare("DELETE FROM people WHERE id = ?").run(
ctx.params.id,
);
ctx.response.body = result > 0
? { message: "person removed successfully as requested" }
: { message: "Failure in deletion operation" };
});

// Health check endpoint
router.options("/healthz", async (ctx) => {
ctx.response.body = { message: `health check verified` };
});

app.use(router.routes());
app.use(router.allowedMethods());
console.log(`Server is running on http://localhost:${PORT}`);
await app.listen({ port: PORT });
// Export the app instance for testing
export default app;
Loading