dyedgreen / deno-sqlite

Deno SQLite module
https://deno.land/x/sqlite
MIT License
409 stars 36 forks source link

Provide specialized query methods for improved performance #132

Closed dyedgreen closed 3 years ago

dyedgreen commented 3 years ago

The common patterns in other libraries are:

This would be a performance only feature; since the existing query already fills all the existing cases:

Originally posted by @dyedgreen in https://github.com/dyedgreen/deno-sqlite/issues/14#issuecomment-847397847

dyedgreen commented 3 years ago

Since this is strictly for performance only, we'd need to check if there are performance improvements, since otherwise the additional API surface is not worth anything, since the functionality already exists.

dyedgreen commented 3 years ago

I had a few thoughts about this, and with the recent issue #135, I believe the current API can be improved as follows:

dyedgreen commented 3 years ago

To sketch the interface a bit more:

class DB {
  // ...

  // equivalent to prepareQuery(); queryAll(); finalize();
  query(sql: string, values?: Record<string, QueryParam> | Array<QueryParam>): Array<Array<any>>

  prepareQuery(sql: string): PreparedQuery

  // ...
}

interface RowIterator {
  next(): IteratorResult<Array<any>>;
  return(): IteratorResult<Array<any>>;
}

class PreparedQuery {
  // ...

  query(values?: Record<string, QueryParam> | Array<QueryParam>): RowIterator

  queryAll(values?: Record<string, QueryParam> | Array<QueryParam>): Array<Array<any>>

  queryOne(values?: Record<string, QueryParam> | Array<QueryParam>): Array<any>

  execute(values?: Record<string, QueryParam> | Array<QueryParam>)

  get columns(): Array<ColumnName>
}