Skip to content

Commit

Permalink
feat(dynamodb)!: use UI table to display rows (#305)
Browse files Browse the repository at this point in the history
Also, removes the DynamoDB admin panel, which didn't work after any hot reload.
  • Loading branch information
skyrpex authored Aug 1, 2024
1 parent d04351d commit 26f339c
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 1,171 deletions.
19 changes: 0 additions & 19 deletions dynamodb/dbadmin.cjs

This file was deleted.

11 changes: 0 additions & 11 deletions dynamodb/dynamodb.extern.d.ts

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

21 changes: 0 additions & 21 deletions dynamodb/dynamodb.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -126,27 +126,6 @@ export const processRecordsAsync = async (
});
};

export const startDbAdmin = async (options) => {
const { endpoint, currentdir, homeEnv, pathEnv } = options;
const code = `
require("${currentdir}/dbadmin.cjs").startDbAdminProcess(${JSON.stringify({ endpoint })});
`
const child = spawn("node", ["-e", code], { env: { HOME: homeEnv, PATH: pathEnv }, cwd: process.cwd() });
const close = () => child.kill("SIGINT");
return new Promise((resolve) => {
child.stdout.on("data", (chunk) => {
const payload = chunk.toString();
if (payload.startsWith("port=")) {
resolve({
port: () => parseInt(payload.slice(5), 10),
close,
});
child.stdout.removeAllListeners("data");
}
});
});
}

export const dirname = () => {
return __dirname;
};
57 changes: 0 additions & 57 deletions dynamodb/dynamodb.sim.w
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
bring sim;
bring util;
bring cloud;
bring ui;
bring "./dynamodb-types.w" as dynamodb_types;
bring "./dynamodb-client.w" as dynamodb_client;

Expand All @@ -12,18 +11,6 @@ inflight interface Client {
inflight describeTable(input: Json): Json;
}

struct StartDbAdminOptions {
endpoint: str;
currentdir: str;
homeEnv: str;
pathEnv: str;
}

inflight interface StartDbAdminResponse {
inflight port(): num;
inflight close(): void;
}

class Util {
extern "./dynamodb.mjs" pub static inflight getPort(): num;
extern "./dynamodb.mjs" pub static inflight createClient(options: dynamodb_types.ClientConfig): Client;
Expand All @@ -33,49 +20,11 @@ class Util {
handler: inflight (dynamodb_types.StreamRecord): void,
options: dynamodb_types.StreamConsumerOptions?,
): void;
extern "./dynamodb.mjs" pub static inflight startDbAdmin(options: StartDbAdminOptions): StartDbAdminResponse;
extern "./dynamodb.mjs" pub static dirname(): str;
}

struct AdminUIProps {
endpoint: str;
}

class AdminUI {
pub endpoint: str;

new(props: AdminUIProps) {
let state = new sim.State();
let port = state.token("dbAdminPort");
this.endpoint = "http://localhost:{port}";

let currentdir = Util.dirname();
let homeEnv = util.tryEnv("HOME") ?? "";
let pathEnv = util.tryEnv("PATH") ?? "";

new cloud.Service(inflight () => {
try {
let res = Util.startDbAdmin(
endpoint: props.endpoint,
currentdir: currentdir,
homeEnv: homeEnv,
pathEnv: pathEnv
);
state.set("dbAdminPort", "{res.port()}");
return () => {
res.close();
};
} catch e {
log(e);
throw e;
}
});
}
}

class Host {
pub endpoint: str;
pub ui: AdminUI?;

new() {
let container = new sim.Container(
Expand All @@ -85,10 +34,6 @@ class Host {
);

this.endpoint = "http://localhost:{container.hostPort!}";

if !nodeof(this).app.isTestEnvironment {
this.ui = new AdminUI(endpoint: this.endpoint);
}
}

pub static of(scope: std.IResource): Host {
Expand All @@ -105,12 +50,10 @@ pub class Table_sim impl dynamodb_types.ITable {
host: Host;
pub tableName: str;
pub connection: dynamodb_types.Connection;
pub adminEndpoint: str?;

new(props: dynamodb_types.TableProps) {
this.host = Host.of(this);

this.adminEndpoint = this.host.ui?.endpoint;
let tableName = props.name ?? nodeof(this).addr;
let state = new sim.State();
this.tableName = state.token("tableName");
Expand Down
12 changes: 6 additions & 6 deletions dynamodb/dynamodb.w
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ pub class Table impl dynamodb_types.ITable {

pub connection: dynamodb_types.Connection;
pub tableName: str;
pub adminEndpoint: str?;

new(props: dynamodb_types.TableProps) {
let target = util.env("WING_TARGET");
if target == "sim" {
let sim = new dynamodb_sim.Table_sim(props);
this.connection = sim.connection;
this.tableName = sim.tableName;
this.adminEndpoint = sim.adminEndpoint;
this.implementation = sim;
nodeof(sim).hidden = true;
} elif target == "tf-aws" {
Expand All @@ -35,10 +33,12 @@ pub class Table impl dynamodb_types.ITable {
return this.tableName;
}) as "Table Name";

if let adminEndpoint = this.adminEndpoint {
new ui.Field("Admin", inflight () => {
return "{adminEndpoint}/tables/{this.tableName}";
}, link: true) as "Admin";
if target == "sim" {
new ui.Table(
scan: inflight () => {
return this.implementation.scan().Items;
},
) as "TableUIField";
}

nodeof(this).icon = "table-cells";
Expand Down
Loading

0 comments on commit 26f339c

Please sign in to comment.