node-sqlite3-wasm icon indicating copy to clipboard operation
node-sqlite3-wasm copied to clipboard

Compatible with kysely

Open ci010 opened this issue 7 months ago • 0 comments

Current interface is pretty similar to better-sqlite3. So I think it can be easy to work with kysely.

Regardless the statement.finalize call, in kysely, the only thing does not fit is the reader property on Statement, which is used to determine if it's a read operation or write:

  executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>> {
    const { sql, parameters } = compiledQuery
    const stmt = this.#db.prepare(sql)

    if (stmt.reader) {
      return Promise.resolve({
        rows: stmt.all(parameters) as O[],
      })
    } else {
      const { changes, lastInsertRowid } = stmt.run(parameters)

      const numAffectedRows =
        changes !== undefined && changes !== null ? BigInt(changes) : undefined

      return Promise.resolve({
        numUpdatedOrDeletedRows: numAffectedRows,
        numAffectedRows,
        insertId:
          lastInsertRowid !== undefined && lastInsertRowid !== null
            ? BigInt(lastInsertRowid)
            : undefined,
        rows: [],
      })
    }
  }

Possible solution

I read the better-sqlite3 code, the value is like

bool returns_data = sqlite3_column_count(handle) >= 1 || pragmaMode;

(not sure what's the pragmaMode here)

So I guess we should just add

const columns = sqlite3.column_count(this._ptr);
return columns >= 1;

to implement it.

I'm not familiar with sqlite library. If the thought above is doable, I can send PR for it.

ci010 avatar Jun 28 '24 09:06 ci010