mirror of
https://github.com/allankoechke/turso.git
synced 2026-11-30 11:12:58 +00:00
Preserve JS row positional values
This commit is contained in:
@@ -30,6 +30,20 @@ test('exec multiple statements', async () => {
|
||||
expect(rows).toEqual([{ x: 1 }, { x: 2 }]);
|
||||
})
|
||||
|
||||
test('expanded rows preserve positional values for duplicate column names', () => {
|
||||
const db = new Database(":memory:");
|
||||
db.exec("CREATE TABLE role(path TEXT); CREATE TABLE org_unit(path TEXT)");
|
||||
db.exec("INSERT INTO role VALUES ('/Employee'); INSERT INTO org_unit VALUES ('/')");
|
||||
|
||||
const row = db.prepare("SELECT role.path, org_unit.path FROM role JOIN org_unit").get();
|
||||
|
||||
expect(Object.keys(row)).toEqual(["path"]);
|
||||
expect(row.path).toBe("/");
|
||||
expect(row[0]).toBe("/Employee");
|
||||
expect(row[1]).toBe("/");
|
||||
expect(row).toEqual({ path: "/" });
|
||||
})
|
||||
|
||||
test('readonly-db', () => {
|
||||
const path = `test-${(Math.random() * 10000) | 0}.db`;
|
||||
try {
|
||||
|
||||
@@ -39,6 +39,20 @@ test('exec multiple statements', async () => {
|
||||
expect(rows).toEqual([{ x: 1 }, { x: 2 }]);
|
||||
})
|
||||
|
||||
test('expanded rows preserve positional values for duplicate column names', async () => {
|
||||
const db = await connect(":memory:");
|
||||
await db.exec("CREATE TABLE role(path TEXT); CREATE TABLE org_unit(path TEXT)");
|
||||
await db.exec("INSERT INTO role VALUES ('/Employee'); INSERT INTO org_unit VALUES ('/')");
|
||||
|
||||
const [row] = await db.all("SELECT role.path, org_unit.path FROM role JOIN org_unit");
|
||||
|
||||
expect(Object.keys(row)).toEqual(["path"]);
|
||||
expect(row.path).toBe("/");
|
||||
expect(row[0]).toBe("/Employee");
|
||||
expect(row[1]).toBe("/");
|
||||
expect(row).toEqual({ path: "/" });
|
||||
})
|
||||
|
||||
test('readonly-db', async () => {
|
||||
const path = `test-${(Math.random() * 10000) | 0}.db`;
|
||||
try {
|
||||
|
||||
@@ -834,22 +834,32 @@ impl Statement {
|
||||
to_js_value(env, value, safe_integers)?
|
||||
}
|
||||
PresentationMode::Expanded => {
|
||||
let row = Object::new(env)?;
|
||||
let mut row = Object::new(env)?;
|
||||
let raw_row = row.raw();
|
||||
let raw_env = env.raw();
|
||||
let mut positional_properties = Vec::with_capacity(row_data.len());
|
||||
for idx in 0..row_data.len() {
|
||||
let value = row_data.get_value(idx);
|
||||
let column_name = &self.column_names[idx];
|
||||
let js_value = to_js_value(env, value, safe_integers)?;
|
||||
unsafe {
|
||||
check_status!(unsafe {
|
||||
napi::sys::napi_set_named_property(
|
||||
raw_env,
|
||||
raw_row,
|
||||
column_name.as_ptr(),
|
||||
js_value.raw(),
|
||||
);
|
||||
}
|
||||
)
|
||||
})?;
|
||||
positional_properties.push(
|
||||
Property::new()
|
||||
.with_utf8_name(&idx.to_string())?
|
||||
.with_value(&js_value)
|
||||
.with_property_attributes(
|
||||
PropertyAttributes::Writable | PropertyAttributes::Configurable,
|
||||
),
|
||||
);
|
||||
}
|
||||
row.define_properties(&positional_properties)?;
|
||||
row.to_unknown()
|
||||
}
|
||||
};
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Session, type SessionConfig, type BatchMode } from './session.js';
|
||||
import { Statement } from './statement.js';
|
||||
import { type QueryOptions } from './protocol.js';
|
||||
import { normalizeArgs, splitBindParameters } from './args.js';
|
||||
import { createExpandedRow } from './row.js';
|
||||
|
||||
export type { BatchMode } from './session.js';
|
||||
|
||||
@@ -156,9 +157,7 @@ export class Connection {
|
||||
const result = await this.session.execute(sql, normalizeArgs(params), this.defaultSafeIntegerMode, queryOptions);
|
||||
const row = result.rows[0];
|
||||
if (!row) return undefined;
|
||||
const obj: any = {};
|
||||
result.columns.forEach((col: string, i: number) => { obj[col] = row[i]; });
|
||||
return obj;
|
||||
return createExpandedRow(row, result.columns);
|
||||
} finally {
|
||||
this.execLock.release();
|
||||
}
|
||||
@@ -173,11 +172,7 @@ export class Connection {
|
||||
await this.execLock.acquire();
|
||||
try {
|
||||
const result = await this.session.execute(sql, normalizeArgs(params), this.defaultSafeIntegerMode, queryOptions);
|
||||
return result.rows.map((row: any) => {
|
||||
const obj: any = {};
|
||||
result.columns.forEach((col: string, i: number) => { obj[col] = row[i]; });
|
||||
return obj;
|
||||
});
|
||||
return result.rows.map((row: any) => createExpandedRow(row, result.columns));
|
||||
} finally {
|
||||
this.execLock.release();
|
||||
}
|
||||
|
||||
15
serverless/javascript/src/row.ts
Normal file
15
serverless/javascript/src/row.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export function createExpandedRow(row: any[], columns: string[]): any {
|
||||
const expanded: any = {};
|
||||
|
||||
columns.forEach((column, index) => {
|
||||
expanded[column] = row[index];
|
||||
Object.defineProperty(expanded, String(index), {
|
||||
value: row[index],
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
});
|
||||
});
|
||||
|
||||
return expanded;
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import { Session, type SessionConfig } from './session.js';
|
||||
import { type AsyncLock } from './async-lock.js';
|
||||
import { DatabaseError } from './error.js';
|
||||
import { normalizeArgs } from './args.js';
|
||||
import { createExpandedRow } from './row.js';
|
||||
|
||||
/**
|
||||
* A prepared SQL statement that can be executed in multiple ways.
|
||||
@@ -202,12 +203,7 @@ export class Statement {
|
||||
return [...row];
|
||||
}
|
||||
|
||||
// In expanded mode, convert to plain object with named properties
|
||||
const obj: any = {};
|
||||
result.columns.forEach((col: string, i: number) => {
|
||||
obj[col] = row[i];
|
||||
});
|
||||
return obj;
|
||||
return createExpandedRow(row, result.columns);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -238,14 +234,7 @@ export class Statement {
|
||||
return result.rows.map((row: any) => [...row]);
|
||||
}
|
||||
|
||||
// In expanded mode, convert rows to plain objects with named properties
|
||||
return result.rows.map((row: any) => {
|
||||
const obj: any = {};
|
||||
result.columns.forEach((col: string, i: number) => {
|
||||
obj[col] = row[i];
|
||||
});
|
||||
return obj;
|
||||
});
|
||||
return result.rows.map((row: any) => createExpandedRow(row, result.columns));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -300,12 +289,7 @@ export class Statement {
|
||||
// In raw mode, yield arrays of values
|
||||
yield decodedRow;
|
||||
} else {
|
||||
// In expanded mode, yield plain objects with named properties (consistent with all())
|
||||
const obj: any = {};
|
||||
columns.forEach((col: string, i: number) => {
|
||||
obj[col] = decodedRow[i];
|
||||
});
|
||||
yield obj;
|
||||
yield createExpandedRow(decodedRow, columns);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -646,6 +646,22 @@ test.serial("Statement.get() [raw]", async (t) => {
|
||||
t.deepEqual(await stmt.raw().get(1), [1, "Alice", "alice@example.org"]);
|
||||
});
|
||||
|
||||
test.serial("Database.all() preserves positional values for duplicate column names", async (t) => {
|
||||
const db = t.context.db;
|
||||
|
||||
await db.exec("DROP TABLE IF EXISTS role; DROP TABLE IF EXISTS org_unit");
|
||||
await db.exec("CREATE TABLE role(path TEXT); CREATE TABLE org_unit(path TEXT)");
|
||||
await db.exec("INSERT INTO role VALUES ('/Employee'); INSERT INTO org_unit VALUES ('/')");
|
||||
|
||||
const [row] = await db.all("SELECT role.path, org_unit.path FROM role JOIN org_unit");
|
||||
|
||||
t.deepEqual(Object.keys(row), ["path"]);
|
||||
t.is(row.path, "/");
|
||||
t.is(row[0], "/Employee");
|
||||
t.is(row[1], "/");
|
||||
t.deepEqual(row, { path: "/" });
|
||||
});
|
||||
|
||||
test.serial("Statement.get() values", async (t) => {
|
||||
const db = t.context.db;
|
||||
|
||||
|
||||
@@ -334,6 +334,22 @@ test.serial("Statement.get() [raw]", async (t) => {
|
||||
t.deepEqual(stmt.raw().get(1), [1, "Alice", "alice@example.org"]);
|
||||
});
|
||||
|
||||
test.serial("Statement.get() preserves positional values for duplicate column names", async (t) => {
|
||||
const db = t.context.db;
|
||||
|
||||
db.exec("DROP TABLE IF EXISTS role; DROP TABLE IF EXISTS org_unit");
|
||||
db.exec("CREATE TABLE role(path TEXT); CREATE TABLE org_unit(path TEXT)");
|
||||
db.exec("INSERT INTO role VALUES ('/Employee'); INSERT INTO org_unit VALUES ('/')");
|
||||
|
||||
const row = db.prepare("SELECT role.path, org_unit.path FROM role JOIN org_unit").get();
|
||||
|
||||
t.deepEqual(Object.keys(row), ["path"]);
|
||||
t.is(row.path, "/");
|
||||
t.is(row[0], "/Employee");
|
||||
t.is(row[1], "/");
|
||||
t.deepEqual(row, { path: "/" });
|
||||
});
|
||||
|
||||
test.serial("Statement.get() values", async (t) => {
|
||||
const db = t.context.db;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user