Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain how to find pg types for custom parsing + add Type example #2207

Merged
merged 1 commit into from
Dec 23, 2024
Merged
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
29 changes: 21 additions & 8 deletions website/docs/api/clients/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ export type ChangeMessage<T extends Row<unknown> = Row> = {

Or a `ControlMessage`, representing an instruction to the client, as [documented here](../http#control-messages).

#### Parsing
#### Parsing and Custom Parsing

To understand the type of each column in your shape, you can check the `electric-schema` response header in the shape response. This header contains the PostgreSQL type information for each column.

By default, when constructing a `ChangeMessage.value`, `ShapeStream` parses the following Postgres types into native JavaScript values:

Expand All @@ -285,20 +287,31 @@ By default, when constructing a `ChangeMessage.value`, `ShapeStream` parses the

All other types aren't parsed and are left in the string format as they were served by the HTTP endpoint.

##### Custom parsing

You can extend this behaviour by configuring a custom parser. This is an object mapping Postgres types to parsing functions for those types. For example, we can extend the [default parser](https://github.com/electric-sql/electric/blob/main/packages/typescript-client/src/parser.ts#L28-L37) to parse booleans into `1` or `0` instead of `true` or `false`:
You can extend the default parsing behavior by defining custom parsers for specific PostgreSQL data types. This is particularly useful when you want to transform string representations of dates, JSON, or other complex types into their corresponding JavaScript objects. Here's an example:

```ts
const stream = new ShapeStream({
url: `http://localhost:3000/v1/shape`,
// Define row type
type CustomRow = {
id: number
title: string
created_at: Date // We want this to be a Date object
}

const stream = new ShapeStream<CustomRow>({
url: 'http://localhost:3000/v1/shape',
params: {
table: `foo`
table: 'posts'
},
parser: {
bool: (value: string) => value === `true` ? 1 : 0
// Parse timestamp columns into JavaScript Date objects
timestampz: (date: string) => new Date(date)
}
})

const shape = new Shape(stream)
shape.subscribe(data => {
console.log(data.created_at instanceof Date) // true
})
```

#### Replica full
Expand Down
Loading