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

Query ResultSet.asMappedResults() isn't useable #307

Open
bbahrman opened this issue Sep 18, 2024 · 5 comments
Open

Query ResultSet.asMappedResults() isn't useable #307

bbahrman opened this issue Sep 18, 2024 · 5 comments

Comments

@bbahrman
Copy link

When running a query and getting ResultSet.asMappedResults(), a QueryResultMap is returned. While factually accurate, the QueryResultMap isn't useable since if you assign it to another type/interface there's no overlap.

For example, this
const folderLookup: { id: number }[] = query.runSuiteQL({ query:SELECT id from MediaItemFolder WHERE parent = 1522749 AND name = '${folderName}'}).asMappedResults();
Will throw an error:

TS2322: Type  QueryResultMap[]  is not assignable to type  { id: number; }[] Property  id  is missing in type  QueryResultMap  but required in type  { id: number; }

The solution that I've used for a while has been to convert to any and then to the type I want
const folderLookup: { id: number }[] = query.runSuiteQL({ query:SELECT id from MediaItemFolder WHERE parent = 1522749 AND name = '${folderName}'}).asMappedResults() as any;
But doing so means that you are now using any which fails standard linting and is an extra step to write every time which can lead to errors.

@bbahrman
Copy link
Author

I've created #308 as a proposed fix

@lyatziv
Copy link
Contributor

lyatziv commented Sep 18, 2024

I've had some success defining a type and casting the type using as

type someCoolType = query.QueryResultMap & { ... }
// then
const queryResults.asMappedResults() as someCoolType[]; 

@darrenhillconsulting
Copy link
Contributor

darrenhillconsulting commented Sep 18, 2024

Really, what it should be is something like this ...

type LowercaseKeys<T> = {
    [K in keyof T as Lowercase<K & string>]: T[K];
};

asMappedResults<T extends LowercaseKeys<T>>(): LowercaseKeys<T>[];

This will save developers from wondering why all the camelcase properties they expect to be returned from SuiteQL are suddenly converted to lowercase

@lyatziv
Copy link
Contributor

lyatziv commented Sep 19, 2024

☝️ That's a good point. I would then point out the convention with SQL is it use snake_case for columns so that can be avoided.

@bbahrman
Copy link
Author

Thanks @darrenhillconsulting for the idea and example. I added it in and have updated my pull request.

@lyatziv - agreed, I like the validation of the interface for things that will break and then assume most people are using snake_case as the solution to that.

bbahrman added a commit to bbahrman/typings-suitescript-2.0 that referenced this issue Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants