loopback-datasource-juggler/support/describe-operation-hooks.js

240 lines
6.1 KiB
JavaScript
Raw Normal View History

2019-05-08 15:45:37 +00:00
// Copyright IBM Corp. 2015,2018. All Rights Reserved.
2016-04-01 22:25:16 +00:00
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2016-08-22 19:55:22 +00:00
'use strict';
/*
* Describe context objects of operation hooks in comprehensive HTML table.
* Usage:
* $ node support/describe-operation-hooks.js > hooks.html
* $ open hooks.hml
*
*/
2018-12-07 14:54:29 +00:00
const Promise = global.Promise = require('bluebird');
const DataSource = require('../').DataSource;
const Memory = require('../lib/connectors/memory').Memory;
2018-12-07 14:54:29 +00:00
const HOOK_NAMES = [
'access',
'before save', 'persist', 'loaded', 'after save',
2016-04-01 11:48:17 +00:00
'before delete', 'after delete',
];
2018-12-07 14:54:29 +00:00
const dataSources = [
createOptimizedDataSource(),
2016-04-01 11:48:17 +00:00
createUnoptimizedDataSource(),
];
2018-12-07 14:54:29 +00:00
const observedContexts = [];
let lastId = 0;
Promise.onPossiblyUnhandledRejection(function(err) {
console.error('POSSIBLY UNHANDLED REJECTION', err.stack);
});
2016-04-01 13:23:42 +00:00
/* eslint-disable camelcase */
2018-12-07 14:54:29 +00:00
const operations = [
function find(ds) {
2016-08-19 17:46:59 +00:00
return ds.TestModel.find({where: {id: '1'}});
},
function count(ds) {
2016-08-19 17:46:59 +00:00
return ds.TestModel.count({id: ds.existingInstance.id});
},
function create(ds) {
2016-08-19 17:46:59 +00:00
return ds.TestModel.create({name: 'created'});
},
function findOrCreate_found(ds) {
return ds.TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: ds.existingInstance.name}},
{name: ds.existingInstance.name},
);
},
function findOrCreate_create(ds) {
return ds.TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: 'new-record'}},
{name: 'new-record'},
);
},
function updateOrCreate_create(ds) {
2016-08-19 17:46:59 +00:00
return ds.TestModel.updateOrCreate({id: 'not-found', name: 'not found'});
},
function updateOrCreate_update(ds) {
return ds.TestModel.updateOrCreate(
{id: ds.existingInstance.id, name: 'new name'},
);
},
function replaceOrCreate_create(ds) {
2016-08-19 17:46:59 +00:00
return ds.TestModel.replaceOrCreate({id: 'not-found', name: 'not found'});
},
function replaceOrCreate_update(ds) {
return ds.TestModel.replaceOrCreate(
{id: ds.existingInstance.id, name: 'new name'},
);
},
function replaceById(ds) {
return ds.TestModel.replaceById(
ds.existingInstance.id,
{name: 'new name'},
);
},
function updateAll(ds) {
2016-08-19 17:46:59 +00:00
return ds.TestModel.updateAll({name: 'searched'}, {name: 'updated'});
},
function prototypeSave(ds) {
ds.existingInstance.name = 'changed';
return ds.existingInstance.save();
},
function prototypeUpdateAttributes(ds) {
2016-08-19 17:46:59 +00:00
return ds.existingInstance.updateAttributes({name: 'changed'});
},
function prototypeDelete(ds) {
return ds.existingInstance.delete();
},
function deleteAll(ds) {
2016-08-19 17:46:59 +00:00
return ds.TestModel.deleteAll({name: ds.existingInstance.name});
},
];
2016-04-01 13:23:42 +00:00
/* eslint-enable camelcase */
2018-12-07 14:54:29 +00:00
let p = setupTestModels();
operations.forEach(function(op) {
p = p.then(runner(op));
});
p.then(report, function(err) { console.error(err.stack); });
function createOptimizedDataSource() {
2018-12-07 14:54:29 +00:00
const ds = new DataSource({connector: Memory});
ds.name = 'Optimized';
return ds;
}
function createUnoptimizedDataSource() {
2018-12-07 14:54:29 +00:00
const ds = new DataSource({connector: Memory});
ds.name = 'Unoptimized';
// disable optimized methods
ds.connector.updateOrCreate = false;
ds.connector.findOrCreate = false;
ds.connector.replaceOrCreate = false;
return ds;
}
function setupTestModels() {
dataSources.forEach(function setupOnDataSource(ds) {
2018-12-07 14:54:29 +00:00
const TestModel = ds.TestModel = ds.createModel('TestModel', {
2016-08-19 17:46:59 +00:00
id: {type: String, id: true, default: uid},
name: {type: String, required: true},
extra: {type: String, required: false},
});
});
return Promise.resolve();
}
function uid() {
lastId += 1;
return '' + lastId;
}
function runner(fn) {
return function() {
2018-12-07 14:54:29 +00:00
let res = Promise.resolve();
dataSources.forEach(function(ds) {
res = res.then(function() {
return resetStorage(ds);
}).then(function() {
observedContexts.push({
operation: fn.name,
connector: ds.name,
2016-04-01 11:48:17 +00:00
hooks: {},
});
return fn(ds);
});
});
return res;
};
}
function resetStorage(ds) {
2018-12-07 14:54:29 +00:00
const TestModel = ds.TestModel;
HOOK_NAMES.forEach(function(hook) {
TestModel.clearObservers(hook);
});
return TestModel.deleteAll()
.then(function() {
2016-08-19 17:46:59 +00:00
return TestModel.create({name: 'first'});
})
.then(function(instance) {
// Look it up from DB so that default values are retrieved
2016-04-01 11:48:17 +00:00
return TestModel.findById(instance.id);
})
.then(function(instance) {
ds.existingInstance = instance;
2016-08-19 17:46:59 +00:00
return TestModel.create({name: 'second'});
})
.then(function() {
HOOK_NAMES.forEach(function(hook) {
TestModel.observe(hook, function(ctx, next) {
2018-12-07 14:54:29 +00:00
const row = observedContexts[observedContexts.length - 1];
row.hooks[hook] = Object.keys(ctx);
next();
});
});
});
}
function report() {
2016-04-01 11:48:17 +00:00
console.log('<style>');
console.log('td { font-family: "monospace": }');
console.log('td, th {');
console.log(' vertical-align: text-top;');
console.log(' padding: 0.5em;');
console.log(' border-bottom: 1px solid gray;');
console.log('</style>');
// merge rows where Optimized and Unoptimized produce the same context
observedContexts.forEach(function(row, ix) {
if (!ix) return;
2018-12-07 14:54:29 +00:00
const last = observedContexts[ix - 1];
if (row.operation != last.operation) return;
if (JSON.stringify(row.hooks) !== JSON.stringify(last.hooks)) return;
last.merge = true;
row.skip = true;
});
console.log('<!-- ==== BEGIN DATA ==== -->\n');
console.log('<table><thead><tr>\n <th></th>');
HOOK_NAMES.forEach(function(h) { console.log(' <th>' + h + '</th>'); });
console.log('</tr></thead><tbody>');
observedContexts.forEach(function(row) {
if (row.skip) return;
2018-12-07 14:54:29 +00:00
let caption = row.operation;
if (!row.merge) caption += ' (' + row.connector + ')';
console.log('<tr><th>' + caption + '</th>');
HOOK_NAMES.forEach(function(h) {
2018-12-07 14:54:29 +00:00
const text = row.hooks[h] ? row.hooks[h].join('<br/>') : '';
console.log(' <td>' + text + '</td>');
});
console.log('</tr>');
});
console.log('</table>');
}