-
Notifications
You must be signed in to change notification settings - Fork 157
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
mailtoraj369
wants to merge
8
commits into
denoland:main
Choose a base branch
from
mailtoraj369:oak_middleware_examples_369
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+49
−0
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
036a837
Example contribution http oak middleware with crud and sqlite DB
mailtoraj369 877e6f0
Updated with JSR library and its context request json object retrieve…
mailtoraj369 57eb10f
Merge branch 'denoland:main' into oak_middleware_examples_369
mailtoraj369 700e3c3
Merge branch 'oak_middleware_examples_369' of https://github.com/mail…
mailtoraj369 fa7ed4c
optimized code to meet guidelines
mailtoraj369 6cf74b1
formatted code after refactoring to meet guideline on line count
mailtoraj369 8d4db64
File renamed as per guidelines
mailtoraj369 330e9ee
Merge branch 'denoland:main' into oak_middleware_examples_369
mailtoraj369 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
examples/http_routes_oak_crud_middleware_with_sqlite3_db.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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