geddy / model

Datastore-agnostic ORM in JavaScript
265 stars 55 forks source link

How to use database adapters #93

Open me-ascii opened 11 years ago

me-ascii commented 11 years ago

We use model without Geddy.js Is it possible to load mongodb adapter without requiring like that: Adapter = require('../node_modules/model/lib/adapters/mongo').Adapter Thank you

mde commented 11 years ago

Yes, you can see how to bootstrap the Mongo adapter in the Mongo test file:

https://github.com/mde/model/blob/master/test/integration/adapters/mongo/index.js

The tests are all mixed in from the shared file, but the bootstrap happens in the 'before'.

me-ascii commented 11 years ago

Sorry, but example from the test file uses relative paths within the project:

Adapter = require('../../../../lib/adapters/mongo').Adapter`

How to do that in my application where the model installed via npm? For example

var model = require( 'model' ),
    Adapter = ??
    // may be something like model.adapters.mongo
eins78 commented 10 years ago

@me-ascii I got it to work like this. Seems hack-ish, but it's the shortest 'clean' way I found after sifting through the model code and the node API.

var model = require('model'),
    path = require('path'),
    Adapter = require(path.join(path.dirname(require.resolve('model')), 'adapters', 'filesystem')).Adapter,
    adapter;

adapter = new Adapter();

var User = function () {
  this.adapter = adapter;
  this.property('name', 'string');
};

Edit: removed unnecessary "model.adapters.filesystem = adapter;".

me-ascii commented 10 years ago

thanks! require.resolve it's realy good idea :+1:

ben-ng commented 10 years ago

+1 for require.resolve, that was useful (:

mde commented 10 years ago

Nicely done.

me-ascii commented 10 years ago

: ) But it would be great to have access to the Adapter through the exports of model like model.adapters.origin.mongo or so

mde commented 10 years ago

You're just wanting to get access to all the different adapter constructors?