1- import { MONGO } from "./depts.ts"
2- const { MongoClient } = MONGO
3- function wait ( time : number ) { return new Promise ( ( res ) => setTimeout ( res , time ) ) }
4-
5- export type ObjectId = MONGO . Bson . ObjectId
6- export type InsertId < Type > = Type & { _id : ObjectId }
7-
8- export default class Model < Schema , PopulateSchema > {
9- public client : MONGO . MongoClient
10- public db ?: MONGO . Database
11- public collectionName : string
12- private Model ?: MONGO . Collection < Schema >
13- constructor ( collectionName : string , conn : string )
14- constructor ( collectionName : string , conn : MONGO . MongoClient )
15- constructor ( collectionName : string , conn : { client : MONGO . MongoClient } )
16- constructor ( collectionName : string , conn : { client : MONGO . MongoClient , database : MONGO . Database } )
17- constructor ( collectionName : string , conn : { client : MONGO . MongoClient , database : string } )
18- constructor ( collectionName : string , conn : { ConnectionOptions : MONGO . ConnectOptions } )
19- constructor ( collectionName : string , conn : { ConnectionOptions : MONGO . ConnectOptions , database : string } )
20- constructor ( collectionName : string , conn ?: string | MONGO . MongoClient | { client ?: MONGO . MongoClient , database ?: MONGO . Database | string , ConnectionOptions ?: MONGO . ConnectOptions } ) {
21- this . collectionName = collectionName
22- if ( typeof conn === 'string' ) {
23- this . client = new MongoClient ( )
24- this . connect ( { connection : conn } )
25- } else if ( conn instanceof MongoClient ) {
26- this . client = conn
27- this . db = this . client . database ( )
28- this . Model = this . db . collection ( this . collectionName )
29- } else if ( ( < { client : MONGO . MongoClient } > conn ) . client ) {
30- if ( ( < { database : MONGO . Database | string } > conn ) . database ) {
31- if ( typeof ( < { database : string } > conn ) . database === 'string' ) {
32- this . client = ( < { client : MONGO . MongoClient } > conn ) . client
33- this . db = this . client . database ( ( < { database : string } > conn ) . database )
34- this . Model = this . db . collection ( this . collectionName )
35- } else /* if ((<{ database: MONGO.Database }>conn) instanceof Database ) */ {
36- this . client = ( < { client : MONGO . MongoClient } > conn ) . client
37- this . db = ( < { database : MONGO . Database } > conn ) . database
38- this . Model = this . db . collection ( this . collectionName )
39- }
40- } else {
41- this . client = ( < { client : MONGO . MongoClient } > conn ) . client
42- this . db = this . client . database ( )
43- this . Model = this . db . collection ( this . collectionName )
44- }
45- } else /* if ((<{ ConnectionOptions: MONGO.ConnectOptions }>conn).ConnectionOptions) */ {
46- this . client = new MongoClient ( )
47- if ( ( < { database : string } > conn ) . database ) {
48- this . connect ( { connection : ( < { ConnectionOptions : MONGO . ConnectOptions } > conn ) . ConnectionOptions , database : ( < { database : string } > conn ) . database } )
49- } else {
50- this . connect ( { connection : ( < { ConnectionOptions : MONGO . ConnectOptions } > conn ) . ConnectionOptions } )
51- }
52- }
53- }
54-
55- public async getModel ( ) : Promise < MONGO . Collection < Schema > > {
56- while ( ! this . Model ) { await wait ( 100 ) }
57- return this . Model
58- }
59-
60- public async connect ( { connection, database } : { connection : string | MONGO . ConnectOptions , database ?: string } ) : Promise < void > {
61- await this . client . connect ( connection )
62- this . db = this . client . database ( database )
63- this . Model = this . db . collection ( this . collectionName )
64- }
65-
66- public async insert ( document : MONGO . InsertDocument < Schema > , InsertOptions ?: MONGO . InsertOptions ) : Promise < ObjectId >
67- public async insert ( document : MONGO . InsertDocument < Schema > [ ] , InsertOptions ?: MONGO . InsertOptions ) : Promise < ObjectId [ ] >
68- public async insert ( document : MONGO . InsertDocument < Schema > [ ] | MONGO . InsertDocument < Schema > , InsertOptions ?: MONGO . InsertOptions ) : Promise < ObjectId | Required < MONGO . InsertDocument < Schema > > [ "_id" ] | ( ObjectId | Required < MONGO . InsertDocument < Schema > > [ "_id" ] ) [ ] > {
69- const Model = await this . getModel ( )
70- if ( Array . isArray ( document ) ) {
71- return ( await Model . insertMany ( document , InsertOptions ) ) . insertedIds
72- } else {
73- return await Model . insertOne ( document )
74- }
75- }
76-
77- public async select ( filter ?: MONGO . Filter < Schema > , options ?: MONGO . FindOptions & { multiple ?: true , populate ?: { [ collection : string ] : string | string [ ] } } ) : Promise < ( Schema & { _id : ObjectId } & ( PopulateSchema & { _id : ObjectId } ) ) [ ] >
78- public async select ( filter ?: MONGO . Filter < Schema > , options ?: MONGO . FindOptions & { multiple ?: false , populate ?: { [ collection : string ] : string | string [ ] } } ) : Promise < Schema & { _id : ObjectId } & ( PopulateSchema & { _id : ObjectId } ) | undefined >
79- public async select ( filter ?: MONGO . Filter < Schema > , options ?: MONGO . FindOptions & { multiple ?: boolean , populate ?: { [ collection : string ] : string | string [ ] } } ) : Promise < Schema & ( PopulateSchema & { _id : ObjectId } ) | undefined | ( Schema & ( PopulateSchema & { _id : ObjectId } ) ) [ ] > {
80- const Model = await this . getModel ( )
81- const _options = Object . assign ( { } , options )
82- const populate = _options . populate
83- if ( populate ) {
84- delete _options . populate
85- }
86- let document : Schema | Schema [ ] | undefined
87- if ( _options . multiple || _options ?. multiple === undefined ) {
88- delete _options . multiple
89- document = await Model . find ( filter , _options ) . toArray ( )
90- } else {
91- delete _options . multiple
92- document = await Model . findOne ( filter , _options )
93- }
94- const promises : Promise < void > [ ] = [ ]
95- if ( document && populate ) {
96- Object . keys ( populate ) . forEach ( ( collection ) => {
97- const documents = Array . isArray ( document ) ? document : [ document ]
98- documents . forEach ( ( doc ) => {
99- const paths = Array . isArray ( populate [ collection ] ) ? populate [ collection ] : [ populate [ collection ] ]
100- ; ( < any > paths ) . forEach ( ( path : string ) => {
101- const [ pointer , pointers ] = ( ( a , b ) => {
102- let planes : any = [ a ]
103- const vals = b . split ( '.' )
104- const pointer : any = vals . pop ( )
105- while ( vals . length > 0 ) {
106- if ( Array . isArray ( planes [ 0 ] ) && isNaN ( parseInt ( vals [ 0 ] ) ) ) {
107- planes = planes . flat ( )
108- }
109- planes = planes . map ( ( a : any ) => a [ vals [ 0 ] ] )
110- vals . shift ( )
111- }
112- if ( Array . isArray ( planes [ 0 ] ) && isNaN ( parseInt ( pointer ) ) ) {
113- planes = planes . flat ( )
114- }
115- return [ pointer , planes ]
116- } ) ( doc , path )
117- pointers . forEach ( ( _p : any ) => {
118- if ( Array . isArray ( _p [ pointer ] ) ) {
119- const promise = Promise . all ( _p [ pointer ] . map ( ( _id : ObjectId ) => new Promise ( ( res ) => {
120- res ( this . db ! . collection ( collection ) . findOne ( { _id } ) )
121- } ) ) ) . then ( ( data ) => _p [ pointer ] = data )
122- promises . push ( < any > promise )
123- } else {
124- promises . push (
125- new Promise ( ( res ) => {
126- res ( this . db ! . collection ( collection ) . findOne ( { _id : _p [ pointer ] } ) )
127- } ) . then ( ( ) => {
128- _p [ pointer ] = _p
129- } )
130- )
131- }
132- } )
133- } )
134- } )
135- } )
136- }
137- await Promise . all ( promises )
138- return < Schema & ( PopulateSchema & { _id : ObjectId } ) | undefined | ( Schema & ( PopulateSchema & { _id : ObjectId } ) ) [ ] > document
139- }
140-
141- public async update ( filter : MONGO . Filter < Schema > , document : Partial < Schema > & MONGO . Bson . Document , options ?: MONGO . FindOptions & { multiple ?: true } ) : Promise < ObjectId [ ] >
142- public async update ( filter : MONGO . Filter < Schema > , document : Partial < Schema > & MONGO . Bson . Document , options ?: MONGO . FindOptions & { multiple ?: false } ) : Promise < ObjectId | unknown >
143- public async update ( filter : MONGO . Filter < Schema > , document : Partial < Schema > & MONGO . Bson . Document , options ?: MONGO . FindOptions & { multiple ?: boolean } ) {
144- const Model = await this . getModel ( )
145- const _options = Object . assign ( { } , options )
146- if ( _options . multiple || _options ?. multiple === undefined ) {
147- const updated = ( await this . select ( filter , { multiple : true , projection : { _id : true } } ) ) . map ( ( { _id } ) => _id )
148- delete _options ?. multiple
149- await Model . updateMany ( { $or : updated . map ( ( a => ( { _id : a } ) ) ) } , < MONGO . UpdateFilter < Schema > > { $set : document } )
150- return updated
151- } else {
152- const updated = ( await this . select ( filter , { multiple : false , projection : { _id : true } } ) ) ?. _id
153- delete _options ?. multiple
154- updated && await Model . updateOne ( { _id : updated } , < MONGO . UpdateFilter < Schema > > { $set : document } )
155- return updated
156- }
157- }
158-
159- public async delete ( filter ?: MONGO . Filter < Schema > , options ?: MONGO . FindOptions & { multiple ?: true } ) : Promise < ObjectId [ ] >
160- public async delete ( filter ?: MONGO . Filter < Schema > , options ?: MONGO . FindOptions & { multiple ?: false } ) : Promise < ObjectId | unknown >
161- public async delete ( filter ?: MONGO . Filter < Schema > , options ?: MONGO . FindOptions & { multiple ?: boolean } ) : Promise < ObjectId | unknown | ObjectId [ ] > {
162- const Model = await this . getModel ( )
163- const _options = Object . assign ( { } , options )
164- if ( _options . multiple || _options ?. multiple === undefined ) {
165- const deleted = ( await this . select ( filter , { multiple : true , projection : { _id : true } } ) ) . map ( ( { _id } ) => _id )
166- await Model . deleteMany ( { $or : deleted . map ( ( a => ( { _id : a } ) ) ) } , options )
167- return deleted
168- } else {
169- const deleted = ( await this . select ( filter , { multiple : false , projection : { _id : true } } ) ) ?. _id
170- await Model . deleteOne ( { _id : deleted } , options )
171- return deleted
172- }
173- }
174- public async count ( filter : MONGO . Filter < Schema > , options ?: MONGO . CountOptions ) {
175- const Model = await this . getModel ( )
176- return await Model . countDocuments ( filter , options )
177- }
178- }
1+ export type { ObjectId } from './src/types.ts'
2+ import { Model } from './src/main.ts'
3+ export default Model
0 commit comments