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

Dynamic case statement building for MySQL updates #1208

Open
MattyD-Pave opened this issue Nov 5, 2024 · 0 comments
Open

Dynamic case statement building for MySQL updates #1208

MattyD-Pave opened this issue Nov 5, 2024 · 0 comments

Comments

@MattyD-Pave
Copy link

I'm having a lot of type issues around building a case statement from dynamic data... trying to build out the following query (MySQL)

UPDATE user
SET json_data = CASE
    WHEN email = '[email protected]'
    THEN JSON_ARRAY_APPEND(json_data, '$', JSON_OBJECT('some', 'data'))
    WHEN email = '[email protected]'
    THEN JSON_ARRAY_APPEND(json_data, '$', JSON_OBJECT('some', 'other data'))
    END
WHERE email IN ('[email protected]', '[email protected]');

The function generating this receives the update values in the form of a dictionary, like the following...

update_data = {
    a@example.com: { some: 'data' },
    b@example.com: { some: 'other data' }
}

This data can be variable, so i have to build a case statement dynamically, which results in type errors as mentioned in this comment. However, i have no workaround other than exiting the type system to build a case statement. even without reduce, here's an example...

    let caseStatement = kysely.case()
    for (const [email, data] of Object.entries(dataByEmail)) {
      caseStatement = caseStatement.when('email', '=', email).then(<JSON_ARRAY_APPEND Call>);
    }
    caseStatement = caseStatement.end();
TS2322: Type CaseWhenBuilder<DB, keyof DB, unknown, any> is not assignable to type CaseBuilder<DB, keyof DB, unknown, never>
Property #private in type CaseWhenBuilder refers to a different member that cannot be accessed from within type CaseBuilder

Any tips here?

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

1 participant