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

Cache model so it doesn't break trying to re-compile #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 12 additions & 11 deletions coffee/index.coffee
Original file line number Diff line number Diff line change
@@ -15,18 +15,18 @@ owl = require 'owl-deepcopy'
Added by @watnotte
###

module.exports =
module.exports =
collectionDelimiter: '__',
connection: mongoose,

setup: () ->
self = @
if arguments.length == 1 and arguments[0]
if _.isString(arguments[0])
@collectionDelimiter = arguments[0]
else
@connection = arguments[0]

if arguments.length == 2
if arguments[0] and _.isString(arguments[0])
@collectionDelimiter = arguments[0]
@@ -51,9 +51,9 @@ module.exports =
return false


newPath =
newPath =
type:mongoose.Schema.Types.ObjectId

for key,val of path.options
if key isnt 'type'
newPath[key] = _.clone(val, true)
@@ -82,9 +82,9 @@ module.exports =
else
newPath = extendPathWithTenantId(tenantId, config.caster)
if newPath

newSchema.path(prop, [newPath])

else
if config.schema?
newSubSchema = extendSchemaWithTenantId(tenantId, config.schema)
@@ -101,7 +101,7 @@ module.exports =
return @schema.$tenantId
schema.statics.getModel = schema.methods.getModel = (name) ->
return connection.mtModel(@getTenantId() + '.' + name)

make = (tenantId, modelName) ->
console.log 'making %s for %s', modelName, tenantId
if connection.mtModel.tenants.indexOf(tenantId) == -1
@@ -134,14 +134,15 @@ module.exports =
preModelName = tenantId + collectionDelimiter + pre
if !connection.models[preModelName]? and mongoose.mtModel.goingToCompile.indexOf(preModelName) < 0
connection.mtModel(tenantId, pre)
return @model(tenantModelName, newSchema, tenantCollectionName)

connection.models[tenantModelName] = @model(tenantModelName, newSchema, tenantCollectionName)
return connection.models[tenantModelName]

if arguments.length == 1
# try to figure out what tenant this is for
tenants = _.sortBy connection.mtModel.tenants, (tenant) ->
return tenant.length
tenants.reverse()

args = arguments
tenantId = _.find tenants, (tenant) ->
return new RegExp('^'+tenant + '.').test(args[0])
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -155,7 +155,8 @@ module.exports = {
}
}
}
return this.model(tenantModelName, newSchema, tenantCollectionName);
connection.models[tenantModelName] = this.model(tenantModelName, newSchema, tenantCollectionName);
return connection.models[tenantModelName];
};
if (arguments.length === 1) {
tenants = _.sortBy(connection.mtModel.tenants, function(tenant) {
Loading