Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into balegas/sst-linearlite
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAMathews committed Nov 15, 2024
2 parents 1f68c74 + b507743 commit 8df6331
Show file tree
Hide file tree
Showing 44 changed files with 216 additions and 30 deletions.
6 changes: 0 additions & 6 deletions .changeset/eighty-apples-sniff.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/funny-spoons-trade.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/modern-taxis-guess.md

This file was deleted.

8 changes: 8 additions & 0 deletions packages/react-hooks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @electric-sql/react

## 0.5.2

### Patch Changes

- Updated dependencies [65af31c]
- Updated dependencies [90ead4f]
- @electric-sql/client@0.7.2

## 0.5.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/react-hooks/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@electric-sql/react",
"version": "0.5.1",
"version": "0.5.2",
"description": "React hooks for ElectricSQL",
"type": "module",
"main": "dist/cjs/index.cjs",
Expand Down
9 changes: 9 additions & 0 deletions packages/sync-service/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @core/sync-service

## 0.8.2

### Patch Changes

- d98d9ed: Fix root table parameter validation to return 400 when missing
- 90ead4f: Support for managing multiple databases on one Electric (multi tenancy).
- 5e60e71: Refactored the tenant manager to store tenant information in an ETS table for improved read performance.
- ae18f4a: Drops the replication slot when `DELETE /v1/admin/database/:database_id` is called

## 0.8.1

### Patch Changes
Expand Down
2 changes: 2 additions & 0 deletions packages/sync-service/lib/electric/plug/serve_shape_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ defmodule Electric.Plug.ServeShapePlug do
end
end

def cast_root_table(%Ecto.Changeset{valid?: false} = changeset, _), do: changeset

def cast_root_table(%Ecto.Changeset{} = changeset, opts) do
table = fetch_change!(changeset, :table)
where = fetch_field!(changeset, :where)
Expand Down
2 changes: 1 addition & 1 deletion packages/sync-service/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@core/sync-service",
"private": true,
"version": "0.8.1",
"version": "0.8.2",
"scripts": {
"publish:hex": "mix do deps.get, hex.publish --yes"
}
Expand Down
27 changes: 24 additions & 3 deletions packages/sync-service/test/electric/plug/serve_shape_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,43 @@ defmodule Electric.Plug.ServeShapePlugTest do
:with_tenant_id
]

test "returns 400 for invalid params", ctx do
test "returns 400 for invalid table", ctx do
conn =
ctx
|> conn(:get, %{"table" => ".invalid_shape"}, "?offset=invalid")
|> conn(:get, %{"table" => ".invalid_shape"}, "?offset=-1")
|> ServeShapePlug.call([])

assert conn.status == 400

assert Jason.decode!(conn.resp_body) == %{
"offset" => ["has invalid format"],
"table" => [
"Invalid zero-length delimited identifier"
]
}
end

test "returns 400 for invalid offset", ctx do
conn =
ctx
|> conn(:get, %{"table" => "foo"}, "?offset=invalid")
|> ServeShapePlug.call([])

assert conn.status == 400

assert Jason.decode!(conn.resp_body) == %{"offset" => ["has invalid format"]}
end

test "returns 400 when table param is missing", ctx do
conn =
ctx
|> conn(:get, %{}, "?offset=-1")
|> ServeShapePlug.call([])

assert conn.status == 400

assert %{"table" => ["can't be blank"]} = Jason.decode!(conn.resp_body)
end

test "returns 400 when table does not exist", ctx do
# this will pass table name validation
# but will fail to find the table
Expand Down
7 changes: 7 additions & 0 deletions packages/typescript-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @electric-sql/client

## 0.7.2

### Patch Changes

- 65af31c: Add params option when creating shapes
- 90ead4f: Support for managing multiple databases on one Electric (multi tenancy).

## 0.7.1

### Patch Changes
Expand Down
12 changes: 12 additions & 0 deletions packages/typescript-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ const stream = new ShapeStream({
table: `foo`,
})

// You can also add custom headers and URL parameters
const streamWithParams = new ShapeStream({
url: `${BASE_URL}/v1/shape`,
table: `foo`,
headers: {
'Authorization': 'Bearer token'
},
params: {
'custom-param': 'value'
}
})

stream.subscribe(messages => {
// messages is an array with one or more row updates
// and the stream will wait for all subscribers to process them
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@electric-sql/client",
"version": "0.7.1",
"version": "0.7.2",
"description": "Postgres everywhere - your data, in sync, wherever you need it.",
"type": "module",
"main": "dist/cjs/index.cjs",
Expand Down
37 changes: 37 additions & 0 deletions packages/typescript-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ import {
REPLICA_PARAM,
} from './constants'

const RESERVED_PARAMS = new Set([
DATABASE_ID_QUERY_PARAM,
COLUMNS_QUERY_PARAM,
LIVE_CACHE_BUSTER_QUERY_PARAM,
SHAPE_HANDLE_QUERY_PARAM,
LIVE_QUERY_PARAM,
OFFSET_QUERY_PARAM,
TABLE_QUERY_PARAM,
WHERE_QUERY_PARAM,
REPLICA_PARAM,
])

type Replica = `full` | `default`

/**
Expand Down Expand Up @@ -98,6 +110,12 @@ export interface ShapeStreamOptions<T = never> {
*/
headers?: Record<string, string>

/**
* Additional request parameters to attach to the URL.
* These will be merged with Electric's standard parameters.
*/
params?: Record<string, string>

/**
* Automatically fetch updates to the Shape. If you just want to sync the current
* shape and stop, pass false.
Expand Down Expand Up @@ -246,6 +264,25 @@ export class ShapeStream<T extends Row<unknown> = Row>
this.options.subscribe
) {
const fetchUrl = new URL(url)

// Add any custom parameters first
if (this.options.params) {
// Check for reserved parameter names
const reservedParams = Object.keys(this.options.params).filter(
(key) => RESERVED_PARAMS.has(key)
)
if (reservedParams.length > 0) {
throw new Error(
`Cannot use reserved Electric parameter names in custom params: ${reservedParams.join(`, `)}`
)
}

for (const [key, value] of Object.entries(this.options.params)) {
fetchUrl.searchParams.set(key, value)
}
}

// Add Electric's internal parameters
if (table) fetchUrl.searchParams.set(TABLE_QUERY_PARAM, table)
if (where) fetchUrl.searchParams.set(WHERE_QUERY_PARAM, where)
if (columns && columns.length > 0)
Expand Down
1 change: 1 addition & 0 deletions website/about/contact.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: Contact
description: >-
Get in touch with us by email or say hello on our community Discord.
image: /img/about/vizinada.jpg
outline: deep
---

Expand Down
2 changes: 1 addition & 1 deletion website/about/jobs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Jobs
description: >-
Join a small, technical, multi-disciplinary team that's passionate
about product, developer experience and database engineering.
about product, engineering and developer experience.
image: /img/about/villa-discussion.jpg
outline: deep
---
Expand Down
3 changes: 2 additions & 1 deletion website/about/team.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
title: Team
description: >-
Meet the team behind ElectricSQL.
Meet the team, advisors and investors behind ElectricSQL.
image: /img/about/team.jpg
outline: deep
---

Expand Down
4 changes: 4 additions & 0 deletions website/docs/api/clients/elixir.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
---
title: Elixir Client
description: >-
Electric provides an Elixir client and a Phoenix integration.
image: /img/integrations/electric-phoenix.jpg
outline: deep
---

Expand Down
62 changes: 57 additions & 5 deletions website/docs/api/clients/typescript.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
---
title: Typescript Client
description: >-
Electric provides an Typescript client for streaming Shapes from Postgres
into the web browser and other Javascript environments.
image: /img/integrations/electric-typescript.jpg
outline: [2, 4]
---

Expand Down Expand Up @@ -121,22 +126,69 @@ export interface ShapeStreamOptions<T = never> {
headers?: Record<string, string>

/**
* Alternatively you can override the fetch function.
* Additional request parameters to attach to the URL.
* These will be merged with Electric's standard parameters.
* Note: You cannot use Electric's reserved parameter names
* (table, where, columns, offset, etc.).
*/
fetchClient?: typeof fetch
params?: Record<string, string>

/**
* Automatically fetch updates to the Shape. If you just want to sync the current
* shape and stop, pass false.
*/
subscribe?: boolean

backoffOptions?: BackoffOptions
parser?: Parser<T>
/**
* Signal to abort the stream.
*/
signal?: AbortSignal

/**
* Custom fetch client implementation.
*/
fetchClient?: typeof fetch

/**
* Custom parser for handling specific data types.
*/
parser?: Parser<T>

backoffOptions?: BackoffOptions
}
```

Note that certain parameter names are reserved for Electric's internal use and cannot be used in custom params:
- `table`
- `where`
- `columns`
- `offset`
- `handle`
- `live`
- `cursor`
- `database_id`
- `replica`

Attempting to use these reserved names will throw an error.

Example usage with custom headers and parameters:

```ts
const stream = new ShapeStream({
url: 'http://localhost:3000/v1/shape',
table: 'items',
// Add authentication header
headers: {
'Authorization': 'Bearer token'
},
// Add custom URL parameters
params: {
'tenant': 'acme-corp',
'version': '1.0'
}
})
```

#### Messages

A `ShapeStream` consumes and emits a stream of messages. These messages can either be a `ChangeMessage` representing a change to the shape data:
Expand All @@ -148,7 +200,7 @@ export type ChangeMessage<T extends Row<unknown> = Row> = {
headers: Header & { operation: `insert` | `update` | `delete` }
offset: Offset
}
````
```
Or a `ControlMessage`, representing an instruction to the client, as [documented here](../http#control-messages).
Expand Down
1 change: 1 addition & 0 deletions website/docs/guides/shapes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: Shapes - Guide
description: >-
Shapes are the core primitive for controlling sync in the ElectricSQL system.
image: /img/guides/sync-shape.jpg
outline: deep
---

Expand Down
2 changes: 2 additions & 0 deletions website/docs/integrations/aws.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
outline: deep
title: Amazon Web Services (AWS) - Integrations
description: >-
How to deploy Electric on Amazon Web Services (AWS).
image: /img/integrations/electric-aws.jpg
---

Expand Down
2 changes: 2 additions & 0 deletions website/docs/integrations/cloudflare.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
outline: deep
title: Cloudflare - Integrations
description: >-
How to use Electric with Cloudflare.
image: /img/integrations/electric-cloudflare.jpg
---

Expand Down
2 changes: 2 additions & 0 deletions website/docs/integrations/crunchy.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
outline: deep
title: Crunchy Data - Integrations
description: >-
How to use Electric with Crunchy Bridge managed Postgres.
image: /img/integrations/electric-crunchy.jpg
---

Expand Down
2 changes: 2 additions & 0 deletions website/docs/integrations/digital-ocean.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
outline: deep
title: Digital Ocean - Integrations
description: >-
How to deploy Electric on Digital Ocean.
image: /img/integrations/electric-digital-ocean.jpg
---

Expand Down
Loading

0 comments on commit 8df6331

Please sign in to comment.