diff --git a/.deno_plugins/deno_mongo_1ba9f61beddbbadd42e76d2e35f00255.so b/.deno_plugins/deno_mongo_1ba9f61beddbbadd42e76d2e35f00255.so new file mode 100644 index 0000000..79dd81c Binary files /dev/null and b/.deno_plugins/deno_mongo_1ba9f61beddbbadd42e76d2e35f00255.so differ diff --git a/.denon b/.denon index 12f64fb..96dcf65 100644 --- a/.denon +++ b/.denon @@ -2,9 +2,9 @@ "files": [ "main.ts" ], - "quiet": false, + "quiet": true, "debug": true, - "fullscreen": true, + "fullscreen": false, "extensions": [ ".js", ".ts", @@ -18,7 +18,10 @@ "deno_args":[ "--allow-net", "--allow-env", - "--allow-read" + "--allow-read", + "--allow-write", + "--allow-plugin", + "--unstable" ], "execute": { ".js": ["deno", "run"], @@ -26,5 +29,5 @@ ".py": ["python"] }, "fmt": false, - "test": true + "test": false } \ No newline at end of file diff --git a/application/bootstrap/database.ts b/application/bootstrap/database.ts new file mode 100644 index 0000000..54598b8 --- /dev/null +++ b/application/bootstrap/database.ts @@ -0,0 +1,57 @@ +import { database } from "./db/index.ts"; + +const databaseList: {[key: string]: Array} = { + mongodb: [ + { + url: 'mongodb://127.0.0.1/', + database: 'Testing' + } + ] +} + +class Database { + connection: Array; + 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 +} \ No newline at end of file diff --git a/application/bootstrap/db/index.ts b/application/bootstrap/db/index.ts new file mode 100644 index 0000000..fcfa2c0 --- /dev/null +++ b/application/bootstrap/db/index.ts @@ -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 } \ No newline at end of file diff --git a/application/bootstrap/db/mongodb.db.ts b/application/bootstrap/db/mongodb.db.ts new file mode 100644 index 0000000..c58db5b --- /dev/null +++ b/application/bootstrap/db/mongodb.db.ts @@ -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 +} diff --git a/application/bootstrap/index.ts b/application/bootstrap/index.ts new file mode 100644 index 0000000..94eab23 --- /dev/null +++ b/application/bootstrap/index.ts @@ -0,0 +1 @@ +export { database } from './db/index.ts'; \ No newline at end of file diff --git a/application/entry.ts b/application/entry.ts index b7975f8..56e8955 100644 --- a/application/entry.ts +++ b/application/entry.ts @@ -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); diff --git a/application/middleware/index.ts b/application/middleware/index.ts index cf30ebe..1640b51 100644 --- a/application/middleware/index.ts +++ b/application/middleware/index.ts @@ -1,7 +1,7 @@ import { Request, Response - } from "../../modules/index.ts"; + } from "../../modules/deps.ts"; /** * This is the example of Attain Middleware diff --git a/application/models/user.model.ts b/application/models/user.model.ts new file mode 100644 index 0000000..55c7b57 --- /dev/null +++ b/application/models/user.model.ts @@ -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 { + return await User.find(params); +} + +const users = { + getUsers +} +export { + users +} \ No newline at end of file diff --git a/application/router/index.ts b/application/router/index.ts index 1026a35..f58252d 100644 --- a/application/router/index.ts +++ b/application/router/index.ts @@ -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(); @@ -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, diff --git a/main.ts b/main.ts index 24626db..294d72b 100644 --- a/main.ts +++ b/main.ts @@ -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(); diff --git a/modules/deps.ts b/modules/deps.ts new file mode 100644 index 0000000..e44ae6a --- /dev/null +++ b/modules/deps.ts @@ -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/mongo@v0.7.0/mod.ts"; + +export { + serve + } from "https://deno.land/std@0.50.0/http/server.ts"; \ No newline at end of file diff --git a/modules/index.ts b/modules/index.ts deleted file mode 100644 index e5f2762..0000000 --- a/modules/index.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { - App, - Request, - Response, - Router, - logger, - parser, - staticServe -} from "https://deno.land/x/attain/mod.ts"; - -export { - App, - Request, - Response, - Router, - logger, - parser, - staticServe -} \ No newline at end of file