You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* eslint-disable @typescript-eslint/no-explicit-any */// @ts-ignoreimporttableConverterfrom'yadda/lib/converters/table-converter';import{Server,ModelInstance,AnyAttrs,ModelInstanceShared}from'ember-cli-mirage';import{dasherize,camelize}from'@ember/string';import{pluralize}from'ember-inflector';import{assert}from'@ember/debug';// @ts-ignoreimportHasManyfrom'ember-cli-mirage/orm/associations/has-many';// @ts-ignoreimportBelongsTofrom'ember-cli-mirage/orm/associations/belongs-to';import{Dict}from'@glimmer/interfaces';constREGEX_COMMA_AND_SEPARATOR=/\s*,\s*|\s+and\s+/g;constREGEX_REL_NAME=/(.+?)\((.+?)\)/;constREGEX_ID_AND_TYPE=/@([^()]+)(?:\((.+?)\))?/;functionfindRelationship(server: Server,type: string,relationshipName: string): HasMany|BelongsTo|undefined{try{constModel=server.schema.modelClassFor(type);returnModel.associationFor(relationshipName);}catch(e){}// eslint-disable-line no-empty}functionfindRelatedRecord(server: Server,idRaw: string,relatedTypeFromRelationship: string): ModelInstanceShared<AnyAttrs>{constresult=REGEX_ID_AND_TYPE.exec(idRaw);if(!result||!result[1]){thrownewError(`Invalid id: ${idRaw}`);}const[,id,typeFromId]=result;constrelatedType=typeFromId||relatedTypeFromRelationship;constrelatedTypePlural=pluralize(camelize(relatedType));constrelatedCollection=server.schema[relatedTypePlural];if(!relatedCollection){thrownewError(`Collection ${relatedTypePlural} does not exist in Mirage Schema`);}constrelatedRecord=relatedCollection.find(id);if(!relatedRecord){thrownewError(`Record of type ${relatedType} with id ${id} not found in Mirage Schema`);}returnrelatedRecord;}functionfindRelatedRecords(server: Server,type: string,relationshipName: string,idOrIdsRaw: string): [ModelInstance|ModelInstance[]|null,string]{idOrIdsRaw=idOrIdsRaw.trim();letresult;letrelationship;letrelatedType: string;if(REGEX_REL_NAME.test(relationshipName)){constresult=REGEX_REL_NAME.exec(relationshipName);if(!result){thrownewError(`Regex parse error for realtionship name '${relationshipName}'`);}relationshipName=result[1];relationship=findRelationship(server,type,relationshipName);assert(`No such relationship "${relationshipName}" on Mirage model ${type}`,relationship);relatedType=dasherize(result[2]);}else{relationship=findRelationship(server,type,relationshipName);assert(`No such relationship "${relationshipName}" on Mirage model ${type}`,relationship);relatedType=relationship.modelName;}// HasManyif(relationshipinstanceofHasMany){result=idOrIdsRaw.split(REGEX_COMMA_AND_SEPARATOR).filter((str)=>str.length).map((idRaw: string)=>findRelatedRecord(server,idRaw,relatedType));// BelongsTo non-empty}elseif(idOrIdsRaw.length){result=findRelatedRecord(server,idOrIdsRaw,relatedType);// BelongsTo empty}else{result=null;}return[result,relationshipName];}functionseedFromRows(server: Server,typeRaw: string,rows: Array<any>): void{consttype=dasherize(typeRaw);consttypePlural=pluralize(camelize(typeRaw));assert(`Collection ${typePlural} does not exist in Mirage`,!!server.db[typePlural]);rows.forEach((row)=>{lettraits: string[]=[];constproperties=Object.entries(row).reduce((result: Dict<any>,[key,value]: [string,any])=>{key=key.trim();value=value.trim();// Relationshipif(REGEX_REL_NAME.test(key)||findRelationship(server,type,key)){[value,key]=findRelatedRecords(server,type,key,value);// Traits}elseif(key==='trait'||key==='traits'){traits=value.split(REGEX_COMMA_AND_SEPARATOR).filter((str: string)=>str.length);// Empty cell}elseif(value.length===0){value=null;// Numbers, Strings, Booleans, Arrays and Objects}else{try{value=JSON.parse(value);}catch(e){thrownewError(`Invalid JSON passed as "${key}"`);}}result[key]=value;returnresult;},{});// @ts-ignoredeleteproperties.trait;// @ts-ignoredeleteproperties.traits;server.create(type,properties, ...traits);});}exportdefaultfunctionseedFromOpinionatedTable(server: Server,modelName: string,tableString: string): void{letrows: Array<any>;tableConverter(tableString.slice(1).trimRight(),(err: Error|null,result: Array<any>)=>{if(err){throwerr;}rows=result;});// @ts-ignoreseedFromRows(server,modelName,rows);}
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: