question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue while extracting init logic to separate module

See original GitHub issue

Hi. I’ve been checking this tool for a few days and find it amazing, thanks!!

I’m developing a serverless application that will deploy a number of different lambdas, many of which need to access kyselys functionalities. For that, I want to extract the init logic new Kysely<Database>{...} to a reusable method which can be called from the different functions that require it.

My current codebase is as follows

const createDBConnection = () => new Kysely<SilverTables>({
    dialect: new MysqlDialect({
        // Empty pool configuration. Pool is mandatory to instantiate a Kysely object.
        // We won't be relying on Kysely to execute the query, only to build it.
        pool: createPool({}),
    })
})

const db = createDBConnection();

//entry point
const handler: Handler<APIGatewayEvent, ReturnType> = async (event) => {
    const queryBuilder = createQueryBuilder('1');
    const queryResult = await executeQuery(queryBuilder);

    return queryResult;
}

const createQueryBuilder = (id: string) => db.selectFrom('table).where('id', '=', id).select(['column']);

const executeQuery = <DB, TB extends keyof DB, O>(queryBuilder: SelectQueryBuilder<DB, TB, O>): Promise<O[]> => {
    const compiledQuery = queryBuilder.compile();
    let queryString = compiledQuery.sql;
    const params = compiledQuery.parameters;

    for (const param of params)
        queryString = queryString.replace('?', String(param));

    return dataAccessAPI(queryString);
}

If if move the createDBConnection method to a separate module, I start getting the following error: Property '#private' in type 'SelectQueryBuilder' refers to a different member that cannot be accessed from within type 'SelectQueryBuilder'.. Any ideas?

Thanks in advance

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
koskimascommented, Sep 27, 2022

One thing that could cause problems like this is if you have two copies of Kysely installed and you try to use them together. For example if the module that exports createDBConnection imports a different version of Kysely than the place that uses it, you’d get an error like that.

Make sure that’s not happening. If you’re not able to do that otherwise, something like this should give you the answer:

import { Kysely } from 'kysely'
import { createDBConnection } from './somewhere'

console.log('Do I have two versions installed: ', (createDBConnection() instanceof Kysely) ? 'no' : 'yes!')
1reaction
koskimascommented, Sep 27, 2022

This is a little bit off topic, but regarding the createDBConnection function: you don’t need to create a dummy mysql pool. You can do this instead:

const createDBConnection = () => new Kysely<SilverTables>({
    dialect: {
        createAdapter: () => new MysqlAdapter(),
        createDriver: () => new DummyDriver(),
        createIntrospector: (db: Kysely<unknown>) => new MysqlIntrospector(db),
        createQueryCompiler: () => new MysqlQueryCompiler(),
    }
})

This way your module doesn’t depend on the mysql2 module at all. All of those classes can be imported from kysely.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Extractor using Function Module - distinguish between INIT ...
When looking at this function module, the logic seems to utilize values in the parameter I_UPDMODE from Type Group SBIWA to make the ......
Read more >
python - Module imports and __init__.py - Stack Overflow
What I mean is that when the script Foo.py is run it will import everything it can and then __init__.py will be called...
Read more >
5. Separation of Concerns - Programming JavaScript ... - O'Reilly
Separation of concerns is the idea that each module or layer in an application should only be responsible for one thing and should...
Read more >
What's __init__ for me? Designing for Python package imports
I have had a few conversations lately about Python packaging, particularly around structuring the import statements to access the various ...
Read more >
Component Reference - Apache JMeter - User's Manual
Several test elements use JMeter properties to control their behaviour. These properties are normally resolved when the class is loaded.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found