xardao 1.1.18 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/driver_mariadb.js +5 -8
- package/lib/driver_mssql.js +37 -18
- package/lib/driver_sqlite3.js +7 -8
- package/lib/esnext.js +7 -2
- package/lib/xardao.js +51 -10
- package/package.json +1 -1
- package/xardao.code-workspace +7 -0
- package/examples/demo_xardao_mariadb.js +0 -134
- package/examples/demo_xardao_mssql.js +0 -147
- package/examples/demo_xardao_pgsql.js +0 -135
- package/examples/demo_xardao_sqlite3.js +0 -140
package/lib/driver_mariadb.js
CHANGED
|
@@ -22,13 +22,14 @@ isFunction = common.isFunction;
|
|
|
22
22
|
then = common.then;
|
|
23
23
|
|
|
24
24
|
class Connection {
|
|
25
|
-
constructor() {
|
|
25
|
+
constructor(connUrlInfo) {
|
|
26
26
|
this.debugMode = false;
|
|
27
27
|
this.lastInsertId = null;
|
|
28
28
|
this.lastStatementChanges = null;
|
|
29
29
|
this.timeoutMilliSeconds = 10000;
|
|
30
30
|
this.saveDatesAsUTC = true
|
|
31
31
|
this.transactionLevel = 0
|
|
32
|
+
this.connUrlInfo=connUrlInfo
|
|
32
33
|
|
|
33
34
|
this.open = ESNext(this.openCB,this)
|
|
34
35
|
this.getDataTable = ESNext(this.getDataTableCB,this)
|
|
@@ -66,14 +67,10 @@ class Connection {
|
|
|
66
67
|
}
|
|
67
68
|
|
|
68
69
|
openCB(dbInfo, callback) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
else then( callback )
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
this.db = new mysql.createConnection(dbInfo);
|
|
70
|
+
if (dbInfo) this.db = new mysql.createConnection(dbInfo);
|
|
71
|
+
else this.db = new mysql.createConnection({ host: this.connUrlInfo.hostname, user: this.connUrlInfo.username, password: this.connUrlInfo.password, database: this.connUrlInfo.database})
|
|
75
72
|
this.debug('open','Opening connection for MariaDB')
|
|
76
|
-
this.db.connect(
|
|
73
|
+
this.db.connect( callback )
|
|
77
74
|
}
|
|
78
75
|
|
|
79
76
|
batch() {
|
package/lib/driver_mssql.js
CHANGED
|
@@ -22,13 +22,14 @@ isFunction = common.isFunction
|
|
|
22
22
|
then = common.then
|
|
23
23
|
|
|
24
24
|
class Connection {
|
|
25
|
-
constructor() {
|
|
25
|
+
constructor(connUrlInfo) {
|
|
26
26
|
this.debugMode = false
|
|
27
27
|
this.lastInsertId = null
|
|
28
28
|
this.lastStatementChanges = null
|
|
29
29
|
this.timeoutMilliSeconds = 10000
|
|
30
30
|
this.saveDatesAsUTC = true
|
|
31
31
|
this.transactionLevel = 0
|
|
32
|
+
this.connUrlInfo=connUrlInfo
|
|
32
33
|
|
|
33
34
|
this.open = ESNext(this.openCB, this)
|
|
34
35
|
this.getDataTable = ESNext(this.getDataTableCB, this)
|
|
@@ -66,20 +67,28 @@ class Connection {
|
|
|
66
67
|
}
|
|
67
68
|
|
|
68
69
|
openCB(dbInfo, callback) {
|
|
69
|
-
function postConnection(cn, sql, callback) {
|
|
70
|
-
if (typeof (sql) == "string" || Array.isArray(sql) ) cn.execCB(sql, callback)
|
|
71
|
-
else then( callback )
|
|
72
|
-
}
|
|
73
|
-
|
|
74
70
|
this.debug('open','Opening connection for MSSQL')
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
71
|
+
let connInfo
|
|
72
|
+
if (dbInfo) {
|
|
73
|
+
connInfo=dbInfo
|
|
74
|
+
} else {
|
|
75
|
+
connInfo= { server: this.connUrlInfo.hostname,
|
|
76
|
+
authentication: { type: 'default', options: { userName: this.connUrlInfo.username, password: this.connUrlInfo.password } },
|
|
77
|
+
options: { database: this.connUrlInfo.database || 'master' }
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
for (let o in this.connUrlInfo.options ) {
|
|
81
|
+
let v=this.connUrlInfo.options[o]
|
|
82
|
+
|
|
83
|
+
if (v=='true') connInfo.options[o] = true
|
|
84
|
+
else if (v=='false') connInfo.options[o] = false
|
|
85
|
+
else if (! isNaN ( parseInt(v))) connInfo.options[o] = parseInt(v)
|
|
86
|
+
else connInfo.options[o] = v
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
connInfo.options.validateBulkLoadParameters = connInfo.options.validateBulkLoadParameters == undefined ? false : connInfo.options.validateBulkLoadParameters
|
|
90
|
+
this.db = new mssql.Connection(connInfo)
|
|
91
|
+
this.db.connect(callback)
|
|
83
92
|
}
|
|
84
93
|
|
|
85
94
|
|
|
@@ -132,7 +141,15 @@ class Connection {
|
|
|
132
141
|
}
|
|
133
142
|
|
|
134
143
|
sqlDate(d) { return common.sqlDate(this,d) }
|
|
135
|
-
sqlParam(value) {
|
|
144
|
+
sqlParam(value) {
|
|
145
|
+
if ( value == undefined ) return 'null'
|
|
146
|
+
if ( typeof(value)=='number') { return value}
|
|
147
|
+
if ( typeof(value)=='boolean') { return value == null ? null : value ? 1 : 0 }
|
|
148
|
+
if ( typeof(value)=='string') { return "N'" + value.replace(/'/g,"''") +"'" }
|
|
149
|
+
if ( typeof(value.getFullYear )=='function') { return "'" + this.sqlDate(value) + "'"}
|
|
150
|
+
return 'null'
|
|
151
|
+
}
|
|
152
|
+
|
|
136
153
|
mergeParams(sql, params) { return common.mergeParams(this, sql, params) }
|
|
137
154
|
getRealSql(queryObject) { return common.getRealSql(this, queryObject) }
|
|
138
155
|
|
|
@@ -399,7 +416,9 @@ class Connection {
|
|
|
399
416
|
this.db.execSql(request)
|
|
400
417
|
}
|
|
401
418
|
|
|
402
|
-
execCB(params, callback) {
|
|
419
|
+
execCB(params, callback) {
|
|
420
|
+
common.exec(this, params, callback)
|
|
421
|
+
}
|
|
403
422
|
execMultipleCB(queries, callback) { common.execMultiple(this, queries, callback) }
|
|
404
423
|
|
|
405
424
|
execSingleCB(params, callback) {
|
|
@@ -411,13 +430,13 @@ class Connection {
|
|
|
411
430
|
let request = new mssql.Request(realSql, function(err, rowcount, rows) {
|
|
412
431
|
if (err) {
|
|
413
432
|
self.debug("execSingle",'Error: '+err.code)
|
|
414
|
-
then( function() {callback(err)})
|
|
433
|
+
then( function() { callback(err) })
|
|
415
434
|
}
|
|
416
435
|
else {
|
|
417
436
|
//self.lastInsertId = 0 //if (result.insertId > 0) self.lastInsertId = result.insertId
|
|
418
437
|
//self.lastStatementChanges = 0 //result.affectedRows
|
|
419
438
|
self.debug("execSingle",'Completed in ' + (Date.now() -ts) + ' ms LastId: ' + self.lastInsertId + ' Changes: ' + self.lastStatementChanges)
|
|
420
|
-
then(callback)
|
|
439
|
+
then( callback )
|
|
421
440
|
}
|
|
422
441
|
})
|
|
423
442
|
request.setTimeout( this.timeoutMilliSeconds )
|
package/lib/driver_sqlite3.js
CHANGED
|
@@ -21,7 +21,7 @@ isFunction = common.isFunction;
|
|
|
21
21
|
then = common.then;
|
|
22
22
|
|
|
23
23
|
class Connection {
|
|
24
|
-
constructor() {
|
|
24
|
+
constructor(connUrlInfo) {
|
|
25
25
|
this.debugMode = false;
|
|
26
26
|
this.lastInsertId = null;
|
|
27
27
|
this.lastStatementChanges = null;
|
|
@@ -29,6 +29,8 @@ class Connection {
|
|
|
29
29
|
this.saveDatesAsUTC = true
|
|
30
30
|
this.pooled = false
|
|
31
31
|
this.transactionLevel = 0
|
|
32
|
+
this.connUrlInfo=connUrlInfo
|
|
33
|
+
|
|
32
34
|
|
|
33
35
|
this.open = ESNext(this.openCB, this)
|
|
34
36
|
this.getDataTable = ESNext(this.getDataTableCB, this)
|
|
@@ -65,19 +67,16 @@ class Connection {
|
|
|
65
67
|
if (this.debugMode) console.error(f + ": " + s)
|
|
66
68
|
}
|
|
67
69
|
|
|
68
|
-
openCB(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
else then( callback )
|
|
72
|
-
}
|
|
73
|
-
|
|
70
|
+
openCB(pDbInfo, callback) {
|
|
71
|
+
let dbInfo = pDbInfo || this.connUrlInfo.database
|
|
72
|
+
|
|
74
73
|
try {
|
|
75
74
|
let dbfile = undefined
|
|
76
75
|
if (typeof(dbInfo) == "string" ) dbfile = dbInfo
|
|
77
76
|
else if (dbInfo.database) dbfile = dbInfo.database
|
|
78
77
|
this.db = new sq3.Database(dbfile);
|
|
79
78
|
this.debug('open','Opening connection for SQLite3')
|
|
80
|
-
|
|
79
|
+
then( callback )
|
|
81
80
|
} catch (e) {
|
|
82
81
|
then( function() { callback(e) } );
|
|
83
82
|
}
|
package/lib/esnext.js
CHANGED
|
@@ -12,11 +12,16 @@ var util = require('util');
|
|
|
12
12
|
function ESNext(f, obj) {
|
|
13
13
|
const fp = util.promisify(f)
|
|
14
14
|
return function(...args) {
|
|
15
|
-
if (typeof (args[args.length-1])=='function' && args.length == f.length)
|
|
15
|
+
if (typeof (args[args.length-1])=='function' && args.length == f.length) {
|
|
16
|
+
//console.log(`${f.name}(${JSON.stringify(args)})`)
|
|
16
17
|
f.apply(obj, args)
|
|
17
|
-
else
|
|
18
|
+
} else {
|
|
19
|
+
while (args.length < fp.length-1) args.push(undefined)
|
|
20
|
+
//console.log(`async ${f.name}(${JSON.stringify(args)})`)
|
|
18
21
|
return fp.apply(obj, args)
|
|
22
|
+
}
|
|
19
23
|
}
|
|
20
24
|
}
|
|
21
25
|
|
|
26
|
+
|
|
22
27
|
module.exports = ESNext
|
package/lib/xardao.js
CHANGED
|
@@ -6,16 +6,51 @@
|
|
|
6
6
|
* Email : stephane.potelle@gmail.com
|
|
7
7
|
********************************************************************************/
|
|
8
8
|
|
|
9
|
-
modCRUDAdapter = require ('./crud-adapter')
|
|
9
|
+
const modCRUDAdapter = require ('./crud-adapter')
|
|
10
|
+
const URL=require('url').URL
|
|
11
|
+
|
|
10
12
|
exports.DataTable = require ('./datatable').DataTable
|
|
11
13
|
exports.CRUDAdapter = modCRUDAdapter.CRUDAdapter
|
|
12
14
|
|
|
13
|
-
function
|
|
15
|
+
function parseConnUrl(url) {
|
|
16
|
+
try {
|
|
17
|
+
let tUrl=new URL(url)
|
|
18
|
+
tUrl.options={}
|
|
19
|
+
for (const [name, value] of tUrl.searchParams) {
|
|
20
|
+
tUrl.options[name]=value
|
|
21
|
+
}
|
|
22
|
+
let pp=tUrl.pathname.substr(1).split("/")
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
url: url,
|
|
26
|
+
protocol: tUrl.protocol,
|
|
27
|
+
hostname: tUrl.hostname,
|
|
28
|
+
port: tUrl.port,
|
|
29
|
+
username: tUrl.username,
|
|
30
|
+
password: tUrl.password,
|
|
31
|
+
pathname: tUrl.pathname,
|
|
32
|
+
database: pp[0],
|
|
33
|
+
schema: pp.length >1 ? pp[1] : undefined,
|
|
34
|
+
options: tUrl.options
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
catch(e){ return undefined }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function Connection(url) {
|
|
14
41
|
let Conn
|
|
42
|
+
let driver
|
|
43
|
+
let connInfo = parseConnUrl(url)
|
|
44
|
+
if (connInfo) {
|
|
45
|
+
driver = connInfo.protocol.substring(0,connInfo.protocol.length-1)
|
|
46
|
+
}
|
|
47
|
+
else driver=url
|
|
48
|
+
|
|
49
|
+
|
|
15
50
|
try {
|
|
16
51
|
if (driver == 'mysql' || driver == 'mariadb') {
|
|
17
52
|
if ( Conn == undefined ) Conn = require ('./driver_mariadb').Connection
|
|
18
|
-
let cn = new Conn()
|
|
53
|
+
let cn = new Conn(connInfo)
|
|
19
54
|
cn.createCRUDAdapter = function(table, pk) {
|
|
20
55
|
return modCRUDAdapter.createCRUDAdapter(this, table, pk)
|
|
21
56
|
}
|
|
@@ -23,7 +58,7 @@ function Connection(driver) {
|
|
|
23
58
|
}
|
|
24
59
|
else if (driver == 'sqlite' || driver == 'sqlite3') {
|
|
25
60
|
if ( Conn == undefined ) Conn = require ('./driver_sqlite3').Connection
|
|
26
|
-
let cn = new Conn()
|
|
61
|
+
let cn = new Conn(connInfo)
|
|
27
62
|
cn.createCRUDAdapter = function(table, pk) {
|
|
28
63
|
return modCRUDAdapter.createCRUDAdapter(this, table, pk)
|
|
29
64
|
}
|
|
@@ -31,7 +66,7 @@ function Connection(driver) {
|
|
|
31
66
|
}
|
|
32
67
|
else if (driver == 'pg' || driver == 'pgsql' || driver == 'postgres') {
|
|
33
68
|
if ( Conn == undefined ) Conn = require ('./driver_pgsql').Connection
|
|
34
|
-
let cn = new Conn()
|
|
69
|
+
let cn = new Conn(connInfo)
|
|
35
70
|
cn.createCRUDAdapter = function(table, pk) {
|
|
36
71
|
return modCRUDAdapter.createCRUDAdapter(this, table, pk)
|
|
37
72
|
}
|
|
@@ -39,7 +74,7 @@ function Connection(driver) {
|
|
|
39
74
|
}
|
|
40
75
|
else if (driver == 'ms' || driver == 'mssql') {
|
|
41
76
|
if ( Conn == undefined ) Conn = require ('./driver_mssql').Connection
|
|
42
|
-
let cn = new Conn()
|
|
77
|
+
let cn = new Conn(connInfo)
|
|
43
78
|
cn.createCRUDAdapter = function(table, pk) {
|
|
44
79
|
return modCRUDAdapter.createCRUDAdapter(this, table, pk)
|
|
45
80
|
}
|
|
@@ -92,11 +127,17 @@ exports.express.usingDBConnection = function usingDBConnection(connSpec, page )
|
|
|
92
127
|
|
|
93
128
|
exports.express.connect = async function connect(config, req, res) {
|
|
94
129
|
let conn
|
|
130
|
+
let connUrlInfo = parseConnUrl(config)
|
|
95
131
|
try {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
132
|
+
if (connUrlInfo==undefined) {
|
|
133
|
+
conn = Connection(config.driver)
|
|
134
|
+
conn.debugMode = config.debugMode || false
|
|
135
|
+
await conn.open(config.dbInfo)
|
|
136
|
+
} else {
|
|
137
|
+
conn = Connection(config)
|
|
138
|
+
conn.debugMode = config.debugMode || false
|
|
139
|
+
await conn.open()
|
|
140
|
+
}
|
|
100
141
|
|
|
101
142
|
res.on("finish",
|
|
102
143
|
function() {
|
package/package.json
CHANGED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
/********************************************************************************
|
|
2
|
-
* test_dbao_sqlite3.js
|
|
3
|
-
* Test and demo Sqlite3 dao library
|
|
4
|
-
*
|
|
5
|
-
* Author : Stephane Potelle
|
|
6
|
-
* Email : stephane.potelle@gmail.com
|
|
7
|
-
********************************************************************************/
|
|
8
|
-
|
|
9
|
-
var rdao = require ('../lib/xardao.js');
|
|
10
|
-
var promisify = require('util').promisify;
|
|
11
|
-
|
|
12
|
-
cn = new rdao.Connection('mariadb');
|
|
13
|
-
|
|
14
|
-
function logError(e) {
|
|
15
|
-
console.log('Error '+ e)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
process.on('unhandledRejection', function(err) {
|
|
19
|
-
logError('unhandledRejection:' + err.stack);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
/* Example of creating a business object
|
|
23
|
-
as a part of the model */
|
|
24
|
-
|
|
25
|
-
function createContactBO(conn) {
|
|
26
|
-
let bo = conn.createCRUDAdapter('contact','Id')
|
|
27
|
-
|
|
28
|
-
bo.updateValidate = function (p, callback ){
|
|
29
|
-
if ( p.Age >= 63 ) callback(new Error("Age must be lower than 63"))
|
|
30
|
-
else callback()
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
bo.createValidate = bo.updateValidate
|
|
34
|
-
|
|
35
|
-
return bo
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/* Example of using a combination of database commands
|
|
39
|
-
and CRUD operations to perform database operations */
|
|
40
|
-
async function test1() {
|
|
41
|
-
let retErr
|
|
42
|
-
try {
|
|
43
|
-
await cn.open({ host: 'localhost', user: 'root', password: 'xenon21', database: 'apptest'})
|
|
44
|
-
//await cn.exec('start transaction')
|
|
45
|
-
cn.beginTrans()
|
|
46
|
-
await cn.exec('drop table if exists contact')
|
|
47
|
-
await cn.exec(`create table contact (
|
|
48
|
-
Id int primary key auto_increment,
|
|
49
|
-
Firstname varchar(50),
|
|
50
|
-
Lastname varchar(50),
|
|
51
|
-
Birthdate datetime ,
|
|
52
|
-
Email varchar(80),
|
|
53
|
-
Age int)`)
|
|
54
|
-
|
|
55
|
-
let contactBO=createContactBO(cn)
|
|
56
|
-
|
|
57
|
-
let tli = await contactBO.create({
|
|
58
|
-
Firstname: 'James',
|
|
59
|
-
Lastname: 'O\'Connor',
|
|
60
|
-
Birthdate: new Date(1957,7,9,12,0,0),
|
|
61
|
-
Age: 62 } )
|
|
62
|
-
console.log(`Inserted ID #${tli}`)
|
|
63
|
-
console.log( JSON.stringify(contactBO.content))
|
|
64
|
-
for (let i=0; i<10; i++) {
|
|
65
|
-
await contactBO.create({
|
|
66
|
-
Firstname: 'John'+i,
|
|
67
|
-
Lastname: 'Doe-'+i,
|
|
68
|
-
Birthdate: new Date(2001,5,8, 18,0,0),
|
|
69
|
-
Age: 18
|
|
70
|
-
})
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
await contactBO.update({Id:2, Age: 57})
|
|
74
|
-
cn.commitTrans()
|
|
75
|
-
|
|
76
|
-
let o = await contactBO.read(2)
|
|
77
|
-
console.log( JSON.stringify(o))
|
|
78
|
-
console.log( JSON.stringify(contactBO.content))
|
|
79
|
-
|
|
80
|
-
contactBO.content.Age=23
|
|
81
|
-
await contactBO.update(undefined)
|
|
82
|
-
o = await contactBO.read(2)
|
|
83
|
-
console.log( JSON.stringify(o))
|
|
84
|
-
|
|
85
|
-
await contactBO.delete(2)
|
|
86
|
-
|
|
87
|
-
console.log(`Last insert Id: ${cn.lastInsertId}`)
|
|
88
|
-
|
|
89
|
-
let dt = await cn.getDataTable("select * from contact")
|
|
90
|
-
console.log( dt.JSON())
|
|
91
|
-
|
|
92
|
-
console.log("OBJECTS")
|
|
93
|
-
let oc = await cn.getObjects("select * from contact")
|
|
94
|
-
console.log( JSON.stringify(oc,undefined,4))
|
|
95
|
-
|
|
96
|
-
let age = await cn.getScalar( { sql:"select age from contact where Firstname=@Firstname", params: { 'Firstname': 'James' } } )
|
|
97
|
-
console.log( age )
|
|
98
|
-
let kv = await cn.getKVList( "select Firstname, Lastname from contact" )
|
|
99
|
-
console.log( JSON.stringify(kv))
|
|
100
|
-
|
|
101
|
-
console.log("Single object")
|
|
102
|
-
let so = await cn.getSingleObject ( "select * from contact where 1=0" )
|
|
103
|
-
console.log( JSON.stringify(so))
|
|
104
|
-
|
|
105
|
-
console.log("Adding 5000 rows")
|
|
106
|
-
await cn.beginTrans()
|
|
107
|
-
contactBO.createValidate = contactBO.defaultCreateValidate
|
|
108
|
-
for ( let i = 0; i < 5; i++ ) {
|
|
109
|
-
await contactBO.create({
|
|
110
|
-
Firstname: 'John'+i,
|
|
111
|
-
Lastname: 'Doe-'+i,
|
|
112
|
-
Birthdate: new Date(2001,5,8, 18,0,0),
|
|
113
|
-
Age: 18
|
|
114
|
-
})
|
|
115
|
-
}
|
|
116
|
-
await cn.commitTrans()
|
|
117
|
-
console.log("Done")
|
|
118
|
-
|
|
119
|
-
console.log("Eachrow")
|
|
120
|
-
await cn.forEachRow( "select * from contact", function(row, callback ) {
|
|
121
|
-
console.log(JSON.stringify(row))
|
|
122
|
-
callback()
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
} catch(err) {
|
|
126
|
-
console.log(err)
|
|
127
|
-
retErr = err
|
|
128
|
-
} finally {
|
|
129
|
-
await cn.close()
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
test1()
|
|
134
|
-
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
/********************************************************************************
|
|
2
|
-
* test_dbao_mssql.js
|
|
3
|
-
* Test and demo MSSQL dao library
|
|
4
|
-
*
|
|
5
|
-
* Author : Stephane Potelle
|
|
6
|
-
* Email : stephane.potelle@gmail.com
|
|
7
|
-
********************************************************************************/
|
|
8
|
-
|
|
9
|
-
var rdao = require ('../lib/xardao.js');
|
|
10
|
-
var promisify = require('util').promisify;
|
|
11
|
-
|
|
12
|
-
cn = new rdao.Connection('mssql');
|
|
13
|
-
|
|
14
|
-
function logError(e) {
|
|
15
|
-
console.log('Error '+ e)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
process.on('unhandledRejection', function(err) {
|
|
19
|
-
logError('unhandledRejection:' + err.stack);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
/* Example of creating a business object
|
|
23
|
-
as a part of the model */
|
|
24
|
-
|
|
25
|
-
function createContactBO(conn) {
|
|
26
|
-
let bo = conn.createCRUDAdapter('contact','Id')
|
|
27
|
-
|
|
28
|
-
bo.updateValidate = function (p, callback ){
|
|
29
|
-
if ( p.Age >= 63 ) callback(new Error("Age must be lower than 63"))
|
|
30
|
-
else callback()
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
bo.createValidate = bo.updateValidate
|
|
34
|
-
|
|
35
|
-
return bo
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/* Example of using a combination of database commands
|
|
39
|
-
and CRUD operations to perform database operations */
|
|
40
|
-
async function test1(next) {
|
|
41
|
-
let retErr
|
|
42
|
-
try {
|
|
43
|
-
let contactBO=createContactBO(cn)
|
|
44
|
-
await cn.open({ server: 'localhost',
|
|
45
|
-
authentication: { type: 'default',
|
|
46
|
-
options: { userName: 'sa', password: 'Xenon21$'} },
|
|
47
|
-
options: { encrypt:false, database: 'master'}}
|
|
48
|
-
)
|
|
49
|
-
try {
|
|
50
|
-
await cn.exec("CREATE DATABASE apptest")
|
|
51
|
-
} catch (e) {
|
|
52
|
-
console.log(e.message)
|
|
53
|
-
}
|
|
54
|
-
await cn.exec("USE apptest")
|
|
55
|
-
// await cn.close()
|
|
56
|
-
/*await cn.open({ server: 'localhost',
|
|
57
|
-
authentication: { type: 'default',
|
|
58
|
-
options: { userName: 'sa', password: 'Xenon21$'} },
|
|
59
|
-
options: { encrypt:false, database: 'apptest'}}
|
|
60
|
-
)*/
|
|
61
|
-
await cn.exec('if exists ( select * from sys.tables where name=\'contact\') drop table contact')
|
|
62
|
-
await cn.exec('create table contact( Id integer primary key identity, Firstname varchar(50), Lastname varchar(50), Birthdate datetime, Age int)')
|
|
63
|
-
await cn.beginTrans()
|
|
64
|
-
let tli = await contactBO.create({
|
|
65
|
-
Firstname: 'James',
|
|
66
|
-
Lastname: 'O\'Connor',
|
|
67
|
-
Birthdate: new Date(1957,7,9, 12,0,0),
|
|
68
|
-
Age: 62 } )
|
|
69
|
-
console.log(`Inserted ID #${tli}`)
|
|
70
|
-
console.log( JSON.stringify(contactBO.content))
|
|
71
|
-
for (let i=0; i<10; i++) {
|
|
72
|
-
await contactBO.create({
|
|
73
|
-
Firstname: 'John'+i,
|
|
74
|
-
Lastname: 'Doe-'+i,
|
|
75
|
-
Birthdate: new Date(2001,5,8,18,0,0),
|
|
76
|
-
Age: 18
|
|
77
|
-
})
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
await contactBO.update({Id:2, Age: 57})
|
|
81
|
-
await cn.commitTrans()
|
|
82
|
-
|
|
83
|
-
let o = await contactBO.read(2)
|
|
84
|
-
console.log( JSON.stringify(o))
|
|
85
|
-
console.log( JSON.stringify(contactBO.content))
|
|
86
|
-
|
|
87
|
-
contactBO.content.Age=23
|
|
88
|
-
await contactBO.update(undefined)
|
|
89
|
-
o = await contactBO.read(2)
|
|
90
|
-
console.log( JSON.stringify(o))
|
|
91
|
-
|
|
92
|
-
await contactBO.delete(2)
|
|
93
|
-
|
|
94
|
-
console.log(`Last insert Id: ${cn.lastInsertId}`)
|
|
95
|
-
|
|
96
|
-
let dt = await cn.getDataTable("select * from contact")
|
|
97
|
-
console.log( dt.JSON())
|
|
98
|
-
|
|
99
|
-
console.log("OBJECTS")
|
|
100
|
-
let oc = await cn.getObjects("select * from contact")
|
|
101
|
-
console.log( JSON.stringify(oc,undefined,4))
|
|
102
|
-
|
|
103
|
-
let age = await cn.getScalar( { sql:"select age from contact where Firstname=@Firstname", params: { 'Firstname': 'James' } } )
|
|
104
|
-
console.log( age )
|
|
105
|
-
let kv = await cn.getKVList( "select Firstname, Lastname from contact" )
|
|
106
|
-
console.log( JSON.stringify(kv))
|
|
107
|
-
|
|
108
|
-
console.log("Single object")
|
|
109
|
-
let so = await cn.getSingleObject ( "select * from contact" )
|
|
110
|
-
console.log( JSON.stringify(so))
|
|
111
|
-
|
|
112
|
-
console.log("Adding 5000 rows")
|
|
113
|
-
await cn.beginTrans()
|
|
114
|
-
contactBO.createValidate = contactBO.defaultCreateValidate
|
|
115
|
-
for ( let i = 0; i < 5000; i++ ) {
|
|
116
|
-
await contactBO.create({
|
|
117
|
-
Firstname: 'John'+i,
|
|
118
|
-
Lastname: 'Doe-'+i,
|
|
119
|
-
Birthdate: new Date(2001,5,8, 18,0,0),
|
|
120
|
-
Age: 18
|
|
121
|
-
})
|
|
122
|
-
}
|
|
123
|
-
await cn.commitTrans()
|
|
124
|
-
console.log("Done")
|
|
125
|
-
|
|
126
|
-
console.log("Eachrow")
|
|
127
|
-
await cn.forEachRow( "select * from contact", function(row, callback ) {
|
|
128
|
-
console.log(JSON.stringify(row))
|
|
129
|
-
callback()
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
} catch(err) {
|
|
133
|
-
console.log(err.stack)
|
|
134
|
-
retErr = err
|
|
135
|
-
} finally {
|
|
136
|
-
await cn.close()
|
|
137
|
-
}
|
|
138
|
-
if(next) next(retErr)
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
test1Async = promisify (test1)
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
test1();
|
|
146
|
-
|
|
147
|
-
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
/********************************************************************************
|
|
2
|
-
* test_dbao_sqlite3.js
|
|
3
|
-
* Test and demo Sqlite3 dao library
|
|
4
|
-
*
|
|
5
|
-
* Author : Stephane Potelle
|
|
6
|
-
* Email : stephane.potelle@gmail.com
|
|
7
|
-
********************************************************************************/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var rdao = require ('../lib/xardao.js');
|
|
11
|
-
var promisify = require('util').promisify;
|
|
12
|
-
|
|
13
|
-
cn = new rdao.Connection('pg');
|
|
14
|
-
|
|
15
|
-
function logError(e) {
|
|
16
|
-
console.log('Error '+ e)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
process.on('unhandledRejection', function(err) {
|
|
20
|
-
logError('unhandledRejection:' + err.stack);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
/* Example of creating a business object
|
|
24
|
-
as a part of the model */
|
|
25
|
-
|
|
26
|
-
function createContactBO(conn) {
|
|
27
|
-
let bo = conn.createCRUDAdapter('contact','id')
|
|
28
|
-
|
|
29
|
-
bo.updateValidate = function (p, callback ){
|
|
30
|
-
if ( p.Age >= 63 ) callback(new Error("Age must be lower than 63"))
|
|
31
|
-
else callback()
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
bo.createValidate = bo.updateValidate
|
|
35
|
-
|
|
36
|
-
return bo
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/* Example of using a combination of database commands
|
|
40
|
-
and CRUD operations to perform database operations */
|
|
41
|
-
async function test1(next) {
|
|
42
|
-
let retErr
|
|
43
|
-
try {
|
|
44
|
-
let contactBO=createContactBO(cn)
|
|
45
|
-
await cn.open({ host: 'localhost', user: 'apptestusr', password: 'apptestpw', database: 'apptest'})
|
|
46
|
-
await cn.beginTrans()
|
|
47
|
-
await cn.exec('drop table if exists contact')
|
|
48
|
-
await cn.exec('create table contact( id serial primary key, firstname varchar(50), lastname varchar(50), birthdate timestamp, age int)')
|
|
49
|
-
let tli = await contactBO.create({
|
|
50
|
-
firstname: 'James',
|
|
51
|
-
lastname: 'O\'Connor',
|
|
52
|
-
birthdate: new Date(1957,7,9, 12,0,0),
|
|
53
|
-
age: 62 } )
|
|
54
|
-
console.log(`Inserted ID #${tli}`)
|
|
55
|
-
console.log( JSON.stringify(contactBO.content))
|
|
56
|
-
for (let i=0; i<10; i++) {
|
|
57
|
-
await contactBO.create({
|
|
58
|
-
firstname: 'John'+i,
|
|
59
|
-
lastname: 'Doe-'+i,
|
|
60
|
-
birthdate: new Date(2001,5,8,18,0,0),
|
|
61
|
-
age: 18
|
|
62
|
-
})
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
await contactBO.update({id:2, age: 57})
|
|
66
|
-
await cn.commitTrans()
|
|
67
|
-
|
|
68
|
-
let o = await contactBO.read(2)
|
|
69
|
-
console.log( JSON.stringify(o))
|
|
70
|
-
console.log( JSON.stringify(contactBO.content))
|
|
71
|
-
|
|
72
|
-
contactBO.content.Age=23
|
|
73
|
-
await contactBO.update(undefined)
|
|
74
|
-
o = await contactBO.read(2)
|
|
75
|
-
console.log( JSON.stringify(o))
|
|
76
|
-
|
|
77
|
-
await contactBO.delete(2)
|
|
78
|
-
|
|
79
|
-
console.log(`Last insert Id: ${cn.lastInsertId}`)
|
|
80
|
-
|
|
81
|
-
let dt = await cn.getDataTable("select * from contact")
|
|
82
|
-
console.log( dt.JSON())
|
|
83
|
-
|
|
84
|
-
console.log("OBJECTS")
|
|
85
|
-
let oc = await cn.getObjects("select * from contact")
|
|
86
|
-
console.log( JSON.stringify(oc,undefined,4))
|
|
87
|
-
|
|
88
|
-
let age = await cn.getScalar( { sql:"select age from contact where Firstname=@Firstname", params: { 'Firstname': 'James' } } )
|
|
89
|
-
console.log( age )
|
|
90
|
-
let kv = await cn.getKVList( "select Firstname, Lastname from contact" )
|
|
91
|
-
console.log( JSON.stringify(kv))
|
|
92
|
-
|
|
93
|
-
console.log("Single object")
|
|
94
|
-
let so = await cn.getSingleObject ( "select * from contact" )
|
|
95
|
-
console.log( JSON.stringify(so))
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
console.log("Adding 5000 rows")
|
|
99
|
-
|
|
100
|
-
await cn.beginTrans()
|
|
101
|
-
contactBO.createValidate = contactBO.defaultCreateValidate
|
|
102
|
-
for ( let i = 0; i < 5000; i++ ) {
|
|
103
|
-
await contactBO.create({
|
|
104
|
-
firstname: 'John'+i,
|
|
105
|
-
lastname: 'Doe-'+i,
|
|
106
|
-
birthdate: new Date(2001,5,8,18,0,0),
|
|
107
|
-
age: 18
|
|
108
|
-
})
|
|
109
|
-
}
|
|
110
|
-
await cn.commitTrans()
|
|
111
|
-
console.log("Done")
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
console.log("Eachrow")
|
|
115
|
-
await cn.forEachRow( "select * from contact", function( row, callback ) {
|
|
116
|
-
console.log(JSON.stringify(row))
|
|
117
|
-
callback()
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
} catch(err) {
|
|
121
|
-
console.log(err)
|
|
122
|
-
retErr = err
|
|
123
|
-
} finally {
|
|
124
|
-
await cn.close()
|
|
125
|
-
}
|
|
126
|
-
if(next) next(retErr)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
test1Async = promisify (test1)
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
test1();
|
|
134
|
-
|
|
135
|
-
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
/********************************************************************************
|
|
2
|
-
* test_dbao_sqlite3.js
|
|
3
|
-
* Test and demo Sqlite3 dao library
|
|
4
|
-
*
|
|
5
|
-
* Author : Stephane Potelle
|
|
6
|
-
* Email : stephane.potelle@gmail.com
|
|
7
|
-
********************************************************************************/
|
|
8
|
-
|
|
9
|
-
var rdao = require ('../lib/xardao.js');
|
|
10
|
-
const promisify = require('util').promisify
|
|
11
|
-
|
|
12
|
-
cn = rdao.Connection('sqlite')
|
|
13
|
-
|
|
14
|
-
const sleep = promisify ( function (t, callback ) { setTimeout(callback, t)} )
|
|
15
|
-
|
|
16
|
-
function logError(e) {
|
|
17
|
-
console.log('Error '+ e)
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
process.on('unhandledRejection', function(err) {
|
|
21
|
-
logError('unhandledRejection:' + err.stack);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
/* Example of creating a business object
|
|
25
|
-
as a part of the model */
|
|
26
|
-
|
|
27
|
-
function createContactBO(conn) {
|
|
28
|
-
let bo = conn.createCRUDAdapter('contact','Id')
|
|
29
|
-
|
|
30
|
-
bo.updateValidate = function (p, callback ){
|
|
31
|
-
console.log("updateValidate: "+JSON.stringify(this.content))
|
|
32
|
-
if ( p.Age >= 63 ) callback(new Error("Age must be lower than 63"))
|
|
33
|
-
else callback()
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
bo.createValidate = bo.updateValidate
|
|
37
|
-
|
|
38
|
-
return bo
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/* Example of using a combination of database commands
|
|
42
|
-
and CRUD operations to perform database operations */
|
|
43
|
-
async function test1(next) {
|
|
44
|
-
let retErr
|
|
45
|
-
try {
|
|
46
|
-
let contactBO=createContactBO(cn)
|
|
47
|
-
await cn.open({ database: "test_database.sqlite",
|
|
48
|
-
initSql: [ "PRAGMA foreign_keys = '1';",
|
|
49
|
-
"PRAGMA autovacuum = '1';"] })
|
|
50
|
-
|
|
51
|
-
await cn.beginTrans()
|
|
52
|
-
await cn.exec('drop table if exists contact')
|
|
53
|
-
await cn.exec('create table contact( Id integer primary key autoincrement, Firstname varchar(50), Lastname varchar(50), Birthdate timestamp, Age int)')
|
|
54
|
-
let tli = await contactBO.create({
|
|
55
|
-
Firstname: 'James',
|
|
56
|
-
Lastname: 'O\'Connor',
|
|
57
|
-
Birthdate: new Date(1957,7,9, 12,0,0),
|
|
58
|
-
Age: 62 } )
|
|
59
|
-
console.log(`Inserted ID #${tli}`)
|
|
60
|
-
console.log( JSON.stringify(contactBO.content))
|
|
61
|
-
await contactBO.read(undefined)
|
|
62
|
-
console.log( JSON.stringify(contactBO.content))
|
|
63
|
-
for (let i=0; i<10; i++) {
|
|
64
|
-
await contactBO.create({
|
|
65
|
-
Firstname: 'John'+i,
|
|
66
|
-
Lastname: 'Doe-'+i,
|
|
67
|
-
Birthdate: new Date(2001,5,8, 18,0,0),
|
|
68
|
-
Age: 18
|
|
69
|
-
})
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
await contactBO.update({Id:2, Age: 57})
|
|
73
|
-
await cn.commitTrans()
|
|
74
|
-
|
|
75
|
-
let o = await contactBO.read(2)
|
|
76
|
-
console.log( JSON.stringify(o))
|
|
77
|
-
console.log( JSON.stringify(contactBO.content))
|
|
78
|
-
|
|
79
|
-
contactBO.content.Age=23
|
|
80
|
-
await contactBO.update(undefined)
|
|
81
|
-
o = await contactBO.read(2)
|
|
82
|
-
console.log( JSON.stringify(o))
|
|
83
|
-
|
|
84
|
-
await contactBO.delete(2)
|
|
85
|
-
|
|
86
|
-
console.log(`Last insert Id: ${cn.lastInsertId}`)
|
|
87
|
-
|
|
88
|
-
let dt = await cn.getDataTable( { sql: "select * from contact", options: { useSnakeCase: true } })
|
|
89
|
-
console.log( dt.JSON())
|
|
90
|
-
|
|
91
|
-
console.log("OBJECTS")
|
|
92
|
-
let oc = await cn.getObjects( { sql: "select * from contact", options: { useSnakeCase: true } })
|
|
93
|
-
console.log( JSON.stringify(oc,undefined,4))
|
|
94
|
-
|
|
95
|
-
let age = await cn.getScalar( { sql:"select age from contact where Firstname=@Firstname", params: { 'Firstname': 'James' } } )
|
|
96
|
-
console.log( age )
|
|
97
|
-
let kv = await cn.getKVList( "select Firstname, Lastname from contact" )
|
|
98
|
-
console.log( JSON.stringify(kv))
|
|
99
|
-
|
|
100
|
-
console.log("Single object")
|
|
101
|
-
let so = await cn.getSingleObject ( { sql: "select * from contact", options: { useSnakeCase: true } })
|
|
102
|
-
console.log( JSON.stringify(so))
|
|
103
|
-
|
|
104
|
-
console.log("Adding 5000 rows")
|
|
105
|
-
await cn.beginTrans()
|
|
106
|
-
contactBO.createValidate = contactBO.defaultCreateValidate
|
|
107
|
-
for ( let i = 0; i < 5000; i++ ) {
|
|
108
|
-
await contactBO.create({
|
|
109
|
-
FIRSTNAME: 'John'+i,
|
|
110
|
-
Lastname: 'Doe-'+i,
|
|
111
|
-
Birthdate: new Date(2001,5,8, 18,0,0),
|
|
112
|
-
Age: 18
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
await cn.commitTrans()
|
|
116
|
-
console.log("Done")
|
|
117
|
-
|
|
118
|
-
console.log("Eachrow")
|
|
119
|
-
cn.forEachRow( "select * from contact", function(row, callback) {
|
|
120
|
-
console.log(JSON.stringify(row))
|
|
121
|
-
//callback()
|
|
122
|
-
})
|
|
123
|
-
console.log("Done")
|
|
124
|
-
|
|
125
|
-
} catch(err) {
|
|
126
|
-
console.log(err.stack)
|
|
127
|
-
retErr = err
|
|
128
|
-
} finally {
|
|
129
|
-
await cn.close()
|
|
130
|
-
}
|
|
131
|
-
if(next) next(retErr)
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
test1Async = promisify (test1)
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
test1();
|
|
139
|
-
|
|
140
|
-
|