mmkal / pgkit

PostgreSQL🤝TypeScript monorepo. SQL client/admin UI/smart migrator/type generator/schema inspector

Home Page:https://pgkit.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@slonik/typegen: Add ability for "naming hints" on queries

trhodeos opened this issue · comments

First off, great work on @slonik/typegen... Super useful stuff.

Essentially, I'd like a way to be able to give "naming hints" for query types.

Right now, we have some queries that spit out long names like interface User_id_other_id_username_....
Ideally, I'd like to be able to give hints on (one of) the queries as to what the name should be.

In practice, I'm not sure exactly what this'd look like from a user-perspective, but open to suggestions!

I haven't tried this, but it might be possible to do this already by hooking into writeTypes. I have this problem too! I'll try it out and report back here.

I was able to get something kinda-sorta working... It's pretty hacky though, but may be a good jumping off point!

writeTypes: (queries) => {
        // As of 2022-03-15, @slonik/typegen creates really long query names... This is a hacky workaround
        let count = 0;
        queries.forEach((query) => {
            query.suggestedTags = [`Db${query.suggestedTags[0]}${count++}`];
        });
        return typegen.defaults.defaultWriteTypes({
            queriesPathFromTS: (filepath) => path.join(path.dirname(filepath), '__sql__', path.basename(filepath)),
            // Override writeFile to format using eslint (instead of prettier) before writing.
            writeFile: async (filepath, content) => {
                // snip
            },
        })(queries);
    },

This works kinda for one file, but count is not unique per-file, so you end up getting random naming across the codebase.

@trhodeos each query has a file prop: https://github.com/mmkal/slonik-tools/blob/656efd5e7ecc54a9afef57d8a6bee6b51ef9f94f/packages/typegen/src/types.ts#L158-L159

Something like this might work?

writeTypes: (queries) => {
        // As of 2022-03-15, @slonik/typegen creates really long query names... This is a hacky workaround
        /** @type {Record<string, number>} */
        let counts = {};
        queries.forEach((query) => {
            const count = counts[query.file] || 0
            query.suggestedTags = [`Db${query.suggestedTags[0]}${count || ''}`];
            counts[query.file] = count + 1
        });
        return typegen.defaults.defaultWriteTypes({
            queriesPathFromTS: (filepath) => path.join(path.dirname(filepath), '__sql__', path.basename(filepath)),
            // Override writeFile to format using eslint (instead of prettier) before writing.
            writeFile: async (filepath, content) => {
                // snip
            },
        })(queries);
    },

Nice, just getting around to this. This seems to work as intended! I didn't realize the file prop existed. Thanks!

Closing because this is doable through config. Will open a new issue for reducing the likelihood of super-long names.