oscarthroedsson
oscarthroedsson
Explore posts from servers
PPrisma
Created by oscarthroedsson on 3/14/2024 in #help-and-questions
Multiple datasources
Wondering if I can use something like this:
const mongoDB = new PrismaClient({
datasourceUrl: 'MONGO uri',
})
const mongoDB = new PrismaClient({
datasourceUrl: 'MONGO uri',
})
I am also wondering how the connection with DB and collection goes with prisma. I am doing old fashion right now. But they use prisma for mySQL and i thought it would be better to use prisma for both, it we can.
import mongoose from "mongoose"; //connects to DB
import { MongoClient } from "mongodb";
//process.env.API_KEY

const uri = process.env.MONGO_URL;
const databaseName = process.env.MONGO_DB_NAME;
const collectionName = process.env.MONGO_COLLECTION;

console.log("๐Ÿ”— URI: ", uri);
console.log("DB NAME: ", databaseName);
console.log("DB NAME: ", collectionName);

const client = new MongoClient(uri);
const database = client.db(databaseName);

const connectToDatabase = async () => {
try {
await mongoose.connect(uri);
console.log("๐Ÿƒ | Successfully connected to MongoDB. ๐ŸŽ‰");
} catch (error) {
console.error("๐Ÿงน | Could not connect to MongoDB: ", error);
}
};
connectToDatabase();

export function mongoDBstring() {
const wholeColl = client.db(databaseName).collection(collectionName);
return wholeColl;
}
import mongoose from "mongoose"; //connects to DB
import { MongoClient } from "mongodb";
//process.env.API_KEY

const uri = process.env.MONGO_URL;
const databaseName = process.env.MONGO_DB_NAME;
const collectionName = process.env.MONGO_COLLECTION;

console.log("๐Ÿ”— URI: ", uri);
console.log("DB NAME: ", databaseName);
console.log("DB NAME: ", collectionName);

const client = new MongoClient(uri);
const database = client.db(databaseName);

const connectToDatabase = async () => {
try {
await mongoose.connect(uri);
console.log("๐Ÿƒ | Successfully connected to MongoDB. ๐ŸŽ‰");
} catch (error) {
console.error("๐Ÿงน | Could not connect to MongoDB: ", error);
}
};
connectToDatabase();

export function mongoDBstring() {
const wholeColl = client.db(databaseName).collection(collectionName);
return wholeColl;
}
3 replies