Seeding with multiple databases

13 views
Skip to first unread message

Jef Harkay

unread,
Apr 10, 2014, 11:46:54 AM4/10/14
to compo...@googlegroups.com
So I wanted to take the approach of explicitly creating my schemas and shying away from the config/database.js file... something similar to this post.  I've got the following code:

config/database.js
module.exports = {
  development
: {},
  test
: {},
  production
: {}
};

db/schema.js
var Schema = require('jugglingdb').Schema;
var mongoDbSchema = new Schema('mongodb', {
  driver
: 'mongodb',
  url
: 'mongodb://localhost/cache-dev'
});
mongoDbSchema
.define('Compression', function(model) {
  model
.property('compression', 'String');
});

This seems to work, as the mongoDbSchema has my model attached to it after starting my server.  The problem, however, comes with the seed file, and I believe it's because my model that I create above is on a different schema than where the seed file is pulling from, so if I have a CoffeeScript seed file like this:

Compression.seed ->
    compression
: 'mp4'

So when I try to run compound seed, I get ReferenceError: Compression is not defined, but like I said, I think this is because the seed is trying to use the default database's schema, which is defined in database.js, but it's just empty objects, so there's no model named Compression in the scope of the seed file.

My question is, can I specify a seed file to use a certain schema that I have defined, or does it only use the default database connection/schema defined in database.js and schema.js?

Jef Harkay

unread,
Apr 10, 2014, 2:05:55 PM4/10/14
to
I figured this out... what I'm now doing is something like this:

db/schema.js
schema('mongodb', {
  host
: 'localhost',
  database
: 'mongo-dev'
}, function() {
  define
('CompressionMongo', function() {
    property
('compression', String);
    setTableName
('Compression');
 
});
});

schema
('postgres', {
  database
: 'postgres-dev'
}, function() {
  define
('CompressionPostgres', function() {
    property
('compression', String);
    setTableName
('Compression');
 
});
});

CompressionMongo
CompressionMongo.seed ->
    compression
: 'mp4'

CompressionPostgres
CompressionPostgres.seed ->
    compression
: 'wma'

Which actually is really really nice to have/know.  That setTableName is the saving grace because otherwise, if both Mongo and Postgres' model had the same name (say "Compression"), Postgres' Compression model would be used because it was defined last, as compound.models is a hash of the model names used at the beginning of define/describe.
Reply all
Reply to author
Forward
0 new messages