Skip to content

Commit

Permalink
playing with mongoose
Browse files Browse the repository at this point in the history
  • Loading branch information
marekzelinka committed Jul 23, 2024
1 parent 53c6b3f commit 78c7bdb
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 1 deletion.
46 changes: 46 additions & 0 deletions mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import mongoose from 'mongoose'

if (process.argv.length < 3) {
console.error('give password as argument')
process.exit(1)
}

let password = encodeURIComponent(process.argv[2])

let uri = `mongodb+srv://user:${password}@cluster0.10skx.mongodb.net/notes-backend?retryWrites=true&w=majority&appName=Cluster0`
let clientOptions = {
serverApi: { version: '1', strict: true, deprecationErrors: true },
}

run().catch(console.dir)

async function run() {
try {
// Create a Mongoose client with a MongoClientOptions object to set the Stable API version
await mongoose.connect(uri, clientOptions)
await mongoose.connection.db.admin().command({ ping: 1 })
console.log(
'Pinged your deployment. You successfully connected to MongoDB!',
)

let noteSchema = new mongoose.Schema({
content: String,
important: Boolean,
})

let Note = mongoose.model('Note', noteSchema)

// let note = new Note({ content: 'React is cool', important: false })
// let result = await note.save()
// console.log('note saved!', result)

let notes = await Note.find({ important: true })
console.log('saved notes')
for (let note of notes) {
console.log(note)
}
} finally {
// Ensures that the client will close when you finish/error
await mongoose.disconnect()
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.19.2"
"express": "^4.19.2",
"mongoose": "^8.5.1"
}
}
178 changes: 178 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 78c7bdb

Please sign in to comment.