-
-
Notifications
You must be signed in to change notification settings - Fork 46
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
Int8 as string #68
Int8 as string #68
Conversation
Thanks, but this is complicated slightly by the fact that // CREATE TABLE int8test (num int8);
await (async () => {
console.log('\n=== PR #68 ===\n');
const insresult = await db.insert('int8test', { num: 12 }).run(pool);
console.log(typeof insresult.num, insresult.num); // number 12
const selresult = await db.selectOne('int8test', db.all).run(pool);
console.log(typeof selresult!.num, selresult!.num); // number 12
const manresult = await db.sql<s.int8test.SQL, s.int8test.Selectable[]>`SELECT * FROM ${"int8test"}`.run(pool);
console.log(typeof manresult[0].num, manresult[0].num); // string 12 -- but wrongly typed as number
const jsonresult = await db.sql<s.int8test.SQL, { result: s.int8test.JSONSelectable[] }[]>`SELECT jsonb_agg(i.*) AS result FROM ${"int8test"} i`.run(pool);
console.log(typeof jsonresult[0].result[0].num, jsonresult[0].result[0].num); // number 12
})(); |
I think it's more important for the default SQL typings to be accurate, as they currently are not, than to account for a special condition where the return of the query is explicitly cast through a extraneous function. However, as a solution, would you consider having the types of int8 fields be exported as a union of 'string' and 'number' to account for both situations? This seems like it might take some changes to how the files are generated but I'd be willing to look into it. |
My own use of the library is heavy on the shortcuts, so I wouldn't call that a special condition. But I certainly agree that having accurate types is important in both cases. Having I guess for |
As I am starting to use the library more, I do see what you mean about the shortcuts. I am now having an issue where I am selecting from a table and a Date field has the string type. I believe I understand why, it's the JSON representation of the table actually being selected, but that means I have to update the type signature of my method to be for the JSON representation of the table which is hardly ideal. For my own fork, I will likely change the date field type to be Date | string but I can certainly see how that only further complicates this situation. Do you happen to have an example repository using this library? I feel like the issues I am running into are fairly significant parts of how our code currently works and that is likely due to a paradigm shift between how zapatos is meant to be used and how we did our querying before. |
The original project the library grew out of is proprietary, unfortunately, so all I can share for now is the examples in the docs (of which there are quite a lot), and those in the It's true that date columns coming to TS as strings is a bit of a pain. This might be the kind of thing that |
I haven't forgotten this, but I haven't got much free time at present, and this requires a bit of time for a small but fundamental refactor of the types generation (to allow for the TS type mapping to depend on context — in this case, whether we're going via JSON or not). |
I completely understand. For the time being, my project is just using my own fork of zapatos with this change. |
OK, I think the changes now in master should have fixed this issue properly.
Are you able to test that this all works for you? To use master from your project you'll need to clone the repo, |
I've been busy but I can give this a try tonight. Thank you! |
@TheNickmaster21 Are we good to close this? |
Very sorry; work has been chaos. I can actually try to test it today. |
So far, it seems to work fine. I had some type mismatches from my own approach. I was using .Selectable interfaces in places that now need to use JSONSelectableForTable interfaces but other than that, it seems good to go. I just wish there was a more generic interface I could use for the table that matches for the JSONSelectableForTable or the Selectable or the Updatable. But for now, this should suffice! Thank you very much. |
Great. This is now released in 3.5. |
@jawj You're doing a great job with zapatos. Just wanted to let you know :-) |
Updated the types to set int8 columns as strings files to match the default behavior of the pg library.