Skip to content

Commit a8b6efc

Browse files
authored
feat: Re-implement TypeORM support on JS side instead of as part of Hybrid object (#258)
* refactor: move `rows` TypeORM object to JS side * chore: update nitrogen files * chore: update logic creating `rows` object
1 parent 186dbfc commit a8b6efc

File tree

8 files changed

+50
-160
lines changed

8 files changed

+50
-160
lines changed

package/cpp/specs/HybridNitroSQLiteQueryResult.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ SQLiteQueryResults HybridNitroSQLiteQueryResult::getResults() {
2020
return _results;
2121
};
2222

23-
std::optional<NitroSQLiteQueryResultRows> HybridNitroSQLiteQueryResult::getRows() {
24-
return _rows;
25-
}
26-
2723
std::optional<SQLiteQueryTableMetadata> HybridNitroSQLiteQueryResult::getMetadata() {
2824
return _metadata;
2925
}

package/cpp/specs/HybridNitroSQLiteQueryResult.hpp

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,19 @@ class HybridNitroSQLiteQueryResult : public HybridNitroSQLiteQueryResultSpec {
1313
HybridNitroSQLiteQueryResult() : HybridObject(TAG) {}
1414
HybridNitroSQLiteQueryResult(SQLiteQueryResults results, std::optional<double> insertId, double rowsAffected,
1515
std::optional<SQLiteQueryTableMetadata> metadata)
16-
: HybridObject(TAG), _insertId(insertId), _rowsAffected(rowsAffected), _results(std::move(results)), _metadata(metadata) {
17-
if (_results.empty()) {
18-
// Empty rows: empty vector, length 0, item callback always returns null
19-
auto emptyItem = [](double /* idx */) -> std::shared_ptr<Promise<std::optional<SQLiteQueryResultRow>>> {
20-
return Promise<std::optional<SQLiteQueryResultRow>>::async([]() -> std::optional<SQLiteQueryResultRow> { return std::nullopt; });
21-
};
22-
_rows = NitroSQLiteQueryResultRows(SQLiteQueryResults{}, 0.0, std::move(emptyItem));
23-
return;
24-
}
25-
26-
auto rows = _results;
27-
auto itemFunction = [rows](double idx) -> std::shared_ptr<Promise<std::optional<SQLiteQueryResultRow>>> {
28-
return Promise<std::optional<SQLiteQueryResultRow>>::async([rows, idx]() -> std::optional<SQLiteQueryResultRow> {
29-
const auto index = static_cast<size_t>(idx);
30-
if (index >= rows.size()) {
31-
return std::nullopt;
32-
}
33-
return rows[index];
34-
});
35-
};
36-
37-
const auto length = static_cast<double>(rows.size());
38-
_rows = NitroSQLiteQueryResultRows(std::move(rows), length, std::move(itemFunction));
39-
}
16+
: HybridObject(TAG), _insertId(insertId), _rowsAffected(rowsAffected), _results(std::move(results)), _metadata(metadata) {}
4017

4118
private:
4219
std::optional<double> _insertId;
4320
double _rowsAffected;
4421
SQLiteQueryResults _results;
45-
std::optional<NitroSQLiteQueryResultRows> _rows;
4622
std::optional<SQLiteQueryTableMetadata> _metadata;
4723

4824
public:
4925
// Properties
5026
std::optional<double> getInsertId() override;
5127
double getRowsAffected() override;
5228
SQLiteQueryResults getResults() override;
53-
std::optional<NitroSQLiteQueryResultRows> getRows() override;
5429
std::optional<SQLiteQueryTableMetadata> getMetadata() override;
5530
};
5631

package/nitrogen/generated/shared/c++/HybridNitroSQLiteQueryResultSpec.cpp

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/nitrogen/generated/shared/c++/HybridNitroSQLiteQueryResultSpec.hpp

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/nitrogen/generated/shared/c++/NitroSQLiteQueryResultRows.hpp

Lines changed: 0 additions & 99 deletions
This file was deleted.

package/src/operations/execute.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { HybridNitroSQLite } from '../nitro'
22
import type { QueryResult, QueryResultRow, SQLiteQueryParams } from '../types'
33
import NitroSQLiteError from '../NitroSQLiteError'
4+
import type { NitroSQLiteQueryResult } from '../specs/NitroSQLiteQueryResult.nitro'
45

56
export function execute<Row extends QueryResultRow = never>(
67
dbName: string,
78
query: string,
89
params?: SQLiteQueryParams,
910
): QueryResult<Row> {
1011
try {
11-
const result = HybridNitroSQLite.execute(dbName, query, params)
12-
return result as QueryResult<Row>
12+
const nativeResult = HybridNitroSQLite.execute(dbName, query, params)
13+
return buildJSQueryResult<Row>(nativeResult)
1314
} catch (error) {
1415
throw NitroSQLiteError.fromError(error)
1516
}
@@ -21,9 +22,35 @@ export async function executeAsync<Row extends QueryResultRow = never>(
2122
params?: SQLiteQueryParams,
2223
): Promise<QueryResult<Row>> {
2324
try {
24-
const result = await HybridNitroSQLite.executeAsync(dbName, query, params)
25-
return result as QueryResult<Row>
25+
const nativeResult = await HybridNitroSQLite.executeAsync(
26+
dbName,
27+
query,
28+
params,
29+
)
30+
return buildJSQueryResult<Row>(nativeResult)
2631
} catch (error) {
2732
throw NitroSQLiteError.fromError(error)
2833
}
2934
}
35+
36+
function buildJSQueryResult<Row extends QueryResultRow = never>(
37+
result: NitroSQLiteQueryResult,
38+
): QueryResult<Row> {
39+
const resultWithRows = result as QueryResult<Row>
40+
41+
resultWithRows.rows = {
42+
_array: result.results as Row[],
43+
length: result.results.length,
44+
item: (idx: number) => result.results[idx] as Row | undefined,
45+
}
46+
47+
return resultWithRows
48+
49+
// return Object.assign(result, {
50+
// rows: {
51+
// _array: result.results,
52+
// length: result.results.length,
53+
// item: (idx: number) => result.results[idx],
54+
// },
55+
// })
56+
}

package/src/specs/NitroSQLiteQueryResult.nitro.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ export interface NitroSQLiteQueryResult
1919
/** Query results */
2020
readonly results: Record<string, SQLiteValue>[]
2121

22-
/** Query results in a row format for TypeORM compatibility */
23-
readonly rows?: NitroSQLiteQueryResultRows
24-
2522
/** Table metadata */
2623
readonly metadata?: Record<string, NitroSQLiteQueryColumnMetadata>
2724
}
@@ -33,20 +30,6 @@ export interface NitroSQLiteQueryResult
3330

3431
// type NitroQueryResultRow = Record<string, SQLiteValue>
3532

36-
export type NitroSQLiteQueryResultRows<
37-
Row extends Record<string, SQLiteValue> = Record<string, SQLiteValue>,
38-
> = {
39-
/** Raw array with all dataset */
40-
_array: Row[]
41-
/** The lengh of the dataset */
42-
length: number
43-
/** A convenience function to acess the index based the row object
44-
* @param idx the row index
45-
* @returns the row structure identified by column names
46-
*/
47-
item: (idx: number) => Row | undefined
48-
}
49-
5033
export type NitroSQLiteQueryColumnMetadata = {
5134
/** The name used for this column for this result set */
5235
name: string

package/src/types.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import type {
2-
NitroSQLiteQueryResult,
3-
NitroSQLiteQueryResultRows,
4-
} from './specs/NitroSQLiteQueryResult.nitro'
1+
import type { NitroSQLiteQueryResult } from './specs/NitroSQLiteQueryResult.nitro'
52

63
export interface NitroSQLiteConnectionOptions {
74
name: string
@@ -42,9 +39,25 @@ export type QueryResultRow = Record<string, SQLiteValue>
4239
export type QueryResult<Row extends QueryResultRow = QueryResultRow> =
4340
NitroSQLiteQueryResult & {
4441
/** Query results in a row format for TypeORM compatibility */
45-
rows?: NitroSQLiteQueryResultRows<Row>
42+
rows: NitroSQLiteQueryResultRows<Row>
4643
}
4744

45+
export type NitroSQLiteQueryResultRows<
46+
Row extends Record<string, SQLiteValue> = Record<string, SQLiteValue>,
47+
> = {
48+
/** Raw array with all dataset */
49+
_array: Row[]
50+
51+
/** The lengh of the dataset */
52+
length: number
53+
54+
/** A convenience function to acess the index based the row object
55+
* @param idx the row index
56+
* @returns the row structure identified by column names
57+
*/
58+
item: (idx: number) => Row | undefined
59+
}
60+
4861
export type ExecuteQuery = <Row extends QueryResultRow = QueryResultRow>(
4962
query: string,
5063
params?: SQLiteValue[],

0 commit comments

Comments
 (0)