forked from storj-archived/bridge
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { ObjectId, Document, Collection, Db, MongoClient } from 'mongodb'; | ||
|
||
export interface MongoDBCollections { | ||
tempShards: Collection<TempShardDocument>; | ||
} | ||
|
||
interface TempShard extends Document { | ||
hash: string; | ||
objectStorageHash: string; | ||
shardId: string; | ||
size: number; | ||
} | ||
|
||
export interface TempShardDocument extends Omit<TempShard, 'shardId'> { | ||
shardId: ObjectId; | ||
} | ||
|
||
export class MongoDB { | ||
private uri: string; | ||
private db: Db | null; | ||
private client: MongoClient; | ||
|
||
constructor(uri: string) { | ||
this.uri = uri; | ||
this.db = null; | ||
this.client = new MongoClient(this.uri); | ||
} | ||
|
||
get URI() { | ||
return this.uri; | ||
} | ||
|
||
async connect(): Promise<MongoDB> { | ||
await this.client.connect(); | ||
|
||
this.db = this.client.db('__inxt-network'); | ||
|
||
return this; | ||
} | ||
|
||
getCollections(): MongoDBCollections { | ||
if (!this.db) { | ||
throw new Error('Database not initialized'); | ||
} | ||
|
||
return { | ||
tempShards: this.db.collection<TempShardDocument>('tempshards'), | ||
}; | ||
} | ||
|
||
disconnect(): Promise<void> { | ||
return this.client.close(); | ||
} | ||
} |