From f9165d3ac0d49e7bfec1a8ae88e0b9edb2c8bd43 Mon Sep 17 00:00:00 2001 From: Jelle De Loecker Date: Mon, 19 Feb 2024 17:56:16 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Create=20Revision=20behaviour=20ind?= =?UTF-8?q?exes,=20and=20do=20it=20on=20boot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + lib/app/behaviour/revision_behaviour.js | 102 +++++++++++++++++------- lib/class/model.js | 5 -- 3 files changed, 74 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03278521..b7f9a2cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * Drop the correct already-existing index when trying to create a similar one * Use field path when creating index names instead of field names +* Create Revision behaviour indexes, and do it on boot ## 1.4.0-alpha.1 (2024-02-15) diff --git a/lib/app/behaviour/revision_behaviour.js b/lib/app/behaviour/revision_behaviour.js index 3aeab610..2b069364 100644 --- a/lib/app/behaviour/revision_behaviour.js +++ b/lib/app/behaviour/revision_behaviour.js @@ -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 - * @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 + * @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 + * @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 - * @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 * diff --git a/lib/class/model.js b/lib/class/model.js index ef0db774..76e5934b 100644 --- a/lib/class/model.js +++ b/lib/class/model.js @@ -59,11 +59,6 @@ Model.postInherit(function setModelName() { model_name = ns + '_' + model_name; } - if (model_name[0] == '_') { - console.log(ns, model_name) - throw new Error('KAK') - } - // The simple name of the model this.model_name = model_name; this.setProperty('model_name', model_name);