Git Product home page Git Product logo

Comments (3)

crookse avatar crookse commented on August 25, 2024

Hi @khanh19934,

You can do this by adding a global member: https://crookse.github.io/deno-drash/#/tutorials/adding-global-members

I know it might not be too intuitive because adding the class/service that handles your database processes will be added to the Drash.Members namespace, but it’ll work.

I’ll look into adding configurations to the server for database connections though. I’m also open to suggestions if you have any.

from drash.

crookse avatar crookse commented on August 25, 2024

Hey @khanh19934 ,

Here's a solution for you:

// File: client.ts

import { Client } from "https://deno.land/x/mysql/mod.ts";

const client = await new Client().connect({
  hostname: "127.0.0.1",
  username: "root",
  db: "deno_mysql",
});

export default client;
// File: home_resource.ts

import Drash from "https://deno.land/x/[email protected]/mod.ts";
import client from "./client.ts";

export default class HomeResource extends Drash.Http.Resource {

  static paths = ["/"];

  public async GET() {
    this.response.body = await client.query(`select * from user`);
    return this.response;
  }
}
// File: app.ts

import Drash from "https://deno.land/x/[email protected]/mod.ts";

import HomeResource from "./home_resource.ts";

let server = new Drash.Http.Server({
  address: "localhost:1337",
  response_output: "application/json",
  resources: [HomeResource]
});

server.run();
deno --allow-net --allow-write app.ts

You'll want to make client.ts fit your MySQL requirements.

Below is a picture of it working. As you can see, I started up my server and ran curl localhost:1337. I got back the data from my user table. The left side of the screen is my project filesystem. The right side is my MySQL table, my server being run, and the curl request.

Screen Shot 2019-12-27 at 21 17 31

from drash.

crookse avatar crookse commented on August 25, 2024

@khanh19934,

I made a documentation page related to this issue: https://crookse.github.io/deno-drash/#/third-party-tutorials/databases/deno-mysql

from drash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.