Skip to content

Commit

Permalink
add some TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
tinyzimmer committed Nov 3, 2023
1 parent e6e4566 commit 59383f8
Show file tree
Hide file tree
Showing 173 changed files with 1,255 additions and 1,217 deletions.
120 changes: 67 additions & 53 deletions cmd/tsutil-gen/templates/ts-rpcdb.ts.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,28 @@

{{ $templateName := .TemplateName -}}
{{ $typesRoot := .Spec.TypesPath -}}
import { PromiseClient } from "@connectrpc/connect";
import { PromiseClient, StreamResponse, CallOptions } from "@connectrpc/connect";
import { AppDaemon } from "{{ $typesRoot }}/app_connect.js";
{{ range $file, $types := .Spec.Imports -}}
import { {{ join $types }} } from "{{ $typesRoot }}/{{ $file }}.js";
{{ end -}}
import {
QueryRequest_QueryCommand,
QueryRequest_QueryType,
QueryRequest,
QueryResponse
} from "{{ $typesRoot }}/storage_query_pb.js";

/**
* AppDaemonClient is the interface for working with an AppDaemon over gRPC.
*
* @generated From {{ $templateName }}
*/
export type AppDaemonClient = PromiseClient<typeof AppDaemon>;

/**
* TODO: Allow the below interfaces to be constructed with a plugin query stream as well.
*/
{{ range .Spec.Interfaces }}
{{- $name := .Name }}
{{- $queryType := .QueryType }}
Expand All @@ -40,31 +51,49 @@ export class {{ $name }}s {
* @param client - The client to use for RPC calls.
* @param connID - The connection ID to use for RPC calls.
*/
constructor(private readonly client: PromiseClient<typeof AppDaemon>, private readonly connID: string) {
}
constructor(
private readonly client: AppDaemonClient,
private readonly connID: string
) {}

/**
* Returns the {{ $name }} with the given {{ andJoin .Identifiers }}.
* Queries the AppDaemon for {{ $name }}s.
*
{{ range $id, $description := .Identifiers -}}
* @param {{ $id }} - {{ $description }}.
{{ end -}}
* @returns The {{ $name }} with the given {{ andJoin .Identifiers }}.
* @param query - The query to run.
* @returns The results of the query.
*/
get({{ stringParams .Identifiers }}): Promise<{{ $name }}> {
private query(query: QueryRequest): Promise<QueryResponse> {
return new Promise((resolve, reject) => {
this.client.query({
id: this.connID,
query: {
command: QueryRequest_QueryCommand.GET,
type: QueryRequest_QueryType.{{ $queryType }},
query: `{{ queryString .Identifiers }}`,
}
query: query
}).then((res: QueryResponse) => {
if (res.error.length > 0) {
reject(new Error(res.error))
return
}
resolve(res)
}).catch((err: Error) => {
reject(err)
})
});
}

/**
* Returns the {{ $name }} with the given {{ andJoin .Identifiers }}.
*
{{ range $id, $description := .Identifiers -}}
* @param {{ $id }} - {{ $description }}.
{{ end -}}
* @returns The {{ $name }} with the given {{ andJoin .Identifiers }}.
*/
get({{ stringParams .Identifiers }}): Promise<{{ $name }}> {
return new Promise((resolve, reject) => {
this.query(new QueryRequest({
command: QueryRequest_QueryCommand.GET,
type: QueryRequest_QueryType.{{ $queryType }},
query: `{{ queryString .Identifiers }}`,
})).then((res: QueryResponse) => {
if (res.items.length == 0) {
reject(new Error("{{ $name }} not found"))
return
Expand All @@ -84,14 +113,11 @@ export class {{ $name }}s {
*/
getBy{{ title $id }}({{ stringParams $id }}): Promise<{{ $name }}> {
return new Promise((resolve, reject) => {
this.client.query({
id: this.connID,
query: {
command: QueryRequest_QueryCommand.GET,
type: QueryRequest_QueryType.{{ $queryType }},
query: `{{ queryString $id }}`,
}
}).then((res: QueryResponse) => {
this.query(new QueryRequest({
command: QueryRequest_QueryCommand.GET,
type: QueryRequest_QueryType.{{ $queryType }},
query: `{{ queryString $id }}`,
})).then((res: QueryResponse) => {
if (res.error.length > 0) {
reject(new Error(res.error))
return
Expand All @@ -116,14 +142,11 @@ export class {{ $name }}s {
*/
delete({{ stringParams .Identifiers }}): Promise<void> {
return new Promise((resolve, reject) => {
this.client.query({
id: this.connID,
query: {
command: QueryRequest_QueryCommand.DELETE,
type: QueryRequest_QueryType.{{ $queryType }},
query: `{{ queryString .Identifiers }}`,
}
}).then((res: QueryResponse) => {
this.query(new QueryRequest({
command: QueryRequest_QueryCommand.DELETE,
type: QueryRequest_QueryType.{{ $queryType }},
query: `{{ queryString .Identifiers }}`,
})).then((res: QueryResponse) => {
if (res.error.length > 0) {
reject(new Error(res.error))
return
Expand All @@ -142,13 +165,10 @@ export class {{ $name }}s {
*/
list(): Promise<{{ $name }}[]> {
return new Promise((resolve, reject) => {
this.client.query({
id: this.connID,
query: {
command: QueryRequest_QueryCommand.LIST,
type: QueryRequest_QueryType.{{ $queryType }},
}
}).then((res: QueryResponse) => {
this.query(new QueryRequest({
command: QueryRequest_QueryCommand.LIST,
type: QueryRequest_QueryType.{{ $queryType }},
})).then((res: QueryResponse) => {
if (res.error.length > 0) {
reject(new Error(res.error))
return
Expand All @@ -168,14 +188,11 @@ export class {{ $name }}s {
*/
listBy{{ title $id }}({{ stringParams $id }}): Promise<{{ $name }}[]> {
return new Promise((resolve, reject) => {
this.client.query({
id: this.connID,
query: {
command: QueryRequest_QueryCommand.LIST,
type: QueryRequest_QueryType.{{ $queryType }},
query: `{{ queryString $id }}`,
}
}).then((res: QueryResponse) => {
this.query(new QueryRequest({
command: QueryRequest_QueryCommand.LIST,
type: QueryRequest_QueryType.{{ $queryType }},
query: `{{ queryString $id }}`,
})).then((res: QueryResponse) => {
if (res.error.length > 0) {
reject(new Error(res.error))
return
Expand All @@ -195,14 +212,11 @@ export class {{ $name }}s {
put(obj: {{ $name }}): Promise<void> {
return new Promise((resolve, reject) => {
const enc = new TextEncoder();
this.client.query({
id: this.connID,
query: {
command: QueryRequest_QueryCommand.PUT,
type: QueryRequest_QueryType.{{ $queryType }},
item: enc.encode(obj.toJsonString()),
}
}).then((res: QueryResponse) => {
this.query(new QueryRequest({
command: QueryRequest_QueryCommand.PUT,
type: QueryRequest_QueryType.{{ $queryType }},
item: enc.encode(obj.toJsonString()),
})).then((res: QueryResponse) => {
if (res.error.length > 0) {
reject(new Error(res.error))
return
Expand Down
2 changes: 1 addition & 1 deletion docs/assets/navigation.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/assets/search.js

Large diffs are not rendered by default.

Loading

0 comments on commit 59383f8

Please sign in to comment.