Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .claude/settings.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"permissions": {
"allow": [
"Bash(find:*)",
"Bash(grep:*)"
]
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local Claude settings committed

Low Severity

The PR adds .claude/settings.local.json, a machine-local configuration file unrelated to the SQLite feature change. Committing local permissions.allow entries (Bash(find:*), Bash(grep:*)) can introduce environment-specific behavior and accidental policy drift in the repository.

Fix in Cursor Fix in Web

3 changes: 1 addition & 2 deletions package/cpp/NitroSQLiteException.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

#include <exception>
#include <iostream>
#include <string>
#include <optional>
#include <string>

const std::string NITRO_SQLITE_EXCEPTION_PREFIX = "[NativeNitroSQLiteException]";


enum NitroSQLiteExceptionType {
UnknownError,
DatabaseCannotBeOpened,
Expand Down
10 changes: 4 additions & 6 deletions package/cpp/operations.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "operations.hpp"
#include "NitroSQLiteException.hpp"
#include "logs.hpp"
#include "specs/HybridNitroSQLiteQueryResult.hpp"
#include "utils.hpp"
#include <NitroModules/ArrayBuffer.hpp>
#include <cmath>
Expand Down Expand Up @@ -121,8 +122,8 @@ void bindStatement(sqlite3_stmt* statement, const SQLiteQueryParams& values) {
}
}

SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::string& query,
const std::optional<SQLiteQueryParams>& params) {
std::shared_ptr<HybridNitroSQLiteQueryResult> sqliteExecute(const std::string& dbName, const std::string& query,
const std::optional<SQLiteQueryParams>& params) {
if (dbMap.count(dbName) == 0) {
throw NitroSQLiteException::DatabaseNotOpen(dbName);
}
Expand Down Expand Up @@ -229,10 +230,7 @@ SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::str

int rowsAffected = sqlite3_changes(db);
long long latestInsertRowId = sqlite3_last_insert_rowid(db);
return {.rowsAffected = rowsAffected,
.insertId = static_cast<double>(latestInsertRowId),
.results = std::move(results),
.metadata = std::move(metadata)};
return std::make_shared<HybridNitroSQLiteQueryResult>(results, static_cast<double>(latestInsertRowId), rowsAffected, metadata);
}

SQLiteOperationResult sqliteExecuteLiteral(const std::string& dbName, const std::string& query) {
Expand Down
4 changes: 3 additions & 1 deletion package/cpp/operations.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "specs/HybridNitroSQLiteQueryResult.hpp"
#include "types.hpp"

namespace margelo::rnnitrosqlite {
Expand All @@ -15,7 +16,8 @@ void sqliteAttachDb(const std::string& mainDBName, const std::string& docPath, c

void sqliteDetachDb(const std::string& mainDBName, const std::string& alias);

SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::string& query, const std::optional<SQLiteQueryParams>& params);
std::shared_ptr<HybridNitroSQLiteQueryResult> sqliteExecute(const std::string& dbName, const std::string& query,
const std::optional<SQLiteQueryParams>& params);

SQLiteOperationResult sqliteExecuteLiteral(const std::string& dbName, const std::string& query);

Expand Down
9 changes: 3 additions & 6 deletions package/cpp/specs/HybridNitroSQLite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,9 @@ void HybridNitroSQLite::detach(const std::string& mainDbName, const std::string&
sqliteDetachDb(mainDbName, alias);
};

using ExecuteQueryResult = std::shared_ptr<HybridNitroSQLiteQueryResultSpec>;

ExecuteQueryResult HybridNitroSQLite::execute(const std::string& dbName, const std::string& query,
const std::optional<SQLiteQueryParams>& params) {
SQLiteExecuteQueryResult result = sqliteExecute(dbName, query, params);
return std::make_shared<HybridNitroSQLiteQueryResult>(std::move(result));
std::shared_ptr<HybridNitroSQLiteQueryResultSpec> HybridNitroSQLite::execute(const std::string& dbName, const std::string& query,
const std::optional<SQLiteQueryParams>& params) {
return sqliteExecute(dbName, query, params);
};

std::shared_ptr<Promise<std::shared_ptr<HybridNitroSQLiteQueryResultSpec>>>
Expand Down
28 changes: 5 additions & 23 deletions package/cpp/specs/HybridNitroSQLiteQueryResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,23 @@
namespace margelo::nitro::rnnitrosqlite {

std::optional<double> HybridNitroSQLiteQueryResult::getInsertId() {
return _result.insertId;
return _insertId;
}

double HybridNitroSQLiteQueryResult::getRowsAffected() {
return _result.rowsAffected;
return _rowsAffected;
}

SQLiteQueryResults HybridNitroSQLiteQueryResult::getResults() {
return _result.results;
return _results;
};

std::optional<NitroSQLiteQueryResultRows> HybridNitroSQLiteQueryResult::getRows() {
if (_result.results.empty()) {
return std::nullopt;
}

auto rows = _result.results;

// Create the item function that returns a Promise
auto itemFunction = [rows](double idx) -> std::shared_ptr<Promise<std::optional<SQLiteQueryResultRow>>> {
return Promise<std::optional<SQLiteQueryResultRow>>::async([rows, idx]() -> std::optional<SQLiteQueryResultRow> {
const auto index = static_cast<size_t>(idx);
if (index >= rows.size()) {
return std::nullopt;
}
return rows[index];
});
};

const auto length = static_cast<double>(rows.size());
return NitroSQLiteQueryResultRows(std::move(rows), length, itemFunction);
return _rows;
}

std::optional<SQLiteQueryTableMetadata> HybridNitroSQLiteQueryResult::getMetadata() {
return _result.metadata;
return _metadata;
}

} // namespace margelo::nitro::rnnitrosqlite
33 changes: 31 additions & 2 deletions package/cpp/specs/HybridNitroSQLiteQueryResult.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,39 @@ namespace margelo::nitro::rnnitrosqlite {
class HybridNitroSQLiteQueryResult : public HybridNitroSQLiteQueryResultSpec {
public:
HybridNitroSQLiteQueryResult() : HybridObject(TAG) {}
HybridNitroSQLiteQueryResult(SQLiteExecuteQueryResult&& result) : HybridObject(TAG), _result(std::move(result)) {}
HybridNitroSQLiteQueryResult(SQLiteQueryResults results, std::optional<double> insertId, double rowsAffected,
std::optional<SQLiteQueryTableMetadata> metadata)
: HybridObject(TAG), _insertId(insertId), _rowsAffected(rowsAffected), _results(std::move(results)), _metadata(metadata) {
if (_results.empty()) {
// Empty rows: empty vector, length 0, item callback always returns null
auto emptyItem = [](double /* idx */) -> std::shared_ptr<Promise<std::optional<SQLiteQueryResultRow>>> {
return Promise<std::optional<SQLiteQueryResultRow>>::async([]() -> std::optional<SQLiteQueryResultRow> { return std::nullopt; });
};
_rows = NitroSQLiteQueryResultRows(SQLiteQueryResults{}, 0.0, std::move(emptyItem));
return;
}

auto rows = _results;
auto itemFunction = [rows](double idx) -> std::shared_ptr<Promise<std::optional<SQLiteQueryResultRow>>> {
return Promise<std::optional<SQLiteQueryResultRow>>::async([rows, idx]() -> std::optional<SQLiteQueryResultRow> {
const auto index = static_cast<size_t>(idx);
if (index >= rows.size()) {
return std::nullopt;
}
return rows[index];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Row index coercion changes item behavior

Medium Severity

rows.item now casts idx with static_cast<size_t>(idx), which coerces non-integer numbers to integer indices. The previous JS behavior (data[idx]) returned undefined for non-integer keys. This can return the wrong row for values like decimals or NaN, changing query result access semantics.

Fix in Cursor Fix in Web

});
};

const auto length = static_cast<double>(rows.size());
_rows = NitroSQLiteQueryResultRows(std::move(rows), length, std::move(itemFunction));
}

private:
SQLiteExecuteQueryResult _result;
std::optional<double> _insertId;
double _rowsAffected;
SQLiteQueryResults _results;
std::optional<NitroSQLiteQueryResultRows> _rows;
std::optional<SQLiteQueryTableMetadata> _metadata;

public:
// Properties
Expand Down
2 changes: 1 addition & 1 deletion package/cpp/sqliteExecuteBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ SQLiteOperationResult sqliteExecuteBatch(const std::string& dbName, const std::v
auto metadata = std::optional<SQLiteQueryTableMetadata>(std::nullopt);
try {
auto result = sqliteExecute(dbName, command.sql, command.params);
rowsAffected += result.rowsAffected;
rowsAffected += result->getRowsAffected();
} catch (NitroSQLiteException& e) {
sqliteExecuteLiteral(dbName, "ROLLBACK");
throw e;
Expand Down
11 changes: 1 addition & 10 deletions package/cpp/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,7 @@ using SQLiteQueryTableMetadata = std::unordered_map<std::string, NitroSQLiteQuer

struct SQLiteOperationResult {
int rowsAffected;
double insertId;
int commands;
};

struct SQLiteExecuteQueryResult {
int rowsAffected;
double insertId;

SQLiteQueryResults results;
std::optional<SQLiteQueryTableMetadata> metadata;
int commands = 0;
};

// constexpr function that maps SQLiteColumnType to string literals
Expand Down
22 changes: 2 additions & 20 deletions package/src/operations/execute.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { HybridNitroSQLite } from '../nitro'
import type { NitroSQLiteQueryResult } from '../specs/NitroSQLiteQueryResult.nitro'
import type { QueryResult, QueryResultRow, SQLiteQueryParams } from '../types'
import NitroSQLiteError from '../NitroSQLiteError'

Expand All @@ -10,7 +9,7 @@ export function execute<Row extends QueryResultRow = never>(
): QueryResult<Row> {
try {
const result = HybridNitroSQLite.execute(dbName, query, params)
return buildJsQueryResult<Row>(result)
return result as QueryResult<Row>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rows item contract changed unexpectedly

High Severity

Returning HybridNitroSQLite.execute*() directly exposes native rows behavior instead of the normalized JS shape from buildJsQueryResult. The rows.item callback now comes from native NitroSQLiteQueryResultRows and resolves asynchronously, so callers expecting synchronous rows.item(idx) can get a Promise instead of a row.

Additional Locations (1)

Fix in Cursor Fix in Web

} catch (error) {
throw NitroSQLiteError.fromError(error)
}
Expand All @@ -23,25 +22,8 @@ export async function executeAsync<Row extends QueryResultRow = never>(
): Promise<QueryResult<Row>> {
try {
const result = await HybridNitroSQLite.executeAsync(dbName, query, params)
return buildJsQueryResult<Row>(result)
return result as QueryResult<Row>
} catch (error) {
throw NitroSQLiteError.fromError(error)
}
}

function buildJsQueryResult<Row extends QueryResultRow = never>(
result: NitroSQLiteQueryResult,
): QueryResult<Row> {
const data = result.results as Row[]

return {
...result,
insertId: result.insertId,
rowsAffected: result.rowsAffected,
rows: {
_array: data,
length: data.length,
item: (idx: number) => data[idx],
},
} as QueryResult<Row>
}
12 changes: 1 addition & 11 deletions package/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type {
NitroSQLiteQueryColumnMetadata,
NitroSQLiteQueryResult,
NitroSQLiteQueryResultRows,
} from './specs/NitroSQLiteQueryResult.nitro'
Expand Down Expand Up @@ -42,17 +41,8 @@ export type QueryResultRow = Record<string, SQLiteValue>

export type QueryResult<Row extends QueryResultRow = QueryResultRow> =
NitroSQLiteQueryResult & {
readonly rowsAffected: number
readonly insertId?: number
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generic QueryResult typing lost for results

Low Severity

QueryResult<Row> no longer redefines results as Row[], so the generic type parameter is effectively ignored for main row data. execute<Row>() now returns broadly typed Record<string, SQLiteValue>[], reducing type safety and making typed query APIs less reliable for consumers.

Fix in Cursor Fix in Web


/** Query results */
readonly results: Row[]

/** Query results in a row format for TypeORM compatibility */
readonly rows?: NitroSQLiteQueryResultRows<Row>

/** Table metadata */
readonly metadata?: Record<string, NitroSQLiteQueryColumnMetadata>
rows?: NitroSQLiteQueryResultRows<Row>
}

export type ExecuteQuery = <Row extends QueryResultRow = QueryResultRow>(
Expand Down
Loading