@balena/abstract-sql-to-typescript
Advanced tools
Comparing version 3.1.0 to 3.1.1-build-reference-aliases-ef99ea06ebdb35456940db014e92764bada5a8fc-1
@@ -7,2 +7,6 @@ # Change Log | ||
## 3.1.1 - 2024-06-03 | ||
* Include reference aliases in generated types [Pagan Gazzard] | ||
## 3.1.0 - 2024-05-28 | ||
@@ -9,0 +13,0 @@ |
@@ -38,19 +38,37 @@ "use strict"; | ||
}; | ||
const fieldsToInterfaceProps = (m, fields, mode) => fields.map((f) => { | ||
const fieldToInterfaceProps = (key, m, f, mode) => { | ||
const nullable = f.required ? '' : ' | null'; | ||
return `${(0, odata_to_abstract_sql_1.sqlNameToODataName)(f.fieldName)}: ${sqlTypeToTypescriptType(m, f, mode)}${nullable};`; | ||
}); | ||
return `${(0, odata_to_abstract_sql_1.sqlNameToODataName)(key)}: ${sqlTypeToTypescriptType(m, f, mode)}${nullable};`; | ||
}; | ||
const fieldsToInterfaceProps = (m, fields, mode) => fields.map((f) => fieldToInterfaceProps(f.fieldName, m, f, mode)); | ||
const recurseRelationships = (m, relationships, inverseSynonyms, mode, currentTable, parentKey) => Object.keys(relationships).flatMap((key) => { | ||
if (key === '$') { | ||
const [localField, referencedField] = relationships.$; | ||
if (currentTable.idField === localField && referencedField != null) { | ||
const referencedTable = m.tables[referencedField[0]]; | ||
if (referencedTable != null) { | ||
const referencedInterface = getReferencedInterface(referencedTable.name, mode); | ||
const propDefinitons = [ | ||
`${parentKey}?: Array<${referencedInterface}>;`, | ||
]; | ||
const synonym = inverseSynonyms[(0, odata_to_abstract_sql_1.odataNameToSqlName)(parentKey)]; | ||
if (referencedField != null) { | ||
if (currentTable.idField === localField) { | ||
const referencedTable = m.tables[referencedField[0]]; | ||
if (referencedTable != null) { | ||
const referencedInterface = getReferencedInterface(referencedTable.name, mode); | ||
const propDefinitons = [ | ||
`${(0, odata_to_abstract_sql_1.sqlNameToODataName)(parentKey)}?: Array<${referencedInterface}>;`, | ||
]; | ||
const synonym = inverseSynonyms[parentKey]; | ||
if (synonym != null) { | ||
propDefinitons.push(`${(0, odata_to_abstract_sql_1.sqlNameToODataName)(synonym)}?: Array<${referencedInterface}>;`); | ||
} | ||
return propDefinitons; | ||
} | ||
} | ||
else { | ||
const f = currentTable.fields.find(({ fieldName }) => fieldName === localField); | ||
const propDefinitons = []; | ||
const addDefinition = (propName) => { | ||
if (f.fieldName !== propName) { | ||
propDefinitons.push(fieldToInterfaceProps(propName, m, f, mode)); | ||
} | ||
}; | ||
addDefinition(parentKey); | ||
const synonym = inverseSynonyms[parentKey]; | ||
if (synonym != null) { | ||
propDefinitons.push(`${(0, odata_to_abstract_sql_1.sqlNameToODataName)(synonym)}?: Array<${referencedInterface}>;`); | ||
addDefinition(synonym); | ||
} | ||
@@ -62,3 +80,3 @@ return propDefinitons; | ||
} | ||
return recurseRelationships(m, relationships[key], inverseSynonyms, mode, currentTable, `${parentKey}__${key.replace(/ /g, '_')}`); | ||
return recurseRelationships(m, relationships[key], inverseSynonyms, mode, currentTable, `${parentKey}-${key}`); | ||
}); | ||
@@ -65,0 +83,0 @@ const relationshipsToInterfaceProps = (m, table, mode) => { |
{ | ||
"name": "@balena/abstract-sql-to-typescript", | ||
"version": "3.1.0", | ||
"version": "3.1.1-build-reference-aliases-ef99ea06ebdb35456940db014e92764bada5a8fc-1", | ||
"description": "A translator for abstract sql into typescript types.", | ||
@@ -53,4 +53,4 @@ "main": "out/index.js", | ||
"versionist": { | ||
"publishedAt": "2024-05-28T15:15:56.246Z" | ||
"publishedAt": "2024-06-03T17:48:27.945Z" | ||
} | ||
} |
@@ -10,6 +10,3 @@ import type { | ||
} from '@balena/abstract-sql-compiler'; | ||
import { | ||
odataNameToSqlName, | ||
sqlNameToODataName, | ||
} from '@balena/odata-to-abstract-sql'; | ||
import { sqlNameToODataName } from '@balena/odata-to-abstract-sql'; | ||
import { replaceResultTransformer, TemplateTag } from 'common-tags'; | ||
@@ -74,2 +71,16 @@ | ||
const fieldToInterfaceProps = ( | ||
key: string, | ||
m: RequiredModelSubset, | ||
f: AbstractSqlField, | ||
mode: Mode, | ||
): string => { | ||
const nullable = f.required ? '' : ' | null'; | ||
return `${sqlNameToODataName(key)}: ${sqlTypeToTypescriptType( | ||
m, | ||
f, | ||
mode, | ||
)}${nullable};`; | ||
}; | ||
const fieldsToInterfaceProps = ( | ||
@@ -80,10 +91,3 @@ m: RequiredModelSubset, | ||
): string[] => | ||
fields.map((f) => { | ||
const nullable = f.required ? '' : ' | null'; | ||
return `${sqlNameToODataName(f.fieldName)}: ${sqlTypeToTypescriptType( | ||
m, | ||
f, | ||
mode, | ||
)}${nullable};`; | ||
}); | ||
fields.map((f) => fieldToInterfaceProps(f.fieldName, m, f, mode)); | ||
@@ -103,18 +107,37 @@ const recurseRelationships = ( | ||
).$; | ||
if (currentTable.idField === localField && referencedField != null) { | ||
const referencedTable = m.tables[referencedField[0]]; | ||
if (referencedTable != null) { | ||
const referencedInterface = getReferencedInterface( | ||
referencedTable.name, | ||
mode, | ||
); | ||
const propDefinitons = [ | ||
`${parentKey}?: Array<${referencedInterface}>;`, | ||
]; | ||
const synonym = inverseSynonyms[odataNameToSqlName(parentKey)]; | ||
if (synonym != null) { | ||
propDefinitons.push( | ||
`${sqlNameToODataName(synonym)}?: Array<${referencedInterface}>;`, | ||
if (referencedField != null) { | ||
if (currentTable.idField === localField) { | ||
const referencedTable = m.tables[referencedField[0]]; | ||
if (referencedTable != null) { | ||
const referencedInterface = getReferencedInterface( | ||
referencedTable.name, | ||
mode, | ||
); | ||
const propDefinitons = [ | ||
`${sqlNameToODataName(parentKey)}?: Array<${referencedInterface}>;`, | ||
]; | ||
const synonym = inverseSynonyms[parentKey]; | ||
if (synonym != null) { | ||
propDefinitons.push( | ||
`${sqlNameToODataName(synonym)}?: Array<${referencedInterface}>;`, | ||
); | ||
} | ||
return propDefinitons; | ||
} | ||
} else { | ||
const f = currentTable.fields.find( | ||
({ fieldName }) => fieldName === localField, | ||
)!; | ||
const propDefinitons: string[] = []; | ||
const addDefinition = (propName: string) => { | ||
// Only add the relationship if it doesn't directly match the field name to avoid duplicates | ||
if (f.fieldName !== propName) { | ||
propDefinitons.push(fieldToInterfaceProps(propName, m, f, mode)); | ||
} | ||
}; | ||
addDefinition(parentKey); | ||
const synonym = inverseSynonyms[parentKey]; | ||
if (synonym != null) { | ||
addDefinition(synonym); | ||
} | ||
return propDefinitons; | ||
@@ -131,3 +154,3 @@ } | ||
currentTable, | ||
`${parentKey}__${key.replace(/ /g, '_')}`, | ||
`${parentKey}-${key}`, | ||
); | ||
@@ -134,0 +157,0 @@ }); |
@@ -246,2 +246,7 @@ import type { AbstractSqlModel } from '@balena/abstract-sql-compiler'; | ||
}, | ||
aliased: { | ||
tag: { | ||
$: ['id', ['test-has-tag key', 'test']], | ||
}, | ||
}, | ||
references: { | ||
@@ -331,2 +336,3 @@ other: { | ||
references__other: { __id: Other['Read']['id'] } | [Other['Read']]; | ||
aliased__tag?: Array<TestTag['Read']>; | ||
test__has__tag_key?: Array<TestTag['Read']>; | ||
@@ -333,0 +339,0 @@ test_tag?: Array<TestTag['Read']>; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
206345
794
2