| 
									
										
										
										
											2019-09-20 12:44:24 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-17 16:17:15 +02:00
										 |  |  | const { getDialect } = require('./dialects'); | 
					
						
							| 
									
										
										
										
											2021-05-18 10:16:03 +02:00
										 |  |  | const createSchemaProvider = require('./schema'); | 
					
						
							|  |  |  | const createMetadata = require('./metadata'); | 
					
						
							| 
									
										
										
										
											2021-06-17 16:17:15 +02:00
										 |  |  | const { createEntityManager } = require('./entity-manager'); | 
					
						
							| 
									
										
										
										
											2021-09-20 19:15:50 +02:00
										 |  |  | const { createMigrationsProvider } = require('./migrations'); | 
					
						
							|  |  |  | const { createLifecyclesProvider } = require('./lifecycles'); | 
					
						
							| 
									
										
										
										
											2022-03-23 10:37:12 +01:00
										 |  |  | const createConnection = require('./connection'); | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  | const errors = require('./errors'); | 
					
						
							| 
									
										
										
										
											2021-06-17 16:17:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // TODO: move back into strapi
 | 
					
						
							|  |  |  | const { transformContentTypes } = require('./utils/content-types'); | 
					
						
							| 
									
										
										
										
											2021-05-10 15:36:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Database { | 
					
						
							|  |  |  |   constructor(config) { | 
					
						
							| 
									
										
										
										
											2021-06-02 15:53:22 +02:00
										 |  |  |     this.metadata = createMetadata(config.models); | 
					
						
							| 
									
										
										
										
											2021-05-18 10:16:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 19:42:03 +01:00
										 |  |  |     this.config = { | 
					
						
							|  |  |  |       connection: {}, | 
					
						
							|  |  |  |       settings: { | 
					
						
							| 
									
										
										
										
											2021-11-15 09:41:00 +01:00
										 |  |  |         forceMigration: true, | 
					
						
							| 
									
										
										
										
											2021-11-10 19:42:03 +01:00
										 |  |  |       }, | 
					
						
							|  |  |  |       ...config, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-09-13 12:03:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 12:34:29 +02:00
										 |  |  |     this.dialect = getDialect(this); | 
					
						
							| 
									
										
										
										
											2021-09-13 12:03:12 +02:00
										 |  |  |     this.dialect.configure(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 19:42:03 +01:00
										 |  |  |     this.connection = createConnection(this.config.connection); | 
					
						
							| 
									
										
										
										
											2021-09-13 12:03:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     this.dialect.initialize(); | 
					
						
							| 
									
										
										
										
											2021-05-18 10:16:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-04 17:47:38 +02:00
										 |  |  |     this.schema = createSchemaProvider(this); | 
					
						
							| 
									
										
										
										
											2021-05-10 15:36:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-20 19:15:50 +02:00
										 |  |  |     this.migrations = createMigrationsProvider(this); | 
					
						
							|  |  |  |     this.lifecycles = createLifecyclesProvider(this); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-17 16:17:15 +02:00
										 |  |  |     this.entityManager = createEntityManager(this); | 
					
						
							| 
									
										
										
										
											2021-05-10 15:36:09 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-05-17 16:34:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-18 10:16:03 +02:00
										 |  |  |   query(uid) { | 
					
						
							| 
									
										
										
										
											2021-06-22 17:13:11 +02:00
										 |  |  |     if (!this.metadata.has(uid)) { | 
					
						
							|  |  |  |       throw new Error(`Model ${uid} not found`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-17 16:17:15 +02:00
										 |  |  |     return this.entityManager.getRepository(uid); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 19:42:03 +01:00
										 |  |  |   getConnection(tableName) { | 
					
						
							|  |  |  |     const schema = this.connection.getSchemaName(); | 
					
						
							|  |  |  |     const connection = tableName ? this.connection(tableName) : this.connection; | 
					
						
							|  |  |  |     return schema ? connection.withSchema(schema) : connection; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getSchemaConnection(trx = this.connection) { | 
					
						
							|  |  |  |     const schema = this.connection.getSchemaName(); | 
					
						
							|  |  |  |     return schema ? trx.schema.withSchema(schema) : trx.schema; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-18 19:00:43 +02:00
										 |  |  |   transaction() { | 
					
						
							|  |  |  |     return this.connection.transaction(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 12:03:12 +02:00
										 |  |  |   queryBuilder(uid) { | 
					
						
							|  |  |  |     return this.entityManager.createQueryBuilder(uid); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-17 16:17:15 +02:00
										 |  |  |   async destroy() { | 
					
						
							| 
									
										
										
										
											2021-08-04 17:47:38 +02:00
										 |  |  |     await this.lifecycles.clear(); | 
					
						
							| 
									
										
										
										
											2021-06-17 16:17:15 +02:00
										 |  |  |     await this.connection.destroy(); | 
					
						
							| 
									
										
										
										
											2021-05-17 16:34:19 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-05-10 15:36:09 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-09-20 12:44:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 14:32:50 +02:00
										 |  |  | // TODO: move into strapi
 | 
					
						
							| 
									
										
										
										
											2021-06-17 16:17:15 +02:00
										 |  |  | Database.transformContentTypes = transformContentTypes; | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | Database.init = async (config) => new Database(config); | 
					
						
							| 
									
										
										
										
											2021-06-17 16:17:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-20 12:44:24 +02:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2021-05-10 15:36:09 +02:00
										 |  |  |   Database, | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |   errors, | 
					
						
							| 
									
										
										
										
											2019-09-20 12:44:24 +02:00
										 |  |  | }; |