-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Create Revision behaviour indexes, and do it on boot
- Loading branch information
Showing
3 changed files
with
74 additions
and
34 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,26 +12,49 @@ var jsondiffpatch = alchemy.use('jsondiffpatch'), | |
* @since 0.0.1 | ||
* @version 0.2.0 | ||
*/ | ||
var Revision = Function.inherits('Alchemy.Behaviour', function RevisionBehaviour(model, options) { | ||
Behaviour.call(this, model, options); | ||
}); | ||
const Revision = Function.inherits('Alchemy.Behaviour', 'RevisionBehaviour'); | ||
|
||
/** | ||
* Get the Revision Model class for the attached model | ||
* Get the Revision model class for the given main model | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 1.0.3 | ||
* @version 1.1.0 | ||
* @since 1.4.0 | ||
* @version 1.4.0 | ||
* | ||
* @param {Schema} schema | ||
* @param {Object} options | ||
*/ | ||
Revision.setProperty(function revision_model_class() { | ||
Revision.setStatic(function getRevisionModel(model) { | ||
|
||
var class_name = this.model.name + 'DataRevision'; | ||
if (typeof model == 'function') { | ||
model = model.model_name; | ||
} | ||
|
||
if (typeof model == 'string') { | ||
model = Model.get(model); | ||
} | ||
|
||
if (Classes.Alchemy.Model[class_name]) { | ||
return Classes.Alchemy.Model[class_name]; | ||
if (!model) { | ||
throw new Error('Unable to add Revision behaviour to undefined model'); | ||
} | ||
|
||
let model_class = Function.inherits('Alchemy.Model', Function.create(class_name, function DataRevision(options) { | ||
let revision_model_name = model.model_name + 'DataRevision', | ||
revision_model; | ||
|
||
try { | ||
revision_model = Model.get(revision_model_name, false); | ||
} catch (err) { | ||
// Ignore | ||
} | ||
|
||
if (revision_model) { | ||
return revision_model; | ||
} | ||
|
||
let namespace = model.constructor.namespace, | ||
class_name = model.name + 'DataRevision'; | ||
|
||
let model_class = Function.inherits('Alchemy.Model', namespace, Function.create(class_name, function DataRevision(options) { | ||
Model.call(this, options); | ||
})); | ||
|
||
|
@@ -48,6 +71,12 @@ Revision.setProperty(function revision_model_class() { | |
if (Classes.Alchemy.Model.User) { | ||
this.belongsTo('User'); | ||
} | ||
|
||
// Add an index on the record_id | ||
this.addIndex('record_id', { | ||
unique : false, | ||
sparse : false, | ||
}); | ||
}); | ||
|
||
// Force the constitutors to load now | ||
|
@@ -56,6 +85,39 @@ Revision.setProperty(function revision_model_class() { | |
return model_class; | ||
}); | ||
|
||
/** | ||
* Listen to attachments to schema's | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 1.0.3 | ||
* @version 1.4.0 | ||
* | ||
* @param {Schema} schema | ||
* @param {Object} options | ||
*/ | ||
Revision.setStatic(function attached(schema, new_options) { | ||
|
||
const context = schema.model_class; | ||
|
||
// Add the revision number to the main model | ||
context.addField('__r', 'Number', { | ||
title: 'Revision', | ||
}); | ||
|
||
Revision.getRevisionModel(schema.model_class); | ||
}); | ||
|
||
/** | ||
* Get the Revision Model class for the attached model | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 1.0.3 | ||
* @version 1.4.0 | ||
*/ | ||
Revision.setProperty(function revision_model_class() { | ||
return Revision.getRevisionModel(this.model); | ||
}); | ||
|
||
/** | ||
* Get the revision model for the attached model | ||
* | ||
|
@@ -122,24 +184,6 @@ Revision.setProperty(function diff_patcher() { | |
return diff_patch_instance; | ||
}); | ||
|
||
/** | ||
* Listen to attachments to schema's | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 1.0.3 | ||
* @version 1.0.3 | ||
* | ||
* @param {Schema} schema | ||
* @param {Object} options | ||
*/ | ||
Revision.setStatic(function attached(schema, new_options) { | ||
|
||
var context = schema.model_class; | ||
|
||
// Add the revision | ||
context.addField('__r', 'Number', {title: 'Revision'}); | ||
}); | ||
|
||
/** | ||
* Compare 2 objects | ||
* | ||
|
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