1+ import { MongoClient , ConnectOptions , Database , Collection , InsertDocument , Filter , InsertOptions , FindOptions , UpdateFilter , UpdateOptions , DeleteOptions , CountOptions } from "https://deno.land/x/[email protected] /mod.ts" 2+ function wait ( time : number ) { return new Promise ( ( res ) => setTimeout ( res , time ) ) }
3+
4+
5+ export default class Model < T > {
6+ public client : MongoClient
7+ public Schema : T
8+ public db ?: Database
9+ public collectionName : string
10+ private Model ?: Collection < T >
11+ constructor ( collectionName : string , Schema : T , { connection, database } : { connection : string | ConnectOptions | MongoClient , database ?: string } ) {
12+ this . Schema = Schema
13+ this . collectionName = collectionName
14+ if ( connection instanceof MongoClient ) {
15+ this . client = connection
16+ } else {
17+ this . client = new MongoClient ( )
18+ this . connect ( { connection, database } )
19+ }
20+ }
21+
22+ public async getModel ( ) : Promise < Collection < T > > {
23+ while ( ! this . Model ) { await wait ( 100 ) }
24+ return this . Model
25+ }
26+
27+ public async connect ( { connection, database } : { connection : string | ConnectOptions , database ?: string } ) : Promise < void > {
28+ await this . client . connect ( connection )
29+ this . db = this . client . database ( database )
30+ this . Model = this . db . collection ( this . collectionName )
31+ }
32+
33+
34+ public async insert ( document : InsertDocument < T > | InsertDocument < T > [ ] , InsertOptions : InsertOptions ) {
35+ const Model = await this . getModel ( )
36+ if ( Array . isArray ( document ) ) {
37+ return await Model . insertMany ( document , InsertOptions )
38+ } else {
39+ return await Model . insertOne ( document )
40+ }
41+ }
42+ public async select ( filter : Filter < T > , options : FindOptions & { multiple ?: boolean } = { multiple : true } ) {
43+ const Model = await this . getModel ( )
44+ if ( options . multiple ) {
45+ delete options . multiple
46+ return await Model . findOne ( filter , options )
47+ } else {
48+ delete options . multiple
49+ return await Model . find ( filter , options )
50+ }
51+ }
52+ public async update ( filter : Filter < T > , document : UpdateFilter < T > , options : UpdateOptions & { multiple ?: boolean } = { multiple : true } ) {
53+ const Model = await this . getModel ( )
54+ if ( options . multiple ) {
55+ delete options . multiple
56+ return await Model . updateMany ( filter , document , options )
57+ } else {
58+ delete options . multiple
59+ return await Model . updateOne ( filter , document , options )
60+ }
61+ }
62+ public async delete ( filter : Filter < T > , options : DeleteOptions & { multiple ?: boolean } = { multiple : true } ) {
63+ const Model = await this . getModel ( )
64+ if ( options . multiple ) {
65+ return await Model . deleteMany ( filter , options )
66+ } else {
67+ return await Model . deleteOne ( filter , options )
68+ }
69+ }
70+ public async count ( filter : Filter < T > , options : CountOptions ) {
71+ const Model = await this . getModel ( )
72+ return await Model . countDocuments ( filter , options )
73+ }
74+ }
0 commit comments