xardao 1.2.1 → 1.2.2
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/package.json +1 -1
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/
|