0

I am building a little Node app and since I want to use OrientDB I cannot use Mongoose or the like for handling my data / models. So I have to come up with something on my own and I find it quite hard since I'm not that well vetted in Node.js (yet).

I am using oriento as my driver for OrientDB. Oriento's API is based on bluebird promises.

This is how far I got. But I am pretty sure there must be a better way to handle errors (like 'record not found').

Any hints on how to improve this are welcome. Maybe there even is a package for creating models that aren't tied to a specific database that I haven't found yet.

./utils/oriento.js

// instantiate it once and share in multiple places(models), since it offers 
// connection pooling
// Connect to Database
var Oriento = require('oriento');

var orientserver = Oriento({
  host: 'localhost',
  port: 2424,
  username: 'root',
  password: 'password'
});

var db = orientserver.use({
  name: 'database',
  username: 'root',
  password: 'password'
});

console.log('Using database: ' + db.name);

module.exports = db;

./models/user.js

var db = require('../utils/oriento.js');

var User = function (data) {  
  this.data = {
    username: data.username,
    email: data.email
  };
}

User.prototype.data = {}

User.findByUsername = function(username, callback){
  db.select().from('users').where({username: username}).one()
  .then(function (user) {
    if (user === undefined) {
        return callback(false);
    } else {
        callback(new User(user));
    }
  });
}

// add getters, setters, find etc...
module.exports = User;

./app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// Models
// I KNOW this doesn't belong here. 
// I just put it here for quick testing before writing any controllers

User = require("./models/user.js");
User.findByUsername('ole', function (user) {  
  if(user) {
    console.log("my object: ", user);
  } else {
    console.log("Nothing found");
  }
});

// etc...
Ole Spaarmann
  • 15,845
  • 27
  • 98
  • 160
  • How would you handle record not found? . It is not an error. The correct way is to return null. I think also mongoose return null if not matched – wolf4ood Jun 16 '15 at 21:40
  • Øle, regarding "I cannot use Mongoose or the like for handling my data / models", using [waterline](https://github.com/balderdashy/waterline) + [sails-orientdb](https://github.com/appscot/sails-orientdb) will give you something *like* mongoose. sails-orientdb uses oriento and exposes it so you can still run oriento queries directly if needs be. – Dário Jun 16 '15 at 23:07
  • I realised I can just raise an error with status 404 and return that together with null in my model. @Dário That would mean I have to use Sails. But I don't want to use sails because it provides many things that I don't actually need. But thanks for the hint! – Ole Spaarmann Jun 17 '15 at 11:28
  • 1
    Not really, Waterline is independent of sails, you can use it [standalone](https://github.com/balderdashy/waterline/tree/master/example). I'm currently using it with [generator-angular-fullstack](https://github.com/DaftMonk/generator-angular-fullstack). At some point I want to create a project to share the lessons I've learned using it standalone. If you go that route and need help feel fee to raise questions with 'waterline' tag or raise issues in sails-orientdb. I'll help where I can. – Dário Jun 17 '15 at 14:46
  • 1
    Thanks. I started working in this direction and so far it is a very good solution. [This article](http://ganeshdatta.com/2015/03/01/waterline-orm-in-express-the-correct-way/) might also help anyone using Waterline together with Express - it's not OrientDB specific but the necessary are quite easy to implement. – Ole Spaarmann Jun 18 '15 at 20:45
  • Nice article, thanks! – Dário Jun 19 '15 at 18:35

0 Answers0