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

JSON utilities for selectNoFrom #1294

Open
jlarmstrongiv opened this issue Dec 16, 2024 · 0 comments
Open

JSON utilities for selectNoFrom #1294

jlarmstrongiv opened this issue Dec 16, 2024 · 0 comments

Comments

@jlarmstrongiv
Copy link
Contributor

Discussed in https://discord.com/channels/890118421587578920/1315878442281209907/1315878442281209907

Currently, all the json utilities expect a column. While it is possible to use these utilities via CTEs:

const exampleJson = {
  foo: {
    bar: 42,
  },
};

function toJson<T>(obj: T): RawBuilder<T> {
  return sql`${JSON.stringify(obj)}`;
}

db.with("my_json_table", (db) =>
      db.selectNoFrom((eb) => toJson(exampleJson).as("my_example_json")),
    )
      .selectFrom("my_json_table")
      .select((eb) => eb.ref("my_example_json", "->").key("foo").as("subkey"));


db.with("my_json_table", (db) =>
      db.selectNoFrom((eb) => toJson(exampleJson).as("my_example_json")),
    )
      .selectFrom("my_json_table")
      .select((eb) => eb.jsonPath<"my_example_json">().key("foo").as("subkey"));

It would be better if they had built-in support via:

sql.jsonPath<T extends object>

eb.val({ foo: { bar: 42 } }, '->>').key('foo')

Not only would these functions be helpful for existing projects, they would also be helpful for creating simpler examples for teaching kysely json utilities.

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