-
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
7c49b3c
commit b5416f5
Showing
24 changed files
with
260 additions
and
107 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
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": ["gecut", "Jsonify", "Jsonize", "promeet", "Virtuals", "Zamanian"] | ||
"cSpell.words": ["gecut", "hono", "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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { GecutLogger } from '@gecut/logger' | ||
import { $GroupSchema, $SessionSchema, $UserSchema } from '@promeet/schemas' | ||
import mongoose from 'mongoose' | ||
import { GecutLogger } from '@gecut/logger'; | ||
import { $GroupSchema, $SessionSchema, $UserSchema } from '@promeet/schemas'; | ||
import mongoose from 'mongoose'; | ||
|
||
import type { GroupInterface, SessionInterface, UserInterface } from '@promeet/types' | ||
import type { GroupInterface, SessionInterface, UserInterface } from '@promeet/types'; | ||
|
||
export class Database { | ||
constructor( | ||
|
@@ -14,49 +14,70 @@ export class Database { | |
dbName: 'test', | ||
|
||
...(this.options ?? {}), | ||
} | ||
}; | ||
} | ||
|
||
$user = mongoose.model<UserInterface>('user', $UserSchema) | ||
$group = mongoose.model<GroupInterface>('group', $GroupSchema) | ||
$session = mongoose.model<SessionInterface>('session', $SessionSchema) | ||
$user = mongoose.model<UserInterface>('user', $UserSchema); | ||
$group = mongoose.model<GroupInterface>('group', $GroupSchema); | ||
$session = mongoose.model<SessionInterface>('session', $SessionSchema); | ||
|
||
connector?: typeof mongoose | ||
connector?: typeof mongoose; | ||
|
||
async connect() { | ||
if (this.uri.startsWith('mongodb://') || this.uri.startsWith('mongodb+srv://')) { | ||
this.logger.method?.('connect') | ||
this.logger.method?.('connect'); | ||
|
||
try { | ||
return (this.connector = await mongoose.connect(this.uri, this.options)) | ||
return (this.connector = await mongoose.connect(this.uri, this.options)); | ||
} | ||
catch (error) { | ||
return this.logger.error('connect', 'connect_failed', error) | ||
return this.logger.error('connect', 'connect_failed', error); | ||
} | ||
} | ||
|
||
this.logger.error('connect', 'uri_not_valid', { uri: this.uri, options: this.options }) | ||
this.logger.error('connect', 'uri_not_valid', { uri: this.uri, options: this.options }); | ||
|
||
return null | ||
return null; | ||
} | ||
|
||
async initialize() { | ||
if (this.connector == null) return -1 | ||
if (this.connector == null) return -1; | ||
|
||
let total = await this.totalDocuments(); | ||
|
||
if (total.user + total.group + total.session < 1) { | ||
await this.$user.db.dropDatabase(); | ||
|
||
await this.createDefaults(); | ||
|
||
const total = await this.totalDocuments() | ||
total = await this.totalDocuments(); | ||
} | ||
|
||
this.logger.methodFull?.('initialize', '', total) | ||
this.logger.methodFull?.('initialize', '', total); | ||
|
||
return 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 }; | ||
} | ||
|
||
private async createDefaults() { | ||
const [user] = await Promise.all([ | ||
this.$user.create({ | ||
firstName: 'سید محمدمهدی', | ||
lastName: 'زمانیان', | ||
phoneNumber: '09155595488', | ||
email: '[email protected]', | ||
}), | ||
]); | ||
|
||
return { user, group, session } | ||
return { user }; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from './group' | ||
export * from './user' | ||
export * from './session' | ||
export * from './group'; | ||
export * from './user'; | ||
export * from './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
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import type { ArrayValues, Jsonify } from '@gecut/types' | ||
import type { Types, Document } from 'mongoose' | ||
import type { ArrayValues, Jsonify } from '@gecut/types'; | ||
import type { Types, Document } from 'mongoose'; | ||
|
||
export const Models = ['user', 'group', 'session'] as const | ||
export type ModelsType = ArrayValues<typeof Models> | ||
export const Models = ['user', 'group', 'session'] as const; | ||
export type ModelsType = ArrayValues<typeof Models>; | ||
|
||
export interface Entity extends Document { | ||
_id: Types.ObjectId | ||
_id: Types.ObjectId; | ||
|
||
disabled?: boolean | ||
createdAt: Date | ||
disabled?: boolean; | ||
createdAt: Date; | ||
} | ||
|
||
export type Jsonize<T extends Entity> = Jsonify<Omit<T, keyof Document> & { _id: string }> | ||
export type Jsonize<T extends Entity> = Jsonify<Omit<T, keyof Document> & { _id: string }>; |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from './group' | ||
export * from './user' | ||
export * from './session' | ||
export * from './group'; | ||
export * from './user'; | ||
export * from './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
Oops, something went wrong.