-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#### Migration notes ... - [x] The change comes with new or modified tests - [x] End-user documentation is updated to reflect the change - [ ] Hard-to-understand functions have explanatory comments
- Loading branch information
Showing
22 changed files
with
1,080 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# skip:start | ||
from typegraph import Graph, Policy, typegraph | ||
from typegraph.graph.params import Cors | ||
from typegraph.runtimes.kv import KvRuntime | ||
|
||
# skip:end | ||
|
||
|
||
@typegraph( | ||
# skip:start | ||
cors=Cors(allow_origin=["https://metatype.dev", "http://localhost:3000"]), | ||
# skip:end | ||
) | ||
def key_value(g: Graph): | ||
kv = KvRuntime("REDIS") | ||
|
||
g.expose( | ||
Policy.public(), | ||
get=kv.get(), | ||
set=kv.set(), | ||
delete=kv.delete(), | ||
keys=kv.keys(), | ||
values=kv.values(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 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( | ||
{ | ||
name: "key-value", | ||
// skip:next-line | ||
cors: { allowOrigin: ["https://metatype.dev", "http://localhost:3000"] }, | ||
}, | ||
(g) => { | ||
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); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 })]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ const list = [ | |
"index", | ||
"injections", | ||
"jwt", | ||
"kv", | ||
"math", | ||
"metagen-deno", | ||
"metagen-py", | ||
|
Oops, something went wrong.