Skip to content

Commit

Permalink
♻️ Also make the fallback datasource use the new procedural context c…
Browse files Browse the repository at this point in the history
…lasses
  • Loading branch information
skerit committed Mar 23, 2024
1 parent 5d126aa commit 8b5b281
Showing 1 changed file with 60 additions and 81 deletions.
141 changes: 60 additions & 81 deletions lib/app/helper_datasource/05-fallback_datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ Fallback.setMethod(function storeInUpperDatasource(model, data, options) {
}

return Function.series(function convertToDatasource(next) {
Pledge.Swift.done(upper.toDatasource(model, data), next);

let context = new Classes.Alchemy.OperationalContext.SaveDocumentToDatasource();
context.setDatasource(upper);
context.setModel(model);
context.setRootData(data);
context.setSaveOptions(options);

Pledge.Swift.done(upper.toDatasource(context), next);
}, function addTempValues(next, result) {
data = result;

Expand Down Expand Up @@ -118,18 +125,19 @@ Fallback.setMethod(function storeInUpperDatasource(model, data, options) {
// Allow extraneous values (the ones we just added in this function)
options.extraneous = true;

let promise;

if (options.create === false) {
upper._update(model, data, options, savedOnUpper);
promise = upper._update(context);
} else {
upper._create(model, data, options, savedOnUpper);
promise = upper._create(context);
}

function savedOnUpper(err, result) {
next(err, result);
}
Swift.done(promise, next);

}, function gotUpdateResult(next, result) {
Pledge.Swift.done(upper.toApp(model, null, options, result), next);
let toapp_context = context.withDatasourceEntry(result);
Pledge.Swift.done(upper.toApp(toapp_context), next);
}, function done(err, result) {

if (err) {
Expand All @@ -145,63 +153,36 @@ Fallback.setMethod(function storeInUpperDatasource(model, data, options) {
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 1.1.0
* @version 1.1.0
* @version 1.4.0
*
* @param {Alchemy.OperationalContext.ReadDocumentFromDatasource} context
*
* @return {Pledge}
*/
Fallback.setMethod(function read(model, criteria, callback) {

var that = this,
lower_criteria = criteria.clone(),
upper_criteria = criteria.clone();

// Prepare both criteria
lower_criteria.datasource = this.lower;
upper_criteria.datasource = this.upper;

Function.series(function doLowerRead(next) {

if (criteria.options.only_local) {
return next();
}

that.lower.read(model, lower_criteria, function done(err, result) {
Fallback.setMethod(function read(context) {

if (err) {
console.log('Failed to read lower datasource: ' + err, err, criteria, model);
return next();
}

next(null, result);
});
}, function checkUpperDatabase(next, lower) {

// @TODO: Make sure we don't have any more recent data
if (lower) {
return next(null, lower);
}

if (!criteria.options.only_local) {
console.log('Doing upper (local) read because lower (remote) failed...');
}
let criteria = context.getCriteria(),
tasks = [];

that.upper.read(model, upper_criteria, function done(err, result) {
if (!criteria.options.only_local) {
let lower_criteria = criteria.clone();
lower_criteria.datasource = this.lower;

if (err) {
return next(err);
}
let lower_context = context.createChild();
lower_context.setCriteria(lower_criteria);

next(null, result);
});
tasks.push(() => this.lower.read(context));
}

}, function done(err, result) {
let upper_criteria = criteria.clone();
upper_criteria.datasource = this.upper;

if (err) {
return callback(err);
}
let upper_context = context.createChild();
upper_context.setCriteria(upper_criteria);

result = result[1];
tasks.push(lower_result => lower_result || this.upper.read(upper_context));

return callback(null, result);
});
return Swift.waterfall(...tasks);
});

/**
Expand Down Expand Up @@ -240,35 +221,29 @@ Fallback.setMethod(function getRecordsToSync(model) {
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 1.1.0
* @version 1.1.0
* @version 1.4.0
*
* @param {Model} model
* @param {Object} data
* @param {Object} options
* @param {Function} callback
* @param {Alchemy.OperationalContext.ReadDocumentFromDatasource} context
*
* @return {Pledge}
*/
Fallback.setMethod(function create(model, data, options, callback) {
return this.createOrUpdate('create', model, data, options, callback);
Fallback.setMethod(function create(context) {
return this.createOrUpdate('create', context);
});

/**
* Update new data into the database
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 1.1.0
* @version 1.1.0
* @version 1.4.0
*
* @param {Model} model
* @param {Object} data
* @param {Object} options
* @param {Function} callback
* @param {Alchemy.OperationalContext.ReadDocumentFromDatasource} context
*
* @return {Pledge}
*/
Fallback.setMethod(function update(model, data, options, callback) {
return this.createOrUpdate('update', model, data, options, callback);
Fallback.setMethod(function update(context) {
return this.createOrUpdate('update', context);
});

/**
Expand All @@ -278,16 +253,17 @@ Fallback.setMethod(function update(model, data, options, callback) {
* @since 1.1.0
* @version 1.4.0
*
* @param {Model} model
* @param {Object} data
* @param {Object} options
* @param {Function} callback
* @param {Alchemy.OperationalContext.ReadDocumentFromDatasource} context
*
* @return {Pledge}
*/
Fallback.setMethod(function createOrUpdate(method, model, data, options, callback) {
Fallback.setMethod(function createOrUpdate(method, context) {

var that = this,
let model = context.getModel(),
data = context.getRootData(),
options = context.getSaveOptions();

let that = this,
upper = this.upper,
lower = this.lower,
pledge,
Expand All @@ -297,9 +273,13 @@ Fallback.setMethod(function createOrUpdate(method, model, data, options, callbac

var tasks = {
lower : function doLowerCreate(next) {
lower[method](model, data, options, function createdLower(err, result) {
next(null, {err: err, result: result});
});

Swift.done(
lower[method](context),
(err, result) => {
next(null, {err: err, result: result});
}
);
},
upper : function doUpperCreate(next) {

Expand Down Expand Up @@ -349,7 +329,8 @@ Fallback.setMethod(function createOrUpdate(method, model, data, options, callbac
// @TODO: add a check for that?
//lower.toApp(model, null, options, saved_data.lower.result, next);
} else {
Pledge.Swift.done(upper.toApp(model, null, options, saved_data.upper.result), next);
let toapp_context = context.withDatasourceEntry(saved_data.upper.result);
Swift.done(upper.toApp(toapp_context), next);
}

}, function done(err, result) {
Expand All @@ -363,8 +344,6 @@ Fallback.setMethod(function createOrUpdate(method, model, data, options, callbac
return result;
});

pledge.done(callback);

return pledge;
});

Expand Down

0 comments on commit 8b5b281

Please sign in to comment.