@balena/abstract-sql-to-typescript
Advanced tools
Comparing version 1.4.0-renovate-node-18-x-4993d785b9fb7aa368c2ba1c4f90b7ab3ad0029c-1 to 2.0.0-build-use-term-form-names-17bf9a104960319cd331ca3cb9124274e52cf184-1
@@ -7,5 +7,5 @@ # Change Log | ||
## 1.4.0 - 2022-11-24 | ||
## 2.0.0 - 2022-12-07 | ||
* Update dependency @types/node to 18.11.9 [Renovate Bot] | ||
* Use the term form name for properties of reverse navigation relations [Thodoris Greasidis] | ||
@@ -12,0 +12,0 @@ ## 1.3.0 - 2022-11-24 |
@@ -80,3 +80,4 @@ "use strict"; | ||
}); | ||
const recurseRelationships = (m, relationships, opts, currentTable, parentKey) => Object.keys(relationships).flatMap((key) => { | ||
const recurseRelationships = (m, relationships, inverseSynonyms, opts, currentTable, parentKey) => Object.keys(relationships).flatMap((key) => { | ||
var _a, _b; | ||
if (key === '$') { | ||
@@ -88,3 +89,7 @@ const [localField, referencedField] = relationships.$; | ||
const referencedInterface = modelNameToCamelCaseName(referencedTable.name); | ||
return `${parentKey}?: ${referencedInterface}[];`; | ||
const normalizedParentKey = parentKey | ||
.replace(/__/g, '-') | ||
.replace(/_/g, ' '); | ||
const propName = (_b = (_a = inverseSynonyms[normalizedParentKey]) === null || _a === void 0 ? void 0 : _a.replace(/ /g, '_')) !== null && _b !== void 0 ? _b : parentKey; | ||
return `${propName}?: ${referencedInterface}[];`; | ||
} | ||
@@ -94,3 +99,3 @@ } | ||
} | ||
return recurseRelationships(m, relationships[key], opts, currentTable, `${parentKey}__${key.replace(/ /g, '_')}`); | ||
return recurseRelationships(m, relationships[key], inverseSynonyms, opts, currentTable, `${parentKey}__${key.replace(/ /g, '_')}`); | ||
}); | ||
@@ -106,3 +111,7 @@ const relationshipsToInterfaceProps = (m, table, opts) => { | ||
} | ||
return recurseRelationships(m, relationships[key], opts, table, key.replace(/ /g, '_')); | ||
const inverseSynonyms = Object.fromEntries(Object.entries(m.synonyms).map(([termForm, factType]) => [ | ||
factType, | ||
termForm, | ||
])); | ||
return recurseRelationships(m, relationships[key], inverseSynonyms, opts, table, key.replace(/ /g, '_')); | ||
}); | ||
@@ -109,0 +118,0 @@ }; |
{ | ||
"name": "@balena/abstract-sql-to-typescript", | ||
"version": "1.4.0-renovate-node-18-x-4993d785b9fb7aa368c2ba1c4f90b7ab3ad0029c-1", | ||
"version": "2.0.0-build-use-term-form-names-17bf9a104960319cd331ca3cb9124274e52cf184-1", | ||
"description": "A translator for abstract sql into typescript types.", | ||
@@ -18,3 +18,3 @@ "main": "out/index.js", | ||
"@balena/odata-to-abstract-sql": "^5.7.0", | ||
"@types/node": "^18.0.0", | ||
"@types/node": "^12.20.47", | ||
"common-tags": "^1.8.2" | ||
@@ -44,4 +44,4 @@ }, | ||
"versionist": { | ||
"publishedAt": "2022-11-24T11:03:30.556Z" | ||
"publishedAt": "2022-12-07T10:42:51.459Z" | ||
} | ||
} |
@@ -125,2 +125,3 @@ import type { | ||
relationships: Relationship, | ||
inverseSynonyms: Record<string, string>, | ||
opts: RequiredOptions, | ||
@@ -141,3 +142,11 @@ currentTable: AbstractSqlTable, | ||
); | ||
return `${parentKey}?: ${referencedInterface}[];`; | ||
// iff the constructed property name matches wuth the full fact type, | ||
// replace it with the fact type if there is one. | ||
const normalizedParentKey = parentKey | ||
.replace(/__/g, '-') | ||
.replace(/_/g, ' '); | ||
const propName = | ||
inverseSynonyms[normalizedParentKey]?.replace(/ /g, '_') ?? | ||
parentKey; | ||
return `${propName}?: ${referencedInterface}[];`; | ||
} | ||
@@ -150,2 +159,3 @@ } | ||
(relationships as RelationshipInternalNode)[key], | ||
inverseSynonyms, | ||
opts, | ||
@@ -171,5 +181,12 @@ currentTable, | ||
} | ||
const inverseSynonyms = Object.fromEntries( | ||
Object.entries(m.synonyms).map(([termForm, factType]) => [ | ||
factType, | ||
termForm, | ||
]), | ||
); | ||
return recurseRelationships( | ||
m, | ||
relationships[key], | ||
inverseSynonyms, | ||
opts, | ||
@@ -176,0 +193,0 @@ table, |
@@ -170,2 +170,56 @@ import type { AbstractSqlModel } from '@balena/abstract-sql-compiler'; | ||
}, | ||
'test-has-tag key': { | ||
fields: [ | ||
{ | ||
dataType: 'Date Time', | ||
fieldName: 'created at', | ||
required: true, | ||
defaultValue: 'CURRENT_TIMESTAMP', | ||
}, | ||
{ | ||
dataType: 'Date Time', | ||
fieldName: 'modified at', | ||
required: true, | ||
defaultValue: 'CURRENT_TIMESTAMP', | ||
}, | ||
{ | ||
dataType: 'ForeignKey', | ||
fieldName: 'test', | ||
required: true, | ||
references: { | ||
resourceName: 'test', | ||
fieldName: 'id', | ||
}, | ||
}, | ||
{ | ||
dataType: 'Short Text', | ||
fieldName: 'tag key', | ||
required: true, | ||
}, | ||
{ | ||
dataType: 'Serial', | ||
fieldName: 'id', | ||
required: true, | ||
index: 'PRIMARY KEY', | ||
}, | ||
], | ||
primitive: false, | ||
name: 'test tag', | ||
indexes: [ | ||
{ | ||
type: 'UNIQUE', | ||
fields: ['test', 'tag key'], | ||
}, | ||
], | ||
idField: 'id', | ||
resourceName: 'test-has-tag key', | ||
triggers: [ | ||
{ | ||
when: 'BEFORE', | ||
operation: 'UPDATE', | ||
level: 'ROW', | ||
fnName: 'trigger_update_modified_at', | ||
}, | ||
], | ||
}, | ||
}, | ||
@@ -181,2 +235,5 @@ relationships: { | ||
}, | ||
'tag key': { | ||
$: ['id', ['test-has-tag key', 'test']], | ||
}, | ||
}, | ||
@@ -194,2 +251,7 @@ references: { | ||
}, | ||
has: { | ||
'tag key': { | ||
$: ['id', ['test-has-tag key', 'test']], | ||
}, | ||
}, | ||
}, | ||
@@ -204,3 +266,19 @@ }, | ||
}, | ||
'test-has-tag key': { | ||
test: { | ||
$: ['test', ['test', 'id']], | ||
}, | ||
'tag key': { | ||
$: ['tag key'], | ||
}, | ||
has: { | ||
'tag key': { | ||
$: ['tag key'], | ||
}, | ||
}, | ||
}, | ||
}, | ||
synonyms: { | ||
'test tag': 'test-has-tag key', | ||
}, | ||
}; | ||
@@ -231,3 +309,12 @@ | ||
references__other: { __id: number } | [Other]; | ||
test_tag?: TestTag[]; | ||
} | ||
export interface TestTag { | ||
created_at: DateString; | ||
modified_at: DateString; | ||
test: { __id: number } | [Test]; | ||
tag_key: string; | ||
id: number; | ||
} | ||
`, | ||
@@ -259,4 +346,12 @@ ); | ||
} | ||
export interface TestTag { | ||
created_at: Date; | ||
modified_at: Date; | ||
test: number; | ||
tag_key: string; | ||
id: number; | ||
} | ||
`, | ||
'write', | ||
); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
82608
731
8559
16
+ Added@types/node@12.20.55(transitive)
+ Addedsemver@7.7.1(transitive)
- Removed@types/node@18.19.74(transitive)
- Removedsemver@7.7.0(transitive)
- Removedundici-types@5.26.5(transitive)
Updated@types/node@^12.20.47