Skip to content

Commit

Permalink
ar(db) names
Browse files Browse the repository at this point in the history
  • Loading branch information
varsnothing committed Jan 20, 2024
1 parent 2d867e1 commit 232a3d4
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 12 deletions.
2 changes: 2 additions & 0 deletions lib/model/interfaces/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// constants.ts
export const DATABASE_STRING = process.env.MONGODB_DATABASE || "test" | "auth";
3 changes: 3 additions & 0 deletions lib/model/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

/* mdb-ifaces */

/* const */
export { DATABASE_STRING } from "./constants"

// read
export { getUserMeta } from "./mdb-get-interface";

Expand Down
18 changes: 9 additions & 9 deletions lib/model/interfaces/mdb-get-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// @ts-nocheck
/* eslint-disable @typescript-eslint/no-unused-vars */
import type { UserSchema, INCharacter, UserDecoration } from "@types";
import { MongoConnector } from "@model";
import { MongoConnector, DATABASE_STRING } from "@model";

type Tdbs = "test";
type Tdbs = "test" | "auth";

interface TModelSingleton {
[x: string]: {
Expand Down Expand Up @@ -40,26 +40,26 @@ const init =
return _db;
};

init.test = init("test");
init[DATABASE_STRING] = init(DATABASE_STRING);

const getCollection =
async (_db = "test") =>
async (_db = DATABASE_STRING) =>
async (_collection = "users") => {
if (!init[_db].db) {
const db = await init[_db]();
init[_db].db = db;
}
const collection = await init[_db].db.collection(_collection);
if (!init[_db].db) return new Error("db not reachable");
init["test"].collections = { ...init["test"].collections };
init["test"].collections[_collection] = collection;
init[DATABASE_STRING].collections = { ...init[DATABASE_STRING].collections };
init[DATABASE_STRING].collections[_collection] = collection;
return collection;
};

const getUserCollection = async () => {
const col = await getCollection("test");
const col = await getCollection(DATABASE_STRING);
const _col = await col("users");
init["test"].collections["users"] = _col;
init[DATABASE_STRING].collections["users"] = _col;
return _col;
};

Expand All @@ -79,7 +79,7 @@ const defineSchema =
};

const defineUserSchema = defineSchema({
db: "test",
db: DATABASE_STRING,
collection: "users",
schema: _UserSchema,
});
Expand Down
2 changes: 1 addition & 1 deletion lib/model/interfaces/mdb-update-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type { UserSchema, INCharacter, UserDecoration } from "@types";
import { MongoConnector } from "@model";

type Tdbs = "test";
type Tdbs = "test" | "auth";

interface TModelSingleton {
[x: string]: {
Expand Down
2 changes: 1 addition & 1 deletion lib/model/mdb-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (!process.env.MONGODB_URI) {
}

const uri = process.env.MONGODB_URI;
const options = { databaseName: 'auth' };
const options = {};

let client;
let clientPromise: Promise<MongoClient>;
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/auth/[...nextauth]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { authOptions } from "@auth";
// https://authjs.dev/reference/providers/oauth

const finalAuth = {
adapter: MongoDBAdapter(MongoConnector),
adapter: MongoDBAdapter(MongoConnector, { databaseName: process.env.MONGODB_DATABASE || 'test'}),
...authOptions,
};

Expand Down

0 comments on commit 232a3d4

Please sign in to comment.