Skip to content

Commit

Permalink
move getValuesInEntityChange to zod
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Feb 23, 2024
1 parent 5061bcf commit efd866b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
1 change: 0 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./src/extract-values.js";
export * as typebox from "./src/typebox.js";
export * as zod from "./src/zod.js";

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@substreams/sink-entity-changes",
"version": "0.3.7",
"version": "0.3.8",
"description": "Substreams Sink Entity Changes",
"type": "module",
"license": "MIT",
Expand Down
22 changes: 0 additions & 22 deletions src/extract-values.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/extract-values.test.ts → src/zod.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect, test } from "vitest";
import { getValuesInEntityChange } from "./extract-values.js";
import { EntityChange } from "./zod.js";
import { EntityChange, getValuesInEntityChange } from "./zod.js";

test("getValuesInEntityChange", () => {
const change: Partial<EntityChange> = {
Expand Down
21 changes: 21 additions & 0 deletions src/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,24 @@ export type Value = z.infer<typeof Value>;
export type Field = z.infer<typeof Field>;
export type EntityChange = z.infer<typeof EntityChange>;
export type EntityChanges = z.infer<typeof EntityChanges>;

export function getValuesInEntityChange(change: EntityChange) {
const values: Record<string, unknown> = {};

for (const field of change.fields) {
for (const [key, value] of Object.entries(field.newValue ?? {})) {
if (!value) {
continue;
}

if (key === "array") {
const { value: array } = value as { value: Record<string, unknown>[] };
values[field.name] = array.flatMap(Object.values);
} else {
values[field.name] = value;
}
}
}

return values;
}

0 comments on commit efd866b

Please sign in to comment.