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

Fix: rename action header -> operation to match PG #1481

Merged
merged 11 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 4 additions & 4 deletions docs/electric-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,14 @@ paths:

Messages can be `control` messages, providing information or
instructions to the client. Or they can be operations that
performed a certain `action` on a row of data in the shape.
performed a certain `operation` on a row of data in the shape.
properties:
control:
type: string
enum:
- up-to-date
- must-refetch
action:
operation:
type: string
enum:
- insert
Expand Down Expand Up @@ -254,15 +254,15 @@ paths:
- `extra_float_digits = 1`
example:
- headers:
action: insert
operation: insert
offset: 0/0
key: issue-1
value:
id: issue-1
title: Electric
status: backlog
- headers:
action: insert
operation: insert
control: up-to-date
offset: 1934/0
key: issue-2
Expand Down
6 changes: 3 additions & 3 deletions examples/bash-client/bash-client.bash
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ process_json() {

# Read the JSON file line by line and save each JSON object to an individual file
while IFS= read -r line; do
# Check if the headers array contains an object with key "action"
if echo "$line" | jq -e '.headers | map(select(.key == "action")) | length == 0' > /dev/null; then
# echo "Skipping line without an action: $action" # Log skipping non-data objects
# Check if the headers array contains an object with key "operation"
if echo "$line" | jq -e '.headers | map(select(.key == "operation")) | length == 0' > /dev/null; then
# echo "Skipping line without an operation: $operation" # Log skipping non-data objects
continue
fi

Expand Down
7 changes: 5 additions & 2 deletions examples/nextjs-example/app/match-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ export async function matchStream<T>({
return new Promise((resolve, reject) => {
const unsubscribe = stream.subscribe((messages) => {
for (const message of messages) {
if (`key` in message && operations.includes(message.headers.action)) {
if (
`key` in message &&
operations.includes(message.headers.operation)
) {
if (
matchFn({
operationType: message.headers.action,
operationType: message.headers.operation,
message: message as ChangeMessage<{ [key: string]: T }>,
})
) {
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs-example/tsconfig.tsbuildinfo
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a generated file. Do we need it in the Git repo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm no, I'll remove that in a separate PR as it came in with the nextjs one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix: #1491

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions examples/remix-basic/app/match-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ export async function matchStream({
return new Promise((resolve, reject) => {
const unsubscribe = stream.subscribe((messages) => {
for (const message of messages) {
if (`key` in message && operations.includes(message.headers.action)) {
if (matchFn({ operationType: message.headers.action, message })) {
if (
`key` in message &&
operations.includes(message.headers.operation)
) {
if (matchFn({ operationType: message.headers.operation, message })) {
return finish(message)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-hooks/test/support/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function forEachMessage<T extends Value>(
message as Message<T>,
messageIdx
)
if (`action` in message.headers) messageIdx++
if (`operation` in message.headers) messageIdx++
} catch (e) {
controller.abort()
return reject(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ defmodule Electric.ShapeCache.CubDbStorage do
{snapshot_key(shape_id, index), {change_key, :insert, serialized_row, %{}}}
end

defp storage_item_to_log_item({key, {change_key, action, value, header_data}})
defp storage_item_to_log_item({key, {change_key, operation, value, header_data}})
when is_binary(change_key) do
%{
key: change_key,
value: value,
headers: Map.put(header_data, :action, action),
headers: Map.put(header_data, :operation, operation),
offset: offset(key)
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ defmodule Electric.ShapeCache.InMemoryStorage do
ets_table = opts.log_ets_table

changes
|> Enum.map(fn {offset, key, action, value, header_data} ->
|> Enum.map(fn {offset, key, operation, value, header_data} ->
offset = storage_offset(offset)
{{shape_id, offset}, key, action, value, header_data}
{{shape_id, offset}, key, operation, value, header_data}
end)
|> then(&:ets.insert(ets_table, &1))

Expand Down Expand Up @@ -141,15 +141,24 @@ defmodule Electric.ShapeCache.InMemoryStorage do
end

defp snapshot_storage_item_to_log_item({_shape_id, snapshot_log_offset}, {key, value}) do
%{key: key, value: value, headers: %{action: :insert}, offset: snapshot_log_offset}
%{key: key, value: value, headers: %{operation: :insert}, offset: snapshot_log_offset}
end

# Turns a stored log item into a log item
# by modifying the turning the tuple offset
# back into a LogOffset value.
defp log_storage_item_to_log_item({_shape_id, position}, {_, key, action, value, header_data}) do
defp log_storage_item_to_log_item(
{_shape_id, position},
{_, key, operation, value, header_data}
) do
offset = LogOffset.new(position)
%{key: key, value: value, headers: Map.put(header_data, :action, action), offset: offset}

%{
key: key,
value: value,
headers: Map.put(header_data, :operation, operation),
offset: offset
}
end

# Turns a LogOffset into a tuple representation
Expand Down
2 changes: 1 addition & 1 deletion packages/sync-service/lib/electric/shape_cache/storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Electric.ShapeCache.Storage do
{
offset :: LogOffset.t(),
key :: String.t(),
action :: :insert | :update | :delete,
operation :: :insert | :update | :delete,
value :: %{String.t() => String.t()},
header_data :: %{optional(atom()) => Jason.Encoder.t()}
}
Expand Down
18 changes: 9 additions & 9 deletions packages/sync-service/test/electric/plug/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule Electric.Plug.RouterTest do

assert [
%{
"headers" => %{"action" => "insert"},
"headers" => %{"operation" => "insert"},
"key" => _,
"offset" => @first_offset,
"value" => %{
Expand Down Expand Up @@ -159,7 +159,7 @@ defmodule Electric.Plug.RouterTest do

assert [
%{
"headers" => %{"action" => "insert"},
"headers" => %{"operation" => "insert"},
"key" => ^key,
"offset" => @first_offset,
"value" => %{
Expand Down Expand Up @@ -198,7 +198,7 @@ defmodule Electric.Plug.RouterTest do

assert [
%{
"headers" => %{"action" => "insert"},
"headers" => %{"operation" => "insert"},
"key" => ^key2,
"offset" => _,
"value" => %{
Expand Down Expand Up @@ -290,17 +290,17 @@ defmodule Electric.Plug.RouterTest do

assert [
%{
"headers" => %{"action" => "delete"},
"headers" => %{"operation" => "delete"},
"value" => %{"id" => "1"},
"key" => ^key
},
%{
"headers" => %{"action" => "insert"},
"headers" => %{"operation" => "insert"},
"value" => %{"id" => "2", "value1" => _, "value2" => _, "value3" => _},
"key" => key2
},
%{
"headers" => %{"action" => "insert"},
"headers" => %{"operation" => "insert"},
"value" => %{"id" => "3", "value1" => _, "value2" => _, "value3" => _},
"key" => key3
},
Expand Down Expand Up @@ -341,17 +341,17 @@ defmodule Electric.Plug.RouterTest do

assert [
%{
"headers" => %{"action" => "delete"},
"headers" => %{"operation" => "delete"},
"value" => %{"col1" => "test1", "col2" => "test2"},
"key" => ^key
},
%{
"headers" => %{"action" => "insert"},
"headers" => %{"operation" => "insert"},
"value" => %{"col1" => "test3", "col2" => "test2"},
"key" => key2
},
%{
"headers" => %{"action" => "insert"},
"headers" => %{"operation" => "insert"},
"value" => %{"col1" => "test4", "col2" => "test5"},
"key" => key3
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ defmodule Electric.ShapeCache.StorageImplimentationsTest do
offset: @snapshot_offset,
value: %{"id" => "00000000-0000-0000-0000-000000000001", "title" => "row1"},
key: ~S|"public"."the-table"/"00000000-0000-0000-0000-000000000001"|,
headers: %{action: :insert}
headers: %{operation: :insert}
},
%{
offset: @snapshot_offset,
value: %{"id" => "00000000-0000-0000-0000-000000000002", "title" => "row2"},
key: ~S|"public"."the-table"/"00000000-0000-0000-0000-000000000002"|,
headers: %{action: :insert}
headers: %{operation: :insert}
}
] = Enum.to_list(stream)
end
Expand Down Expand Up @@ -125,13 +125,13 @@ defmodule Electric.ShapeCache.StorageImplimentationsTest do
offset: @snapshot_offset,
value: %{"id" => "00000000-0000-0000-0000-000000000001", "title" => "row1"},
key: ~S|"public"."the-table"/"00000000-0000-0000-0000-000000000001"|,
headers: %{action: :insert}
headers: %{operation: :insert}
},
%{
offset: @snapshot_offset,
value: %{"id" => "00000000-0000-0000-0000-000000000002", "title" => "row2"},
key: ~S|"public"."the-table"/"00000000-0000-0000-0000-000000000002"|,
headers: %{action: :insert}
headers: %{operation: :insert}
}
] = Enum.to_list(stream)
end
Expand Down Expand Up @@ -188,7 +188,7 @@ defmodule Electric.ShapeCache.StorageImplimentationsTest do

assert entry.key == ~S|"public"."test_table"/"123"|
assert entry.value == %{"id" => "123", "name" => "Test"}
assert entry.headers == %{action: :insert, txid: 1, relation: ["public", "test_table"]}
assert entry.headers == %{operation: :insert, txid: 1, relation: ["public", "test_table"]}
assert entry.offset == offset
end
end
Expand Down Expand Up @@ -238,9 +238,9 @@ defmodule Electric.ShapeCache.StorageImplimentationsTest do
entries = Enum.to_list(stream)

assert [
%{headers: %{action: :insert}},
%{headers: %{action: :update}},
%{headers: %{action: :delete}}
%{headers: %{operation: :insert}},
%{headers: %{operation: :update}},
%{headers: %{operation: :delete}}
] = entries
end

Expand Down Expand Up @@ -283,8 +283,8 @@ defmodule Electric.ShapeCache.StorageImplimentationsTest do
entries = Enum.to_list(stream)

assert [
%{headers: %{action: :update}},
%{headers: %{action: :delete}}
%{headers: %{operation: :update}},
%{headers: %{operation: :delete}}
] = entries
end

Expand Down Expand Up @@ -334,7 +334,7 @@ defmodule Electric.ShapeCache.StorageImplimentationsTest do

entries = Enum.to_list(stream)

assert [%{headers: %{action: :update}}] = entries
assert [%{headers: %{operation: :update}}] = entries
end

test "returns only logs for the requested shape_id", %{module: storage, opts: opts} do
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,10 @@ export class Shape {
messages.forEach((message) => {
if (`key` in message) {
dataMayHaveChanged = [`insert`, `update`, `delete`].includes(
message.headers.action
message.headers.operation
)

switch (message.headers.action) {
switch (message.headers.operation) {
case `insert`:
this.data.set(message.key, message.value)
break
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type ControlMessage = {
export type ChangeMessage<T> = {
key: string
value: T
headers: Header & { action: `insert` | `update` | `delete` }
headers: Header & { operation: `insert` | `update` | `delete` }
offset: Offset
}

Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-client/test/support/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function forEachMessage<T extends Value>(
message as Message<T>,
messageIdx
)
if (`action` in message.headers) messageIdx++
if (`operation` in message.headers) messageIdx++
} catch (e) {
controller.abort()
return reject(e)
Expand Down
Loading