Open wjessup opened 7 years ago
Sorry for not getting back to you sooner. I've been away from the project for a bit but am finally getting back to maintaining it.
Thanks for the suggestion. You're right that the documentation (especially for .connect()
) is lacking. I'll include some improvements in the next release. Thanks!
Also don't like the idea of connect just storing a global CLIENT object assuming my entire app is going to use a single connection/database. would be nice if I could have collections in different files and be able to specify which connection goes to which file (for NEDB).
I had to play around with it for a little bit, but after reading the README and Scott's blog post, I realized almost everything returns a promise, so control flow can be managed with async/await
or wrapping with co(function * () {}))
and halting with yield
.
Since a lot of people learn Mongoose first (and in some cases have to use it at work), it might help to demonstrate an example using Mongoose and how Camo differs. For example, in an Express app with Mongoose, you can just put:
mongoose.connect('mongodb://localhost:27017/database');
const db = mongoose.connection;
At the top of your app.js
and it'll just work.
Whereas in Camo, all database interactions must follow camo.connect()
after the promise has been resolved.
Here's a basic demo using async/await (you can use try/catch blocks for error handling), that might help anyone just getting started.
Assume that a Person
class was defined before this:
(async function () {
// Connect to NeDB database
// Creates 'nedb' folder at the project root if it doesn't exist already
await camo.connect('nedb://nedb');
console.log('Connected to database...');
// Delete all documents
await Person.deleteMany({});
// Create a new Person document
const adam = Person.create({ name: 'Adam' });
// Save the adam document to the persons collection
// Will create './nedb/persons.db' unless overridden by static collectionName()
await adam.save();
// Find all documents
const results = await Person.find({});
console.log('\n' + 'Documents found:');
console.log(results);
}());
@lurchpop, could you clarify your comment a little more - perhaps explain what your use case is? When you connect to a NeDB folder, Camo will create collection files for each Model you've defined (unless the Model is an EmbeddedDocument
). A NeDB database is just a folder with .db
files in it. Each file (collection) is just a bunch of JSON objects (documents) separated by line endings.
Just a quick suggestion to clear this up with examples. I had to check the source at https://github.com/scottwrobinson/camo/blob/master/lib/db.js to get the right format.