Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clarify documentation for .connect method #86

Open
wjessup opened this issue Nov 13, 2016 · 3 comments
Open

clarify documentation for .connect method #86

wjessup opened this issue Nov 13, 2016 · 3 comments

Comments

@wjessup
Copy link

wjessup commented Nov 13, 2016

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.

@scottwrobinson
Copy link
Owner

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!

@lurchpop
Copy link

lurchpop commented Apr 7, 2017

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).

@adamelliotfields
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants