-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: first pass at a database repository interface
- Loading branch information
Jon Carlson
committed
Dec 5, 2024
1 parent
52d1c48
commit ca6dd50
Showing
7 changed files
with
126 additions
and
90 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,58 @@ | ||
import { DatabaseRepositoryInterface } from 'database/types' | ||
import { Db, Document, MongoClient, WithId } from 'mongodb' | ||
|
||
export class MongoRepository<T> implements DatabaseRepositoryInterface<T> { | ||
#client: MongoClient | ||
#db: Db | ||
|
||
constructor(client: MongoClient, dbName?: string) { | ||
this.#client = client | ||
this.#db = this.#client.db(dbName ?? process.env.DB_NAME) | ||
} | ||
|
||
getDb() { | ||
// TODO: remove | ||
return this.#db | ||
} | ||
|
||
async findAll(collection: string): Promise<T[]> { | ||
const models = await this.#db | ||
.collection(collection) | ||
.aggregate( | ||
[ | ||
{ $sort: { 'x-meditor.modifiedOn': -1 } }, // Sort descending by version (date) | ||
{ $group: { _id: '$name', doc: { $first: '$$ROOT' } } }, // Grab all fields in the most recent version | ||
{ $replaceRoot: { newRoot: '$doc' } }, // Put all fields of the most recent doc back into root of the document | ||
], | ||
{ allowDiskUse: true } | ||
) | ||
.toArray() | ||
|
||
return this.#makeSafeObjectIDs(models) | ||
} | ||
|
||
async find( | ||
collection: string, | ||
title: string, | ||
titleProperty: string = 'title' | ||
): Promise<T> { | ||
const models = await this.#db | ||
.collection(collection) | ||
.find({ [titleProperty]: title }) | ||
.sort({ 'x-meditor.modifiedOn': -1 }) | ||
.limit(1) | ||
.toArray() | ||
|
||
return this.#makeSafeObjectIDs(models)[0] | ||
} | ||
|
||
/** | ||
* Next doesn't know how to process the Mongo _id property, as it's an object, not a string. So this hack parses ahead of time | ||
* https://github.com/vercel/next.js/issues/11993 | ||
*/ | ||
#makeSafeObjectIDs( | ||
records: Record<string, any> | Record<string, any>[] | WithId<Document> | null | ||
) { | ||
return !!records ? JSON.parse(JSON.stringify(records)) : records | ||
} | ||
} |
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,46 @@ | ||
import { DatabaseRepositoryInterface } from './types' | ||
import { MongoRepository } from './repositories/mongo.repository' | ||
import log from 'lib/log' | ||
import { MongoClient } from 'mongodb' | ||
|
||
export async function getDatabaseRepository<T>(): Promise< | ||
DatabaseRepositoryInterface<T> | ||
> { | ||
const type = process.env.DB_CONNECTION ?? 'mongo' | ||
|
||
if (type === 'mongo') { | ||
const client = await getMongoClient() | ||
return new MongoRepository<T>(client) | ||
} | ||
|
||
throw new Error(`Unsupported repository type: ${type}`) | ||
} | ||
|
||
async function getMongoClient(): Promise<MongoClient> { | ||
const uri = `mongodb://${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_DATABASE}` | ||
|
||
let mongoClient: MongoClient | ||
let mongoClientPromise: Promise<MongoClient> | ||
|
||
if (process.env.NODE_ENV === 'development') { | ||
// In development mode, use a global variable so that the value | ||
// is preserved across module reloads caused by HMR (Hot Module Replacement). | ||
if (!globalThis._mongoClientPromise) { | ||
log.info('Connecting to MongoDB (DEV): ', uri) | ||
|
||
mongoClient = new MongoClient(uri) | ||
// @ts-ignore in development | ||
global._mongoClientPromise = mongoClient.connect() | ||
} | ||
// @ts-ignore in development | ||
mongoClientPromise = global._mongoClientPromise | ||
} else { | ||
log.info('Connecting to MongoDB: ', uri) | ||
|
||
// In production mode, it's best to not use a global variable. | ||
mongoClient = new MongoClient(uri) | ||
mongoClientPromise = mongoClient.connect() | ||
} | ||
|
||
return mongoClientPromise | ||
} |
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,4 @@ | ||
export interface DatabaseRepositoryInterface<T> { | ||
findAll(collection: string): Promise<T[]> | ||
find(collection: string, title: string, titleProperty?: string): Promise<T> | ||
} |
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 was deleted.
Oops, something went wrong.
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