Created
September 3, 2014 14:45
-
-
Save medikoo/247ef6e3bad39247aeaf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Database, defineUser, migrateObject, migrateProperty, migrateType, memoize, getPrototypeOf | |
, DbjsEvent, isGetter; | |
migrateType = function (type, targetDatabase) { | |
var prototype, sourceEvent, targetEvent; | |
if (targetDatabase.objects.getById(type.__id__)) return; | |
prototype = getPrototypeOf(type); | |
migrateType(prototype, targetDatabase); | |
sourceEvent = type._lastOwnEvent_; | |
targetEvent = new DbjsEvent(targetDatabase.objects.unserialize(type.__id__, prototype), | |
prototype, (sourceEvent && sourceEvent.stamp) || 0); | |
migrateObject(type, targetEvent.object); | |
}; | |
migrateProperty = function (sourceDesc, targetDatabase) { | |
var hasInformation = false, value, sourceEvent; | |
if (targetDatabase.objects.getById(sourceDesc.__id__)) return hasInformation; | |
if (migrateProperty(getPrototypeOf(sourceDesc))) hasInformation = true; | |
sourceDesc._forEachOwnDescriptors_(function (subDesc) { | |
var key = subDesc.key, sourceEvent; | |
if (key === 'statsBase') return; | |
if (key === 'type') migrateType(sourceDesc.type, targetDatabase); | |
sourceEvent = subDesc._lastOwnEvent_; | |
new DbjsEvent(targetDatabase.objects.unserialize(subDesc.__id__), sourceDesc[key], | |
(sourceEvent && sourceEvent.stamp) || 0); //jslint: ignore | |
hasInformation = true; | |
}); | |
if (!sourceDesc.hasOwnProperty('_value_')) return hasInformation; | |
value = sourceDesc._value_; | |
if (sourceDesc.master instanceof sourceDesc.database.Object) { | |
if (isGetter(value)) { | |
if (!sourceDesc.hasOwnProperty('statsBase')) return hasInformation; | |
value = sourceDesc.statsBase; | |
if (value == null) return hasInformation; | |
} | |
} | |
sourceEvent = sourceDesc._lastOwnEvent_; | |
new DbjsEvent(targetDatabase.objects.unserialize(sourceDesc.__valueId__), value, | |
(sourceEvent && sourceEvent.stamp) || 0); //jslint: ignore | |
return true; | |
}; | |
migrateObject = function (source, targetDatabase) { | |
var isFullCopy = !(source.master instanceof source.database.Object), desc, anyDefined; | |
for (desc in source.__descriptors__) { | |
if (desc.nested) { | |
if (migrateObject(source.get(desc.key), targetDatabase)) anyDefined = true; | |
continue; | |
} | |
if (desc.object !== source) continue; | |
if (!isFullCopy && !desc.hasOwnProperty('statsBase')) continue; | |
if (migrateProperty(desc, targetDatabase)) anyDefined = true; | |
} | |
return anyDefined; | |
}; | |
module.exports = function (mainDb) { | |
var statsDb = new Database(); | |
defineUser(statsDb); | |
migrateObject(mainDb.User.prototype, statsDb); | |
return statsDb; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment