Skip to content
This repository has been archived by the owner on Aug 27, 2022. It is now read-only.

Commit

Permalink
add mongodb examplees
Browse files Browse the repository at this point in the history
  • Loading branch information
burhanahmeed committed May 21, 2020
1 parent 37dde88 commit 3120c00
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 15 deletions.
Binary file not shown.
1 change: 1 addition & 0 deletions .denon
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"--allow-net",
"--allow-env",
"--allow-read",
"--allow-write",
"--allow-plugin",
"--unstable"
],
Expand Down
3 changes: 2 additions & 1 deletion application/bootstrap/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { database } from "./db/index.ts";
const databaseList: {[key: string]: Array<Object>} = {
mongodb: [
{
url: 'mongodb://127.0.0.1/somedb'
url: 'mongodb://127.0.0.1/',
database: 'Testing'
}
]
}
Expand Down
11 changes: 3 additions & 8 deletions application/bootstrap/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { Connection as mongoConnection } from './mongodb.db.ts';

function database (dbname: string, number?: number): void {
function database (dbname: string, number?: number) {
if (!number) number = 0;
switch (dbname) {
case 'mongodb':
mongoConnection.getConnection(number);
break;

default:
break;
if (dbname == 'mongodb') {
return mongoConnection.getConnection(number);
}
}

Expand Down
7 changes: 3 additions & 4 deletions application/bootstrap/db/mongodb.db.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Database } from "../database.ts";
import { MongoClient, init } from "../../../modules/deps.ts";

await init();
import { MongoClient } from "../../../modules/deps.ts";

const mongoConnect = (config: {[key: string]: string}) => {
const client = new MongoClient();
client.connectWithUri(config.url);
client.database('Testing');
let db = client.database(config.database);
return db;
}

let Connection = new Database(mongoConnect, 'mongodb');
Expand Down
1 change: 1 addition & 0 deletions application/bootstrap/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { database } from './db/index.ts';
15 changes: 15 additions & 0 deletions application/models/user.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { database } from '../bootstrap/index.ts';

const db = database("mongodb");
const User = db.collection("users");

async function getUsers (params?: object): Promise<object> {
return await User.find(params);
}

const users = {
getUsers
}
export {
users
}
13 changes: 12 additions & 1 deletion application/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
} from '../../modules/deps.ts';
import {
exampleMiddleware
} from "../middleware/index.ts";
} from "../middleware/index.ts";
import {
users
} from "../models/user.model.ts";

const route = new Router();

Expand All @@ -16,6 +19,14 @@ route.get('/', (req: Request, res: Response) => {
})
});

route.get('/users', async (req: Request, res: Response) => {
let getUsers = await users.getUsers();
return res.send({
status: 200,
data: getUsers
});
});

route.get('/query', exampleMiddleware, (req: Request, res: Response) => {
res.send({
status: 200,
Expand Down
2 changes: 1 addition & 1 deletion modules/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export {
staticServe
} from "https://deno.land/x/attain/mod.ts";

export { init, MongoClient } from "https://deno.land/x/mongo@v0.6.0/mod.ts";
export { MongoClient } from "https://deno.land/x/mongo@v0.7.0/mod.ts";

export {
serve
Expand Down

0 comments on commit 3120c00

Please sign in to comment.