Skip to content

Commit

Permalink
✨ Create Revision behaviour indexes, and do it on boot
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Feb 19, 2024
1 parent 8b38843 commit f9165d3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
102 changes: 73 additions & 29 deletions lib/app/behaviour/revision_behaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));

Expand All @@ -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
Expand All @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
5 changes: 0 additions & 5 deletions lib/class/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f9165d3

Please sign in to comment.