Skip to content

Commit f71f2f3

Browse files
Add access to rows as arrays
1 parent f07abc1 commit f71f2f3

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

src/rowset.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,30 @@ import { SQLCloudRowsetMetadata, SQLiteCloudDataTypes, SQLiteCloudError } from '
88
export class SQLiteCloudRow {
99
constructor(rowset: SQLiteCloudRowset, columnsNames: string[], data: SQLiteCloudDataTypes[]) {
1010
this.#rowset = rowset
11+
this.#data = data
1112
for (let i = 0; i < columnsNames.length; i++) {
1213
this[columnsNames[i]] = data[i]
1314
}
1415
}
1516

16-
// @ts-expect-error
17-
private #rowset: SQLiteCloudRowset
17+
// rowset is private
18+
#rowset: SQLiteCloudRowset
19+
20+
// data is private
21+
#data: SQLiteCloudDataTypes[]
1822

1923
/** Returns the rowset that this row belongs to */
2024
// @ts-expect-error
2125
public getRowset(): SQLiteCloudRowset {
2226
return this.#rowset
2327
}
2428

29+
/** Returns rowset data as a plain array of values */
30+
// @ts-expect-error
31+
public getData(): SQLiteCloudDataTypes[] {
32+
return this.#data
33+
}
34+
2535
/** Column values are accessed by column name */
2636
[columnName: string]: SQLiteCloudDataTypes
2737
}

test/row.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* row.test.ts - test rows in rowset container
3+
*/
4+
5+
import { SQLiteCloudRowset, SQLiteCloudRow } from '../src/index'
6+
import { getChinookConnection } from './shared'
7+
8+
describe('row', () => {
9+
it('can be accessed as a dictionary', done => {
10+
const connection = getChinookConnection()
11+
connection.sendCommands('SELECT * FROM tracks LIMIT 10;', (error, rowset) => {
12+
expect(rowset).toBeInstanceOf(SQLiteCloudRowset)
13+
14+
const row = rowset[0]
15+
expect(row).toBeInstanceOf(SQLiteCloudRow)
16+
expect(rowset[0]).toMatchObject({
17+
AlbumId: 1,
18+
Bytes: 11170334,
19+
Composer: 'Angus Young, Malcolm Young, Brian Johnson',
20+
GenreId: 1,
21+
MediaTypeId: 1,
22+
Milliseconds: 343719,
23+
Name: 'For Those About To Rock (We Salute You)',
24+
TrackId: 1,
25+
UnitPrice: 0.99
26+
})
27+
28+
connection.close()
29+
done()
30+
})
31+
})
32+
33+
it('can be accessed as an array', done => {
34+
const connection = getChinookConnection()
35+
connection.sendCommands('SELECT * FROM tracks LIMIT 10;', (error, rowset) => {
36+
expect(rowset).toBeInstanceOf(SQLiteCloudRowset)
37+
38+
const row = rowset[0]
39+
expect(row).toBeInstanceOf(SQLiteCloudRow)
40+
41+
const rowData = row.getData()
42+
expect(rowData).toMatchObject([
43+
1,
44+
'For Those About To Rock (We Salute You)',
45+
1,
46+
1,
47+
1,
48+
'Angus Young, Malcolm Young, Brian Johnson',
49+
343719,
50+
11170334,
51+
0.99
52+
])
53+
54+
const rowColumns = rowset.metadata.columns.map((column: any) => column.name)
55+
expect(rowColumns).toMatchObject(['TrackId', 'Name', 'AlbumId', 'MediaTypeId', 'GenreId', 'Composer', 'Milliseconds', 'Bytes', 'UnitPrice'])
56+
57+
connection.close()
58+
done()
59+
})
60+
})
61+
})

0 commit comments

Comments
 (0)