Deno Rest APIs CRUD mysql

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • mysql

    MySQL driver for Deno (by denodrivers)

  • Full doc mysql for deno => https://github.com/denodrivers/mysql

  • dero

    Fast web framework for Deno (support native HTTP/2 Hyper and std/http).

  • I hope the code above can help you in finding crud deno mysql. full source code https://github.com/herudi/dero/tree/main/examples/with-crud-mysql

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
  • deno

    A modern runtime for JavaScript and TypeScript.

  • import { dero, Request, Response, NextFunction } from "https://deno.land/x/[email protected]/mod.ts"; import vs from "https://deno.land/x/value_schema/mod.ts"; import { Client } from "https://deno.land/x/[email protected]/mod.ts"; import { json, urlencoded } from 'https://deno.land/x/parsec/mod.ts'; const client = await new Client().connect({ hostname: "127.0.0.1", username: "root", db: "dero-crud", password: "" }); const bodyValidator = (req: Request, res: Response, next: NextFunction) => { const schema = { name: vs.string(), brand: vs.string(), price: vs.number() }; let message: any; vs.applySchemaObject(schema, req.parsedBody, (err) => { const key = err.keyStack.shift(); if (key) { if (message === undefined) message = {}; message[key] = `Field '${key}' is required`; } }); if (message) return req.pond({ status: 422, message }, { status: 422 }); next(); } // middleware body parser dero.use(json, urlencoded); // find all items dero.get("/items", async (req) => { const sql = `select * from items`; const { rows } = await client.execute(sql); req.pond({ statusCode: 200, data: rows }); }); // find by items id dero.get("/items/:id", async (req) => { const sql = `select * from items where id = ?`; const { rows } = await client.execute(sql, [req.params.id]); const data = rows && rows.length ? rows[0] : null; req.pond({ statusCode: 200, data }); }); // search items /items-search?text=yourfavitems dero.get("/items-search", async (req) => { const qry = req.query; if (!qry.text) { throw new Error("query parameter text is required"); } const sql = `select * from items where name like '%${qry.text}%' or brand like '%${qry.text}%'`; const { rows } = await client.execute(sql); req.pond({ statusCode: 200, data: rows }); }); // save items dero.post("/items", bodyValidator, async (req) => { const body = req.parsedBody || {}; const sql = `insert into items(name, brand, price) values(?, ?, ?)`; await client.execute(sql, [ body.name, body.brand, body.price ]); req.pond({ statusCode: 201, messsage: "Success save items" }, { status: 201 }); }); // update items by id dero.put("/items/:id", bodyValidator, async (req) => { const body = req.parsedBody || {}; const sql = `update items set name = ?, brand = ?, price = ? where id = ?`; await client.execute(sql, [ body.name, body.brand, body.price, req.params.id ]); req.pond({ statusCode: 200, messsage: "Success update items" }); }); // delete items by id dero.delete("/items/:id", async (req) => { const sql = `delete from items where id = ?`; await client.execute(sql, [req.params.id]); req.pond({ statusCode: 200, messsage: "Success delete items" }); }); // error handling dero.onError((err: any, req: Request, res: Response, next: NextFunction) => { let status = err.code || err.status || err.statusCode || 500; if (typeof status !== 'number') status = 500; req.pond({ statusCode: status, message: err.message }, { status }); }); // not found error handling dero.onNotFound((req: Request, res: Response, next: NextFunction) => { req.pond({ statusCode: 404, message: err.message }, { status: 404 }); }); // listen the server await dero.listen(3000);

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts