Initial commit

This commit is contained in:
2021-03-14 11:09:02 +01:00
commit 21e364ee38
1046 changed files with 126647 additions and 0 deletions

323
node_modules/node-mysql/lib/db.js generated vendored Normal file
View File

@ -0,0 +1,323 @@
var Class = require('better-js-class');
var cps = require('cps');
var mysql = require('mysql');
var $U = require('underscore');
var getValue = function(o) {
for (var k in o) {
return o[k];
}
};
module.exports = function() {
var DB = Class({
_init: function(cfg) {
var transactionOverride = cfg['useTransaction'];
delete cfg['useTransaction'];
var cursorOverride = cfg['useCursor'];
delete cfg['useCursor'];
this._cfg = cfg;
// console.log(this._cfg);
this._pool = mysql.createPool(this._cfg);
if (transactionOverride) {
this._transactionCfg = this._buildCfg(cfg, transactionOverride);
// console.log('transactionCfg:', this._transactionCfg);
this._transactionPool = mysql.createPool(this._transactionCfg);
}
if (cursorOverride) {
this._cursorCfg = this._buildCfg(cfg, cursorOverride);
// console.log('cursorCfg:', this._cursorCfg);
this._cursorPool = mysql.createPool(this._cursorCfg);
}
this._schema = {};
this._prepared = false;
},
_buildCfg: function(cfg, override) {
var res = {};
for (var k in cfg) {
res[k] = cfg[k];
}
$U.extend(res, override);
return res;
},
connect: function(proc, cb) {
var me = this;
cps.seq([
function(_, cb) {
me._prepare(cb);
},
function(_, cb) {
me._connect(me._pool, proc, cb);
}
], cb);
},
_prepare: function(cb) {
if (this._prepared) {
return cb();
}
// console.log('call prepare');
var me = this;
var conn;
this._connect(me._pool, function(conn, cb) {
cps.seq([
function(res, cb) {
conn.query('show tables', cb);
},
function(tables, cb) {
cps.peach(tables, function(table, cb) {
var tableName = getValue(table);
cps.seq([
function(_, cb) {
conn.query('desc ' + tableName, cb);
},
function(columns, cb) {
me._schema[tableName] = $U.map(columns, function(column) {
return column['Field'];
});
me._prepared = true;
cb();
}
], cb);
}, cb);
}
], cb);
}, cb);
},
_connect: function(pool, proc, cb) {
var me = this;
var conn;
cps.seq([
function(_, cb) {
pool.getConnection(cb);
},
function(res, cb) {
conn = res;
cps.rescue({
'try': function(cb) {
proc(conn, cb);
},
'finally': function(cb) {
// console.log('release connection');
conn.release();
cb();
}
}, cb);
}
], cb);
},
transaction: function(conn, proc, cb) {
var me = this;
if (!me._transactionPool) {
cb(new Error('transaction-not-setup-error'));
return;
}
var txnConn;
var commitRes;
if (me._isTxnConnection(conn)) {
proc(conn, cb);
} else {
cps.seq([
function(_, cb) {
me._prepare(cb);
},
/*
function(_, cb) {
me._getTxnConnection(cb);
},
*/
function(_, cb) {
me._connect(me._transactionPool, function(conn, cb) {
me._enterTransaction(conn);
txnConn = conn;
cps.rescue({
'try': function(cb) {
cps.seq([
function(_, cb) {
// console.log('start transaction');
txnConn.query('START TRANSACTION', cb);
},
function(_, cb) {
cps.rescue({
'try': function(cb) {
cps.seq([
function(_, cb) {
proc(txnConn, cb);
},
function(res, cb) {
commitRes = res;
// console.log('committing');
txnConn.query('COMMIT', cb);
},
function(_, cb) {
// console.log('committed');
cb(null, commitRes);
}
], cb);
},
'catch': function(err, cb) {
cps.seq([
function(_, cb) {
// console.log('rolling back ...');
txnConn.query('ROLLBACK', cb);
},
function(_, cb) {
// console.log('rolled back');
throw(err);
}
], cb);
}
}, cb);
}
], cb);
},
'finally': function(cb) {
// console.log('txn connection release');
// txnConn.release();
me._leaveTransaction(txnConn);
cb();
}
}, cb);
}, cb);
}
], cb);
}
},
cursor: function(q, proc, _cb) {
var me = this;
if (!me._cursorPool) {
_cb(new Error('cursor-not-setup-error'));
return;
}
var returned = false;
var cb = function(err, res) {
if (!returned) {
returned = true;
_cb(err, res);
} else {
}
}
var breakCB = cb;
this._cursorPool.getConnection(function(err, conn) {
var query = conn.query(q);
query
.on('error', function(err) {
// console.log('cursor error');
conn.release();
cb(new Error(err));
})
.on('result', function(res) {
// console.log('cursor result');
conn.pause();
var cb = function(err, res) {
if (err) {
conn.release();
breakCB(err);
} else {
conn.resume();
}
};
cps.seq([
function(_, cb) {
// console.log('call row processor');
proc(res, cb);
}
], cb);
})
.on('end', function() {
// console.log('cursor end');
conn.release();
cb();
})
;
});
},
_isTxnConnection: function(conn) {
return conn != null && conn.__transaction__;
},
_enterTransaction: function(conn) {
conn.__transaction__ = true;
},
_leaveTransaction: function(conn) {
conn.__transaction__ = false;
},
end: function() {
this._pool.end();
if (this._transactionPool) {
this._transactionPool.end();
}
if (this._cursorPool) {
this._cursorPool.end();
}
},
getConnection: function(cb) {
var me = this;
cps.seq([
function(_, cb) {
me._prepare(cb);
},
function(_, cb) {
me._pool.getConnection(cb);
}
], cb);
}
});
$U.extend(DB, {
format: function(str, bindings) {
var l = str.split('?')
if (l.length - 1 != bindings.length) {
throw new Error('sql string format error');
}
var res = [];
for (var i = 0; i < bindings.length; i++) {
res.push(l[i]);
res.push(mysql.escape(bindings[i]));
}
res.push(l[l.length - 1]);
return res.join(' ');
}
});
return DB;
}();

487
node_modules/node-mysql/lib/model.js generated vendored Normal file
View File

@ -0,0 +1,487 @@
var Class = require('better-js-class');
var cps = require('cps');
var $U = require('underscore');
var DB = require('./db.js');
module.exports = function() {
var Model = {
OPTIMISTIC_LOCK_EXCEPTION: 'optimistic_lock_exception'
};
var Row = Class({
_init: function(data, cfg) {
this._table = cfg.table;
this._data = data;
},
getId: function() {
return this._data[this._table.getIdFieldName()];
},
_getVersion: function() {
return this._data[this._table.getVersionFieldName()];
},
_nextVersion: function() {
return this._data[this._table.getVersionFieldName()] + 1;
},
_updateLocalData: function(dto) {
for (var k in dto) {
var v = dto[k];
this._data[k] = v;
}
},
_refineDtoForUpdate: function(dto) {
var res = {};
for (var k in dto) {
if (this._table._hasUpdatableField(k)) {
res[k] = dto[k];
}
}
var d = new Date();
res[this._table.getUpdatedFieldName()] = d;
return res;
},
updateWithoutOptimisticLock: function(conn, dto, cb) {
this._update(conn, dto, null, cb);
},
update: function(conn, dto, cb) {
var me = this;
dto['version'] = this._nextVersion();
var cond = DB.format('version = ?', [this._getVersion()]);
cps.seq([
function(_, cb) {
me._update(conn, dto, cond, cb);
},
function(res, cb) {
if (res.changedRows === 0) {
throw new Error(Model.OPTIMISTIC_LOCK_EXCEPTION);
} else {
cb(null, res);
}
}
], cb);
},
_update: function(conn, dto, conditions, cb) {
var me = this;
// dto[this._table.getUpdatedFieldName()] = new Date();
dto = this._refineDtoForUpdate(dto);
var l = [
' update ', this._table.getName(), ' set '
];
var first = true;
for (var k in dto) {
var v = dto[k];
if (first) {
first = false;
} else {
l.push(', ');
}
l.push(
' ', k, ' = ', conn.escape(v)
);
}
l.push(
' where ', this._table.getIdFieldName(), ' = ', this.getId()
);
if (conditions) {
l.push(
' and ', conditions
);
}
l.push(' ; ');
var q = l.join('');
// console.log(q);
cps.seq([
function(_, cb) {
conn.query(q, cb);
},
function(res, cb) {
me._updateLocalData(dto);
cb(null, res);
}
], cb);
},
get: function(fieldName) {
return this._data[fieldName]
},
_load: function(conn, name, cb) {
var cfg = this._table._lookupLinksToMap(name);
if (cfg) {
return this.linksTo(conn, name, cb);
}
var cfg = this._table._lookupLinkedByMap(name);
if (cfg) {
return this.linkedBy(conn, name, cb);
}
var cfg = this._table._lookupRelatesToMap(name);
if (cfg) {
return this.relatesTo(conn, name, cb);
}
throw new Error('no linksTo or linkedBy or relatesTo: ' + name + ' defined on: ' + this._table.getName());
},
load: function(conn, name, cb) {
var value = this.get(name);
if (value) {
cb(null, value);
} else {
this._load(conn, name, cb);
}
},
linksTo: function(conn, name, cb) {
var me = this;
var cfg = this._table._lookupLinksToMap(name);
if (cfg) {
var otherTable = this._table._db.get(cfg.table).Table;
return cps.seq([
function(_, cb) {
otherTable.findById(conn, me.get(cfg.key), cb);
},
function(res, cb) {
me._data[cfg.name] = res;
cb(null, res);
}
], cb);
} else {
throw new Error('no linksTo: ' + name + ' defined on: ' + this._table.getName());
}
},
linkedBy: function(conn, name, cb) {
var me = this;
var cfg = this._table._lookupLinkedByMap(name);
if (cfg) {
var otherTable = this._table._db.get(cfg.table).Table;
return cps.seq([
function(_, cb) {
otherTable.find(conn, otherTable.baseQuery('where ' + cfg.key + ' = ?', [me.getId()]), cb);
},
function(res, cb) {
me._data[cfg.name] = res;
cb(null, me._data[cfg.name]);
}
], cb);
} else {
throw new Error('no linkedBy: ' + name + ' defined on: ' + this._table.getName());
}
},
relatesTo: function(conn, name, cb) {
var me = this;
var cfg = this._table._lookupRelatesToMap(name);
if (cfg) {
var otherTable = this._table._db.get(cfg.table).Table;
var throughTable = this._table._db.get(cfg.through).Table;
return cps.seq([
function(_, cb) {
var _q = 'select t.* from ' +
otherTable.getName() + ' t, ' +
throughTable.getName() + ' r where ' +
'r.' + cfg['leftKey'] + ' = ? and ' +
'r.' + cfg['rightKey'] + ' = t.' + otherTable.getIdFieldName()
;
var q = DB.format(_q, [me.getId()]);
otherTable.find(conn, q, cb);
},
function(res, cb) {
me._data[cfg.name] = res;
cb(null, me._data[cfg.name]);
}
], cb);
} else {
throw new Error('no relatesTo: ' + name + ' defined on: ' + this._table.getName());
}
}
});
var Table = Class({
_init: function(cfg) {
this._name = cfg.name;
this._idFieldName = cfg.idFieldName || 'id';
this._versionFieldName = cfg.versionFieldName || 'version';
this._createdFieldName = cfg.createdFieldName || 'date_created';
this._updatedFieldName = cfg.updatedFieldName || 'last_updated';
this._rowClass = cfg['rowClass'];
// this._schema = cfg['schema'];
this._db = cfg['db'];
this._linksToMap = {};
this._linkedByMap = {};
this._relatesToMap = {};
},
getName: function() {
return this._name;
},
getIdFieldName: function() {
return this._idFieldName;
},
getVersionFieldName: function() {
return this._versionFieldName;
},
getCreatedFieldName: function() {
return this._createdFieldName;
},
getUpdatedFieldName: function() {
return this._updatedFieldName;
},
_hasUpdatableField: function(field) {
if (field == this.getIdFieldName()) {
return false;
}
if (field == this.getCreatedFieldName()) {
return false;
}
var schema = this._db._schema[this.getName()];
return $U.contains(schema, field);
},
_refineDto: function(dto, autoId) {
autoId = (autoId === undefined)? true: autoId;
var res = {};
var schema = this._db._schema[this.getName()];
$U.each(schema, function(field) {
res[field] = dto[field];
});
if (autoId) {
delete res[this.getIdFieldName()];
}
return res;
},
_refineDtoForCreate: function(dto) {
var res = this._refineDto(dto);
var d = new Date();
res[this.getCreatedFieldName()] = d;
res[this.getUpdatedFieldName()] = d;
res[this.getVersionFieldName()] = 0;
return res;
},
create: function(conn, dto, cb) {
dto = this._refineDtoForCreate(dto);
this._create(conn, dto, cb);
},
_create: function(conn, dto, cb) {
var me = this;
var l = [
' insert into ' + this.getName() + ' set '
];
var first = true;
$U.each(dto, function(v, k) {
if (first) {
first = false;
} else {
l.push(', ');
}
l.push(
' ', k, ' = ', conn.escape(v)
);
});
l.push(' ; ');
var q = l.join('');
// console.log(q);
cps.seq([
function(_, cb) {
conn.query(q, cb);
},
function(res, cb) {
dto[me._idFieldName] = res.insertId;
cb(null, new me._rowClass(dto));
}
], cb);
},
clone: function(conn, dto, cb) {
dto = this._refineDto(dto, false);
this._create(conn, dto, cb);
},
baseQuery: function(str, bindings) {
var q = ' select * from ' + this.getName() + ' ';
var further;
if (str != null) {
if (bindings == null) {
further = str;
} else {
further = DB.format(str, bindings);
}
}
if (further != null) {
q += further;
}
return q;
},
findById: function(conn, id, cb) {
var me = this;
cps.seq([
function(_, cb) {
me.find(conn, me.baseQuery('where ' + me.getIdFieldName() + ' = ? ', [id]), cb);
},
function(res) {
cb(null, res[0])
}
], cb);
},
lockById: function(conn, id, cb) {
var me = this;
cps.seq([
function(_, cb) {
me.find(conn, me.baseQuery('where ' + me.getIdFieldName() + ' = ? for update', [id]), cb);
},
function(res) {
cb(null, res[0])
}
], cb);
},
findAll: function(conn, cb) {
this.find(conn, this.baseQuery(), cb);
},
find: function(conn, q, cb) {
var me = this;
cps.seq([
function(_, cb) {
conn.query(q, cb);
},
function(res, cb) {
cb(null, $U.map(res, function(o) {
return new me._rowClass(o);
}));
}
], cb);
},
linksTo: function(cfg) {
this._linksToMap[cfg.name] = {
table: cfg.table,
key: cfg.key
};
return this;
},
linkedBy: function(cfg) {
this._linkedByMap[cfg.name] = {
table: cfg.table,
key: cfg.key
};
return this;
},
relatesTo: function(cfg) {
this._relatesToMap[cfg.name] = {
table: cfg.table,
through: cfg.through,
leftKey: cfg.leftKey,
rightKey: cfg.rightKey
};
return this;
},
_lookupLinksToMap: function(name) {
var cfg = this._linksToMap[name];
if (cfg) {
cfg.name = name;
}
return cfg;
},
_lookupLinkedByMap: function(name) {
var cfg = this._linkedByMap[name];
if (cfg) {
cfg.name = name;
}
return cfg;
},
_lookupRelatesToMap: function(name) {
var cfg = this._relatesToMap[name];
if (cfg) {
cfg.name = name;
}
return cfg;
},
findFirst: function(conn, q, cb) {
var me = this;
cps.seq([
function(_, cb) {
me.find(conn, q, cb);
},
function(res, cb) {
cb(null, res[0]);
}
], cb);
}
});
$U.extend(Model, {
Row: Row,
Table: Table
});
return Model;
}();

135
node_modules/node-mysql/lib/node-mysql.js generated vendored Normal file
View File

@ -0,0 +1,135 @@
var Class = require('better-js-class');
var $U = require('underscore');
var _DB = require('./db.js');
var $M = require('./model.js');
var DB = Class(_DB, {
_init: function(cfg) {
this.parent._init.call(this, cfg);
this._models = {};
},
add: function(cfg) {
var me = this;
var model = {};
var RowTemplate = function(model) {
var closedRowClass = Class($M.Row, {
_init: function(data) {
this.parent._init.call(this, data, {
table: model.Table
});
}
});
return Class(closedRowClass, cfg.Row || {});
};
var Row = RowTemplate(model);
$U.extend(model, {
Row: Row,
_RowTemplate: RowTemplate
});
var TableTemplate = function(model) {
var closedTableClass = Class($M.Table, {
_init: function() {
this.parent._init.call(this, {
db: me,
name: cfg.name,
rowClass: model.Row,
idFieldName: cfg.idFieldName || 'id',
versionFieldName: cfg.versionFieldName || 'version',
createdFieldName: cfg.createdFieldName || 'date_created',
updatedFieldName: cfg.updatedFieldName || 'last_updated'
});
}
});
return Class(closedTableClass, cfg.Table || {});
};
var TableClass = TableTemplate(model);
var Table = new TableClass();
$U.extend(model, {
Table: Table,
_TableTemplate: TableTemplate
});
this._models[cfg.name] = model;
return Table;
},
clone: function() {
var tempClass = function() {
this._models = $U.clone(this._models);
};
tempClass.prototype = this;
return new tempClass();
},
extend: function(cfg) {
var _model = this.get(cfg.name);
if (!_model) {
throw new Error('node-mysql-runtime-error: db.extend can not be used on empty model.');
}
var model = {};
var _RowTemplate = _model._RowTemplate;
var RowTemplate = function(model) {
var superClass = _RowTemplate(model);
return Class(superClass, cfg.Row || {});
};
var Row = RowTemplate(model);
$U.extend(model, {
Row: Row,
_RowTemplate: RowTemplate
});
var _TableTemplate = _model._TableTemplate;
var TableTemplate = function(model) {
var superClass = _TableTemplate(model);
return Class(superClass, cfg.Table || {});
};
var TableClass = TableTemplate(model);
var Table = new TableClass();
$U.extend(model, {
Table: Table,
_TableTemplate: TableTemplate
});
this._models[cfg.name] = model;
return Table;
},
get: function(name) {
return this._models[name];
}
});
DB.format = _DB.format;
module.exports = {
DB: DB,
Row: $M.Row,
Table: $M.Table,
OPTIMISTIC_LOCK_EXCEPTION: $M.OPTIMISTIC_LOCK_EXCEPTION
}