Skip to content

Commit

Permalink
Merge pull request #710 from magieno/add-mysql-instance-profile
Browse files Browse the repository at this point in the history
- Fixes mapping.
  • Loading branch information
etiennenoel authored Jun 6, 2024
2 parents 65b5583 + adad90b commit 864ebc8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/mysql/src/clients/mysql.client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,27 @@ describe('MySQL Client', () => {
expect(searchResults.results[2].firstName).toBe("Peter");
expect(searchResults.results[2].lastName).toBe("Ricardo");
})

it("should properly map the results", async () => {
const mysqlClient = new MysqlClient([],{
critical(message: string, extra?: any, module?: string): void {
}, debug(message: string, extra?: any, module?: string): void {
}, error(message: string, extra?: any, module?: string): void {
}, info(message: string, extra?: any, module?: string): void {
}, terminate(): void {
}, warning(message: string, extra?: any, module?: string): void {
}
}, new DataMapper(new AutoDataMappingBuilder(), [new DateNormalizer(), new StringNormalizer(), new NumberNormalizer()], []));

const users = await mysqlClient.mapResults(User, [
{"unique_id": "1", "first_name": "John", "last_name": "Smith"},
{"unique_id": "2", "first_name": "Rick", "last_name": "Sanchez"},
{"unique_id": "3", "first_name": "Peter", "last_name": "Ricardo"},
]);

expect(users).toBeDefined();
expect(Array.isArray(users)).toBeTruthy()
expect(users[0] instanceof User).toBeTruthy()
expect(users[0].uniqueId).toBe("1")
})
});
5 changes: 4 additions & 1 deletion packages/mysql/src/clients/mysql.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ export class MysqlClient implements MysqlClientInterface {
for(const key in result) {
const newKey = tableMetadata.autoColumnNamingStrategyReverse!(key);
result[newKey] = result[key];
delete result[key];

if(key !== newKey) {
delete result[key];
}
}
return result;
});
Expand Down
21 changes: 21 additions & 0 deletions packages/mysql/src/strategies/camel-case-column.strategy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {camelCaseColumnStrategy} from "./camel-case-column.strategy";

describe('Camel Case Column Strategy', () => {
it('should return the same value when the column name is in camel case', () => {
const column = 'columnName';
const result = camelCaseColumnStrategy(column);
expect(result).toBe(column);
});

it('should return the same value when the column name is in snake case', () => {
const column = 'column_name';
const result = camelCaseColumnStrategy(column);
expect(result).toBe('columnName');
});

it("should return the same value when there's nothing to change", () => {
const column = 'id';
const result = camelCaseColumnStrategy(column);
expect(result).toBe('id');
});
});

0 comments on commit 864ebc8

Please sign in to comment.