Comments (3)
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.
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.
from drash.
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)
- Service.end() keeps the send property set to true HOT 1
- CLEANUP CREW: Need to add/revise doc blocks, file markers, interfaces, etc HOT 1
- chore: use rhum v2 HOT 1
- bug: 406 thrown for empty string accept header
- perf: dont try to find resource if it was already found previously
- feat: graphql service no longer needs empty resource; add option to define playground endpoint
- feat: check for trailing/leading slash in jae
- feat: add connInfo to IErrorHandler? HOT 2
- feat: tengine to work in Deploy
- feat: Add skip function on dexter service HOT 8
- bug: ResponseTimeService not work when an exception occurs
- BREAKING!: v3-beta HOT 1
- feat(http/server): add new config for control flow changes
- chore(service/tengine): verify Deno Deploy compatibility HOT 1
- bug: Response.download doesn't work with deno deploy HOT 2
- question: k6 tests a lot of fails HOT 2
- Module not found "https://deno.land/x/[email protected]/mod.ts"bug: HOT 2
- feat(v3-beta): finish leftover tasks
- bug(ci): send webhook after CI finishes release, not at the time it runs
- chore: check bundle sizes when drash files are imported
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from drash.