vadimdemedes / mongorito

🍹 MongoDB ODM for Node.js apps based on Redux
1.38k stars 90 forks source link

Add usage examples #84

Open kulakowka opened 8 years ago

kulakowka commented 8 years ago

@vdemedes can you add a simple koa-application example?

vadimdemedes commented 8 years ago

Sure

hafffe commented 8 years ago

:+1:

regniblod commented 8 years ago

+1

vadimdemedes commented 8 years ago

Added a few basic examples on how to use Mongorito, more coming soon: https://github.com/vdemedes/mongorito/tree/master/examples

vadimdemedes commented 8 years ago

Readme got also updated with a lot more specific code snippets and quick docs.

hanaarena commented 8 years ago

looks coooool~ :+1: Have the road map or coming feature for this project?

vadimdemedes commented 8 years ago

Everything that's planned is in the Issues, no separate roadmap yet.

dcsan commented 8 years ago

those are pretty convoluted examples can you write one simple self contained example of how to use mongorito?

vadimdemedes commented 8 years ago

@dcsan What specifically is unclear to you?

dcsan commented 8 years ago

attributes example seems to be the one that actually does something useful.. but how to extract that?

const mongorito = require('../');

where is the connection made?

yield mongorito.connect # has to be defined

const run = require('./run');

function run (fn) {
    co(fn).catch(function (err) {
        console.error(err.stack);
    });
}

run(function * () {
dcsan commented 8 years ago

trying to get a single file example to work

i'm not familiar with co so this is probably obvious to you but ... the above doesn't run as I guess its still waiting on a generator?

dcsan commented 8 years ago
'use strict';

const co = require('co');
const mongorito = require('mongorito');

/*
 * Run a generator function and print errors if any
 */
function run (fn) {
    co(fn).catch(function (err) {
        console.error(err.stack);
    });
}

function connect() {
    run(function * () {
        console.log('connecting');
        yield mongorito.connect('localhost/mongorito-test');
        console.log('connecting DONE');
    });
}

function disconnect() {
    run(function * () {
        yield mongorito.disconnect();
        console.log('disconnected');
    });
}

const Model = mongorito.Model;
class Tags extends Model {}

var tag = new Tags({
    title: 'cool-stuff',
    body: 'info about this tag'
});

function* main() {
    console.log('main');
    yield connect();
    yield tag.save();
    console.log('saved');
}

run(main() );
dcsan commented 8 years ago

TypeError: You may only yield a function, promise, generator, array, or object, but the following object was passed: "undefined"

how to actually start things off?

vadimdemedes commented 8 years ago

Your errors have nothing to do with mongorito. I recommend giving a read on generators and co module one more time. And after that, check out examples again, I'm sure you will find them useful. They have more than enough to get started. Also, of course, readme.

P.S. You don't have to use generators at all, all methods return promises, if you find that more suitable.

dcsan commented 8 years ago

OK fair enough, the issues are definitely about how to wrap your library, so it can be used. If you are interested in broadening the appeal, it would be great to see a more easy to modify example. I'm reading up on co() and friends, but there aren't any great references I've found to get going quickly.

crapthings commented 8 years ago

i can't get this working either

i want to use babel with this lib

vadimdemedes commented 8 years ago

@crapthings if you are getting some errors related to mongorito, open a new issue so that we can discuss.

crapthings commented 8 years ago

i can get things work.

https://github.com/crapthings/inventor-server

use babel async await

git clone https://github.com/crapthings/inventor-server
npm install
nodemon loader.js
ranisalt commented 6 years ago

Bumping. Most examples linked above use older Koa and Mongorito versions and are no longer valid.

vadimdemedes commented 6 years ago

Hey everyone, I don't have time to work on this project, so feel free to submit a PR for an example you'd like to see. Thanks for understanding!