forked from cerc-io/snowballtools-base
		
	Update environment variable entity schema to update them individually (#53)
* Change environment field to enum instead of array * Update gql client for get environment variables query --------- Co-authored-by: neeraj <neeraj.rtly@gmail.com>
This commit is contained in:
		
							parent
							
								
									8ead083ab3
								
							
						
					
					
						commit
						2f8d21baf5
					
				| @ -1,7 +1,6 @@ | ||||
| import { DataSource, DeepPartial } from 'typeorm'; | ||||
| import path from 'path'; | ||||
| import debug from 'debug'; | ||||
| import assert from 'assert'; | ||||
| 
 | ||||
| import { DatabaseConfig } from './config'; | ||||
| import { User } from './entity/User'; | ||||
| @ -156,9 +155,6 @@ export class Database { | ||||
|     const environmentVariableRepository = this.dataSource.getRepository(EnvironmentVariable); | ||||
| 
 | ||||
|     const environmentVariables = await environmentVariableRepository.find({ | ||||
|       relations: { | ||||
|         project: true | ||||
|       }, | ||||
|       where: { | ||||
|         project: { | ||||
|           id: projectId | ||||
| @ -181,27 +177,27 @@ export class Database { | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   async addEnvironmentVariablesByProjectId (projectId: string, environmentVariables: any[]): Promise<boolean> { | ||||
|   async addEnvironmentVariablesByProjectId (projectId: string, environmentVariables: { | ||||
|     environments: string[]; | ||||
|     key: string; | ||||
|     value: string; | ||||
|   }[]): Promise<boolean> { | ||||
|     const environmentVariableRepository = this.dataSource.getRepository(EnvironmentVariable); | ||||
|     const projectRepository = this.dataSource.getRepository(Project); | ||||
| 
 | ||||
|     const project = await projectRepository.findOneBy({ | ||||
|       id: projectId | ||||
|     }); | ||||
|     assert(project); | ||||
|     const formattedEnvironmentVariables = environmentVariables.map((environmentVariable) => { | ||||
|       return environmentVariable.environments.map((environment) => { | ||||
|         return ({ | ||||
|           key: environmentVariable.key, | ||||
|           value: environmentVariable.value, | ||||
|           environment: environment as Environment, | ||||
|           project: Object.assign(new Project(), { | ||||
|             id: projectId | ||||
|           }) | ||||
|         }); | ||||
|       }); | ||||
|     }).flat(); | ||||
| 
 | ||||
|     const environmentVariablesPromises = environmentVariables.map(async environmentVariable => { | ||||
|       const envVar = new EnvironmentVariable(); | ||||
| 
 | ||||
|       envVar.key = environmentVariable.key; | ||||
|       envVar.value = environmentVariable.value; | ||||
|       envVar.environments = environmentVariable.environments; | ||||
|       envVar.project = project; | ||||
| 
 | ||||
|       return environmentVariableRepository.save(envVar); | ||||
|     }); | ||||
| 
 | ||||
|     const savedEnvironmentVariables = await Promise.all(environmentVariablesPromises); | ||||
|     const savedEnvironmentVariables = await environmentVariableRepository.save(formattedEnvironmentVariables); | ||||
|     return savedEnvironmentVariables.length > 0; | ||||
|   } | ||||
| 
 | ||||
|  | ||||
| @ -26,9 +26,9 @@ export class EnvironmentVariable { | ||||
|     project!: Project; | ||||
| 
 | ||||
|   @Column({ | ||||
|     type: 'simple-array' | ||||
|     enum: Environment | ||||
|   }) | ||||
|     environments!: Environment[]; | ||||
|     environment!: Environment; | ||||
| 
 | ||||
|   @Column('varchar') | ||||
|     key!: string; | ||||
|  | ||||
| @ -72,12 +72,7 @@ export const createResolvers = async (db: Database): Promise<any> => { | ||||
| 
 | ||||
|       environmentVariables: async (_: any, { projectId }: { projectId: string }) => { | ||||
|         const dbEnvironmentVariables = await db.getEnvironmentVariablesByProjectId(projectId); | ||||
| 
 | ||||
|         const environmentVariables = dbEnvironmentVariables.map(dbEnvironmentVariable => { | ||||
|           return environmentVariableToGqlType(dbEnvironmentVariable); | ||||
|         }); | ||||
| 
 | ||||
|         return environmentVariables; | ||||
|         return dbEnvironmentVariables; | ||||
|       }, | ||||
| 
 | ||||
|       projectMembers: async (_: any, { projectId }: { projectId: string }) => { | ||||
|  | ||||
| @ -106,7 +106,7 @@ type Domain { | ||||
| 
 | ||||
| type EnvironmentVariable { | ||||
|   id: String! | ||||
|   environments: [Environment!]! | ||||
|   environment: Environment! | ||||
|   key: String! | ||||
|   value: String! | ||||
|   createdAt: String! | ||||
|  | ||||
| @ -74,7 +74,7 @@ export const projectMemberToGqlType = (dbProjectMember: ProjectMember): any => { | ||||
| export const environmentVariableToGqlType = (dbEnvironmentVariable: EnvironmentVariable): any => { | ||||
|   return { | ||||
|     id: dbEnvironmentVariable.id, | ||||
|     environments: dbEnvironmentVariable.environments, | ||||
|     environments: dbEnvironmentVariable.environment, | ||||
|     key: dbEnvironmentVariable.key, | ||||
|     value: dbEnvironmentVariable.value, | ||||
|     createdAt: dbEnvironmentVariable.createdAt, | ||||
|  | ||||
| @ -3,60 +3,90 @@ | ||||
|     "projectIndex": 0, | ||||
|     "key": "ABC", | ||||
|     "value": "ABC", | ||||
|     "environments": ["Production", "Preview"] | ||||
|     "environment": "Production" | ||||
|   }, | ||||
|   { | ||||
|     "projectIndex": 0, | ||||
|     "key": "ABC", | ||||
|     "value": "ABC", | ||||
|     "environment": "Preview" | ||||
|   }, | ||||
|   { | ||||
|     "projectIndex": 0, | ||||
|     "key": "XYZ", | ||||
|     "value": "abc3", | ||||
|     "environments": ["Preview"] | ||||
|     "environment": "Preview" | ||||
|   }, | ||||
|   { | ||||
|     "projectIndex": 1, | ||||
|     "key": "ABC", | ||||
|     "value": "ABC", | ||||
|     "environments": ["Production", "Preview"] | ||||
|     "environment": "Production" | ||||
|   }, | ||||
|   { | ||||
|     "projectIndex": 1, | ||||
|     "key": "ABC", | ||||
|     "value": "ABC", | ||||
|     "environment": "Preview" | ||||
|   }, | ||||
|   { | ||||
|     "projectIndex": 1, | ||||
|     "key": "XYZ", | ||||
|     "value": "abc3", | ||||
|     "environments": ["Preview"] | ||||
|     "environment": "Preview" | ||||
|   }, | ||||
|   { | ||||
|     "projectIndex": 2, | ||||
|     "key": "ABC", | ||||
|     "value": "ABC", | ||||
|     "environments": ["Production", "Preview"] | ||||
|     "environment": "Production" | ||||
|   }, | ||||
|   { | ||||
|     "projectIndex": 2, | ||||
|     "key": "ABC", | ||||
|     "value": "ABC", | ||||
|     "environment": "Preview" | ||||
|   }, | ||||
|   { | ||||
|     "projectIndex": 2, | ||||
|     "key": "XYZ", | ||||
|     "value": "abc3", | ||||
|     "environments": ["Preview"] | ||||
|     "environment": "Preview" | ||||
|   }, | ||||
|   { | ||||
|     "projectIndex": 3, | ||||
|     "key": "ABC", | ||||
|     "value": "ABC", | ||||
|     "environments": ["Production", "Preview"] | ||||
|     "environment": "Production" | ||||
|   }, | ||||
|   { | ||||
|     "projectIndex": 3, | ||||
|     "key": "ABC", | ||||
|     "value": "ABC", | ||||
|     "environment": "Preview" | ||||
|   }, | ||||
|   { | ||||
|     "projectIndex": 3, | ||||
|     "key": "XYZ", | ||||
|     "value": "abc3", | ||||
|     "environments": ["Preview"] | ||||
|     "environment": "Preview" | ||||
|   }, | ||||
|   { | ||||
|     "projectIndex": 4, | ||||
|     "key": "ABC", | ||||
|     "value": "ABC", | ||||
|     "environments": ["Production", "Preview"] | ||||
|     "environment": "Production" | ||||
|   }, | ||||
|   { | ||||
|     "projectIndex": 4, | ||||
|     "key": "ABC", | ||||
|     "value": "ABC", | ||||
|     "environment": "Preview" | ||||
|   }, | ||||
|   { | ||||
|     "projectIndex": 4, | ||||
|     "key": "XYZ", | ||||
|     "value": "abc3", | ||||
|     "environments": ["Preview"] | ||||
|     "environment": "Preview" | ||||
|   } | ||||
| ] | ||||
|  | ||||
| @ -11,7 +11,7 @@ interface OverviewProps { | ||||
|   project: Project; | ||||
| } | ||||
| 
 | ||||
| // TODO: Check any live domain is set for production branch
 | ||||
| // TODO: Check if any live domain is set for production branch
 | ||||
| const IS_DOMAIN_SETUP = true; | ||||
| 
 | ||||
| const OverviewTabPanel = ({ project }: OverviewProps) => { | ||||
|  | ||||
| @ -72,8 +72,8 @@ export const EnvironmentVariablesTabPanel = () => { | ||||
| 
 | ||||
|   const getEnvironmentVariable = useCallback( | ||||
|     (environment: Environment) => { | ||||
|       return environmentVariables.filter((item) => | ||||
|         item.environments.includes(environment), | ||||
|       return environmentVariables.filter( | ||||
|         (item) => item.environment === environment, | ||||
|       ); | ||||
|     }, | ||||
|     [environmentVariables, id], | ||||
|  | ||||
| @ -43,7 +43,7 @@ const MembersTabPanel = ({ project }: { project: Project }) => { | ||||
| 
 | ||||
|   useEffect(() => { | ||||
|     fetchProjectMembers(); | ||||
|   }, [project.id]); | ||||
|   }, [project.id, fetchProjectMembers]); | ||||
| 
 | ||||
|   return ( | ||||
|     <div className="p-2 mb-20"> | ||||
|  | ||||
| @ -122,14 +122,6 @@ query { | ||||
|           email | ||||
|         } | ||||
|       } | ||||
|       environmentVariables { | ||||
|         id | ||||
|         environments | ||||
|         key | ||||
|         value | ||||
|         createdAt | ||||
|         updatedAt | ||||
|       } | ||||
|       createdAt | ||||
|       updatedAt | ||||
|     } | ||||
| @ -171,7 +163,7 @@ export const getEnvironmentVariables = gql` | ||||
| query ($projectId: String!)  { | ||||
|   environmentVariables(projectId: $projectId) { | ||||
|     createdAt | ||||
|     environments | ||||
|     environment | ||||
|     id | ||||
|     key | ||||
|     updatedAt | ||||
|  | ||||
| @ -30,7 +30,7 @@ export enum DomainStatus { | ||||
| 
 | ||||
| export type EnvironmentVariable = { | ||||
|   id: string | ||||
|   environments: Environment[] | ||||
|   environment: Environment | ||||
|   key: string | ||||
|   value: string | ||||
|   createdAt: string | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user