Skip to content

Commit

Permalink
move kv typegrahs examples inside docs to playground
Browse files Browse the repository at this point in the history
  • Loading branch information
j03-dev committed Aug 11, 2024
1 parent ae62a57 commit b21a0da
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 44 deletions.
16 changes: 16 additions & 0 deletions examples/typegraphs/kv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typegraph import Graph, Policy, typegraph
from typegraph.runtimes.kv import KvRuntime


@typegraph()
def kv(g: Graph):
kv = KvRuntime("REDIS")

g.expose(
Policy.public(),
get=kv.get(),
set=kv.set(),
delete=kv.delete(),
keys=kv.keys(),
values=kv.values(),
)
14 changes: 14 additions & 0 deletions examples/typegraphs/kv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Policy, typegraph } from "@typegraph/sdk/index.ts";
import { KvRuntime } from "@typegraph/sdk/runtimes/kv.ts";

export const tg = await typegraph("kv", (g: any) => {
const kv = new KvRuntime("REDIS");
const pub = Policy.public();
g.expose({
get: kv.get().withPolicy(pub),
set: kv.set().withPolicy(pub),
delete: kv.delete().withPolicy(pub),
keys: kv.keys().withPolicy(pub),
values: kv.values().withPolicy(pub),
});
});
2 changes: 1 addition & 1 deletion website/docs/reference/runtimes/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This includes:
- [Random](./runtimes/random)
- [Temporal](./runtimes/temporal)
- [S3](./runtimes/s3)
- [Kv](./runtimes/kv)
- [KV](./runtimes/kv)

:::tip Missing your favorite runtime?
Submit your request and vote for your preferred ones [here](https://github.com/metatypedev/metatype/discussions/305).
Expand Down
50 changes: 7 additions & 43 deletions website/docs/reference/runtimes/kv/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,10 @@

The KvRuntime enables interaction with a Redis database by setting, retrieving, deleting, and managing keys and values.

### Python Example

In the following Python example, we use the typegraph to create a TypeGraph with a KvRuntime connected to a Redis database. The graph exposes public operations for managing key-value pairs in Redis.

```python
from typegraph import Graph, Policy, typegraph
from typegraph.runtimes.kv import KvRuntime


@typegraph()
def kv(g: Graph):
kv = KvRuntime("REDIS")

g.expose(
Policy.public(),
get=kv.get(),
set=kv.set(),
delete=kv.delete(),
keys=kv.keys(),
values=kv.values(),
)
```

### TypeScript Example

In the TypeScript example below, we achieve the same functionality using the @typegraph/sdk. The KvRuntime is connected to a Redis instance, and public policies are applied to the exposed key-value operations.

```typescript
import { Policy, typegraph } from "@typegraph/sdk/index.ts";
import { KvRuntime } from "@typegraph/sdk/runtimes/kv.ts";

export const tg = await typegraph("kv", (g: any) => {
const kv = new KvRuntime("REDIS");
const pub = Policy.public();
g.expose({
get: kv.get().withPolicy(pub),
set: kv.set().withPolicy(pub),
delete: kv.delete().withPolicy(pub),
keys: kv.keys().withPolicy(pub),
values: kv.values().withPolicy(pub),
});
});
```
<TGExample
typegraph="kv"
python={require("!!code-loader!../../../../../examples/typegraphs/kv.py")}
typescript={require("!!code-loader!../../../../../examples/typegraphs/kv.ts")}
disablePlayground={true}
query={{ content: "" }}
/>

0 comments on commit b21a0da

Please sign in to comment.