Created
October 29, 2021 19:54
-
-
Save dbasilioesp/890bd1b3b572dba19a6f7561ee8ee526 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const mongoose = require("mongoose"); | |
const { Schema } = mongoose; | |
const url = "mongodb://localhost:27017/goji"; | |
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }); | |
function connect() { | |
return new Promise((resolve, reject) => { | |
const db = mongoose.connection; | |
db.on("error", (error) => { | |
console.error("connection error:", error); | |
reject(); | |
}); | |
db.once("open", function () { | |
// we're connected! | |
resolve(); | |
}); | |
}); | |
} | |
async function main() { | |
await connect(); | |
const personSchema = Schema({ | |
name: String, | |
age: Number, | |
stories: [{ type: Schema.Types.ObjectId, ref: "Story" }], | |
}); | |
const storySchema = Schema({ | |
author: { type: Schema.Types.ObjectId, ref: "Person" }, | |
title: String, | |
fans: [{ type: Schema.Types.ObjectId, ref: "Person" }], | |
}); | |
const Story = mongoose.model("Story", storySchema); | |
const Person = mongoose.model("Person", personSchema); | |
let david = new Person({ name: "David", age: 31 }); | |
let story = new Story({ author: david, title: "Running code" }); | |
// await david.save(); | |
console.log(david, story); | |
mongoose.disconnect(); | |
// const Cat = mongoose.model("Cat", { name: String }); | |
// const kitty = new Cat({ name: "Zildjian" }); | |
// await kitty.save(); | |
// console.log("meow"); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment