xxxxxxxxxx
// /src/app/lib/db.js
import mongoose from 'mongoose'
const MONGODB_URI = process.env.MONGO_URI
if (!MONGODB_URI) {
throw new Error(
'Please define the MONGODB_URI environment variable inside .env.local',
)
}
let cached = global.mongoose
if (!cached) {
cached = global.mongoose = { conn: null, promise: null }
}
async function dbConnect() {
if (cached.conn) {
return cached.conn
}
if (!cached.promise) {
const opts = {
bufferCommands: false,
}
cached.promise = mongoose.connect(MONGODB_URI, opts).then(mongoose => {
console.log('Db connected')
return mongoose
})
}
try {
cached.conn = await cached.promise
} catch (e) {
cached.promise = null
throw e
}
return cached.conn
}
export default dbConnect
xxxxxxxxxx
import mongoose from 'mongoose'
const MONGODB_URI = process.env.MONGODB_URI
if (!MONGODB_URI) {
throw new Error(
'Please define the MONGODB_URI environment variable inside .env.local'
)
}
/**
* Global is used here to maintain a cached connection across hot reloads
* in development. This prevents connections growing exponentially
* during API Route usage.
*/
let cached = global.mongoose
if (!cached) {
cached = global.mongoose = { conn: null, promise: null }
}
async function dbConnect() {
if (cached.conn) {
return cached.conn
}
if (!cached.promise) {
const opts = {
bufferCommands: false,
}
cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
return mongoose
})
}
cached.conn = await cached.promise
return cached.conn
}
export default dbConnect
xxxxxxxxxx
import mongoose from "mongoose"
const MONGO_URI = process.env.MONGO_URI
const cached = {}
async function connectMongo() {
if (!MONGO_URI) {
throw new Error(
"Please define the MONGO_URI environment variable inside .env.local"
)
}
if (cached.connection) {
return cached.connection
}
if (!cached.promise) {
const opts = {
bufferCommands: false
}
cached.promise = mongoose.connect(MONGO_URI, opts)
}
try {
cached.connection = await cached.promise;
console.log("database connected successfully!");
} catch (e) {
cached.promise = undefined
throw e
}
return cached.connection
}
export default connectMongo
xxxxxxxxxx
import mongoose from 'mongoose'
const MONGODB_URI = process.env.MONGODB_URI
if (!MONGODB_URI) {
throw new Error(
'Please define the MONGODB_URI environment variable inside .env.local'
)
}
/**
* Global is used here to maintain a cached connection across hot reloads
* in development. This prevents connections growing exponentially
* during API Route usage.
*/
let cached = global.mongoose
if (!cached) {
cached = global.mongoose = { conn: null, promise: null }
}
async function dbConnect() {
if (cached.conn) {
return cached.conn
}
if (!cached.promise) {
const opts = {
bufferCommands: false,
}
cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
return mongoose
})
}
cached.conn = await cached.promise
return cached.conn
}
export default dbConnect
xxxxxxxxxx
( https://stackoverflow.com/questions/7880921/what-is-a-parent-table-and-a-child-table-in-database )
Child tables and parent tables are just normal database tables, but they’re linked in a way that's described by a parent–child relationship.
It’s usually used to specify where one table’s value refers to the value in another table (usually a primary key of another table).
For example, imagine a news article. This could be represented by a table called articles and has fields for id, headline, body, published_date and author. But instead of placing a name in the author field, you could instead put the ID value of a user in a separate table—maybe called authors—that has information on authors such as id, name, and email.
Therefore, if you need to update an author’s name, you only need to do so in the authors (parent) table; because the articles (child) table only contains the ID of the corresponding author record.
Hope this helps you understand better.
( chat gpt )
Parent Table
The parent table is the main table that holds the primary information. It typically contains a primary key, which uniquely identifies each record in that table. Other tables (child tables) refer to this primary key to establish relationships between the data.
Primary Key (PK): A column (or set of columns) that uniquely identifies each row in the table.
The parent table does not rely on any other table for its key data.
Child Table
The child table is the table that depends on the parent table. It holds a foreign key column, which references the primary key of the parent table. This establishes a relationship between the two tables, often meaning that a record in the child table corresponds to a record in the parent table.
Foreign Key (FK): A column (or set of columns) in the child table that refers to the primary key of the parent table.
The child table often stores detailed or dependent information related to the records in the parent table.