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

.toSQL() return SqlNative[] should be a logic bug instead of type definition bug #5483

Open
loynoir opened this issue Feb 11, 2023 · 0 comments

Comments

@loynoir
Copy link

loynoir commented Feb 11, 2023

Environment

Knex version: 2.4.2
Database + version: pg any
OS: any

@lorefnon

Bug

Brief

.toSQL() return SqlNative[]

  • some may consider this as type definition bug
  • I prefer consider this as logic bug

And because .toSQL() return SqlNative[], .toSQL().toNative() got error.

Actual

> var reproduceSql = (require('knex')({ client: 'pg' })
  .schema.createTable('users', function (table) {
    table.increments()
    table.string('name')
    table.timestamps()
  })
  .toSQL())
[
  {
    sql: 'create table "users" ("id" serial primary key, "name" varchar(255), "created_at" timestamptz, "updated_at" timestamptz)',
    bindings: []
  }
]
> var reproduceSqlNative = reproduceSql.toNative()
Uncaught TypeError: reproduceSql.toNative is not a function

Expected

> var reproduceSql = (require('knex')({ client: 'pg' })
  .schema.createTable('users', function (table) {
    table.increments()
    table.string('name')
    table.timestamps()
  })
  .toSQL())
// no error, and `reproduceSql` is type `Sql`

> var reproduceSqlNative = reproduceSql.toNative()
 // no error, and `reproduceSqlNative` is type `SqlNative`
{
  sql: 'create table "users" ("id" serial primary key, "name" varchar(255), "created_at" timestamptz, "updated_at" timestamptz)',
  bindings: []
}

Additional

  • Would be nice to cover all .toSQL().toNative() cases in tests.

  • If developers don't think .toSQL() return SqlNative[] is bug, would be nice to have document on how to build very clean sql code like below ❤️❤️❤️

  interface SqlNative {
    bindings: readonly Value[];
    sql: string;
  }

Related

#5369

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