Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unregister model discriminators when disconnecting #35

Merged
merged 1 commit into from
Apr 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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