-
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
Ryan Smith
committed
Feb 24, 2024
1 parent
4b5dd08
commit b581bcd
Showing
4 changed files
with
150 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
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 { Collection, Db, Filter, MongoClient, WithId } from 'mongodb'; | ||
import { CharityUser } from '../../appsync'; | ||
|
||
export class CharityUserRepository { | ||
private static instance: CharityUserRepository; | ||
private readonly client: MongoClient; | ||
private readonly db: Db; | ||
private readonly collection: Collection<CharityUser>; | ||
|
||
private constructor() { | ||
this.client = new MongoClient( | ||
process?.env?.MONGODB_CONNECTION_STRING ?? 'mongodb://localhost:27017/', | ||
{ | ||
auth: { | ||
username: process?.env?.MONGODB_ADMIN_USERNAME, | ||
password: process?.env?.MONGODB_ADMIN_PASSWORD, | ||
}, | ||
} | ||
); | ||
this.db = this.client.db('D2E'); | ||
this.collection = this.db.collection<CharityUser>('CharityUser'); | ||
} | ||
|
||
static getInstance(): CharityUserRepository { | ||
if (!this.instance) { | ||
this.instance = new CharityUserRepository(); | ||
} | ||
return this.instance; | ||
} | ||
|
||
private async getByQuery(query: Filter<CharityUser>): Promise<WithId<CharityUser>[]> { | ||
const cursor = this.collection.find(query); | ||
|
||
if (!(await cursor.hasNext())) { | ||
return []; | ||
} | ||
|
||
return await cursor.toArray(); | ||
} | ||
|
||
private async getOne(query: Filter<CharityUser>): Promise<WithId<CharityUser> | undefined> { | ||
const result = await this.collection.findOne(query); | ||
|
||
if (!result) { | ||
return undefined; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public async getByEmail(email: string): Promise<WithId<CharityUser> | undefined> { | ||
return await this.getOne({ email }); | ||
} | ||
|
||
public async list(): Promise<WithId<CharityUser>[]> { | ||
return await this.getByQuery({}); | ||
} | ||
|
||
public async insert(charity: CharityUser): Promise<boolean> { | ||
return (await this.collection.insertOne(charity)).acknowledged; | ||
} | ||
} |
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 { Collection, Db, Filter, MongoClient, WithId } from 'mongodb'; | ||
import { SchoolUser } from '../../appsync'; | ||
|
||
export class SchoolUserRepository { | ||
private static instance: SchoolUserRepository; | ||
private readonly client: MongoClient; | ||
private readonly db: Db; | ||
private readonly collection: Collection<SchoolUser>; | ||
|
||
private constructor() { | ||
this.client = new MongoClient( | ||
process?.env?.MONGODB_CONNECTION_STRING ?? 'mongodb://localhost:27017/', | ||
{ | ||
auth: { | ||
username: process?.env?.MONGODB_ADMIN_USERNAME, | ||
password: process?.env?.MONGODB_ADMIN_PASSWORD, | ||
}, | ||
} | ||
); | ||
this.db = this.client.db('D2E'); | ||
this.collection = this.db.collection<SchoolUser>('SchoolUser'); | ||
} | ||
|
||
static getInstance(): SchoolUserRepository { | ||
if (!this.instance) { | ||
this.instance = new SchoolUserRepository(); | ||
} | ||
return this.instance; | ||
} | ||
|
||
private async getByQuery(query: Filter<SchoolUser>): Promise<WithId<SchoolUser>[]> { | ||
const cursor = this.collection.find(query); | ||
|
||
if (!(await cursor.hasNext())) { | ||
return []; | ||
} | ||
|
||
return await cursor.toArray(); | ||
} | ||
|
||
private async getOne(query: Filter<SchoolUser>): Promise<WithId<SchoolUser> | undefined> { | ||
const result = await this.collection.findOne(query); | ||
|
||
if (!result) { | ||
return undefined; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public async getByEmail(email: string): Promise<WithId<SchoolUser> | undefined> { | ||
return await this.getOne({ email }); | ||
} | ||
|
||
public async list(): Promise<WithId<SchoolUser>[]> { | ||
return await this.getByQuery({}); | ||
} | ||
|
||
public async insert(school: SchoolUser): Promise<boolean> { | ||
return (await this.collection.insertOne(school)).acknowledged; | ||
} | ||
} |