node-firebird 1.1.8 → 1.1.9

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 CHANGED
@@ -63,28 +63,24 @@ options.database = 'database.fdb';
63
63
  options.user = 'SYSDBA';
64
64
  options.password = 'masterkey';
65
65
  options.lowercase_keys = false; // set to true to lowercase keys
66
- options.role = null; // default
67
- options.pageSize = 4096; // default when creating database
68
- options.pageSize = 4096; // default when creating database
66
+ options.role = null; // default
67
+ options.pageSize = 4096; // default when creating database
69
68
  options.retryConnectionInterval = 1000; // reconnect interval in case of connection drop
70
69
  options.blobAsText = false; // set to true to get blob as text, only affects blob subtype 1
71
- options.encoding = 'UTF-8'; // default encoding for connection is UTF-8
70
+ options.encoding = 'UTF8'; // default encoding for connection is UTF-8
72
71
  ```
73
72
 
74
73
  ### Classic
75
74
 
76
75
  ```js
77
- Firebird.attach(options, function(err, db) {
78
-
79
- if (err)
80
- throw err;
81
-
82
- // db = DATABASE
83
- db.query('SELECT * FROM TABLE', function(err, result) {
84
- // IMPORTANT: close the connection
85
- db.detach();
86
- });
76
+ Firebird.attach(options, function (err, db) {
77
+ if (err) throw err;
87
78
 
79
+ // db = DATABASE
80
+ db.query('SELECT * FROM TABLE', function (err, result) {
81
+ // IMPORTANT: close the connection
82
+ db.detach();
83
+ });
88
84
  });
89
85
  ```
90
86
 
@@ -95,16 +91,14 @@ Firebird.attach(options, function(err, db) {
95
91
  var pool = Firebird.pool(5, options);
96
92
 
97
93
  // Get a free pool
98
- pool.get(function(err, db) {
99
-
100
- if (err)
101
- throw err;
94
+ pool.get(function (err, db) {
95
+ if (err) throw err;
102
96
 
103
- // db = DATABASE
104
- db.query('SELECT * FROM TABLE', function(err, result) {
105
- // IMPORTANT: release the pool connection
106
- db.detach();
107
- });
97
+ // db = DATABASE
98
+ db.query('SELECT * FROM TABLE', function (err, result) {
99
+ // IMPORTANT: release the pool connection
100
+ db.detach();
101
+ });
108
102
  });
109
103
 
110
104
  // Destroy pool
@@ -135,187 +129,192 @@ pool.destroy();
135
129
  ### Parameters
136
130
 
137
131
  ```js
138
- Firebird.attach(options, function(err, db) {
139
-
140
- if (err)
141
- throw err;
142
-
143
- // db = DATABASE
144
- db.query('INSERT INTO USERS (ID, ALIAS, CREATED) VALUES(?, ?, ?) RETURNING ID', [1, 'Pe\'ter', new Date()], function(err, result) {
145
- console.log(result[0].id);
146
- db.query('SELECT * FROM USERS WHERE Alias=?', ['Peter'], function(err, result) {
147
- console.log(result);
148
- db.detach();
149
- });
150
- });
132
+ Firebird.attach(options, function (err, db) {
133
+ if (err) throw err;
134
+
135
+ // db = DATABASE
136
+ db.query(
137
+ 'INSERT INTO USERS (ID, ALIAS, CREATED) VALUES(?, ?, ?) RETURNING ID',
138
+ [1, "Pe'ter", new Date()],
139
+ function (err, result) {
140
+ console.log(result[0].id);
141
+ db.query(
142
+ 'SELECT * FROM USERS WHERE Alias=?',
143
+ ['Peter'],
144
+ function (err, result) {
145
+ console.log(result);
146
+ db.detach();
147
+ }
148
+ );
149
+ }
150
+ );
151
151
  });
152
152
  ```
153
153
 
154
154
  ### BLOB (stream)
155
155
 
156
156
  ```js
157
- Firebird.attach(options, function(err, db) {
158
-
159
- if (err)
160
- throw err;
161
-
162
- // db = DATABASE
163
- // INSERT STREAM as BLOB
164
- db.query('INSERT INTO USERS (ID, ALIAS, FILE) VALUES(?, ?, ?)', [1, 'Peter', fs.createReadStream('/users/image.jpg')], function(err, result) {
165
- // IMPORTANT: close the connection
166
- db.detach();
167
- });
157
+ Firebird.attach(options, function (err, db) {
158
+ if (err) throw err;
159
+
160
+ // db = DATABASE
161
+ // INSERT STREAM as BLOB
162
+ db.query(
163
+ 'INSERT INTO USERS (ID, ALIAS, FILE) VALUES(?, ?, ?)',
164
+ [1, 'Peter', fs.createReadStream('/users/image.jpg')],
165
+ function (err, result) {
166
+ // IMPORTANT: close the connection
167
+ db.detach();
168
+ }
169
+ );
168
170
  });
169
171
  ```
170
172
 
171
173
  ### BLOB (buffer)
172
174
 
173
175
  ```js
174
- Firebird.attach(options, function(err, db) {
175
-
176
- if (err)
177
- throw err;
178
-
179
- // db = DATABASE
180
- // INSERT BUFFER as BLOB
181
- db.query('INSERT INTO USERS (ID, ALIAS, FILE) VALUES(?, ?, ?)', [1, 'Peter', fs.readFileSync('/users/image.jpg')], function(err, result) {
182
- // IMPORTANT: close the connection
183
- db.detach();
184
- });
176
+ Firebird.attach(options, function (err, db) {
177
+ if (err) throw err;
178
+
179
+ // db = DATABASE
180
+ // INSERT BUFFER as BLOB
181
+ db.query(
182
+ 'INSERT INTO USERS (ID, ALIAS, FILE) VALUES(?, ?, ?)',
183
+ [1, 'Peter', fs.readFileSync('/users/image.jpg')],
184
+ function (err, result) {
185
+ // IMPORTANT: close the connection
186
+ db.detach();
187
+ }
188
+ );
185
189
  });
186
190
  ```
187
191
 
188
192
  ### Reading Blobs (Asynchronous)
189
193
 
190
194
  ```js
191
- Firebird.attach(options, function(err, db) {
195
+ Firebird.attach(options, function (err, db) {
196
+ if (err) throw err;
192
197
 
193
- if (err)
194
- throw err;
195
-
196
- // db = DATABASE
197
- db.query('SELECT ID, ALIAS, USERPICTURE FROM USER', function(err, rows) {
198
-
199
- if (err)
200
- throw err;
201
-
202
- // first row
203
- rows[0].userpicture(function(err, name, e) {
198
+ // db = DATABASE
199
+ db.query('SELECT ID, ALIAS, USERPICTURE FROM USER', function (err, rows) {
200
+ if (err) throw err;
204
201
 
205
- if (err)
206
- throw err;
202
+ // first row
203
+ rows[0].userpicture(function (err, name, e) {
204
+ if (err) throw err;
207
205
 
208
- // +v0.2.4
209
- // e.pipe(writeStream/Response);
206
+ // +v0.2.4
207
+ // e.pipe(writeStream/Response);
210
208
 
211
- // e === EventEmitter
212
- e.on('data', function(chunk) {
213
- // reading data
214
- });
215
-
216
- e.on('end', function() {
217
- // end reading
218
- // IMPORTANT: close the connection
219
- db.detach();
220
- });
221
- });
209
+ // e === EventEmitter
210
+ e.on('data', function (chunk) {
211
+ // reading data
212
+ });
222
213
 
214
+ e.on('end', function () {
215
+ // end reading
216
+ // IMPORTANT: close the connection
217
+ db.detach();
218
+ });
223
219
  });
220
+ });
224
221
  });
225
222
  ```
226
223
 
227
224
  ### Reading Multiples Blobs (Asynchronous)
225
+
228
226
  ```js
229
227
  Firebird.attach(options, (err, db) => {
230
- if (err)
231
- throw err;
228
+ if (err) throw err;
232
229
 
233
- db.transaction(Firebird.ISOLATION_READ_COMMITTED, (err, transaction) => {
234
- if (err) {
235
- throw err;
236
- }
230
+ db.transaction(Firebird.ISOLATION_READ_COMMITTED, (err, transaction) => {
231
+ if (err) {
232
+ throw err;
233
+ }
237
234
 
238
- transaction.query('SELECT FIRST 10 * FROM JOB', (err, result) => {
239
- if (err) {
240
- transaction.rollback();
241
- return;
242
- }
243
-
244
- const arrBlob = [];
245
- for (const item of result) {
246
- const fields = Object.keys(item);
247
- for (const key of fields) {
248
- if (typeof item[key] === 'function') {
249
- item[key] = new Promise((resolve, reject) => {
250
- // the same transaction is used (better performance)
251
- // this is optional
252
- item[key](transaction, (error, name, event, row) => {
253
- if (error) {
254
- return reject(error);
255
- }
256
-
257
- // reading data
258
- let value = '';
259
- event.on('data', (chunk) => {
260
- value += chunk.toString('binary');
261
- });
262
- event.on('end', () => {
263
- resolve({ value, column: name, row });
264
- });
265
- });
266
- });
267
- arrBlob.push(item[key]);
268
- }
269
- }
270
- }
271
-
272
- Promise.all(arrBlob).then((blobs) => {
273
- for (const blob of blobs) {
274
- result[blob.row][blob.column] = blob.value;
235
+ transaction.query('SELECT FIRST 10 * FROM JOB', (err, result) => {
236
+ if (err) {
237
+ transaction.rollback();
238
+ return;
239
+ }
240
+
241
+ const arrBlob = [];
242
+ for (const item of result) {
243
+ const fields = Object.keys(item);
244
+ for (const key of fields) {
245
+ if (typeof item[key] === 'function') {
246
+ item[key] = new Promise((resolve, reject) => {
247
+ // the same transaction is used (better performance)
248
+ // this is optional
249
+ item[key](transaction, (error, name, event, row) => {
250
+ if (error) {
251
+ return reject(error);
275
252
  }
276
253
 
277
- transaction.commit((err) => {
278
- if (err) {
279
- transaction.rollback();
280
- return;
281
- }
282
-
283
- db.detach();
284
- console.log(result);
254
+ // reading data
255
+ let value = '';
256
+ event.on('data', (chunk) => {
257
+ value += chunk.toString('binary');
258
+ });
259
+ event.on('end', () => {
260
+ resolve({ value, column: name, row });
285
261
  });
286
- }).catch((err) => {
287
- transaction.rollback();
262
+ });
288
263
  });
264
+ arrBlob.push(item[key]);
265
+ }
266
+ }
267
+ }
268
+
269
+ Promise.all(arrBlob)
270
+ .then((blobs) => {
271
+ for (const blob of blobs) {
272
+ result[blob.row][blob.column] = blob.value;
273
+ }
274
+
275
+ transaction.commit((err) => {
276
+ if (err) {
277
+ transaction.rollback();
278
+ return;
279
+ }
280
+
281
+ db.detach();
282
+ console.log(result);
283
+ });
284
+ })
285
+ .catch((err) => {
286
+ transaction.rollback();
289
287
  });
290
288
  });
289
+ });
291
290
  });
292
291
  ```
293
292
 
294
293
  ### Streaming a big data
295
294
 
296
295
  ```js
297
- Firebird.attach(options, function(err, db) {
298
-
299
- if (err)
300
- throw err;
301
-
302
- // db = DATABASE
303
- db.sequentially('SELECT * FROM BIGTABLE', function(row, index) {
304
-
305
- // EXAMPLE
306
- stream.write(JSON.stringify(row));
307
-
308
- }, function(err) {
309
- // END
310
- // IMPORTANT: close the connection
311
- db.detach();
312
- });
296
+ Firebird.attach(options, function (err, db) {
297
+ if (err) throw err;
298
+
299
+ // db = DATABASE
300
+ db.sequentially(
301
+ 'SELECT * FROM BIGTABLE',
302
+ function (row, index) {
303
+ // EXAMPLE
304
+ stream.write(JSON.stringify(row));
305
+ },
306
+ function (err) {
307
+ // END
308
+ // IMPORTANT: close the connection
309
+ db.detach();
310
+ }
311
+ );
313
312
  });
314
313
  ```
315
314
 
316
315
  ### Transactions
317
316
 
318
- __Transaction types:__
317
+ **Transaction types:**
319
318
 
320
319
  - `Firebird.ISOLATION_READ_UNCOMMITTED`
321
320
  - `Firebird.ISOLATION_READ_COMMITTED`
@@ -324,77 +323,67 @@ __Transaction types:__
324
323
  - `Firebird.ISOLATION_READ_COMMITTED_READ_ONLY`
325
324
 
326
325
  ```js
327
- Firebird.attach(options, function(err, db) {
328
-
329
- if (err)
330
- throw err;
331
-
332
- // db = DATABASE
333
- db.transaction(Firebird.ISOLATION_READ_COMMITTED, function(err, transaction) {
334
- transaction.query('INSERT INTO users VALUE(?,?)', [1, 'Janko'], function(err, result) {
335
-
336
- if (err) {
337
- transaction.rollback();
338
- return;
339
- }
340
-
341
- transaction.commit(function(err) {
342
- if (err)
343
- transaction.rollback();
344
- else
345
- db.detach();
346
- });
347
- });
348
- });
326
+ Firebird.attach(options, function (err, db) {
327
+ if (err) throw err;
328
+
329
+ // db = DATABASE
330
+ db.transaction(
331
+ Firebird.ISOLATION_READ_COMMITTED,
332
+ function (err, transaction) {
333
+ transaction.query(
334
+ 'INSERT INTO users VALUE(?,?)',
335
+ [1, 'Janko'],
336
+ function (err, result) {
337
+ if (err) {
338
+ transaction.rollback();
339
+ return;
340
+ }
341
+
342
+ transaction.commit(function (err) {
343
+ if (err) transaction.rollback();
344
+ else db.detach();
345
+ });
346
+ }
347
+ );
348
+ }
349
+ );
349
350
  });
350
351
  ```
351
352
 
352
353
  ### Events
353
354
 
354
355
  ```js
355
- Firebird.attach(options, function(err, db) {
356
-
357
- if (err)
358
- throw err;
359
-
360
- db.on('row', function(row, index, isObject) {
361
- // index === Number
362
- // isObject === is row object or array?
363
- });
364
-
365
- db.on('result', function(result) {
366
- // result === Array
367
- });
368
-
369
- db.on('attach', function() {
370
-
371
- });
372
-
373
- db.on('detach', function(isPoolConnection) {
374
- // isPoolConnection == Boolean
375
- });
356
+ Firebird.attach(options, function (err, db) {
357
+ if (err) throw err;
376
358
 
377
- db.on('reconnect', function() {
359
+ db.on('row', function (row, index, isObject) {
360
+ // index === Number
361
+ // isObject === is row object or array?
362
+ });
378
363
 
379
- });
364
+ db.on('result', function (result) {
365
+ // result === Array
366
+ });
380
367
 
381
- db.on('error', function(err) {
368
+ db.on('attach', function () {});
382
369
 
383
- });
370
+ db.on('detach', function (isPoolConnection) {
371
+ // isPoolConnection == Boolean
372
+ });
384
373
 
385
- db.on('transaction', function(isolation) {
386
- // isolation === Number
387
- });
374
+ db.on('reconnect', function () {});
388
375
 
389
- db.on('commit', function() {
376
+ db.on('error', function (err) {});
390
377
 
391
- });
378
+ db.on('transaction', function (isolation) {
379
+ // isolation === Number
380
+ });
392
381
 
393
- db.on('rollback', function() {
382
+ db.on('commit', function () {});
394
383
 
395
- });
384
+ db.on('rollback', function () {});
396
385
 
397
- db.detach();
386
+ db.detach();
398
387
  });
399
388
  ```
400
389
 
@@ -402,8 +391,9 @@ Firebird.attach(options, function(err, db) {
402
391
 
403
392
  ```js
404
393
  var sql1 = 'SELECT * FROM TBL_USER WHERE ID>' + Firebird.escape(1);
405
- var sql2 = 'SELECT * FROM TBL_USER WHERE NAME=' + Firebird.escape('Pe\'er');
406
- var sql3 = 'SELECT * FROM TBL_USER WHERE CREATED<=' + Firebird.escape(new Date());
394
+ var sql2 = 'SELECT * FROM TBL_USER WHERE NAME=' + Firebird.escape("Pe'er");
395
+ var sql3 =
396
+ 'SELECT * FROM TBL_USER WHERE CREATED<=' + Firebird.escape(new Date());
407
397
  var sql4 = 'SELECT * FROM TBL_USER WHERE NEWSLETTER=' + Firebird.escape(true);
408
398
 
409
399
  // or db.escape()
@@ -419,19 +409,20 @@ console.log(sql4);
419
409
  ```js
420
410
  var { GDSCode } = require('node-firebird/lib/gdscodes');
421
411
  /*...*/
422
- db.query('insert into my_table(id, name) values (?, ?)', [1, 'John Doe'],
423
- function (err) {
424
- if(err.gdscode == GDSCode.UNIQUE_KEY_VIOLATION){
425
- console.log('constraint name:'+ err.gdsparams[0]);
426
- console.log('table name:'+ err.gdsparams[0]);
427
- /*...*/
428
- }
429
- /*...*/
430
- });
431
-
412
+ db.query(
413
+ 'insert into my_table(id, name) values (?, ?)',
414
+ [1, 'John Doe'],
415
+ function (err) {
416
+ if (err.gdscode == GDSCode.UNIQUE_KEY_VIOLATION) {
417
+ console.log('constraint name:' + err.gdsparams[0]);
418
+ console.log('table name:' + err.gdsparams[0]);
419
+ /*...*/
420
+ }
421
+ /*...*/
422
+ }
423
+ );
432
424
  ```
433
425
 
434
-
435
426
  ### Service Manager functions
436
427
 
437
428
  - backup
@@ -523,59 +514,62 @@ const RESTORE_OPTS = {
523
514
  Firebird.attach(config, (err, srv) => {
524
515
  srv.restore(RESTORE_OPTS, (err, data) => {
525
516
  data.on('data', () => {});
526
- data.on('end', () =>
527
- srv.detach();
517
+ data.on('end', () =>{
518
+ srv.detach();})
528
519
  });
529
520
  });
530
- });
531
521
  ```
532
522
 
533
523
  ### getLog and getFbserverInfos Service examples with use of stream and object return
534
524
 
535
525
  ```js
536
- fb.attach(_connection, function(err, svc) {
537
- if (err)
538
- return;
539
- // all function that return a stream take two optional parameter
540
- // optread => byline or buffer byline use isc_info_svc_line and buffer use isc_info_svc_to_eof
541
- // buffersize => is the buffer for service manager it can't exceed 8ko (i'm not sure)
542
-
543
- svc.getLog({optread:'buffer', buffersize:2048}, function (err, data) {
544
- // data is a readablestream that contain the firebird.log file
545
- console.log(err);
546
- data.on('data', function (data) {
547
- console.log(data.toString());
548
- });
549
- data.on('end', function() {
550
- console.log('finish');
551
- });
552
- });
553
-
554
- // an other exemple to use function that return object
555
- svc.getFbserverInfos(
556
- {
557
- "dbinfo" : true,
558
- "fbconfig" : true,
559
- "svcversion" : true,
560
- "fbversion" : true,
561
- "fbimplementation" : true,
562
- "fbcapatibilities" : true,
563
- "pathsecuritydb" : true,
564
- "fbenv" : true,
565
- "fbenvlock" : true,
566
- "fbenvmsg" : true
567
- }, {}, function (err, data) {
568
- console.log(err);
569
- console.log(data);
570
- });
526
+ fb.attach(_connection, function (err, svc) {
527
+ if (err) return;
528
+ // all function that return a stream take two optional parameter
529
+ // optread => byline or buffer byline use isc_info_svc_line and buffer use isc_info_svc_to_eof
530
+ // buffersize => is the buffer for service manager it can't exceed 8ko (i'm not sure)
531
+
532
+ svc.getLog({ optread: 'buffer', buffersize: 2048 }, function (err, data) {
533
+ // data is a readablestream that contain the firebird.log file
534
+ console.log(err);
535
+ data.on('data', function (data) {
536
+ console.log(data.toString());
537
+ });
538
+ data.on('end', function () {
539
+ console.log('finish');
540
+ });
541
+ });
542
+
543
+ // an other exemple to use function that return object
544
+ svc.getFbserverInfos(
545
+ {
546
+ dbinfo: true,
547
+ fbconfig: true,
548
+ svcversion: true,
549
+ fbversion: true,
550
+ fbimplementation: true,
551
+ fbcapatibilities: true,
552
+ pathsecuritydb: true,
553
+ fbenv: true,
554
+ fbenvlock: true,
555
+ fbenvmsg: true,
556
+ },
557
+ {},
558
+ function (err, data) {
559
+ console.log(err);
560
+ console.log(data);
561
+ }
562
+ );
571
563
  });
572
-
573
564
  ```
574
565
 
575
566
  ### Charset for database connection is always UTF-8
576
567
 
577
- node-firebird doesn't let you choose the charset connection, it will always use UTF-8.
578
- Node is unicode, no matter if your database is using another charset to store string or blob, Firebird will transliterate automatically.
568
+ Node Firebird uses UTF-8 as the default charset. If you want a different one, such as Latin1, you will need to go into the library and modify the default_encoding in the index.js file
569
+
570
+ ```js
571
+ const default_encoding = 'latin1';
572
+ ```
579
573
 
580
574
  This is why you should use **Firebird 2.5** server at least.
581
575
 
@@ -604,8 +598,6 @@ UserManager = Legacy_UserManager
604
598
  Please read also Authorization with Firebird 2.5 client library from Firebird 4 migration guide
605
599
  <https://ib-aid.com/download/docs/fb4migrationguide.html#_authorization_with_firebird_2_5_client_library_fbclient_dll>
606
600
 
607
-
608
-
609
601
  ## Contributors
610
602
 
611
603
  - Henri Gourvest, <https://github.com/hgourvest>
@@ -614,7 +606,6 @@ Please read also Authorization with Firebird 2.5 client library from Firebird 4
614
606
 
615
607
  [license-image]: http://img.shields.io/badge/license-MOZILLA-blue.svg?style=flat
616
608
  [license-url]: LICENSE
617
-
618
609
  [npm-url]: https://npmjs.org/package/node-firebird
619
610
  [npm-version-image]: http://img.shields.io/npm/v/node-firebird.svg?style=flat
620
611
  [npm-downloads-image]: http://img.shields.io/npm/dm/node-firebird.svg?style=flat