@ts-safeql/generate
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -7,3 +7,3 @@ "use strict"; | ||
const postgres_1 = require("postgres"); | ||
const getLeftJoinTables_1 = require("./utils/getLeftJoinTables"); | ||
const get_relations_with_joins_1 = require("./utils/get-relations-with-joins"); | ||
function createGenerator() { | ||
@@ -21,3 +21,3 @@ const cacheMap = new Map(); | ||
const { sql, query, cacheMetadata = true } = params; | ||
const { pgCols, pgColsByTableOidCache, pgTypes } = cacheMetadata | ||
const { pgColsByTableOidCache, pgTypes } = cacheMetadata | ||
? await (0, shared_1.getOrSetFromMap)({ | ||
@@ -45,3 +45,3 @@ map: cacheMap, | ||
} | ||
const leftTables = (0, getLeftJoinTables_1.getLeftJoinTablesFromParsed)(params.pgParsed).map((tableName) => pgCols.find((col) => col.tableName === tableName).tableOid); | ||
const relationsWithJoins = (0, get_relations_with_joins_1.flattenRelationsWithJoinsMap)((0, get_relations_with_joins_1.getRelationsWithJoins)(params.pgParsed)); | ||
const columns = result.columns.map((col) => { | ||
@@ -58,3 +58,3 @@ var _a; | ||
pgTypes, | ||
leftTables, | ||
relationsWithJoins, | ||
typesMap, | ||
@@ -90,3 +90,3 @@ fieldTransform: params.fieldTransform, | ||
pgTypes: params.pgTypes, | ||
leftTables: params.leftTables, | ||
relationsWithJoins: params.relationsWithJoins, | ||
typesMap: params.typesMap, | ||
@@ -102,2 +102,36 @@ fieldTransform: params.fieldTransform, | ||
} | ||
function isNullableDueToRelation(params) { | ||
const { col, relationsWithJoins } = params; | ||
const findByJoin = relationsWithJoins.find((x) => x.joinRelName === col.tableName); | ||
if (findByJoin !== undefined) { | ||
switch (findByJoin.joinType) { | ||
case "JOIN_FULL": | ||
case "JOIN_LEFT": | ||
return true; | ||
case "JOIN_ANTI": | ||
case "JOIN_INNER": | ||
case "JOIN_RIGHT": | ||
case "JOIN_SEMI": | ||
return false; | ||
default: | ||
(0, shared_1.assertNever)(findByJoin.joinType); | ||
} | ||
} | ||
const findByRel = relationsWithJoins.filter((x) => x.relName === col.tableName); | ||
for (const rel of findByRel) { | ||
switch (rel.joinType) { | ||
case "JOIN_RIGHT": | ||
case "JOIN_FULL": | ||
return true; | ||
case "JOIN_LEFT": | ||
case "JOIN_ANTI": | ||
case "JOIN_INNER": | ||
case "JOIN_SEMI": | ||
continue; | ||
default: | ||
(0, shared_1.assertNever)(rel.joinType); | ||
} | ||
} | ||
return false; | ||
} | ||
function mapColumnAnalysisResultToPropertySignature(params) { | ||
@@ -108,8 +142,11 @@ var _a; | ||
const value = params.col.introspected.colNotNull ? tsType : `${tsType} | null`; | ||
const isFromLeftJoin = params.leftTables.includes(params.col.introspected.tableOid); | ||
const key = (_a = params.col.described.name) !== null && _a !== void 0 ? _a : params.col.introspected.colName; | ||
const isNullable = isNullableDueToRelation({ | ||
col: params.col.introspected, | ||
relationsWithJoins: params.relationsWithJoins, | ||
}); | ||
return buildInterfacePropertyValue({ | ||
key: (0, shared_1.toCase)(key, params.fieldTransform), | ||
value: value, | ||
isNullable: isFromLeftJoin, | ||
isNullable: isNullable, | ||
}); | ||
@@ -116,0 +153,0 @@ } |
@@ -127,2 +127,26 @@ "use strict"; | ||
}); | ||
(0, mocha_1.test)("select with right join should return all cols from the other table as nullable", async () => { | ||
await testQuery({ | ||
query: ` | ||
SELECT | ||
caregiver.id as caregiver_id, | ||
caregiver_agency.id as assoc_id | ||
FROM caregiver | ||
RIGHT JOIN caregiver_agency ON caregiver.id = caregiver_agency.caregiver_id | ||
`, | ||
expected: `{ caregiver_id: number | null; assoc_id: number; }`, | ||
}); | ||
}); | ||
(0, mocha_1.test)("select with full join should return all cols as nullable", async () => { | ||
await testQuery({ | ||
query: ` | ||
SELECT | ||
caregiver.id as caregiver_id, | ||
caregiver_agency.id as assoc_id | ||
FROM caregiver | ||
FULL JOIN caregiver_agency ON caregiver.id = caregiver_agency.caregiver_id | ||
`, | ||
expected: `{ caregiver_id: number | null; assoc_id: number | null; }`, | ||
}); | ||
}); | ||
(0, mocha_1.test)("select with duplicate columns should throw duplicate columns error", async () => { | ||
@@ -129,0 +153,0 @@ await testQuery({ |
{ | ||
"name": "@ts-safeql/generate", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41219
635