@raideno/convex-kv 0.1.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.
- package/README.md +73 -0
- package/dist/index.d.ts +148 -0
- package/dist/server.js +2421 -0
- package/package.json +51 -0
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# Convex KV
|
|
2
|
+
|
|
3
|
+
Use a Convex table as a key-value store with optional expiration.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @raideno/convex-kv
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
`convex/schema.ts`
|
|
10
|
+
```ts
|
|
11
|
+
import { defineSchema } from "convex/server";
|
|
12
|
+
import { kvTables } from "@raideno/convex-kv/server";
|
|
13
|
+
|
|
14
|
+
export default defineSchema({
|
|
15
|
+
...kvTables,
|
|
16
|
+
/*
|
|
17
|
+
* Your app tables...
|
|
18
|
+
*/
|
|
19
|
+
});
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
`convex/kv.ts`
|
|
23
|
+
```ts
|
|
24
|
+
import { internalConvexKv } from "@raideno/convex-kv/server";
|
|
25
|
+
|
|
26
|
+
export const { store, kv } = internalConvexKv();
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`convex/actions.ts`
|
|
30
|
+
```ts
|
|
31
|
+
import { action } from "./_generated/server";
|
|
32
|
+
import { v } from "convex/values";
|
|
33
|
+
|
|
34
|
+
import { kv } from "./kv";
|
|
35
|
+
|
|
36
|
+
export const save = action({
|
|
37
|
+
args: {
|
|
38
|
+
key: v.string(),
|
|
39
|
+
value: v.any(),
|
|
40
|
+
ttlMs: v.optional(v.number()),
|
|
41
|
+
},
|
|
42
|
+
handler: async (context, args) => {
|
|
43
|
+
await kv.setWithExpiration(context, {
|
|
44
|
+
key: args.key,
|
|
45
|
+
value: args.value,
|
|
46
|
+
expiresInMs: args.ttlMs,
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
export const load = action({
|
|
52
|
+
args: {
|
|
53
|
+
key: v.string(),
|
|
54
|
+
},
|
|
55
|
+
handler: async (context, args) => {
|
|
56
|
+
return await kv.getOrDefault(context, {
|
|
57
|
+
key: args.key,
|
|
58
|
+
defaultValue: "not_found",
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## API
|
|
65
|
+
|
|
66
|
+
- `kv.set(context, { key, value })`
|
|
67
|
+
- `kv.setWithExpiration(context, { key, value, expiresInMs? | expiresAt? })`
|
|
68
|
+
- `kv.get(context, { key })`
|
|
69
|
+
- `kv.getOrDefault(context, { key, defaultValue })`
|
|
70
|
+
- `kv.has(context, { key })`
|
|
71
|
+
- `kv.delete(context, { key })`
|
|
72
|
+
|
|
73
|
+
Expired keys are treated as missing and cleaned up lazily on reads.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { AnyDataModel } from 'convex/server';
|
|
2
|
+
import { GenericActionCtx } from 'convex/server';
|
|
3
|
+
import { GenericMutationCtx } from 'convex/server';
|
|
4
|
+
import { RegisteredMutation } from 'convex/server';
|
|
5
|
+
import { TableDefinition } from 'convex/server';
|
|
6
|
+
import { VAny } from 'convex/values';
|
|
7
|
+
import { VFloat64 } from 'convex/values';
|
|
8
|
+
import { VNull } from 'convex/values';
|
|
9
|
+
import { VObject } from 'convex/values';
|
|
10
|
+
import { VString } from 'convex/values';
|
|
11
|
+
import { VUnion } from 'convex/values';
|
|
12
|
+
|
|
13
|
+
declare type DeleteArgs = {
|
|
14
|
+
key: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
declare const DeleteImplementation: (context: GenericActionCtx<AnyDataModel> | GenericMutationCtx<AnyDataModel>, args: DeleteArgs, execution: Execution_2, configuration: InternalConfiguration, options: InternalOptions) => Promise<boolean>;
|
|
18
|
+
|
|
19
|
+
declare type Execution = {
|
|
20
|
+
blocking: boolean;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
declare type Execution_2 = {
|
|
24
|
+
blocking: boolean;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
declare type GetArgs = {
|
|
28
|
+
key: string;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
declare const GetImplementation: (context: GenericActionCtx<AnyDataModel> | GenericMutationCtx<AnyDataModel>, args: GetArgs, configuration: InternalConfiguration, options: InternalOptions) => Promise<any | null>;
|
|
32
|
+
|
|
33
|
+
declare const HasImplementation: (context: GenericActionCtx<AnyDataModel> | GenericMutationCtx<AnyDataModel>, args: GetArgs, configuration: InternalConfiguration, options: InternalOptions) => Promise<boolean>;
|
|
34
|
+
|
|
35
|
+
export declare type InputConfiguration = WithOptional<InternalConfiguration, "execution">;
|
|
36
|
+
|
|
37
|
+
export declare type InputOptions = WithOptional<InternalOptions, "store" | "debug" | "logger" | "base">;
|
|
38
|
+
|
|
39
|
+
declare interface InternalConfiguration {
|
|
40
|
+
execution: Execution;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export declare const internalConvexKv: (configuration_?: InputConfiguration, options_?: InputOptions) => {
|
|
44
|
+
kv: {
|
|
45
|
+
/**
|
|
46
|
+
* Stores a value for the provided key.
|
|
47
|
+
* @param context the context, can be either an action or mutation context.
|
|
48
|
+
* @param args the arguments for the set function.
|
|
49
|
+
* @param execution execution options.
|
|
50
|
+
* @returns void
|
|
51
|
+
*/
|
|
52
|
+
set: (context: GenericActionCtx<AnyDataModel> | GenericMutationCtx<AnyDataModel>, args: Parameters<typeof SetImplementation>[1], execution?: {
|
|
53
|
+
blocking?: boolean;
|
|
54
|
+
}) => Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Stores a value with expiration metadata.
|
|
57
|
+
* @param context the context, can be either an action or mutation context.
|
|
58
|
+
* @param args the arguments for the setWithExpiration function.
|
|
59
|
+
* @param execution execution options.
|
|
60
|
+
* @returns void
|
|
61
|
+
*/
|
|
62
|
+
setWithExpiration: (context: GenericActionCtx<AnyDataModel> | GenericMutationCtx<AnyDataModel>, args: {
|
|
63
|
+
key: string;
|
|
64
|
+
value: any;
|
|
65
|
+
expiresAt?: number | null;
|
|
66
|
+
expiresInMs?: number | null;
|
|
67
|
+
}, execution?: {
|
|
68
|
+
blocking?: boolean;
|
|
69
|
+
}) => Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* Retrieves a value by key.
|
|
72
|
+
* Expired values are automatically treated as missing.
|
|
73
|
+
*/
|
|
74
|
+
get: (context: GenericActionCtx<AnyDataModel> | GenericMutationCtx<AnyDataModel>, args: Parameters<typeof GetImplementation>[1]) => Promise<any>;
|
|
75
|
+
/**
|
|
76
|
+
* Retrieves a value by key and falls back to defaultValue if missing.
|
|
77
|
+
*/
|
|
78
|
+
getOrDefault: <T = any>(context: GenericActionCtx<AnyDataModel> | GenericMutationCtx<AnyDataModel>, args: {
|
|
79
|
+
key: string;
|
|
80
|
+
defaultValue: T;
|
|
81
|
+
}) => Promise<T>;
|
|
82
|
+
/**
|
|
83
|
+
* Checks whether a non-expired value exists for a key.
|
|
84
|
+
*/
|
|
85
|
+
has: (context: GenericActionCtx<AnyDataModel> | GenericMutationCtx<AnyDataModel>, args: Parameters<typeof HasImplementation>[1]) => Promise<boolean>;
|
|
86
|
+
/**
|
|
87
|
+
* Deletes a key from the store.
|
|
88
|
+
* @returns true when at least one entry was deleted.
|
|
89
|
+
*/
|
|
90
|
+
delete: (context: GenericActionCtx<AnyDataModel> | GenericMutationCtx<AnyDataModel>, args: Parameters<typeof DeleteImplementation>[1], execution?: {
|
|
91
|
+
blocking?: boolean;
|
|
92
|
+
}) => Promise<boolean>;
|
|
93
|
+
};
|
|
94
|
+
store: RegisteredMutation<"internal", {
|
|
95
|
+
operation: string;
|
|
96
|
+
args: any;
|
|
97
|
+
}, Promise<boolean | void | {
|
|
98
|
+
found: boolean;
|
|
99
|
+
value: any;
|
|
100
|
+
expiresAt: number | null;
|
|
101
|
+
}>>;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
declare interface InternalOptions {
|
|
105
|
+
store: string;
|
|
106
|
+
debug: boolean;
|
|
107
|
+
logger: Logger;
|
|
108
|
+
base: string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export declare const kvTables: {
|
|
112
|
+
kvEntries: TableDefinition<VObject< {
|
|
113
|
+
key: string;
|
|
114
|
+
value: any;
|
|
115
|
+
expiresAt: number | null;
|
|
116
|
+
updatedAt: number;
|
|
117
|
+
}, {
|
|
118
|
+
key: VString<string, "required">;
|
|
119
|
+
value: VAny<any, "required", string>;
|
|
120
|
+
expiresAt: VUnion<number | null, [VNull<null, "required">, VFloat64<number, "required">], "required", never>;
|
|
121
|
+
updatedAt: VFloat64<number, "required">;
|
|
122
|
+
}, "required", "key" | "value" | "expiresAt" | "updatedAt" | `value.${string}`>, {
|
|
123
|
+
byKey: ["key", "_creationTime"];
|
|
124
|
+
byExpiresAt: ["expiresAt", "_creationTime"];
|
|
125
|
+
}, {}, {}>;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
export declare class Logger {
|
|
129
|
+
debug_: boolean;
|
|
130
|
+
constructor(debug: boolean);
|
|
131
|
+
log(message: string): void;
|
|
132
|
+
error(message: string): void;
|
|
133
|
+
warn(message: string): void;
|
|
134
|
+
debug(message: string): void;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
declare type SetArgs = {
|
|
138
|
+
key: string;
|
|
139
|
+
value: any;
|
|
140
|
+
expiresAt?: number | null;
|
|
141
|
+
expiresInMs?: number | null;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
declare const SetImplementation: (context: GenericActionCtx<AnyDataModel> | GenericMutationCtx<AnyDataModel>, args: SetArgs, execution: Execution_2, configuration: InternalConfiguration, options: InternalOptions) => Promise<void>;
|
|
145
|
+
|
|
146
|
+
declare type WithOptional<T, K extends keyof T = never> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
147
|
+
|
|
148
|
+
export { }
|