Skip to content

Commit

Permalink
feat(bindings/nodejs): support value opt variant_as_object (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc authored Dec 4, 2024
1 parent e8901ce commit 04696e4
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 260 deletions.
1 change: 1 addition & 0 deletions bindings/nodejs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ napi = { version = "2.16", default-features = false, features = [
] }
napi-derive = "2.16"
once_cell = "1.18"
serde_json = "1.0"

[build-dependencies]
napi-build = "2"
28 changes: 18 additions & 10 deletions bindings/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ while (row) {
}

// get rows of map
const rows = await conn.queryIterMap("SELECT * FROM test");
const rows = await conn.queryIter("SELECT * FROM test");
let row = await rows.next();
while (row) {
console.log(row.data());
Expand Down Expand Up @@ -68,15 +68,15 @@ while (row) {

### Semi-Structured Data Types

| Databend | Node.js |
| ----------- | -------- |
| `ARRAY` | `Array` |
| `TUPLE` | `Array` |
| `MAP` | `Object` |
| `VARIANT` | `String` |
| `BITMAP` | `String` |
| `GEOMETRY` | `String` |
| `GEOGRAPHY` | `String` |
| Databend | Node.js |
| ----------- | ----------------- |
| `ARRAY` | `Array` |
| `TUPLE` | `Array` |
| `MAP` | `Object` |
| `VARIANT` | `String / Object` |
| `BITMAP` | `String` |
| `GEOMETRY` | `String` |
| `GEOGRAPHY` | `String` |

Note: `VARIANT` is a json encoded string. Example:

Expand All @@ -94,6 +94,14 @@ const value = JSON.parse(data);
console.log(value);
```

We also provide a helper function to convert `VARIANT` to `Object`:

```javascript
const row = await conn.queryRow("SELECT * FROM example limit 1;");
row.setOpts({ variantAsObject: true });
console.log(row.data());
```

## Development

```shell
Expand Down
5 changes: 2 additions & 3 deletions bindings/nodejs/generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,16 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { Client, Connection, ConnectionInfo, Schema, Field, RowIterator, NamedRowIterator, RowIteratorExt, RowOrStats, Row, NamedRow, ServerStats } = nativeBinding
const { ValueOptions, Client, Connection, ConnectionInfo, Schema, Field, RowIterator, RowIteratorExt, RowOrStats, Row, ServerStats } = nativeBinding

module.exports.ValueOptions = ValueOptions
module.exports.Client = Client
module.exports.Connection = Connection
module.exports.ConnectionInfo = ConnectionInfo
module.exports.Schema = Schema
module.exports.Field = Field
module.exports.RowIterator = RowIterator
module.exports.NamedRowIterator = NamedRowIterator
module.exports.RowIteratorExt = RowIteratorExt
module.exports.RowOrStats = RowOrStats
module.exports.Row = Row
module.exports.NamedRow = NamedRow
module.exports.ServerStats = ServerStats
17 changes: 5 additions & 12 deletions bindings/nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@

/* auto-generated by NAPI-RS */

export class ValueOptions {
variantAsObject: boolean
}
export class Client {
/** Create a new databend client with a given DSN. */
constructor(dsn: string)
constructor(dsn: string, opts?: ValueOptions | undefined | null)
/** Get a connection from the client. */
getConn(): Promise<Connection>
}
Expand All @@ -38,8 +41,6 @@ export class Connection {
queryAll(sql: string): Promise<Array<Row>>
/** Execute a SQL query, and return all rows. */
queryIter(sql: string): Promise<RowIterator>
/** Execute a SQL query, and return all rows keyed by column name. */
queryIterMap(sql: string): Promise<NamedRowIterator>
/** Execute a SQL query, and return all rows with schema and stats. */
queryIterExt(sql: string): Promise<RowIteratorExt>
/**
Expand Down Expand Up @@ -72,13 +73,6 @@ export class RowIterator {
/** Get Schema for rows. */
schema(): Schema
}
export class NamedRowIterator {
/**
* Fetch next row.
* Returns `None` if there are no more rows.
*/
next(): Promise<Error | NamedRow | null>
}
export class RowIteratorExt {
/**
* Fetch next row or stats.
Expand All @@ -93,9 +87,8 @@ export class RowOrStats {
get stats(): ServerStats | null
}
export class Row {
setOpts(opts: ValueOptions): void
values(): Array<any>
}
export class NamedRow {
data(): Record<string, any>
}
export class ServerStats {
Expand Down
Loading

0 comments on commit 04696e4

Please sign in to comment.