| 
									
										
										
										
											2019-09-20 12:44:24 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-18 10:16:03 +02:00
										 |  |  | const knex = require('knex'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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-06-28 12:34:29 +02:00
										 |  |  |     // TODO: validate meta
 | 
					
						
							| 
									
										
										
										
											2021-06-02 15:53:22 +02:00
										 |  |  |     // this.metadata.validate();
 | 
					
						
							| 
									
										
										
										
											2021-05-18 10:16:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-29 16:27:35 +02:00
										 |  |  |     this.config = config; | 
					
						
							| 
									
										
										
										
											2021-06-28 12:34:29 +02:00
										 |  |  |     this.dialect = getDialect(this); | 
					
						
							| 
									
										
										
										
											2021-05-18 10:16:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 15:53:22 +02:00
										 |  |  |     this.schema = createSchemaProvider(this); | 
					
						
							| 
									
										
										
										
											2021-06-17 16:17:15 +02:00
										 |  |  |     // TODO: migrations -> allow running them through cli before startup
 | 
					
						
							| 
									
										
										
										
											2021-05-10 15:36:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-06-28 12:34:29 +02:00
										 |  |  |   async initialize() { | 
					
						
							|  |  |  |     await this.dialect.initialize(); | 
					
						
							| 
									
										
										
										
											2021-06-29 16:27:35 +02:00
										 |  |  |     this.connection = knex(this.config.connection); | 
					
						
							| 
									
										
										
										
											2021-06-28 12:34:29 +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); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async destroy() { | 
					
						
							|  |  |  |     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; | 
					
						
							| 
									
										
										
										
											2021-06-28 12:34:29 +02:00
										 |  |  | Database.init = async config => { | 
					
						
							|  |  |  |   const db = new Database(config); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await db.initialize(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return db; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2019-09-20 12:44:24 +02:00
										 |  |  | }; |