| 
									
										
										
										
											2014-04-16 10:41:16 -04:00
										 |  |  | // "Base Client"
 | 
					
						
							|  |  |  | // ------
 | 
					
						
							| 
									
										
										
										
											2014-04-15 11:43:47 -04:00
										 |  |  | var Promise    = require('./promise'); | 
					
						
							| 
									
										
										
										
											2014-05-28 22:29:34 -04:00
										 |  |  | var _          = require('lodash'); | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-16 10:41:16 -04:00
										 |  |  | // The base client provides the general structure
 | 
					
						
							|  |  |  | // for a dialect specific client object. The client
 | 
					
						
							|  |  |  | // object attaches fresh constructors for each component
 | 
					
						
							|  |  |  | // of the library.
 | 
					
						
							| 
									
										
										
										
											2014-05-28 22:29:34 -04:00
										 |  |  | function Client(config) { | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  |   this.initFormatter(); | 
					
						
							|  |  |  |   this.initRaw(); | 
					
						
							|  |  |  |   this.initTransaction(); | 
					
						
							|  |  |  |   this.initQuery(); | 
					
						
							| 
									
										
										
										
											2014-05-28 22:29:34 -04:00
										 |  |  |   this.migrationConfig = _.clone(config && config.migrations); | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Set the "isDebugging" flag on the client to "true" to log
 | 
					
						
							|  |  |  | // all queries run by the client.
 | 
					
						
							|  |  |  | Client.prototype.isDebugging = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-15 11:43:47 -04:00
										 |  |  | // Acquire a connection from the pool.
 | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  | Client.prototype.acquireConnection = function() { | 
					
						
							|  |  |  |   var pool = this.pool; | 
					
						
							|  |  |  |   return new Promise(function(resolver, rejecter) { | 
					
						
							| 
									
										
										
										
											2014-04-16 01:23:50 -04:00
										 |  |  |     if (!pool) return rejecter(new Error('There is no pool defined on the current client')); | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  |     pool.acquire(function(err, connection) { | 
					
						
							|  |  |  |       if (err) return rejecter(err); | 
					
						
							|  |  |  |       resolver(connection); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Releases a connection from the connection pool,
 | 
					
						
							| 
									
										
										
										
											2014-04-15 11:43:47 -04:00
										 |  |  | // returning a promise resolved when the connection is released.
 | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  | Client.prototype.releaseConnection = function(connection) { | 
					
						
							|  |  |  |   var pool = this.pool; | 
					
						
							|  |  |  |   return new Promise(function(resolver, rejecter) { | 
					
						
							|  |  |  |     pool.release(connection, function(err) { | 
					
						
							|  |  |  |       if (err) return rejecter(err); | 
					
						
							| 
									
										
										
										
											2014-04-16 01:23:50 -04:00
										 |  |  |       resolver(connection); | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-21 23:08:59 -04:00
										 |  |  | // Return the database being used by this client.
 | 
					
						
							|  |  |  | Client.prototype.database = function() { | 
					
						
							|  |  |  |   return this.connectionSettings.database; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  | module.exports = Client; |