|
| 1 | +--- |
| 2 | +title: "Zod Bird" |
| 3 | +description: Zodbird is an e2e typed tinybird.co client library for typescript, leveraging zod for type safety and transformations |
| 4 | +repository: chronark/zod-bird |
| 5 | +date: "2023-05-21" |
| 6 | +published: true |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +- typesafe |
| 11 | +- handles building the url params for you |
| 12 | +- easy transformation of resulting data |
| 13 | +- built in cache directives for nextjs |
| 14 | +- built in retry logic for ratelimited requests |
| 15 | + |
| 16 | + |
| 17 | +```ts |
| 18 | +import { Tinybird } from "@chronark/zod-bird"; |
| 19 | +import { z } from "zod"; |
| 20 | + |
| 21 | +const tb = new Tinybird({ token: "token" }); |
| 22 | + |
| 23 | +export const getEvents = tb.buildPipe({ |
| 24 | + pipe: "get_events__v1", |
| 25 | + parameters: z.object({ |
| 26 | + tenantId: z.string(), |
| 27 | + }), |
| 28 | + data: z.object({ |
| 29 | + event: z.string(), |
| 30 | + time: z.number().transform((t) => new Date(t)), |
| 31 | + }), |
| 32 | +}); |
| 33 | + |
| 34 | + |
| 35 | +const res = await getEvents({ tenantId: "chronark" }) |
| 36 | + |
| 37 | +// res.data = {event: string, time: Date}[] |
| 38 | +``` |
| 39 | + |
| 40 | +## Install |
| 41 | + |
| 42 | +``` |
| 43 | +npm i @chronark/zod-bird |
| 44 | +``` |
| 45 | + |
| 46 | + |
| 47 | +## Ingesting Data |
| 48 | + |
| 49 | +```ts |
| 50 | +// lib/tinybird.ts |
| 51 | +import { Tinybird } from "./client"; |
| 52 | +import { z } from "zod"; |
| 53 | + |
| 54 | +const tb = new Tinybird({ token: process.env.TINYBIRD_TOKEN! }); |
| 55 | + |
| 56 | +export const publishEvent = tb.buildIngestEndpoint({ |
| 57 | + datasource: "events__v1", |
| 58 | + event: z.object({ |
| 59 | + id: z.string(), |
| 60 | + tenantId: z.string(), |
| 61 | + channelId: z.string(), |
| 62 | + time: z.number().int(), |
| 63 | + event: z.string(), |
| 64 | + content: z.string().optional().default(""), |
| 65 | + metadata: z.string().optional().default(""), |
| 66 | + }), |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +```ts |
| 71 | +// somewhere.ts |
| 72 | +import { publishEvent } from "./lib/tinybird"; |
| 73 | + |
| 74 | +await publishEvent({ |
| 75 | + id: "1", |
| 76 | + tenantId: "1", |
| 77 | + channelId: "1", |
| 78 | + time: Date.now(), |
| 79 | + event: "test", |
| 80 | + content: "test", |
| 81 | + metadata: JSON.stringify({ test: "test" }), |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +## Querying Endpoints |
| 88 | + |
| 89 | +```ts |
| 90 | +// lib/tinybird.ts |
| 91 | +import { Tinybird } from "./client"; |
| 92 | +import { z } from "zod"; |
| 93 | + |
| 94 | +const tb = new Tinybird({ token: process.env.TINYBIRD_TOKEN! }); |
| 95 | + |
| 96 | +export const getChannelActivity = tb.buildPipe({ |
| 97 | + pipe: "get_channel_activity__v1", |
| 98 | + parameters: z.object({ |
| 99 | + tenantId: z.string(), |
| 100 | + channelId: z.string().optional(), |
| 101 | + start: z.number(), |
| 102 | + end: z.number().optional(), |
| 103 | + granularity: z.enum(["1m", "1h", "1d", "1w", "1M"]), |
| 104 | + }), |
| 105 | + data: z.object({ |
| 106 | + time: z.string().transform((t) => new Date(t).getTime()), |
| 107 | + count: z |
| 108 | + .number() |
| 109 | + .nullable() |
| 110 | + .optional() |
| 111 | + .transform((v) => (typeof v === "number" ? v : 0)), |
| 112 | + }), |
| 113 | +}); |
| 114 | +``` |
| 115 | + |
| 116 | +```ts |
| 117 | +// somewhere.ts |
| 118 | +import { getChannelActivity } from "./lib/tinybird"; |
| 119 | + |
| 120 | + |
| 121 | +const res = await getChannelActivity({ |
| 122 | + tenantId: "1", |
| 123 | + channelId: "1", |
| 124 | + start: 123, |
| 125 | + end: 1234, |
| 126 | + granularity: "1h" |
| 127 | +}) |
| 128 | + |
| 129 | +``` |
| 130 | +`res` is the response from the tinybird endpoint, but now fully typed and the data has been parsed according to the schema defined in `data`. |
| 131 | + |
| 132 | +## Advanced |
| 133 | + |
| 134 | +### Caching |
| 135 | + |
| 136 | +You can add cache directives to each pipe. |
| 137 | + |
| 138 | + |
| 139 | +#### Disable caching (useful in Next.js where everything is cached by default) |
| 140 | + |
| 141 | +```ts |
| 142 | +tb.buildPipe({ |
| 143 | + pipe: "some_pipe__v1", |
| 144 | + parameters: z.object({ |
| 145 | + hello: z.string() |
| 146 | + }), |
| 147 | + data: z.object({ |
| 148 | + response: z.string() |
| 149 | + }), |
| 150 | + opts: { |
| 151 | + cache: "no-store" // <-------- Add this |
| 152 | + }; |
| 153 | +}); |
| 154 | + |
| 155 | +``` |
| 156 | + |
| 157 | +#### Cache for 15 minutes |
| 158 | + |
| 159 | +```ts |
| 160 | + |
| 161 | +tb.buildPipe({ |
| 162 | + pipe: "some_pipe__v1", |
| 163 | + parameters: z.object({ |
| 164 | + hello: z.string() |
| 165 | + }), |
| 166 | + data: z.object({ |
| 167 | + response: z.string() |
| 168 | + }), |
| 169 | + opts: { |
| 170 | + revalidate: 900 ;// <-------- Add this |
| 171 | + }; |
| 172 | +}); |
0 commit comments