-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
0a46e14
commit 7c49b3c
Showing
9 changed files
with
105 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"cSpell.words": ["Jsonify", "Jsonize", "promeet", "Virtuals", "Zamanian"] | ||
"cSpell.words": ["gecut", "Jsonify", "Jsonize", "promeet", "Virtuals", "Zamanian"] | ||
} |
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,20 @@ | ||
{ | ||
"name": "@promeet/database", | ||
"private": true, | ||
"type": "module", | ||
"dependencies": { | ||
"@promeet/schemas": "workspace:^", | ||
"@promeet/types": "workspace:^", | ||
"mongoose": "^8.7.0" | ||
}, | ||
"devDependencies": { | ||
"@gecut/logger": "^2.0.0", | ||
"@gecut/types": "^3.0.0", | ||
"@gecut/utilities": "^6.0.0", | ||
"@types/bun": "latest" | ||
}, | ||
"scripts": { | ||
"build": "bun build ./src/index.ts", | ||
"build:all": "bun build ./src/**.ts" | ||
} | ||
} |
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,62 @@ | ||
import { GecutLogger } from '@gecut/logger' | ||
import { $GroupSchema, $SessionSchema, $UserSchema } from '@promeet/schemas' | ||
import mongoose from 'mongoose' | ||
|
||
import type { GroupInterface, SessionInterface, UserInterface } from '@promeet/types' | ||
|
||
export class Database { | ||
constructor( | ||
private uri: string, | ||
private logger: GecutLogger = new GecutLogger('db-connector'), | ||
private options?: mongoose.ConnectOptions, | ||
) { | ||
this.options = { | ||
dbName: 'test', | ||
|
||
...(this.options ?? {}), | ||
} | ||
} | ||
|
||
$user = mongoose.model<UserInterface>('user', $UserSchema) | ||
$group = mongoose.model<GroupInterface>('group', $GroupSchema) | ||
$session = mongoose.model<SessionInterface>('session', $SessionSchema) | ||
|
||
connector?: typeof mongoose | ||
|
||
async connect() { | ||
if (this.uri.startsWith('mongodb://') || this.uri.startsWith('mongodb+srv://')) { | ||
this.logger.method?.('connect') | ||
|
||
try { | ||
return (this.connector = await mongoose.connect(this.uri, this.options)) | ||
} | ||
catch (error) { | ||
return this.logger.error('connect', 'connect_failed', error) | ||
} | ||
} | ||
|
||
this.logger.error('connect', 'uri_not_valid', { uri: this.uri, options: this.options }) | ||
|
||
return null | ||
} | ||
|
||
async initialize() { | ||
if (this.connector == null) return -1 | ||
|
||
const total = await this.totalDocuments() | ||
|
||
this.logger.methodFull?.('initialize', '', total) | ||
|
||
return total | ||
} | ||
|
||
private async totalDocuments() { | ||
const [user, group, session] = await Promise.all([ | ||
this.$user.countDocuments(), | ||
this.$group.countDocuments(), | ||
this.$session.countDocuments(), | ||
]) | ||
|
||
return { user, group, session } | ||
} | ||
} |
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,3 @@ | ||
{ | ||
"extends": "../../../tsconfig.json" | ||
} |
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
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