@fatagnus/remote-cmd-relay-convex 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,96 @@
1
+ import { v } from "convex/values";
2
+ import { query, mutation } from "./_generated/server";
3
+ import { configPushTypeValidator } from "./schema";
4
+
5
+ /**
6
+ * Queue a config push for a relay
7
+ */
8
+ export const queue = mutation({
9
+ args: {
10
+ relayId: v.string(),
11
+ pushType: v.string(),
12
+ payload: v.string(),
13
+ },
14
+ returns: v.id("configPushQueue"),
15
+ handler: async (ctx, args) => {
16
+ const now = Date.now();
17
+ // Map string pushType to valid enum values
18
+ const validPushTypes = ["credential", "ssh_targets", "allowed_commands", "metrics_interval"] as const;
19
+ const pushType = validPushTypes.includes(args.pushType as typeof validPushTypes[number])
20
+ ? (args.pushType as typeof validPushTypes[number])
21
+ : "credential"; // Default to credential if invalid
22
+
23
+ return await ctx.db.insert("configPushQueue", {
24
+ relayId: args.relayId,
25
+ pushType,
26
+ payload: args.payload,
27
+ status: "pending",
28
+ createdBy: "system", // Required by schema
29
+ createdAt: now,
30
+ });
31
+ },
32
+ });
33
+
34
+ /**
35
+ * Get a config push by ID
36
+ */
37
+ export const get = query({
38
+ args: {
39
+ id: v.id("configPushQueue"),
40
+ },
41
+ returns: v.union(
42
+ v.object({
43
+ _id: v.id("configPushQueue"),
44
+ relayId: v.string(),
45
+ pushType: v.string(),
46
+ payload: v.string(),
47
+ status: v.string(),
48
+ createdAt: v.number(),
49
+ ackedAt: v.optional(v.number()),
50
+ errorMessage: v.optional(v.string()),
51
+ }),
52
+ v.null()
53
+ ),
54
+ handler: async (ctx, args) => {
55
+ const push = await ctx.db.get(args.id);
56
+ if (!push) return null;
57
+ return {
58
+ _id: push._id,
59
+ relayId: push.relayId,
60
+ pushType: push.pushType,
61
+ payload: push.payload,
62
+ status: push.status,
63
+ createdAt: push.createdAt,
64
+ ackedAt: push.ackedAt,
65
+ errorMessage: push.errorMessage,
66
+ };
67
+ },
68
+ });
69
+
70
+ /**
71
+ * List all config pushes
72
+ */
73
+ export const listAll = query({
74
+ args: {},
75
+ returns: v.array(
76
+ v.object({
77
+ _id: v.id("configPushQueue"),
78
+ relayId: v.string(),
79
+ pushType: v.string(),
80
+ payload: v.string(),
81
+ status: v.string(),
82
+ createdAt: v.number(),
83
+ })
84
+ ),
85
+ handler: async (ctx) => {
86
+ const pushes = await ctx.db.query("configPushQueue").collect();
87
+ return pushes.map((p) => ({
88
+ _id: p._id,
89
+ relayId: p.relayId,
90
+ pushType: p.pushType,
91
+ payload: p.payload,
92
+ status: p.status,
93
+ createdAt: p.createdAt,
94
+ }));
95
+ },
96
+ });
@@ -0,0 +1,5 @@
1
+ import { defineComponent } from "convex/server";
2
+
3
+ const component = defineComponent("remoteCmdRelay");
4
+
5
+ export default component;
@@ -0,0 +1,112 @@
1
+ import { v } from "convex/values";
2
+ import { query, mutation } from "./_generated/server";
3
+ import { credentialTypeValidator, storageModeValidator } from "./schema";
4
+
5
+ /**
6
+ * Create a shared credential
7
+ */
8
+ export const create = mutation({
9
+ args: {
10
+ name: v.string(),
11
+ credentialType: credentialTypeValidator,
12
+ encryptedValue: v.string(),
13
+ assignedRelays: v.array(v.string()),
14
+ targetHost: v.optional(v.string()),
15
+ },
16
+ returns: v.id("sharedCredentials"),
17
+ handler: async (ctx, args) => {
18
+ const now = Date.now();
19
+ return await ctx.db.insert("sharedCredentials", {
20
+ name: args.name,
21
+ credentialType: args.credentialType,
22
+ encryptedValue: args.encryptedValue,
23
+ assignedRelays: args.assignedRelays,
24
+ targetHost: args.targetHost,
25
+ createdBy: "system", // Required by schema
26
+ createdAt: now,
27
+ updatedAt: now,
28
+ });
29
+ },
30
+ });
31
+
32
+ /**
33
+ * List credential inventory by relay ID
34
+ */
35
+ export const listByRelay = query({
36
+ args: {
37
+ relayId: v.string(),
38
+ },
39
+ returns: v.array(
40
+ v.object({
41
+ _id: v.id("relayCredentialInventory"),
42
+ relayId: v.string(),
43
+ credentialName: v.string(),
44
+ credentialType: credentialTypeValidator,
45
+ targetHost: v.optional(v.string()),
46
+ storageMode: storageModeValidator,
47
+ lastUpdatedAt: v.number(),
48
+ reportedAt: v.number(),
49
+ })
50
+ ),
51
+ handler: async (ctx, args) => {
52
+ const inventory = await ctx.db
53
+ .query("relayCredentialInventory")
54
+ .withIndex("by_relayId", (q) => q.eq("relayId", args.relayId))
55
+ .collect();
56
+
57
+ return inventory.map((c) => ({
58
+ _id: c._id,
59
+ relayId: c.relayId,
60
+ credentialName: c.credentialName,
61
+ credentialType: c.credentialType,
62
+ targetHost: c.targetHost,
63
+ storageMode: c.storageMode,
64
+ lastUpdatedAt: c.lastUpdatedAt,
65
+ reportedAt: c.reportedAt,
66
+ }));
67
+ },
68
+ });
69
+
70
+ /**
71
+ * List all shared credentials
72
+ */
73
+ export const listAll = query({
74
+ args: {},
75
+ returns: v.array(
76
+ v.object({
77
+ _id: v.id("sharedCredentials"),
78
+ name: v.string(),
79
+ credentialType: credentialTypeValidator,
80
+ assignedRelays: v.array(v.string()),
81
+ targetHost: v.optional(v.string()),
82
+ createdAt: v.number(),
83
+ updatedAt: v.number(),
84
+ })
85
+ ),
86
+ handler: async (ctx) => {
87
+ const creds = await ctx.db.query("sharedCredentials").collect();
88
+ return creds.map((c) => ({
89
+ _id: c._id,
90
+ name: c.name,
91
+ credentialType: c.credentialType,
92
+ assignedRelays: c.assignedRelays,
93
+ targetHost: c.targetHost,
94
+ createdAt: c.createdAt,
95
+ updatedAt: c.updatedAt,
96
+ }));
97
+ },
98
+ });
99
+
100
+ /**
101
+ * Delete a shared credential
102
+ */
103
+ export const remove = mutation({
104
+ args: {
105
+ id: v.id("sharedCredentials"),
106
+ },
107
+ returns: v.null(),
108
+ handler: async (ctx, args) => {
109
+ await ctx.db.delete(args.id);
110
+ return null;
111
+ },
112
+ });