@@ -148,15 +148,90 @@ export default {
148148
149149> With the config/database file created you can use Database class to start handling operations in your database.
150150
151- #### Create/drop tables and databases
151+ #### Connecting to database
152152
153153``` ts
154- import { Knex } from ' knex'
155- import { Database , TableBuilder } from ' @secjs/database'
154+ import { Database } from ' @secjs/database'
156155
157156// Database class will always use the default value set in config/database
158157// to handle operations, in this case, postgres.
159- const database = await new Database ().connection (' postgres' ).connect ()
158+
159+ // If true will force the creation of the connection for that driver
160+ // even if that driver is already connected to database
161+ const force = false // default is false
162+
163+ // If true will save the connection inside DriverFactory and this connection
164+ // will be shared in all Database/Driver instances that use this connection
165+ const saveOnDriver = true // default is true
166+
167+ // You can create the connection for postgres calling connect method
168+ const database = await new Database ()
169+ .connection (' postgres' )
170+ .connect (force , saveOnDriver )
171+
172+ // Now every time that you need postgres connection, you don't need to call connect again
173+ const newDb = new Database ().connection (' postgres' )
174+ // Database/Driver always share the same database connection for each instance!
175+
176+ // We recommend using the static method createConnections in the application startup.
177+ // It will create the connections according to config/database file
178+ await Database .createConnections (' postgres' , ' mongo' )
179+
180+ // Now you don't need to call connect method when changing your connection
181+ database .connection (' mongo' ) // will use mongo driver
182+ ```
183+
184+ #### Disconnecting from database
185+
186+ ``` ts
187+ // There are three ways to disconect from database
188+
189+ // WARN - Becarefull with close if you are using a shared connection,
190+ // it will close the connection for all Database/Driver instances
191+
192+ // 1 - Calling close in the database instance
193+ await database .close ()
194+
195+ // 2 - Calling the static closeConnections method
196+ await Database .closeConnections (' postgres' , ' mongo' )
197+
198+ // 3 - Calling the static closeAllDrivers method
199+ await Database .closeAllDrivers () // Close all Drivers connections inside DriverFactory
200+ ```
201+
202+ #### Creating a specific connection with database subscribing configs in runtime
203+
204+ ``` ts
205+ // As you can see above, each Driver class share the same database connection.
206+ // But maybe you need to create a specific connection with runtime configurations,
207+ // without implicating in other Driver connections.
208+
209+ const force = true
210+ const saveOnDriver = false
211+ const runtimeConfigs = { database: ' testing' }
212+
213+ const runtimeDb = new Database ()
214+ .connection (' postgres' , runtimeConfigs )
215+ // You need to set force as true and saveOnDriver as false,
216+ // this way you will force creating a new connection for postgres but
217+ // it will not implicate in other Database/Driver instances connections.
218+ // We can use connect method this way to create a specific connection
219+ // to work on.
220+ .connect (force , saveOnDriver )
221+
222+ // This connection won't be available in static method Database.closeDriver.
223+ // If calling this method it will close the main PostgresDriver connection
224+ await Database .closeDriver (' postgres' )
225+
226+ // So always remember closing this connection!
227+ await runtimeDb .close ()
228+ ```
229+
230+ #### Create/drop tables and databases
231+
232+ ``` ts
233+ import { Knex } from ' knex'
234+ import { Database , TableBuilder } from ' @secjs/database'
160235
161236// All SQL Drivers from Database are using Knex as query builder and for Mongo NoSQL, mongoose.
162237await database .createTable (' products' , (tableBuilder : Knex .TableBuilder ) => {
@@ -177,7 +252,7 @@ await database.createTable('product_details', (tableBuilder: Knex.TableBuilder)
177252// but it does not have all the methods from Knex table builder.
178253
179254// Changing the connection to mongo database
180- await database .connection (' mongo' ). connect ( )
255+ database .connection (' mongo' )
181256
182257// With mongo connection we do not have to specify the id because
183258// mongoose auto create the _id property
@@ -200,10 +275,15 @@ await database.createDatabase('testing-database')
200275// Then you can create a new database instance to connect to this new database
201276const runtimeConfigurations = { database: ' testing-database' }
202277
203- const testingDatabase = await new Database (runtimeConfigurations ).connection (' mongo' ).connect ()
278+ const testingDatabase = await new Database (runtimeConfigurations )
279+ .connection (' mongo' )
280+ // Force connection and don't save the connection
281+ .connect (true , false )
204282
205- // Or a more simple way using the same instance, is just calling the connection method again but with runtimeConfigs
206- await database .connection (' mongo' , runtimeConfigurations ).connect ()
283+ // Do operations using testingDatabase....
284+
285+ // Close the connection with testing database
286+ await testingDatabase .close ()
207287
208288// You can drop databases too
209289await database .dropDatabase (' testing-database' )
@@ -533,7 +613,7 @@ database.buildTable('products')
533613
534614// Clone the database query chain, this will create a new instance of the Database class
535615// but with the exactly same query chain.
536- const clonedDatabase = await database .clone ()
616+ const clonedDatabase = database .clone ()
537617
538618console .log (database === clonedDatabase ) // false
539619
@@ -554,7 +634,7 @@ const arrayOfIds = await client.insert({ name: 'AirPods 2' }, 'id')
554634
555635{
556636 // For mongoose you can set the Schema as type
557- await database .connection (' mongo' ). connect ( )
637+ database .connection (' mongo' )
558638
559639 const { client, session } = await database .cloneQuery <UserSchema >()
560640
@@ -563,23 +643,6 @@ const arrayOfIds = await client.insert({ name: 'AirPods 2' }, 'id')
563643}
564644```
565645
566- ### Subscribing configs of connections in runtime
567-
568- ``` ts
569- // Using connection method approach
570- await database
571- .connection (' postgres' , { database: ' test-db' })
572- .connect ()
573-
574- // Using constructor method approach
575- const newDatabase = new Database ({ database: ' test-db' })
576-
577- // You can reset configs using an empty object in the connection method
578- await database
579- .connection (' postgres' , {}) // Clear the runtime configuration
580- .connect ()
581- ```
582-
583646### Extending connections and drivers
584647
585648> Nowadays, @secjs/database has only MongoDriver, MySqlDriver, PostgresDriver, SqliteDriver and SqlServerDriver support, but you can extend the drivers for Database class if you implement DriverContract interface
@@ -634,7 +697,8 @@ console.log(Database.drivers) // ['mysql', 'mongo', 'sqlite', 'mssql', 'postgres
634697> Now, if you have implemented your connection in config/database, you can use him inside Database
635698
636699``` ts
637- // Will use CustomDriver to handle the database operations
700+ // Will use CustomDriver to handle the database operations and
701+ // save the shared connection in DriverFactory
638702await database .connection (' myconnection' ).connect ()
639703```
640704
0 commit comments