123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- const {Events} = require('./events');
- const npm = {
- spex: require('spex'),
- utils: require('./utils'),
- mode: require('./tx-mode'),
- query: require('./query'),
- text: require('./text')
- };
- function Task(ctx, tag, isTX, config) {
- const $p = config.promise;
-
- this.ctx = ctx.ctx = {};
- npm.utils.addReadProp(this.ctx, 'isTX', isTX);
- if ('context' in ctx) {
- npm.utils.addReadProp(this.ctx, 'context', ctx.context);
- }
- npm.utils.addReadProp(this.ctx, 'connected', !ctx.db);
- npm.utils.addReadProp(this.ctx, 'tag', tag);
- npm.utils.addReadProp(this.ctx, 'dc', ctx.dc);
- npm.utils.addReadProp(this.ctx, 'level', ctx.level);
- npm.utils.addReadProp(this.ctx, 'inTransaction', ctx.inTransaction);
- if (isTX) {
- npm.utils.addReadProp(this.ctx, 'txLevel', ctx.txLevel);
- }
- npm.utils.addReadProp(this.ctx, 'parent', ctx.parentCtx);
-
- this.query = function (query, values, qrm) {
- if (!ctx.db) {
- return $p.reject(new Error(npm.text.looseQuery));
- }
- return config.$npm.query.call(this, ctx, query, values, qrm);
- };
-
- this.batch = function (values, options) {
- return config.$npm.spex.batch.call(this, values, options);
- };
-
- this.page = function (source, options) {
- return config.$npm.spex.page.call(this, source, options);
- };
-
- this.sequence = function (source, options) {
- return config.$npm.spex.sequence.call(this, source, options);
- };
- }
- const callback = (ctx, obj, cb, config) => {
- const $p = config.promise;
- let result;
- try {
- if (cb.constructor.name === 'GeneratorFunction') {
-
-
- throw new TypeError('ES6 generator functions are no longer supported!');
- }
- result = cb.call(obj, obj);
- } catch (err) {
- Events.error(ctx.options, err, {
- client: ctx.db && ctx.db.client,
- dc: ctx.dc,
- ctx: ctx.ctx
- });
- return $p.reject(err);
- }
- if (result && typeof result.then === 'function') {
- return result;
- }
- return $p.resolve(result);
- };
- const execute = (ctx, obj, isTX, config) => {
- const $p = config.promise;
-
- function update(start, success, result) {
- const c = ctx.ctx;
- if (start) {
- npm.utils.addReadProp(c, 'start', new Date());
- } else {
- c.finish = new Date();
- c.success = success;
- c.result = result;
- c.duration = c.finish - c.start;
- }
- (isTX ? Events.transact : Events.task)(ctx.options, {
- client: ctx.db && ctx.db.client,
- dc: ctx.dc,
- ctx: c
- });
- }
- let cbData, cbReason, success,
- spName;
- const capSQL = ctx.options.capSQL;
- update(true);
- if (isTX) {
-
- spName = `sp_${ctx.txLevel}_${ctx.nextTxCount}`;
- return begin()
- .then(() => callback(ctx, obj, ctx.cb, config)
- .then(data => {
- cbData = data;
- success = true;
- return commit();
- }, err => {
- cbReason = err;
- return rollback();
- })
- .then(() => {
- if (success) {
- update(false, true, cbData);
- return cbData;
- }
- update(false, false, cbReason);
- return $p.reject(cbReason);
- },
- err => {
-
-
-
- update(false, false, err);
-
- return $p.reject(err);
- }),
- err => {
-
-
-
- update(false, false, err);
-
- return $p.reject(err);
- });
- }
- function begin() {
- if (!ctx.txLevel && ctx.mode instanceof npm.mode.TransactionMode) {
- return exec(ctx.mode.begin(capSQL), 'savepoint');
- }
- return exec('begin', 'savepoint');
- }
- function commit() {
- return exec('commit', 'release savepoint');
- }
- function rollback() {
- return exec('rollback', 'rollback to savepoint');
- }
- function exec(top, nested) {
- if (ctx.txLevel) {
- return obj.none((capSQL ? nested.toUpperCase() : nested) + ' ' + spName);
- }
- return obj.none(capSQL ? top.toUpperCase() : top);
- }
-
- return callback(ctx, obj, ctx.cb, config)
- .then(data => {
- update(false, true, data);
- return data;
- })
- .catch(error => {
- update(false, false, error);
- return $p.reject(error);
- });
- };
- module.exports = config => {
- const npmLocal = config.$npm;
-
-
-
- npmLocal.query = npmLocal.query || npm.query(config);
- npmLocal.spex = npmLocal.spex || npm.spex(config.promiseLib);
- return {
- Task, execute, callback
- };
- };
|