Skip to content

Commit

Permalink
Merge pull request #35 from IONISx/tusbar/cleanup-models
Browse files Browse the repository at this point in the history
Unregister model discriminators when disconnecting
  • Loading branch information
tusbar committed Apr 20, 2016
2 parents 88a9ef9 + c0b496e commit 8e8c71f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Modulestore.prototype.detachConnection = function () {
throw new Error('modulestore: no connection attached');
}

models.cleanup();

this.removeAllListeners(this.connection);
delete this.connection;

Expand Down
29 changes: 23 additions & 6 deletions lib/models/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
'use strict';

var mongoose = require('mongoose');
var mongoose = require('mongoose');

// Register `Long' type
require('mongoose-long')(mongoose);

var ModuleSchema = require('./module');
var CourseSchema = require('./course');
var AboutSchema = require('./about');
var ChapterSchema = require('./chapter');
var ModuleSchema = require('./module');
var CourseSchema = require('./course');
var AboutSchema = require('./about');
var ChapterSchema = require('./chapter');
var SequentialSchema = require('./sequential');
var ProblemSchema = require('./problem');
var ProblemSchema = require('./problem');

// ## //

var Module, Course, About, Chapter, Sequential, Problem;

var setup = function (connection) {
Module = connection.model('Module', ModuleSchema);

Course = Module.discriminator('course', CourseSchema);
About = Module.discriminator('about', AboutSchema);
Chapter = Module.discriminator('chapter', ChapterSchema);
Sequential = Module.discriminator('sequential', SequentialSchema);
Problem = Module.discriminator('problem', ProblemSchema);
};

var cleanup = function () {
// This removes the discriminatorKey from the schemas.
// We’re doing this to make sure that the schema/models
// are re-usable.
// Model.discriminator will call Schema.add() with the
// configured discriminatorKey. Calling Model.discriminator()
// when the discriminatorKey is already defined will fail.

CourseSchema.remove(CourseSchema.options.discriminatorKey);
AboutSchema.remove(AboutSchema.options.discriminatorKey);
ChapterSchema.remove(ChapterSchema.options.discriminatorKey);
SequentialSchema.remove(SequentialSchema.options.discriminatorKey);
ProblemSchema.remove(ProblemSchema.options.discriminatorKey);
};

// ## //

exports.setup = setup;
exports.cleanup = cleanup;

exports.__defineGetter__('Module', function () { return Module; });
exports.__defineGetter__('Course', function () { return Course; });
Expand Down

0 comments on commit 8e8c71f

Please sign in to comment.