Skip to content

Commit

Permalink
Merge pull request #955 from exogee-technology/fix/introspection-for-…
Browse files Browse the repository at this point in the history
…one-to-one-relationships

Fix / Introspection for one to one relationships
  • Loading branch information
thekevinbrown authored Aug 1, 2024
2 parents 2cc4c39 + ef81620 commit 35c4779
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/packages/cli/src/import/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export const importDataSource = async (
}
console.log(`${fileCount} files have been successfully created in the project.`);
} catch (err: unknown) {
console.error(err);
if (isIntrospectionError(err)) {
console.warn(`\n\n${err.title}\n${err.message}\n\n`);
} else {
Expand Down
13 changes: 13 additions & 0 deletions src/packages/end-to-end/databases/postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ CREATE TABLE "Track"
CONSTRAINT "PK_Track" PRIMARY KEY ("TrackId")
);

-- These are here to test a specific scenario that used to crash the introspection process.
CREATE TABLE "ToOne" (
id text PRIMARY KEY,
name text NOT NULL
);

CREATE UNIQUE INDEX to_one_pkey ON "ToOne"(id text_ops);
CREATE UNIQUE INDEX to_one_name_key ON "ToOne"(name text_ops);

CREATE TABLE "ToOneKeyOwner" (
id text PRIMARY KEY,
related_entity_name text NOT NULL REFERENCES "ToOne"(name) ON DELETE CASCADE ON UPDATE CASCADE
);


/*******************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export class DataEntityFile extends BaseFile {
}

protected getPropertyType(prop: EntityProperty): string {
if ([ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind)) {
return prop.type.charAt(0).toUpperCase() + prop.type.slice(1);
}

const columnType = prop.columnTypes?.[0]?.toLowerCase();

if (['jsonb', 'json', 'any'].includes(columnType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ export class SchemaEntityFile extends BaseFile {
}

protected getTypescriptPropertyType(prop: EntityProperty): string {
if ([ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind)) {
return prop.type.charAt(0).toUpperCase() + prop.type.slice(1);
}

if (['jsonb', 'json', 'any'].includes(prop.columnTypes?.[0])) {
return `Record<string, unknown>`;
}
Expand Down Expand Up @@ -185,6 +189,10 @@ export class SchemaEntityFile extends BaseFile {
return `[${prop.type.charAt(0).toUpperCase() + prop.type.slice(1).replace('[]', '')}]`;
}

if ([ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind)) {
return prop.type.charAt(0).toUpperCase() + prop.type.slice(1);
}

if ([ReferenceKind.MANY_TO_MANY, ReferenceKind.ONE_TO_MANY].includes(prop.kind)) {
return `[${prop.type.charAt(0).toUpperCase() + prop.type.slice(1).replace('[]', '')}]`;
}
Expand All @@ -193,7 +201,14 @@ export class SchemaEntityFile extends BaseFile {
return `[${prop.type.charAt(0).toUpperCase() + prop.type.slice(1)}]`;
}

return prop.runtimeType.charAt(0).toUpperCase() + prop.runtimeType.slice(1);
const lastChanceType = prop.runtimeType ?? prop.type;

if (!lastChanceType) {
console.error(`Property is malformed, it has no type or runtimeType:`, prop);
throw new Error(`Property ${prop.name} on ${prop.entity} entity has no type or runtimeType.`);
}

return lastChanceType.charAt(0).toUpperCase() + lastChanceType.slice(1);
}

private getPropertyDecorator(prop: EntityProperty): string {
Expand Down
3 changes: 3 additions & 0 deletions src/packages/mikroorm/src/introspection/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ export const generate = async (databaseType: DatabaseType, options: ConnectionOp
} catch (err) {
if (err instanceof IntrospectionError) throw err;

console.error('Got error during introspection:');
console.error(err);

throw new IntrospectionError(
`Warning: Unable to connect to database.`,
hasErrorMessage(err) ? err.message : 'Please check the connection settings and try again'
Expand Down

0 comments on commit 35c4779

Please sign in to comment.