Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions node.js/best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,22 @@ srv.before("UPDATE", "EntityName", (req) => {
Internally the [timestamp](events#timestamp) is a JavaScript `Date` object, that is converted to the right format, when sent to the database. So if in any case a date string is needed, the best solution would be to initialize a Date object, that is then translated to the correct UTC String for the database.


## Decimals and Int64 as Strings { #decimals-int64 }

`Decimal` and `Int64` values are always returned as **strings**, not JavaScript numbers. JavaScript's `Number` type cannot represent these values without risk of losing precision, so CAP aligns all databases — [HANA](../guides/databases/hana), [PostgreSQL](../guides/databases/postgres), and [SQLite](../guides/databases/sqlite) — on this behavior.

#### Do arithmetic in the database { .good }

Avoid calculations on `Decimal` / `Int64` fields in JavaScript — do them in the database instead:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Avoid calculations on `Decimal` / `Int64` fields in JavaScript — do them in the database instead:
Calculations on `Decimal` / `Int64` fields should be done in the database:


```js
await UPDATE(Books).set('stock = stock + 1').where({ ID: 1 })
```

::: warning Integer arithmetic can exceed JavaScript's safe range
Multiplication or addition on integer fields can produce values beyond `Number.MAX_SAFE_INTEGER`. Doing such calculations in JavaScript risks silently losing precision — do them in the database instead.
:::
Comment on lines +397 to +399

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
::: warning Integer arithmetic can exceed JavaScript's safe range
Multiplication or addition on integer fields can produce values beyond `Number.MAX_SAFE_INTEGER`. Doing such calculations in JavaScript risks silently losing precision — do them in the database instead.
:::
#### Arithmetic in Javascript {.bad}
Calculations on `Decimal` / `Int64` fields in JavaScript should be avoided as integer arithmetic can exceed JavaScript's safe range. Multiplication or addition on integer fields can produce values beyond `Number.MAX_SAFE_INTEGER`. Doing such calculations in JavaScript risks silently losing precision — do them in the database instead.


## Custom Streaming <Beta /> { #custom-streaming-beta }

[Media Data](../guides/services/media-data) can be served from custom handlers of the type `READ`, `action`, or `function`.
Expand Down
Loading