Skip to content

Commit

Permalink
Maintain Foreign Keys (#56)
Browse files Browse the repository at this point in the history
* Add test for foreign keys

* Explicitly add foreign keys

* Add changelog entry
  • Loading branch information
daogrady authored Aug 22, 2023
1 parent 64b1ef1 commit e0316b4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

### Added
- Support for `[many] $self` syntax in bound action parameters
- Foreign keys are now present in the generated types in addition to the resolved property

### Fixed
## Version 0.6.1 - 2023-08-10
Expand Down
5 changes: 4 additions & 1 deletion lib/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class Visitor {
* @param {VisitorOptions} options
*/
constructor(csn, options = {}, logger = new Logger()) {
util.fixCSN(csn)
this.options = { ...defaults, ...options }
this.logger = logger
this.csn = csn
Expand Down Expand Up @@ -130,6 +129,10 @@ class Visitor {
buffer.indent()
for (const [ename, element] of Object.entries(entity.elements ?? {})) {
this.visitElement(ename, element, file, buffer)
// make foreign keys explicit
for (const [fkname, fkelement] of Object.entries(element.foreignKeys ?? {})) {
this.visitElement(`${ename}_${fkname}`, fkelement, file, buffer)
}
}
for (const [aname, action] of Object.entries(entity.actions ?? {})) {
buffer.add(
Expand Down
5 changes: 4 additions & 1 deletion test/unit/files/references/model.cds
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace references;

entity Foo {}
entity Foo {
key first_key: UUID;
key second_key: String;
}

entity Bar {
key id: Integer; // required for composition of many ... to work.
Expand Down
6 changes: 6 additions & 0 deletions test/unit/references.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ describe('References', () => {
&& m.type.name === 'many'
&& m.type.args[0].name === 'Foo_'
)).toBeTruthy()
expect(ast.exists('_BarAspect', 'assoc_one_first_key', m => true
&& m.type.keyword === 'string'
)).toBeTruthy()
expect(ast.exists('_BarAspect', 'assoc_one_second_key', m => true
&& m.type.keyword === 'string'
)).toBeTruthy()
})

test('Inline', async () => {
Expand Down

0 comments on commit e0316b4

Please sign in to comment.