cube-js / cube-js/cube

Helper function to rename the keys in a record from Foo.bar → fooBar

Open
#4,468 2 comments 0 reactions 0 assignees View on GitHub
help wanted
Dominant language
Rust
Stars
20.8k
Forks
2.1k
Avg merge
1d 2h
Merged PRs (30d)
181

Description

The data returned from Cube is an array of objects, whose keys are dimension and measure names. However, these keys are not JavaScript identifiers, which makes them awkward to work with:

```ts
const query = {
dimensions: ["Item.name"],
measures: ["Sale.count"],
} as const;

const record = (await api.load(query)).rawData()[0];

const name = record["Item.name"];
const count = record["Sale.count"];
```

To make this easier, I've written a helper function, `camelizeRecord`, that rewrites the key names:

```ts
const record = (await api.load(query)).rawData().map(camelizeRecord)[0];

const name = record.itemName;
const count = record.saleCount;
```

The function is type safe, so if the type of `record` is inferred to be `{ "Site.name": string | number | boolean | null, "Sale.count": string | number | boolean | null }` (as it is with #4446), then the type of `camelizeRecord(record)` will be inferred to be `{ "siteName": string | number | boolean | null, "saleCount": string | number | boolean | null }`.

The implementation is straightforward and about 20 LOC.

After having tried this out for a couple weeks, I can confirm that my teammates and I find it very useful. It's a minor ergonomic improvement, but it adds up, and the code looks much nicer. I think it would be worth adding to `@cubejs-client/core`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.