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

Commit

Permalink
Merge pull request #2 from burhanahmeed/dev.0.1
Browse files Browse the repository at this point in the history
Dev.0.1
  • Loading branch information
burhanahmeed authored May 21, 2020
2 parents 58ef334 + 3120c00 commit 6d05b4d
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 28 deletions.
Binary file not shown.
11 changes: 7 additions & 4 deletions .denon
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"files": [
"main.ts"
],
"quiet": false,
"quiet": true,
"debug": true,
"fullscreen": true,
"fullscreen": false,
"extensions": [
".js",
".ts",
Expand All @@ -18,13 +18,16 @@
"deno_args":[
"--allow-net",
"--allow-env",
"--allow-read"
"--allow-read",
"--allow-write",
"--allow-plugin",
"--unstable"
],
"execute": {
".js": ["deno", "run"],
".ts": ["deno", "run"],
".py": ["python"]
},
"fmt": false,
"test": true
"test": false
}
57 changes: 57 additions & 0 deletions application/bootstrap/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { database } from "./db/index.ts";

const databaseList: {[key: string]: Array<Object>} = {
mongodb: [
{
url: 'mongodb://127.0.0.1/',
database: 'Testing'
}
]
}

class Database {
connection: Array<any>;
connection_driver: any;
dbname: string;
constructor (databaseConnection: any, dbname: string) {
this.connection = [];
this.connection_driver = databaseConnection;
this.dbname = dbname;
}

getConnection (number?: number) {
if (!number) {
number = 0;
}
if (this.connection[number]) {
return this.connection[number];
} else {
if (databaseList[this.dbname].hasOwnProperty(number)) {
this.connection[number] = this.connection_driver(databaseList[this.dbname][number]);
return this.connection[number]
} else {
console.warn(`app:database Database ${this.dbname} number ${number} is not exist on configuration !`)
return null

}
}
}
}

const connectAll = () => {
let conn: {[key: string]: any} = {};
for (const dbname in databaseList) {
conn[dbname] = []
for (var i = 0; i < databaseList[dbname].length; i++) {
conn[dbname].push(database(dbname, i));
}
}
return conn;
}



export {
connectAll,
Database
}
10 changes: 10 additions & 0 deletions application/bootstrap/db/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Connection as mongoConnection } from './mongodb.db.ts';

function database (dbname: string, number?: number) {
if (!number) number = 0;
if (dbname == 'mongodb') {
return mongoConnection.getConnection(number);
}
}

export { database }
16 changes: 16 additions & 0 deletions application/bootstrap/db/mongodb.db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Database } from "../database.ts";
import { MongoClient } from "../../../modules/deps.ts";

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

let Connection = new Database(mongoConnect, 'mongodb');

export {
Connection,
mongoConnect
}
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';
2 changes: 1 addition & 1 deletion application/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import api from './router/index.ts';
import {
Response,
Request
} from '../modules/index.ts';
} from '../modules/deps.ts';

const app = (app: any) => {
app.use('/api', api);
Expand Down
2 changes: 1 addition & 1 deletion application/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Request,
Response
} from "../../modules/index.ts";
} from "../../modules/deps.ts";

/**
* This is the example of Attain Middleware
Expand Down
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
}
15 changes: 13 additions & 2 deletions application/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import {
Router,
Request,
Response
} from '../../modules/index.ts';
} 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 main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
logger,
parser,
staticServe
} from "./modules/index.ts";
} from "./modules/deps.ts";
import Application from './application/entry.ts';

const app = new App();
Expand Down
15 changes: 15 additions & 0 deletions modules/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export {
App,
Request,
Response,
Router,
logger,
parser,
staticServe
} from "https://deno.land/x/attain/mod.ts";

export { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";

export {
serve
} from "https://deno.land/[email protected]/http/server.ts";
19 changes: 0 additions & 19 deletions modules/index.ts

This file was deleted.

0 comments on commit 6d05b4d

Please sign in to comment.