Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use translationAbstractSqlModel for resolving translations #846

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/migrator/migrations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// These types were generated by @balena/abstract-sql-to-typescript v5.0.1
// These types were generated by @balena/abstract-sql-to-typescript v5.0.2

import type { Types } from '@balena/abstract-sql-to-typescript';

Expand Down
2 changes: 1 addition & 1 deletion src/sbvr-api/dev.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// These types were generated by @balena/abstract-sql-to-typescript v5.0.1
// These types were generated by @balena/abstract-sql-to-typescript v5.0.2

import type { Types } from '@balena/abstract-sql-to-typescript';

Expand Down
22 changes: 10 additions & 12 deletions src/sbvr-api/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ export type AliasValidNodeType =
| UnknownTypeNodes
| NullNode;
const aliasFields = (
fromAbstractSqlModel: AbstractSqlModel,
toAbstractSqlModel: AbstractSqlModel,
translationAbstractSqlModel: AbstractSqlModel,
fromResourceName: string,
toResource: string,
aliases: Dictionary<string | AliasValidNodeType>,
): SelectNode[1] => {
const fromFieldNames = fromAbstractSqlModel.tables[
const fromFieldNames = translationAbstractSqlModel.tables[
fromResourceName
].fields.map(({ fieldName }) => fieldName);
const nonexistentFields = _.difference(Object.keys(aliases), fromFieldNames);
Expand All @@ -39,9 +38,9 @@ const aliasFields = (
`Tried to alias non-existent fields: '${nonexistentFields.join(', ')}'`,
);
}
const toFieldNames = toAbstractSqlModel.tables[toResource].fields.map(
({ fieldName }) => fieldName,
);
const toFieldNames = translationAbstractSqlModel.tables[
toResource
].fields.map(({ fieldName }) => fieldName);
const checkToFieldExists = (fromFieldName: string, toFieldName: string) => {
if (!toFieldNames.includes(toFieldName)) {
throw new Error(
Expand Down Expand Up @@ -70,13 +69,12 @@ const aliasFields = (
};

const aliasResource = (
fromAbstractSqlModel: AbstractSqlModel,
toAbstractSqlModel: AbstractSqlModel,
translationAbstractSqlModel: AbstractSqlModel,
fromResourceName: string,
toResource: string,
aliases: Dictionary<string | AliasValidNodeType>,
): Definition => {
if (!toAbstractSqlModel.tables[toResource]) {
if (!translationAbstractSqlModel.tables[toResource]) {
throw new Error(`Tried to alias to a non-existent resource: ${toResource}`);
}
return {
Expand All @@ -85,8 +83,7 @@ const aliasResource = (
[
'Select',
aliasFields(
fromAbstractSqlModel,
toAbstractSqlModel,
translationAbstractSqlModel,
fromResourceName,
toResource,
aliases,
Expand Down Expand Up @@ -247,8 +244,9 @@ export const translateAbstractSqlModel = (
table.definition = definition;
} else {
table.definition = aliasResource(
// fromAbstractSqlModel is the translation model as it contains
// both the unaliased fromResource and the aliased toResource
fromAbstractSqlModel,
toAbstractSqlModel,
key,
aliasedToResource,
definition,
Expand Down
2 changes: 1 addition & 1 deletion src/sbvr-api/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// These types were generated by @balena/abstract-sql-to-typescript v5.0.1
// These types were generated by @balena/abstract-sql-to-typescript v5.0.2

import type { Types } from '@balena/abstract-sql-to-typescript';

Expand Down
2 changes: 1 addition & 1 deletion src/tasks/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// These types were generated by @balena/abstract-sql-to-typescript v5.0.1
// These types were generated by @balena/abstract-sql-to-typescript v5.0.2

import type { Types } from '@balena/abstract-sql-to-typescript';

Expand Down
Loading