2121
2222const BaseDbObject = require ( './dbObject.js' ) ;
2323const EventEmitter = require ( 'events' ) ;
24- const QueryStream = require ( './querystream .js' ) ;
24+ const QueryStream = require ( './queryStream .js' ) ;
2525const nodbUtil = require ( './util.js' ) ;
26+ const util = require ( 'util' ) ;
2627
2728// fetchRowsToReturn is used to materialize the rows for an execute call using
2829// the resultSet returned from the C layer.
@@ -73,24 +74,21 @@ function execute(sql, a2, a3, a4) {
7374 let executeCb ;
7475 let custExecuteCb ;
7576
76- nodbUtil . assert ( arguments . length > 1 && arguments . length < 5 , 'NJS-009' ) ;
77+ nodbUtil . checkAsyncArgs ( arguments , 2 , 4 ) ;
7778 nodbUtil . assert ( typeof sql === 'string' , 'NJS-005' , 1 ) ;
7879
7980 switch ( arguments . length ) {
8081 case 2 :
81- nodbUtil . assert ( typeof a2 === 'function' , 'NJS-005' , 2 ) ;
8282 executeCb = a2 ;
8383 break ;
8484 case 3 :
8585 nodbUtil . assert ( nodbUtil . isObjectOrArray ( a2 ) , 'NJS-005' , 2 ) ;
86- nodbUtil . assert ( typeof a3 === 'function' , 'NJS-005' , 3 ) ;
8786 binds = a2 ;
8887 executeCb = a3 ;
8988 break ;
9089 case 4 :
9190 nodbUtil . assert ( nodbUtil . isObjectOrArray ( a2 ) , 'NJS-005' , 2 ) ;
9291 nodbUtil . assert ( nodbUtil . isObject ( a3 ) , 'NJS-005' , 3 ) ;
93- nodbUtil . assert ( typeof a4 === 'function' , 'NJS-005' , 4 ) ;
9492 binds = a2 ;
9593 executeOpts = a3 ;
9694 executeCb = a4 ;
@@ -177,7 +175,7 @@ function executeMany(sql, bindsOrNumIters, a3, a4) {
177175 let executeCb ;
178176 let okBinds ;
179177
180- nodbUtil . assert ( arguments . length > 2 && arguments . length < 5 , 'NJS-009' ) ;
178+ nodbUtil . checkAsyncArgs ( arguments , 3 , 4 ) ;
181179 nodbUtil . assert ( typeof sql === 'string' , 'NJS-005' , 1 ) ;
182180 if ( typeof bindsOrNumIters === 'number' ) {
183181 nodbUtil . assert ( Number . isInteger ( bindsOrNumIters ) , 'NJS-005' , 2 ) ;
@@ -189,12 +187,10 @@ function executeMany(sql, bindsOrNumIters, a3, a4) {
189187
190188 switch ( arguments . length ) {
191189 case 3 :
192- nodbUtil . assert ( typeof a3 === 'function' , 'NJS-005' , 3 ) ;
193190 executeCb = a3 ;
194191 break ;
195192 case 4 :
196193 nodbUtil . assert ( nodbUtil . isObject ( a3 ) , 'NJS-005' , 3 ) ;
197- nodbUtil . assert ( typeof a4 === 'function' , 'NJS-005' , 4 ) ;
198194 options = a3 ;
199195 executeCb = a4 ;
200196 break ;
@@ -211,9 +207,8 @@ function executeMany(sql, bindsOrNumIters, a3, a4) {
211207// first, but if not found, the database is queried and the result is cached
212208// using the fully qualified name
213209function getDbObjectClass ( name , cb ) {
214- nodbUtil . assert ( arguments . length === 2 , 'NJS-009' ) ;
210+ nodbUtil . checkAsyncArgs ( arguments , 2 , 2 ) ;
215211 nodbUtil . assert ( typeof name === 'string' , 'NJS-005' , 1 ) ;
216- nodbUtil . assert ( typeof cb === 'function' , 'NJS-005' , 2 ) ;
217212
218213 // check the cache; if the class is found there, nothing further to do
219214 let cls = this . _dbObjectClasses [ name ] ;
@@ -228,41 +223,36 @@ function getDbObjectClass(name, cb) {
228223}
229224
230225// This getStatementInfo function is just a place holder to allow for easier extension later.
231- function getStatementInfo ( sql , getStatementInfoCb ) {
226+ function getStatementInfo ( sql , getStatementInfoCb ) { //eslint-disable-line
232227 const self = this ;
233228
234- nodbUtil . assert ( arguments . length === 2 , 'NJS-009' ) ;
235- nodbUtil . assert ( typeof getStatementInfoCb === 'function' , 'NJS-005' , 1 ) ;
229+ nodbUtil . checkAsyncArgs ( arguments , 2 , 2 ) ;
236230
237231 self . _getStatementInfo . apply ( self , arguments ) ;
238232}
239233
240234// This commit function is just a place holder to allow for easier extension later.
241- function commit ( commitCb ) {
235+ function commit ( commitCb ) { //eslint-disable-line
242236 const self = this ;
243237
244- nodbUtil . assert ( arguments . length === 1 , 'NJS-009' ) ;
245- nodbUtil . assert ( typeof commitCb === 'function' , 'NJS-005' , 1 ) ;
246-
238+ nodbUtil . checkAsyncArgs ( arguments , 1 , 1 ) ;
247239 self . _commit . apply ( self , arguments ) ;
248240}
249241
250242// This createLob function is just a place holder to allow for easier extension later.
251- function createLob ( type , createLobCb ) {
243+ function createLob ( type , createLobCb ) { //eslint-disable-line
252244 const self = this ;
253245
254- nodbUtil . assert ( arguments . length === 2 , 'NJS-009' ) ;
255- nodbUtil . assert ( typeof createLobCb === 'function' , 'NJS-005' , 2 ) ;
246+ nodbUtil . checkAsyncArgs ( arguments , 2 , 2 ) ;
256247
257248 self . _createLob . apply ( self , arguments ) ;
258249}
259250
260251// This rollback function is just a place holder to allow for easier extension later.
261- function rollback ( rollbackCb ) {
252+ function rollback ( rollbackCb ) { //eslint-disable-line
262253 const self = this ;
263254
264- nodbUtil . assert ( arguments . length === 1 , 'NJS-009' ) ;
265- nodbUtil . assert ( typeof rollbackCb === 'function' , 'NJS-005' , 1 ) ;
255+ nodbUtil . checkAsyncArgs ( arguments , 1 , 1 ) ;
266256
267257 self . _rollback . apply ( self , arguments ) ;
268258}
@@ -274,23 +264,25 @@ function close(a1, a2) {
274264 let options = { } ;
275265 let closeCb ;
276266
277- nodbUtil . assert ( arguments . length >= 1 && arguments . length <= 2 , 'NJS-009' ) ;
267+ nodbUtil . checkAsyncArgs ( arguments , 1 , 2 ) ;
278268
279269 switch ( arguments . length ) {
280270 case 1 :
281- nodbUtil . assert ( typeof a1 === 'function' , 'NJS-005' , 1 ) ;
282271 closeCb = a1 ;
283272 break ;
284273 case 2 :
285274 nodbUtil . assert ( nodbUtil . isObject ( a1 ) , 'NJS-005' , 1 ) ;
286- nodbUtil . assert ( typeof a2 === 'function' , 'NJS-005' , 2 ) ;
287275 options = a1 ;
288276 closeCb = a2 ;
289277 break ;
290278 }
291279
292280 self . _close ( options , function ( err ) {
293281 if ( ! err ) {
282+ for ( const cls of Object . values ( this . _dbObjectClasses ) ) {
283+ cls . prototype . constructor = Object ;
284+ cls . prototype = null ;
285+ }
294286 self . emit ( '_after_close' ) ;
295287 }
296288
@@ -300,11 +292,10 @@ function close(a1, a2) {
300292
301293// This break function is just a place holder to allow for easier extension later.
302294// It's attached to the module as break is a reserved word.
303- module . break = function ( breakCb ) {
295+ module . break = function ( breakCb ) { //eslint-disable-line
304296 const self = this ;
305297
306- nodbUtil . assert ( arguments . length === 1 , 'NJS-009' ) ;
307- nodbUtil . assert ( typeof breakCb === 'function' , 'NJS-005' , 1 ) ;
298+ nodbUtil . checkAsyncArgs ( arguments , 1 , 1 ) ;
308299
309300 self . _break . apply ( self , arguments ) ;
310301} ;
@@ -313,7 +304,7 @@ module.break = function(breakCb) {
313304function changePassword ( user , password , newPassword , changePasswordCb ) {
314305 const self = this ;
315306
316- nodbUtil . assert ( arguments . length === 4 , 'NJS-009' ) ;
307+ nodbUtil . checkAsyncArgs ( arguments , 4 , 4 ) ;
317308 nodbUtil . assert ( typeof user === 'string' , 'NJS-005' , 1 ) ;
318309 nodbUtil . assert ( typeof password === 'string' , 'NJS-005' , 2 ) ;
319310 nodbUtil . assert ( typeof newPassword === 'string' , 'NJS-005' , 3 ) ;
@@ -326,16 +317,15 @@ function changePassword(user, password, newPassword, changePasswordCb) {
326317function getQueue ( name , a2 , a3 ) {
327318 let options = { } ;
328319 let queueCb ;
329- nodbUtil . assert ( arguments . length >= 2 && arguments . length <= 3 , 'NJS-009' ) ;
320+
321+ nodbUtil . checkAsyncArgs ( arguments , 2 , 3 ) ;
330322 nodbUtil . assert ( typeof name === 'string' , 'NJS-005' , 1 ) ;
331323 switch ( arguments . length ) {
332324 case 2 :
333- nodbUtil . assert ( typeof a2 === 'function' , 'NJS-005' , 2 ) ;
334325 queueCb = a2 ;
335326 break ;
336327 case 3 :
337328 nodbUtil . assert ( nodbUtil . isObject ( a2 ) , 'NJS-005' , 2 ) ;
338- nodbUtil . assert ( typeof a3 === 'function' , 'NJS-005' , 3 ) ;
339329 options = a2 ;
340330 queueCb = a3 ;
341331 break ;
@@ -344,12 +334,10 @@ function getQueue(name, a2, a3) {
344334}
345335
346336// This ping function is just a place holder to allow for easier extension later.
347- function ping ( pingCb ) {
337+ function ping ( pingCb ) { //eslint-disable-line
348338 const self = this ;
349339
350- nodbUtil . assert ( arguments . length === 1 , 'NJS-009' ) ;
351- nodbUtil . assert ( typeof pingCb === 'function' , 'NJS-005' , 1 ) ;
352-
340+ nodbUtil . checkAsyncArgs ( arguments , 1 , 1 ) ;
353341 self . _ping . apply ( self , arguments ) ;
354342}
355343
@@ -358,24 +346,47 @@ function ping(pingCb) {
358346function subscribe ( name , options , subscribeCb ) {
359347 const self = this ;
360348
361- nodbUtil . assert ( arguments . length == 3 , 'NJS-009' ) ;
349+ nodbUtil . checkAsyncArgs ( arguments , 3 , 3 ) ;
362350 nodbUtil . assert ( typeof name === 'string' , 'NJS-005' , 1 ) ;
363351 nodbUtil . assert ( nodbUtil . isObject ( options ) , 'NJS-005' , 2 ) ;
364- nodbUtil . assert ( typeof subscribeCb === 'function' , 'NJS-005' , 3 ) ;
365352 self . _subscribe . call ( self , name , options , subscribeCb ) ;
366353}
367354
368355// destroy a subscription which was earlier created using subscribe()
369356function unsubscribe ( name , cb ) {
370357 const self = this ;
371358
372- nodbUtil . assert ( arguments . length == 2 , 'NJS-009' ) ;
359+ nodbUtil . checkAsyncArgs ( arguments , 2 , 2 ) ;
373360 nodbUtil . assert ( typeof name === 'string' , 'NJS-005' , 1 ) ;
374- nodbUtil . assert ( typeof cb === 'function' , 'NJS-005' , 2 ) ;
375361
376362 self . _unsubscribe . call ( self , name , cb ) ;
377363}
378364
365+ // build a database object class
366+ function buildDbObjectClass ( schema , name , fqn ) {
367+ const DbObject = function ( initialValue ) {
368+ if ( this . isCollection ) {
369+ const proxy = new Proxy ( this , BaseDbObject . _collectionProxyHandler ) ;
370+ if ( initialValue !== undefined ) {
371+ for ( let i = 0 ; i < initialValue . length ; i ++ ) {
372+ this . append ( initialValue [ i ] ) ;
373+ }
374+ }
375+ return proxy ;
376+ } else if ( initialValue !== undefined ) {
377+ Object . assign ( this , initialValue ) ;
378+ }
379+ } ;
380+ DbObject . prototype = Object . create ( BaseDbObject . prototype ) ;
381+ DbObject . prototype . constructor = DbObject ;
382+ DbObject . prototype . schema = schema ;
383+ DbObject . prototype . name = name ;
384+ DbObject . prototype . fqn = fqn ;
385+ DbObject . toString = function ( ) {
386+ return 'DbObjectClass [' + fqn + ']' ;
387+ } ;
388+ return DbObject ;
389+ }
379390
380391// define class
381392class Connection extends EventEmitter {
@@ -409,26 +420,20 @@ class Connection extends EventEmitter {
409420 const fqn = `${ schema } .${ name } ` ;
410421 let cls = this . _dbObjectClasses [ fqn ] ;
411422 if ( ! cls ) {
412- class DbObject extends BaseDbObject {
413- get [ Symbol . toStringTag ] ( ) {
414- return fqn ;
415- }
416- }
417- this . _dbObjectClasses [ fqn ] = cls = DbObject ;
418- cls . prototype . schema = schema ;
419- cls . prototype . name = name ;
420- cls . prototype . fqn = fqn ;
421- cls . toString = function ( ) {
422- return 'DbObjectClass [' + fqn + ']' ;
423- } ;
423+ cls = buildDbObjectClass ( schema , name , fqn ) ;
424+ this . _dbObjectClasses [ fqn ] = cls ;
424425 }
425426 return cls ;
426427 }
427428
429+ _isDate ( val ) {
430+ return util . isDate ( val ) ;
431+ }
432+
428433 // To obtain a SodaDatabase object (high-level SODA object associated with
429434 // current connection)
430435 getSodaDatabase ( ) {
431- nodbUtil . assert ( arguments . length === 0 , 'NJS-009' ) ;
436+ nodbUtil . checkArgCount ( arguments , 0 , 0 ) ;
432437 return this . _getSodaDatabase ( ) ;
433438 }
434439
@@ -438,7 +443,7 @@ class Connection extends EventEmitter {
438443 const self = this ;
439444 let stream ;
440445
441- nodbUtil . assert ( arguments . length > 0 && arguments . length < 4 , 'NJS-009' ) ;
446+ nodbUtil . checkArgCount ( arguments , 1 , 3 ) ;
442447 nodbUtil . assert ( typeof sql === 'string' , 'NJS-005' , 1 ) ;
443448
444449 if ( binding ) {
0 commit comments