Skip to content

Commit db404ff

Browse files
committed
DAO Refactoring
1 parent 47fc480 commit db404ff

File tree

6 files changed

+752
-454
lines changed

6 files changed

+752
-454
lines changed

src/lib/dao.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { ObjectId } from 'mongodb'
2+
3+
/**
4+
* DAO Interface
5+
* @seed Boot/Reboot a table, collection, or key and insert a list of elements
6+
* @getOne Retrieve one element from a table, collection, or key
7+
* @getAll Retrieve all elements from a table, collection, or key
8+
* @setOne Insert an element into a table, collection, or key
9+
* @setMany Insert a list of elements into a table, collection, or key
10+
* @updateOne Update one element from a table, collection, or key
11+
* @deleteOne Delete one element from a table, collection, or key
12+
* @deleteAll Delete all elements from a table, collection, or key
13+
*/
14+
export interface DAO
15+
{
16+
17+
/**
18+
* Boot/Reboot a table, collection, or key and insert a list of elements
19+
* @param target Name of the target table, collection, or key
20+
* @param elements List of elements to insert
21+
* @param callback Process to execute on response (optional)
22+
*/
23+
seed: (target: string, elements: DataElement[], callback?: Callback) => void
24+
25+
/**
26+
* Retrieve one element from a table, collection, or key
27+
* @param target Name of the target table, collection, or key
28+
* @param id Target element's ID
29+
* @param callback Process to execute on response's element or error
30+
*/
31+
getOne: (target: string, id: number|string, callback: Callback) => void
32+
33+
/**
34+
* Retrieve all elements from a table, collection, or key
35+
* @param target Name of the target table, collection, or key
36+
* @param callback Process to execute on response's list of elements or error
37+
*/
38+
getAll: (target: string, callback: Callback) => void
39+
40+
/**
41+
* Insert an element into a table, collection, or key
42+
* @param target Name of the target table, collection, or key
43+
* @param element Element to insert
44+
* @param callback Process to execute on response (optional)
45+
*/
46+
setOne: (target: string, element: DataElement, callback?: Callback) => void
47+
48+
/**
49+
* Insert a list of elements into a table, collection, or key
50+
* @param target Name of the target table, collection, or key
51+
* @param elements List of elements to insert
52+
* @param callback Process to execute on response (optional)
53+
*/
54+
setMany: (target: string, elements: DataElement[], callback?: Callback) => void
55+
56+
/**
57+
* Update one element from a table, collection, or key
58+
* @param target Name of the target table, collection, or key
59+
* @param element Updated element
60+
* @param callback Process to execute on response (optional)
61+
*/
62+
updateOne: (target: string, element: DataElement, callback?: Callback) => void
63+
64+
/**
65+
* Delete one element from a table, collection, or key
66+
* @param target Name of the target table, collection, or key
67+
* @param id Target element's ID
68+
* @param callback Process to execute on response (optional)
69+
*/
70+
deleteOne: (target: string, id: number|string, callback?: Callback) => void
71+
72+
/**
73+
* Delete all elements from a table, collection, or key
74+
* @param target Name of the target table, collection, or key
75+
* @param callback Process to execute on response (optional)
76+
*/
77+
deleteAll: (target: string, callback?: Callback) => void
78+
79+
}
80+
81+
/** Standard DAO Connector type */
82+
export type Connector = {
83+
driver: string,
84+
user: string,
85+
password: string,
86+
host: string,
87+
port?: number|string,
88+
database: string,
89+
params?: ConnectorParams
90+
}
91+
92+
/** Standard DAO Connector parameters type */
93+
export type ConnectorParams = {
94+
[key: string]: any
95+
}
96+
97+
/** Standard DAO DataElement type */
98+
export type DataElement = {
99+
id?: number|string,
100+
_id?: ObjectId|string,
101+
[key: string]: any
102+
}
103+
104+
/** Standard DAO Callback type */
105+
export type Callback = (...arg: any) => void
106+
107+
// export { DAO, Callback, DataElement, Connection }

src/lib/mongo.ts

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import { DAO, Callback, DataElement, Connector, ConnectorParams } from './dao'
2+
import { MongoClient } from 'mongodb'
3+
import { ObjectID } from 'bson'
4+
5+
/**
6+
* MongoDB DAO
7+
* @constructor ???
8+
* @seed Boot/Reboot a collection and insert a list of elements
9+
* @getOne Retrieve one element from a collection
10+
* @getAll Retrieve all elements from a collection
11+
* @setOne Insert an element into a collection
12+
* @setMany Insert a list of elements into a collection
13+
* @updateOne Update one element from a collection
14+
* @deleteOne Delete one element from a collection
15+
* @deleteAll Delete all elements from a collection
16+
*/
17+
export default class Mongo implements DAO
18+
{
19+
20+
#db: string
21+
#uri: string
22+
23+
/**
24+
* ???
25+
* @param obj Database Connector object : { driver, user, password, host, port?, database, { params? } }
26+
*/
27+
constructor(obj: Connector)
28+
{
29+
if (!this.#check(obj)) throw new Error('CONNECTION ERROR: Wrong or missing Parameters')
30+
this.#uri = this.#parse(obj)
31+
this.#db = obj.database
32+
}
33+
34+
async #connect(callback: Callback) : Promise<void>
35+
{
36+
const client = await MongoClient.connect(this.#uri)
37+
await callback(client.db(this.#db))
38+
client.close()
39+
}
40+
41+
seed(target: string, elements: DataElement[], callback?: Callback) : void
42+
{
43+
this.#connect(async db => {
44+
const resultDel = await db.collection(target).deleteMany({ })
45+
const resultSet = await db.collection(target).insertMany(elements)
46+
if (callback) callback((resultDel && resultSet) ? true : false)
47+
})
48+
}
49+
50+
getOne(target: string, id: number|string, callback: Callback) : void
51+
{
52+
this.#connect(async db => {
53+
const element = await db.collection(target).findOne({ _id : new ObjectID(id) })
54+
callback(this.#sanitize(element))
55+
})
56+
}
57+
58+
getAll(target: string, callback: Callback) : void
59+
{
60+
this.#connect(async db => {
61+
const elements = await db.collection(target).find().toArray()
62+
callback(elements.map(this.#sanitize))
63+
})
64+
}
65+
66+
setOne(target: string, element: DataElement, callback?: Callback) : void
67+
{
68+
this.#connect(async db => {
69+
const result = await db.collection(target).insertOne(element)
70+
if (callback) callback(result.acknowledged)
71+
})
72+
}
73+
74+
setMany(target: string, elements: DataElement[], callback?: Callback) : void
75+
{
76+
this.#connect(async db => {
77+
const result = await db.collection(target).insertMany(elements)
78+
if (callback) callback(result.insertedCount)
79+
})
80+
}
81+
82+
updateOne(target: string, element: DataElement, callback?: Callback) : void
83+
{
84+
this.#connect(async db => {
85+
const result = await db.collection(target).replaceOne({ _id : new ObjectID(element.id)}, this.#sanitize(element))
86+
if (callback) callback(result.acknowledged)
87+
})
88+
}
89+
90+
deleteOne(target: string, id: number|string, callback?: Callback) : void
91+
{
92+
this.#connect(async db => {
93+
const result = await db.collection(target).deleteOne({ _id : new ObjectID(id)})
94+
if (callback) callback(result.acknowledged)
95+
})
96+
}
97+
98+
deleteAll(target: string, callback?: Callback) : void
99+
{
100+
this.#connect(async db => {
101+
const result = await db.collection(target).deleteMany({ })
102+
if (callback) callback(result.deletedCount)
103+
})
104+
}
105+
106+
#sanitize(element: DataElement) : DataElement
107+
{
108+
if (element._id) { element.id = element._id.toString(); delete element._id }
109+
else if (element.id) { element._id = new ObjectID(element.id); delete element.id }
110+
return element
111+
}
112+
113+
#check(obj: Connector) : boolean
114+
{
115+
return (obj.driver && obj.user && obj.password && obj.host && obj.database) ? true : false
116+
}
117+
118+
#parse(obj: Connector) : string
119+
{
120+
const port: string = (obj.port) ? `:${obj.port}` : ''
121+
const params: string = (obj.params) ? `?${this.#params(obj.params)}` : ''
122+
123+
return `${ obj.driver }://${ obj.user }:${ obj.password }@${ obj.host }${ port }/${ obj.database }${ params }`
124+
}
125+
126+
#params(obj: ConnectorParams) : string
127+
{
128+
return Object.keys(obj).map(key => (obj) ? `${key}=${obj[key]}` : '').join('&')
129+
}
130+
131+
}
132+
133+
export { Mongo, DAO, Callback, DataElement, Connector, ConnectorParams }

0 commit comments

Comments
 (0)