enabled connection to mongodb

This commit is contained in:
2023-11-27 10:56:38 -06:00
parent 2fd3eae66a
commit 368a38c1b0
10 changed files with 60 additions and 15 deletions

1
.gitignore vendored
View File

@@ -8,6 +8,7 @@ package-lock.json
# assets
/seed_data
/certs
# testing
/coverage

0
app/log/layout.tsx Normal file
View File

17
mdx-components.tsx Normal file
View File

@@ -0,0 +1,17 @@
import type { MDXComponents } from "mdx/types";
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
...components,
h1: (props) =>
<h1 className="text-3xl text-black font-semi-bold" {...props} />,
h2: (props) =>
<h2 className="text-2xl text-black font-semi-bold" {...props} />,
h3: (props) =>
<h3 className="text-xl text-black font-semi-bold" {...props} />,
p: (props) => <p className="text-black" {...props} />,
a: (props) => <a className="text-violet-900" {...props} />,
ul: (props) => <ul className="list-disc" {...props} />,
li: (props) => <li className="text-black" {...props} />,
}
}

View File

@@ -1,7 +1,9 @@
import createMDX from '@next/mdx'
/** @type {import('next').NextConfig} */
const nextConfig = {
pageExtensions: ['js', 'jsx', 'ts', 'tsx'],
pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'mdx'],
reactStrictMode: true,
}
export default nextConfig;
export default createMDX()(nextConfig);

View File

@@ -8,12 +8,15 @@
"build": "next build",
"start": "next start",
"lint": "next lint",
"db-seed": "node server/db/seed.js $1",
"db-seed": "node --env-file=.env ./server/db/seed.js",
"s3-seed": "ts-node --project ./tsconfig.json ./server/s3/seed.ts"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.367.0",
"@aws-sdk/s3-request-presigner": "^3.445.0",
"@mdx-js/loader": "^3.0.0",
"@mdx-js/react": "^3.0.0",
"@next/mdx": "^14.0.3",
"@smithy/node-http-handler": "^2.1.8",
"@t3-oss/env-nextjs": "^0.7.0",
"@vercel/style-guide": "^5.0.1",
@@ -28,6 +31,7 @@
"uuid": "^9.0.0"
},
"devDependencies": {
"@types/mdx": "^2.0.10",
"@types/node": "^20.10.0",
"@types/pg": "^8.10.3",
"@types/react": "18.2.7",

View File

@@ -1,24 +1,24 @@
import { createDBClient } from '../db/createClient';
import { Maybe } from '@/util/helpers';
import { ParseParams, SafeParseReturnType } from 'zod';
import { ParseParams } from 'zod';
import { MongoClient, WithId, Filter, InsertOneResult } from 'mongodb';
type FullParserType<T extends { [key: string]: any }> = (data: any, params?: Partial<ParseParams> | undefined) => SafeParseReturnType<any, T>
type FullParserType<T extends { [key: string]: any }> = (data: any, params?: Partial<ParseParams>) => T;
type ControllerOptions<T extends { [key: string]: any }> = {
tableName: string
parser?: FullParserType<T>
parse: FullParserType<T>
}
export default abstract class BaseController<T extends { _id?: any, [key: string]: any }> {
protected client: MongoClient
collectionName: string
parser?: FullParserType<T>
parse: FullParserType<T>
constructor(options: ControllerOptions<T>) {
this.collectionName = options.tableName;
this.client = createDBClient();
this.parser = options.parser;
this.parse = options.parse;
}
async getAll() {
@@ -59,6 +59,7 @@ export default abstract class BaseController<T extends { _id?: any, [key: string
async post(data: T) {
let result: Maybe<InsertOneResult<T>>;
this.parse(data);
try {
await this.client.connect();

View File

@@ -5,7 +5,7 @@ export default class BlogPostController extends BaseController<BlogPost> {
constructor() {
super({
tableName: "blogposts",
parser: ZBlogPost.safeParse,
parse: ZBlogPost.parse,
})
}
}

View File

@@ -5,7 +5,7 @@ export default class MusicController extends BaseController<MusicStreamingEntry>
constructor() {
super({
tableName: "music",
parser: ZMusicStreamingEntry.safeParse,
parse: ZMusicStreamingEntry.parse,
})
}
}

View File

@@ -1,6 +1,9 @@
import { env } from "@/env.mjs";
import { MongoClient, MongoClientOptions } from "mongodb";
import { MongoClient } from "mongodb";
export function createDBClient(opts?: MongoClientOptions) {
return new MongoClient(env.MONGO_URL, opts)
export function createDBClient() {
return new MongoClient(env.MONGO_URL, {
tls: true,
tlsCertificateKeyFile: process.cwd() + "/certs/mongo_cert.pem",
});
}

View File

@@ -5,7 +5,20 @@ import { z } from "zod";
async function main() {
console.log("Preparing seed data...")
const connectionString = z.string().safeParse(process.argv[2]).data;
const nodeMajorVersion = parseInt(process.version.split(".")[0].split("v")[1]);
const nodeMinorVersion = parseInt(process.version.split(".")[1]);
if (nodeMajorVersion < 20 || (nodeMajorVersion === 20 && nodeMinorVersion < 10)) {
throw new Error("Database seed depends on Node version 20.10.0 or higher");
}
if (!process.env.MONGO_URL) throw new Error("Missing required variable `MONGO_URL` for database connection");
const connectionString = z.string().url().parse(process.env.MONGO_URL);
// check if documents and collections exist
const client = new MongoClient(connectionString, {
tlsCertificateKeyFile: process.cwd() + "/certs/mongo_cert.pem",
});
/** @type {import("./schema").MusicStreamingEntry} */
const jisei = {
@@ -44,7 +57,10 @@ async function main() {
try {
console.log("Connecting to MongoDB...")
const client = new MongoClient(connectionString);
const client = new MongoClient(connectionString, {
tlsCertificateKeyFile: process.cwd() + "/certs/mongo_cert.pem",
});
await client.connect();
const db = client.db("mikayladotdev");
@@ -63,6 +79,7 @@ async function main() {
}
console.log("Done!")
process.exit(0);
}
main();