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

feat: kv runtime #797

Merged
merged 24 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8bf1b57
impl: kv runtime
j03-dev Jul 19, 2024
9dde08f
feat: add kv runtime
j03-dev Jul 22, 2024
35eeb5c
feat: update kv runtime with host and port options
j03-dev Jul 30, 2024
bf4de1a
use redis instead of memory
j03-dev Jul 30, 2024
0930071
feat: update KvRuntimeData struct to use non-nullable host field
j03-dev Aug 1, 2024
e3773a8
feat: update KvRuntimeData struct to use non-nullable host field
j03-dev Aug 1, 2024
0ba1fa3
finish to implementing, python kvruntime
j03-dev Aug 1, 2024
d3a1f76
feat: add KvRuntime implementation for Python and TypeScript and add …
j03-dev Aug 5, 2024
622e211
finish implement test
j03-dev Aug 6, 2024
94b2458
finish add test for kv runtime
j03-dev Aug 7, 2024
eaf6d7f
add missing file
j03-dev Aug 7, 2024
9e03af6
change the args on constructor to url
j03-dev Aug 7, 2024
85b3ce4
set all to values
j03-dev Aug 7, 2024
d837511
add documentation for kv runtime
j03-dev Aug 9, 2024
ae62a57
just remove a space
j03-dev Aug 9, 2024
b21a0da
move kv typegrahs examples inside docs to playground
j03-dev Aug 10, 2024
9160863
add missing import on docs
j03-dev Aug 11, 2024
2f06e1f
fix website_test, and add table of support operation in the kv doc
j03-dev Aug 12, 2024
31ae81d
fix the table in kv docs
j03-dev Aug 12, 2024
dfa9ceb
:( why precommit why
j03-dev Aug 12, 2024
ca03682
finally :(
j03-dev Aug 12, 2024
1b615ca
update package.json and pnpm-lock file
j03-dev Aug 12, 2024
21dbe42
format lock file
j03-dev Aug 12, 2024
4ed8246
exclude kv typegraphs
j03-dev Aug 13, 2024
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
19 changes: 19 additions & 0 deletions examples/typegraphs/kv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# skip:start
from typegraph import Graph, Policy, typegraph
from typegraph.runtimes.kv import KvRuntime

# skip:end


@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(),
)
17 changes: 17 additions & 0 deletions examples/typegraphs/kv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// skip:start
import { Policy, typegraph } from "@typegraph/sdk/index.ts";
import { KvRuntime } from "@typegraph/sdk/runtimes/kv.ts";

// skip:end

export const tg = await typegraph("kv", (g: any) => {
const kv = new KvRuntime("REDIS");
const pub = Policy.public();
g.expose({
get: kv.get(),
set: kv.set(),
delete: kv.delete(),
keys: kv.keys(),
values: kv.values(),
}, pub);
});
2 changes: 1 addition & 1 deletion examples/typegraphs/metagen/rs/mdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Router {
}

pub fn init(&self, args: InitArgs) -> Result<InitResponse, InitError> {
static MT_VERSION: &str = "0.4.7-0";
static MT_VERSION: &str = "0.4.8-0";
if args.metatype_version != MT_VERSION {
return Err(InitError::VersionMismatch(MT_VERSION.into()));
}
Expand Down
9 changes: 9 additions & 0 deletions libs/common/src/typegraph/runtimes/kv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright Metatype OÜ, licensed under the Elastic License 2.0.
// SPDX-License-Identifier: Elastic-2.0

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct KvRuntimeData {
pub url: String,
}
4 changes: 4 additions & 0 deletions libs/common/src/typegraph/runtimes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
use self::deno::DenoRuntimeData;
use self::graphql::GraphQLRuntimeData;
use self::http::HTTPRuntimeData;
use self::kv::KvRuntimeData;
use self::prisma::PrismaRuntimeData;
use self::python::PythonRuntimeData;
use self::random::RandomRuntimeData;
Expand All @@ -19,6 +20,7 @@ use self::wasm::WasmRuntimeData;
pub mod deno;
pub mod graphql;
pub mod http;
pub mod kv;
pub mod prisma;
pub mod python;
pub mod random;
Expand Down Expand Up @@ -53,6 +55,7 @@ pub enum KnownRuntime {
WasmWire(WasmRuntimeData),
Typegate(TypegateRuntimeData),
Typegraph(TypegraphRuntimeData),
Kv(KvRuntimeData),
}

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
Expand Down Expand Up @@ -85,6 +88,7 @@ impl TGRuntime {
KnownRuntime::WasmReflected(_) => "wasm_reflected",
KnownRuntime::Typegate(_) => "typegate",
KnownRuntime::Typegraph(_) => "typegraph",
KnownRuntime::Kv(_) => "kv",
},
TGRuntime::Unknown(UnknownRuntime { name, .. }) => name,
}
Expand Down
89 changes: 89 additions & 0 deletions typegate/src/runtimes/kv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright Metatype OÜ, licensed under the Elastic License 2.0.
// SPDX-License-Identifier: Elastic-2.0

import { connect, parseURL, Redis } from "redis";
import { ComputeStage } from "../engine/query_engine.ts";
import { getLogger, Logger } from "../log.ts";
import { TypeGraph } from "../typegraph/mod.ts";
import { KvRuntimeData } from "../typegraph/types.ts";
import { Resolver, RuntimeInitParams } from "../types.ts";
import { registerRuntime } from "./mod.ts";
import { Runtime } from "./Runtime.ts";

const logger = getLogger(import.meta);

@registerRuntime("kv")
export class KvRuntime extends Runtime {
private logger: Logger;
private redis: Redis;

private constructor(typegraphName: string, redis: Redis) {
super(typegraphName);
this.logger = getLogger(`kv:'${typegraphName}'`);
this.redis = redis;
}

static async init(params: RuntimeInitParams): Promise<Runtime> {
logger.info("Initializing KvRuntime");
logger.debug(`Init params: ${JSON.stringify(params)}`);
const { typegraph, args, secretManager } = params as RuntimeInitParams<
KvRuntimeData
>;
const typegraphName = TypeGraph.formatName(typegraph);
const url = secretManager.secretOrFail(args.url);
const redisConnectionOption = parseURL(url);
const connection = await connect(redisConnectionOption);
const instance = new KvRuntime(typegraphName, connection);
instance.logger.info("Registered KvRuntime");

return instance;
}

// deno-lint-ignore require-await
async deinit(): Promise<void> {
this.redis.close();
}

materialize(
stage: ComputeStage,
_waitlist: ComputeStage[],
_verbose: boolean,
): ComputeStage[] | Promise<ComputeStage[]> {
const name = stage.props.materializer?.name;

const resolver: Resolver = async (args) => {
if (name == "kv_set") {
const { key, value } = args;
return await this.redis.set(key, value);
}

if (name == "kv_get") {
const { key } = args;
return await this.redis.get(key);
}

if (name == "kv_delete") {
const { key } = args;
return await this.redis.del(key);
}

if (name == "kv_keys") {
const { filter } = args;
return await this.redis.keys(filter ?? "*");
}

if (name === "kv_values") {
const { filter } = args;
const keys = await this.redis.keys(filter ?? "*");
const values = await Promise.all(
keys.map(async (key) => {
const value = await this.redis.get(key);
return value;
}),
);
return values;
}
};
return [new ComputeStage({ ...stage.props, resolver })];
}
}
1 change: 1 addition & 0 deletions typegate/src/runtimes/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ export async function init_runtimes(): Promise<void> {
import("./typegraph.ts"),
import("./wasm_wire.ts"),
import("./wasm_reflected.ts"),
import("./kv.ts"),
]);
}
4 changes: 4 additions & 0 deletions typegate/src/typegraph/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,7 @@ export interface WasiMatData {
func: string;
wasmArtifact: string;
}

export interface KvRuntimeData {
url: string;
}
1 change: 1 addition & 0 deletions typegate/tests/e2e/website/website_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const list = [
"index",
"injections",
"jwt",
"kv",
"math",
"metagen-deno",
"metagen-py",
Expand Down
Loading
Loading