xardao 1.1.20 → 1.2.3
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/README.md +1 -199
- package/lib/driver_mariadb.js +5 -8
- package/lib/driver_mssql.js +28 -19
- 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/README.md
CHANGED
|
@@ -1,202 +1,4 @@
|
|
|
1
|
-
|
|
2
1
|
# xardao
|
|
3
|
-
--------
|
|
4
|
-
## Overview
|
|
5
|
-
|
|
6
2
|
Xardao Asynchronous Relational Database Access Object
|
|
7
3
|
|
|
8
|
-
|
|
9
|
-
Currently supports SQLite, MariaDB (mysql), PostgreSQL, SQL server
|
|
10
|
-
|
|
11
|
-
The library is fully asynchronous and all functions can be used both as for promise-style of callback-style calls.
|
|
12
|
-
|
|
13
|
-
```javascript
|
|
14
|
-
// Callback style
|
|
15
|
-
cn.getObjects ("select * from mytable", function(err, arrayOfObject) { do something ...} )
|
|
16
|
-
|
|
17
|
-
// Promise style
|
|
18
|
-
try {
|
|
19
|
-
arrayOfObject = await cn.getObjects ("select * from mytable")
|
|
20
|
-
do something ...
|
|
21
|
-
} catch(e) {
|
|
22
|
-
// in case there is an error
|
|
23
|
-
}
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
It provides the basic functions of a database driver through the same interface for all databases:
|
|
27
|
-
|
|
28
|
-
* Read query result to an object
|
|
29
|
-
* Read a set of rows to a data table or array of objects
|
|
30
|
-
* Read a single value
|
|
31
|
-
* Execute SQL statement, either individually or sequentially
|
|
32
|
-
|
|
33
|
-
It also provides a base CRUD adapter that can be used as the foundation for creating business objects
|
|
34
|
-
|
|
35
|
-
The package DOES NOT specify the original driver as dependencies. This is done on purpose bacause you
|
|
36
|
-
generally use only one type of database in your project.
|
|
37
|
-
|
|
38
|
-
The underlying database driver module must be added as a module dependency.
|
|
39
|
-
|
|
40
|
-
## API
|
|
41
|
-
|
|
42
|
-
### Class Connection
|
|
43
|
-
|
|
44
|
-
#### Opening a connection
|
|
45
|
-
Before you connect to a database engine, you need to get an instance of *Connection*.
|
|
46
|
-
It is not recommended to instanciate it using the "new" keyword from the driver class.
|
|
47
|
-
|
|
48
|
-
Instead you should use the *Connection* function from the xardao namespace.
|
|
49
|
-
|
|
50
|
-
```javascript
|
|
51
|
-
const xardao = require ('../lib/xardao.js');
|
|
52
|
-
|
|
53
|
-
cn = xardao.Connection('sqlite')
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
The Connection function accepts the following values as driver name:
|
|
57
|
-
|
|
58
|
-
* 'sqlite' or 'sqlite3'
|
|
59
|
-
* 'mysql' or 'mariadb'
|
|
60
|
-
* 'pg' or 'pgsql' or 'postgres'
|
|
61
|
-
* 'ms' or 'mssql'
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
#### One-line query specification
|
|
66
|
-
Most functions need a first parameter *query* that will indicates the SQL to be executed by the database engine.
|
|
67
|
-
XARDAO provides a common method for passing SQL parameters to the underlying database engine.
|
|
68
|
-
Query may be one of the following:
|
|
69
|
-
|
|
70
|
-
* A SQL statement as a string
|
|
71
|
-
* An object with 2 or 3 attributes:
|
|
72
|
-
* *sql* : a string containing a SQL statement. Query parameters are prefixed with "@"
|
|
73
|
-
* *params*: an object whose attributes are named after the parameters specified in *sql* (Without "@" prefix)
|
|
74
|
-
* *options*: options to modify the function behavior
|
|
75
|
-
|
|
76
|
-
#####Example
|
|
77
|
-
```javascript
|
|
78
|
-
await cn.exec ({
|
|
79
|
-
sql: "insert into contacts(first_name, last_name) values (@firstName, @lastName) ",
|
|
80
|
-
params: {
|
|
81
|
-
firstName: "John",
|
|
82
|
-
lastName: "Smith"
|
|
83
|
-
}
|
|
84
|
-
})
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
#### Opening and closing
|
|
89
|
-
##### Connection#open( spec, [callback] )
|
|
90
|
-
Opens a database connection. *spec* is the database connection specification and is specific to the database driver chosen.
|
|
91
|
-
Refer to each driver documentation for details. *Callback* is optional, but needed when using callback style.
|
|
92
|
-
|
|
93
|
-
##### Connection#close( [callback] )
|
|
94
|
-
Closes a database connection.
|
|
95
|
-
*Callback* is optional, but needed when using callback style.
|
|
96
|
-
|
|
97
|
-
#### Reading
|
|
98
|
-
##### Connection#getObjects( query, [callback] )
|
|
99
|
-
Runs the *query* and returns an array of objects, one for each row.
|
|
100
|
-
*Callback* is optional, but needed when using callback style.
|
|
101
|
-
|
|
102
|
-
##### Connection#getSingleObject( query, [callback] )
|
|
103
|
-
Runs the *query* and returns the first row as an object.
|
|
104
|
-
*Callback* is optional, but needed when using callback style.
|
|
105
|
-
|
|
106
|
-
##### Connection#getDataTable( query, [callback] )
|
|
107
|
-
Runs the *query* and returns the results as a datatable object.
|
|
108
|
-
*Callback* is optional, but needed when using callback style.
|
|
109
|
-
|
|
110
|
-
##### Connection#getScalar( query, [callback] )
|
|
111
|
-
Runs the *query* and returns the first field of the first row as single value.
|
|
112
|
-
*Callback* is optional, but needed when using callback style.
|
|
113
|
-
|
|
114
|
-
##### Connection#getList( query, [callback] )
|
|
115
|
-
Runs the *query* and returns the results as a array of scalar values containing only the fist column of each row.
|
|
116
|
-
*Callback* is optional, but needed when using callback style.
|
|
117
|
-
|
|
118
|
-
##### Connection#getKVList( query, [callback] )
|
|
119
|
-
Runs the *query* and returns the results as a an object. The first column is the Key and the second column is the value.
|
|
120
|
-
*Callback* is optional, but needed when using callback style.
|
|
121
|
-
|
|
122
|
-
##### Connection#forEachRow( query, eachRowFunc, [callback] )
|
|
123
|
-
|
|
124
|
-
#### Updating
|
|
125
|
-
##### Connection#execSingle( query, [callback] )
|
|
126
|
-
Executes a single query.
|
|
127
|
-
*Callback* is optional, but needed when using callback style.
|
|
128
|
-
|
|
129
|
-
##### Connection#execMultiple( arrayOfQueries, [callback] )
|
|
130
|
-
Executes multiple queries in a sequence.
|
|
131
|
-
*Callback* is optional, but needed when using callback style.
|
|
132
|
-
|
|
133
|
-
##### Connection#exec( queryOrArrayOfQueries, [callback] )
|
|
134
|
-
Provides a unique function for single and multiple queries. Act as an alias for *execSingle* or *execMultiple*, depending on the type of the first parmameter.
|
|
135
|
-
|
|
136
|
-
#### Working with transactions
|
|
137
|
-
The transaction methods provide a common wait of managing transactions.
|
|
138
|
-
|
|
139
|
-
##### Connection#beginTrans()
|
|
140
|
-
Starts a transaction
|
|
141
|
-
|
|
142
|
-
##### Connection#commitTrans()
|
|
143
|
-
Commits current transaction. If no transaction is in progress, this call has no effect.
|
|
144
|
-
|
|
145
|
-
##### Connection#rollbackTrans()
|
|
146
|
-
Rolls back current transaction. If no transaction is in progress, this call has no effect.
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
### Class Datatable
|
|
150
|
-
This class provides a storage structure for the results. The columns and rows are stored separately which give the advantage that the columns information is available event of the resultset has no rows.
|
|
151
|
-
|
|
152
|
-
The Datatable object has the structure below:
|
|
153
|
-
|
|
154
|
-
```javascript
|
|
155
|
-
{
|
|
156
|
-
columns: [], // Each columns is a string
|
|
157
|
-
rows: [] // Each row is an array of values, in the same order as the columns
|
|
158
|
-
}
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
*_Note_* The use of datatables may be deprecated in future releases.
|
|
162
|
-
|
|
163
|
-
### Integration with ExpressJS
|
|
164
|
-
|
|
165
|
-
In an web server context, each page call requires an isolated connection. It then becomes necessary to ensure that connections always get closed after each HTTP request.
|
|
166
|
-
|
|
167
|
-
Xardao provides 2 methods to ensure that the open connection will always be closed:
|
|
168
|
-
|
|
169
|
-
+ A decorator function to use in the router
|
|
170
|
-
+ A connection opener function
|
|
171
|
-
|
|
172
|
-
#### xardao.express.usingDBConnection(connSpec, controller)
|
|
173
|
-
"Decorates" the function *Controller* by adding an open connection to request object.
|
|
174
|
-
|
|
175
|
-
```javascript
|
|
176
|
-
// In the router:
|
|
177
|
-
|
|
178
|
-
routes.get('/dtjson', xardao.express.usingDBConnection(connSpec, controllers.dtjson))
|
|
179
|
-
|
|
180
|
-
// In the controller, simply use the connection:
|
|
181
|
-
|
|
182
|
-
req.conn.getDataTable('select * from assets', function(err, dt) {
|
|
183
|
-
if (!err) {
|
|
184
|
-
dt.className ='basic-table'
|
|
185
|
-
res.render('dt', { title: 'Express', hitcount: req.session.hitcount, tbl: dt })
|
|
186
|
-
next()
|
|
187
|
-
}
|
|
188
|
-
next(err)
|
|
189
|
-
})
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
#### xardao.express.connect(connSpec, req, res)
|
|
193
|
-
Returns the connection specified by *connSpec*. A listener to the 'finish' event of the response is
|
|
194
|
-
added to guarantee that the connection will always be closed, so there is no need to write specific code
|
|
195
|
-
to ensure proper connection closing.
|
|
196
|
-
|
|
197
|
-
```javascript
|
|
198
|
-
// In the controller
|
|
199
|
-
|
|
200
|
-
let conn = await xardao.express.connect(req.appconfig.database, req, res)
|
|
201
|
-
|
|
202
|
-
```
|
|
4
|
+
See https://stefpo.github.io/node-xardao/
|
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,22 +67,28 @@ class Connection {
|
|
|
66
67
|
}
|
|
67
68
|
|
|
68
69
|
openCB(dbInfo, callback) {
|
|
69
|
-
let me = this
|
|
70
|
-
function postConnection(cn, sql, callback) {
|
|
71
|
-
if (dbInfo.options.requestTimeout) me.timeoutMilliSeconds = dbInfo.options.requestTimeout
|
|
72
|
-
if (typeof (sql) == "string" || Array.isArray(sql) ) cn.execCB(sql, callback)
|
|
73
|
-
else then( callback )
|
|
74
|
-
}
|
|
75
|
-
|
|
76
70
|
this.debug('open','Opening connection for MSSQL')
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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)
|
|
85
92
|
}
|
|
86
93
|
|
|
87
94
|
|
|
@@ -409,7 +416,9 @@ class Connection {
|
|
|
409
416
|
this.db.execSql(request)
|
|
410
417
|
}
|
|
411
418
|
|
|
412
|
-
execCB(params, callback) {
|
|
419
|
+
execCB(params, callback) {
|
|
420
|
+
common.exec(this, params, callback)
|
|
421
|
+
}
|
|
413
422
|
execMultipleCB(queries, callback) { common.execMultiple(this, queries, callback) }
|
|
414
423
|
|
|
415
424
|
execSingleCB(params, callback) {
|
|
@@ -421,13 +430,13 @@ class Connection {
|
|
|
421
430
|
let request = new mssql.Request(realSql, function(err, rowcount, rows) {
|
|
422
431
|
if (err) {
|
|
423
432
|
self.debug("execSingle",'Error: '+err.code)
|
|
424
|
-
then( function() {callback(err)})
|
|
433
|
+
then( function() { callback(err) })
|
|
425
434
|
}
|
|
426
435
|
else {
|
|
427
436
|
//self.lastInsertId = 0 //if (result.insertId > 0) self.lastInsertId = result.insertId
|
|
428
437
|
//self.lastStatementChanges = 0 //result.affectedRows
|
|
429
438
|
self.debug("execSingle",'Completed in ' + (Date.now() -ts) + ' ms LastId: ' + self.lastInsertId + ' Changes: ' + self.lastStatementChanges)
|
|
430
|
-
then(callback)
|
|
439
|
+
then( callback )
|
|
431
440
|
}
|
|
432
441
|
})
|
|
433
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.substring(1).split("/")
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
url: url,
|
|
26
|
+
protocol: tUrl.protocol,
|
|
27
|
+
hostname: tUrl.hostname,
|
|
28
|
+
port: tUrl.port,
|
|
29
|
+
username: decodeURIComponent(tUrl.username),
|
|
30
|
+
password: decodeURIComponent(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
|
-
|