@convex-dev/sharded-counter 0.1.1

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.
Files changed (63) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +246 -0
  3. package/dist/commonjs/client/index.d.ts +37 -0
  4. package/dist/commonjs/client/index.d.ts.map +1 -0
  5. package/dist/commonjs/client/index.js +30 -0
  6. package/dist/commonjs/client/index.js.map +1 -0
  7. package/dist/commonjs/component/_generated/api.d.ts +14 -0
  8. package/dist/commonjs/component/_generated/api.d.ts.map +1 -0
  9. package/dist/commonjs/component/_generated/api.js +24 -0
  10. package/dist/commonjs/component/_generated/api.js.map +1 -0
  11. package/dist/commonjs/component/_generated/server.d.ts +64 -0
  12. package/dist/commonjs/component/_generated/server.d.ts.map +1 -0
  13. package/dist/commonjs/component/_generated/server.js +76 -0
  14. package/dist/commonjs/component/_generated/server.js.map +1 -0
  15. package/dist/commonjs/component/convex.config.d.ts +3 -0
  16. package/dist/commonjs/component/convex.config.d.ts.map +1 -0
  17. package/dist/commonjs/component/convex.config.js +3 -0
  18. package/dist/commonjs/component/convex.config.js.map +1 -0
  19. package/dist/commonjs/component/public.d.ts +10 -0
  20. package/dist/commonjs/component/public.d.ts.map +1 -0
  21. package/dist/commonjs/component/public.js +42 -0
  22. package/dist/commonjs/component/public.js.map +1 -0
  23. package/dist/commonjs/component/schema.d.ts +15 -0
  24. package/dist/commonjs/component/schema.d.ts.map +1 -0
  25. package/dist/commonjs/component/schema.js +10 -0
  26. package/dist/commonjs/component/schema.js.map +1 -0
  27. package/dist/commonjs/package.json +3 -0
  28. package/dist/esm/client/index.d.ts +37 -0
  29. package/dist/esm/client/index.d.ts.map +1 -0
  30. package/dist/esm/client/index.js +30 -0
  31. package/dist/esm/client/index.js.map +1 -0
  32. package/dist/esm/component/_generated/api.d.ts +14 -0
  33. package/dist/esm/component/_generated/api.d.ts.map +1 -0
  34. package/dist/esm/component/_generated/api.js +24 -0
  35. package/dist/esm/component/_generated/api.js.map +1 -0
  36. package/dist/esm/component/_generated/server.d.ts +64 -0
  37. package/dist/esm/component/_generated/server.d.ts.map +1 -0
  38. package/dist/esm/component/_generated/server.js +76 -0
  39. package/dist/esm/component/_generated/server.js.map +1 -0
  40. package/dist/esm/component/convex.config.d.ts +3 -0
  41. package/dist/esm/component/convex.config.d.ts.map +1 -0
  42. package/dist/esm/component/convex.config.js +3 -0
  43. package/dist/esm/component/convex.config.js.map +1 -0
  44. package/dist/esm/component/public.d.ts +10 -0
  45. package/dist/esm/component/public.d.ts.map +1 -0
  46. package/dist/esm/component/public.js +42 -0
  47. package/dist/esm/component/public.js.map +1 -0
  48. package/dist/esm/component/schema.d.ts +15 -0
  49. package/dist/esm/component/schema.d.ts.map +1 -0
  50. package/dist/esm/component/schema.js +10 -0
  51. package/dist/esm/component/schema.js.map +1 -0
  52. package/dist/esm/package.json +3 -0
  53. package/package.json +76 -0
  54. package/src/client/index.ts +82 -0
  55. package/src/component/_generated/api.d.ts +58 -0
  56. package/src/component/_generated/api.js +27 -0
  57. package/src/component/_generated/dataModel.d.ts +64 -0
  58. package/src/component/_generated/server.d.ts +153 -0
  59. package/src/component/_generated/server.js +94 -0
  60. package/src/component/convex.config.ts +3 -0
  61. package/src/component/counter.test.ts +45 -0
  62. package/src/component/public.ts +43 -0
  63. package/src/component/schema.ts +10 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,246 @@
1
+ # Convex Sharded Counter Component
2
+
3
+ [![npm version](https://badge.fury.io/js/@convex-dev%2Fsharded-counter.svg)](https://badge.fury.io/js/@convex-dev%2Fsharded-counter)
4
+
5
+ This component adds counters to Convex. It acts as a key-value store from
6
+ string to number, with sharding to increase throughput when updating values.
7
+
8
+ Since it's built on Convex, everything is automatically consistent, reactive,
9
+ and cached.
10
+
11
+ For example, if you want to display
12
+ [one million checkboxes](https://en.wikipedia.org/wiki/One_Million_Checkboxes)
13
+ [on your Convex site](https://www.youtube.com/watch?v=LRUWplYoejQ), you want to
14
+ count the checkboxes in real-time while allowing lots of the boxes to change in
15
+ parallel.
16
+
17
+ More generally, whenever you have a counter that is changing frequently, you
18
+ can use this component to keep track of it efficiently.
19
+
20
+ ```ts
21
+ export const checkBox = mutation({
22
+ args: {i: v.number()},
23
+ handler: async (ctx, args) => {
24
+ const checkbox = await ctx.db.query("checkboxes")
25
+ .withIndex("i", q=>q.eq("i", args.i)).unique();
26
+ if (!checkbox.isChecked) {
27
+ await ctx.db.patch(checkbox._id, { isChecked: true });
28
+
29
+ // Here we increment the number of checkboxes.
30
+ await numCheckboxes.inc(ctx);
31
+ }
32
+ },
33
+ });
34
+ export const getCount = query({
35
+ args: {},
36
+ handler: async (ctx, _args) => {
37
+ return await numCheckboxes.count(ctx);
38
+ },
39
+ });
40
+ ```
41
+
42
+ ## Installation
43
+
44
+ First, install the component package:
45
+
46
+ ```ts
47
+ npm install @convex-dev/sharded-counter
48
+ ```
49
+
50
+ Then, create a `convex.config.ts` file in your app's `convex/` folder and install the
51
+ component by calling `use`:
52
+
53
+ ```ts
54
+ // convex/convex.config.ts
55
+ import { defineApp } from "convex/server";
56
+ import shardedCounter from "@convex-dev/sharded-counter/convex.config";
57
+
58
+ const app = defineApp();
59
+ app.use(shardedCounter);
60
+
61
+ export default app;
62
+ ```
63
+
64
+ Finally, create a new `ShardedCounter` within your `convex/` folder, and point it to
65
+ the installed component.
66
+
67
+ ```ts
68
+ import { components } from "./_generated/api";
69
+ import { ShardedCounter } from "@convex-dev/counter";
70
+
71
+ const counter = new ShardedCounter(components.counter, {
72
+ ...options
73
+ });
74
+ ```
75
+
76
+ ## Updating and reading counters
77
+
78
+ Once you have a `ShardedCounter`, there are a few methods you can use to update
79
+ the counter for a key in a mutation or action.
80
+
81
+ ```ts
82
+ await counter.add(ctx, "checkboxes"); // increment
83
+ await counter.add(ctx, "checkboxes", -5); // decrement by 5
84
+
85
+ const numCheckboxes = counter.for("checkboxes");
86
+ await numCheckboxes.inc(ctx); // increment
87
+ await numCheckboxes.dec(ctx); // decrement
88
+ await numCheckboxes.add(ctx, 5); // add 5
89
+ await numCheckboxes.subtract(ctx, 5); // subtract 5
90
+ ```
91
+
92
+ And you can read the counter's value in a query, mutation, or action.
93
+
94
+ ```ts
95
+ await counter.count(ctx, "checkboxes");
96
+ await numCheckboxes.count(ctx);
97
+ ```
98
+
99
+ See more example usage in [example.ts](./example/convex/example.ts).
100
+
101
+ ## Sharding the counter
102
+
103
+ When a single document is modified by two mutations at the same time, the
104
+ mutations slow down to achieve
105
+ [serializable results](https://docs.convex.dev/database/advanced/occ).
106
+
107
+ To achieve high throughput, the ShardedCounter distributes counts across
108
+ multiple documents, called "shards". Increments and decrements update a random
109
+ shard, while queries of the total count read from all shards.
110
+
111
+ 1. More shards => greater throughput when incrementing or decrementing.
112
+ 2. Fewer shards => better latency when querying the count.
113
+
114
+ You can set the number of shards when initializing the ShardedCounter, either
115
+ setting it specially for each key:
116
+
117
+ ```ts
118
+ const counter = new ShardedCounter(components.shardedCounter, {
119
+ shards: { checkboxes: 100 }, // 100 shards for the key "checkboxes"
120
+ });
121
+ ```
122
+
123
+ Or by setting a default that applies to all keys not specified in `shards`:
124
+
125
+ ```ts
126
+ const counter = new ShardedCounter(components.shardedCounter, {
127
+ shards: { checkboxes: 100 },
128
+ defaultShards: 20,
129
+ });
130
+ ```
131
+
132
+ The default number of shards if none is specified is 16.
133
+
134
+ Note your keys can be a subtype of string. e.g. if you want to store a count of
135
+ friends for each user, and you don't care about throughput for a single user,
136
+ you would declare ShardedCounter like so:
137
+
138
+ ```ts
139
+ const friendCounts = new ShardedCounter<Record<Id<"users">, number>>(
140
+ components.shardedCounter,
141
+ { defaultShards: 1 },
142
+ );
143
+ ```
144
+
145
+ ## Backfilling an existing count
146
+
147
+ If you want to count items like documents in a table, you may already have
148
+ documents before installing the ShardedCounter component, and these should be
149
+ accounted for.
150
+
151
+ The tricky part is making sure to merge active updates to counts with old
152
+ values that you want to backfill.
153
+
154
+ See example code at the bottom of
155
+ [example/convex/example.ts](example/convex/example.ts).
156
+
157
+ Walkthrough of steps:
158
+
159
+ 1. Create `backfillCursor` table in schema.ts
160
+ 2. Create a new document in this table, with fields
161
+ `{ creationTime: 0, id: "", isDone: false }`
162
+ 3. Wherever you want to update a counter based on a document changing, wrap the
163
+ update in a conditional, so it only gets updated if the backfill has processed
164
+ that document. In the example, you would be changing `insertUserBeforeBackfill`
165
+ to be implemented as `insertUserDuringBackfill`.
166
+ 4. Define backfill functions similar to `backfillUsers` and `backfillUsersBatch`
167
+ 5. Call `backfillUsersBatch` from the dashboard.
168
+ 6. Remove the conditional when updating counters. In the example, you would be
169
+ changing `insertUserDuringBackfill` to be implemented as
170
+ `insertUserAfterBackfill`.
171
+ 7. Delete the `backfillCursor` table.
172
+
173
+ <!-- END: Include on https://convex.dev/components -->
174
+
175
+ # 🧑‍🏫 What is Convex?
176
+
177
+ [Convex](https://convex.dev) is a hosted backend platform with a
178
+ built-in database that lets you write your
179
+ [database schema](https://docs.convex.dev/database/schemas) and
180
+ [server functions](https://docs.convex.dev/functions) in
181
+ [TypeScript](https://docs.convex.dev/typescript). Server-side database
182
+ [queries](https://docs.convex.dev/functions/query-functions) automatically
183
+ [cache](https://docs.convex.dev/functions/query-functions#caching--reactivity) and
184
+ [subscribe](https://docs.convex.dev/client/react#reactivity) to data, powering a
185
+ [realtime `useQuery` hook](https://docs.convex.dev/client/react#fetching-data) in our
186
+ [React client](https://docs.convex.dev/client/react). There are also clients for
187
+ [Python](https://docs.convex.dev/client/python),
188
+ [Rust](https://docs.convex.dev/client/rust),
189
+ [ReactNative](https://docs.convex.dev/client/react-native), and
190
+ [Node](https://docs.convex.dev/client/javascript), as well as a straightforward
191
+ [HTTP API](https://docs.convex.dev/http-api/).
192
+
193
+ The database supports
194
+ [NoSQL-style documents](https://docs.convex.dev/database/document-storage) with
195
+ [opt-in schema validation](https://docs.convex.dev/database/schemas),
196
+ [relationships](https://docs.convex.dev/database/document-ids) and
197
+ [custom indexes](https://docs.convex.dev/database/indexes/)
198
+ (including on fields in nested objects).
199
+
200
+ The
201
+ [`query`](https://docs.convex.dev/functions/query-functions) and
202
+ [`mutation`](https://docs.convex.dev/functions/mutation-functions) server functions have transactional,
203
+ low latency access to the database and leverage our
204
+ [`v8` runtime](https://docs.convex.dev/functions/runtimes) with
205
+ [determinism guardrails](https://docs.convex.dev/functions/runtimes#using-randomness-and-time-in-queries-and-mutations)
206
+ to provide the strongest ACID guarantees on the market:
207
+ immediate consistency,
208
+ serializable isolation, and
209
+ automatic conflict resolution via
210
+ [optimistic multi-version concurrency control](https://docs.convex.dev/database/advanced/occ) (OCC / MVCC).
211
+
212
+ The [`action` server functions](https://docs.convex.dev/functions/actions) have
213
+ access to external APIs and enable other side-effects and non-determinism in
214
+ either our
215
+ [optimized `v8` runtime](https://docs.convex.dev/functions/runtimes) or a more
216
+ [flexible `node` runtime](https://docs.convex.dev/functions/runtimes#nodejs-runtime).
217
+
218
+ Functions can run in the background via
219
+ [scheduling](https://docs.convex.dev/scheduling/scheduled-functions) and
220
+ [cron jobs](https://docs.convex.dev/scheduling/cron-jobs).
221
+
222
+ Development is cloud-first, with
223
+ [hot reloads for server function](https://docs.convex.dev/cli#run-the-convex-dev-server) editing via the
224
+ [CLI](https://docs.convex.dev/cli),
225
+ [preview deployments](https://docs.convex.dev/production/hosting/preview-deployments),
226
+ [logging and exception reporting integrations](https://docs.convex.dev/production/integrations/),
227
+ There is a
228
+ [dashboard UI](https://docs.convex.dev/dashboard) to
229
+ [browse and edit data](https://docs.convex.dev/dashboard/deployments/data),
230
+ [edit environment variables](https://docs.convex.dev/production/environment-variables),
231
+ [view logs](https://docs.convex.dev/dashboard/deployments/logs),
232
+ [run server functions](https://docs.convex.dev/dashboard/deployments/functions), and more.
233
+
234
+ There are built-in features for
235
+ [reactive pagination](https://docs.convex.dev/database/pagination),
236
+ [file storage](https://docs.convex.dev/file-storage),
237
+ [reactive text search](https://docs.convex.dev/text-search),
238
+ [vector search](https://docs.convex.dev/vector-search),
239
+ [https endpoints](https://docs.convex.dev/functions/http-actions) (for webhooks),
240
+ [snapshot import/export](https://docs.convex.dev/database/import-export/),
241
+ [streaming import/export](https://docs.convex.dev/production/integrations/streaming-import-export), and
242
+ [runtime validation](https://docs.convex.dev/database/schemas#validators) for
243
+ [function arguments](https://docs.convex.dev/functions/args-validation) and
244
+ [database data](https://docs.convex.dev/database/schemas#schema-validation).
245
+
246
+ Everything scales automatically, and it’s [free to start](https://www.convex.dev/plans).
@@ -0,0 +1,37 @@
1
+ import { Expand, FunctionReference, GenericDataModel, GenericMutationCtx, GenericQueryCtx } from "convex/server";
2
+ import { GenericId } from "convex/values";
3
+ import { api } from "../component/_generated/api";
4
+ export declare class ShardedCounter<Shards extends Record<string, number>> {
5
+ component: UseApi<typeof api>;
6
+ options?: {
7
+ shards?: Shards | undefined;
8
+ defaultShards?: number | undefined;
9
+ } | undefined;
10
+ constructor(component: UseApi<typeof api>, options?: {
11
+ shards?: Shards | undefined;
12
+ defaultShards?: number | undefined;
13
+ } | undefined);
14
+ add<Name extends string = keyof Shards & string>(ctx: RunMutationCtx, name: Name, count?: number): Promise<null>;
15
+ count<Name extends string = keyof Shards & string>(ctx: RunQueryCtx, name: Name): Promise<number>;
16
+ for<Name extends string = keyof Shards & string>(name: Name): {
17
+ add: (ctx: RunMutationCtx, count?: number) => Promise<null>;
18
+ subtract: (ctx: RunMutationCtx, count?: number) => Promise<null>;
19
+ inc: (ctx: RunMutationCtx) => Promise<null>;
20
+ dec: (ctx: RunMutationCtx) => Promise<null>;
21
+ count: (ctx: RunQueryCtx) => Promise<number>;
22
+ };
23
+ }
24
+ type RunQueryCtx = {
25
+ runQuery: GenericQueryCtx<GenericDataModel>["runQuery"];
26
+ };
27
+ type RunMutationCtx = {
28
+ runMutation: GenericMutationCtx<GenericDataModel>["runMutation"];
29
+ };
30
+ export type OpaqueIds<T> = T extends GenericId<infer _T> ? string : T extends (infer U)[] ? OpaqueIds<U>[] : T extends object ? {
31
+ [K in keyof T]: OpaqueIds<T[K]>;
32
+ } : T;
33
+ export type UseApi<API> = Expand<{
34
+ [mod in keyof API]: API[mod] extends FunctionReference<infer FType, "public", infer FArgs, infer FReturnType, infer FComponentPath> ? FunctionReference<FType, "internal", OpaqueIds<FArgs>, OpaqueIds<FReturnType>, FComponentPath> : UseApi<API[mod]>;
35
+ }>;
36
+ export {};
37
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,iBAAiB,EACjB,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,EAChB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC1C,OAAO,EAAE,GAAG,EAAE,MAAM,6BAA6B,CAAC;AAElD,qBAAa,cAAc,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAEtD,SAAS,EAAE,MAAM,CAAC,OAAO,GAAG,CAAC;IAC7B,OAAO,CAAC;;;;gBADR,SAAS,EAAE,MAAM,CAAC,OAAO,GAAG,CAAC,EAC7B,OAAO,CAAC;;;iBAA6C;IAExD,GAAG,CAAC,IAAI,SAAS,MAAM,GAAG,MAAM,MAAM,GAAG,MAAM,EACnD,GAAG,EAAE,cAAc,EACnB,IAAI,EAAE,IAAI,EACV,KAAK,GAAE,MAAU;IASb,KAAK,CAAC,IAAI,SAAS,MAAM,GAAG,MAAM,MAAM,GAAG,MAAM,EACrD,GAAG,EAAE,WAAW,EAChB,IAAI,EAAE,IAAI;IAKZ,GAAG,CAAC,IAAI,SAAS,MAAM,GAAG,MAAM,MAAM,GAAG,MAAM,EAAE,IAAI,EAAE,IAAI;mBAEtC,cAAc,UAAS,MAAM;wBAExB,cAAc,UAAS,MAAM;mBAElC,cAAc;mBACd,cAAc;qBACZ,WAAW;;CAGnC;AAID,KAAK,WAAW,GAAG;IACjB,QAAQ,EAAE,eAAe,CAAC,gBAAgB,CAAC,CAAC,UAAU,CAAC,CAAC;CACzD,CAAC;AACF,KAAK,cAAc,GAAG;IACpB,WAAW,EAAE,kBAAkB,CAAC,gBAAgB,CAAC,CAAC,aAAa,CAAC,CAAC;CAClE,CAAC;AAEF,MAAM,MAAM,SAAS,CAAC,CAAC,IACrB,CAAC,SAAS,SAAS,CAAC,MAAM,EAAE,CAAC,GACzB,MAAM,GACN,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GACnB,SAAS,CAAC,CAAC,CAAC,EAAE,GACd,CAAC,SAAS,MAAM,GACd;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,GACnC,CAAC,CAAC;AAEZ,MAAM,MAAM,MAAM,CAAC,GAAG,IAAI,MAAM,CAAC;KAC9B,GAAG,IAAI,MAAM,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,SAAS,iBAAiB,CACpD,MAAM,KAAK,EACX,QAAQ,EACR,MAAM,KAAK,EACX,MAAM,WAAW,EACjB,MAAM,cAAc,CACrB,GACG,iBAAiB,CACf,KAAK,EACL,UAAU,EACV,SAAS,CAAC,KAAK,CAAC,EAChB,SAAS,CAAC,WAAW,CAAC,EACtB,cAAc,CACf,GACD,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;CACrB,CAAC,CAAC"}
@@ -0,0 +1,30 @@
1
+ export class ShardedCounter {
2
+ component;
3
+ options;
4
+ constructor(component, options) {
5
+ this.component = component;
6
+ this.options = options;
7
+ }
8
+ async add(ctx, name, count = 1) {
9
+ const shards = this.options?.shards?.[name] ?? this.options?.defaultShards;
10
+ return ctx.runMutation(this.component.public.add, {
11
+ name,
12
+ count,
13
+ shards,
14
+ });
15
+ }
16
+ async count(ctx, name) {
17
+ return ctx.runQuery(this.component.public.count, { name });
18
+ }
19
+ // Another way of exporting functionality
20
+ for(name) {
21
+ return {
22
+ add: async (ctx, count = 1) => this.add(ctx, name, count),
23
+ subtract: async (ctx, count = 1) => this.add(ctx, name, -count),
24
+ inc: async (ctx) => this.add(ctx, name, 1),
25
+ dec: async (ctx) => this.add(ctx, name, -1),
26
+ count: async (ctx) => this.count(ctx, name),
27
+ };
28
+ }
29
+ }
30
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/client/index.ts"],"names":[],"mappings":"AAUA,MAAM,OAAO,cAAc;IAEhB;IACA;IAFT,YACS,SAA6B,EAC7B,OAAqD;QADrD,cAAS,GAAT,SAAS,CAAoB;QAC7B,YAAO,GAAP,OAAO,CAA8C;IAC3D,CAAC;IACJ,KAAK,CAAC,GAAG,CACP,GAAmB,EACnB,IAAU,EACV,QAAgB,CAAC;QAEjB,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE,aAAa,CAAC;QAC3E,OAAO,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,EAAE;YAChD,IAAI;YACJ,KAAK;YACL,MAAM;SACP,CAAC,CAAC;IACL,CAAC;IACD,KAAK,CAAC,KAAK,CACT,GAAgB,EAChB,IAAU;QAEV,OAAO,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;IAC7D,CAAC;IACD,yCAAyC;IACzC,GAAG,CAA8C,IAAU;QACzD,OAAO;YACL,GAAG,EAAE,KAAK,EAAE,GAAmB,EAAE,QAAgB,CAAC,EAAE,EAAE,CACpD,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,CAAC;YAC5B,QAAQ,EAAE,KAAK,EAAE,GAAmB,EAAE,QAAgB,CAAC,EAAE,EAAE,CACzD,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,KAAK,CAAC;YAC7B,GAAG,EAAE,KAAK,EAAE,GAAmB,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC;YAC1D,GAAG,EAAE,KAAK,EAAE,GAAmB,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;YAC3D,KAAK,EAAE,KAAK,EAAE,GAAgB,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC;SACzD,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * A utility for referencing Convex functions in your app's API.
3
+ *
4
+ * Usage:
5
+ * ```js
6
+ * const myFunctionReference = api.myModule.myFunction;
7
+ * ```
8
+ */
9
+ export const api: import("convex/server").AnyApi;
10
+ export const internal: import("convex/server").AnyApi;
11
+ export const components: {
12
+ [x: string]: import("../../../node_modules/convex/dist/esm-types/server/components").AnyComponentReference;
13
+ };
14
+ //# sourceMappingURL=api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../../../src/component/_generated/api.js"],"names":[],"mappings":"AAcA;;;;;;;GAOG;AACH,iDAA0B;AAC1B,sDAA+B;AAC/B;;EAA8C"}
@@ -0,0 +1,24 @@
1
+ /* prettier-ignore-start */
2
+ /* eslint-disable */
3
+ /**
4
+ * Generated `api` utility.
5
+ *
6
+ * THIS CODE IS AUTOMATICALLY GENERATED.
7
+ *
8
+ * To regenerate, run `npx convex dev`.
9
+ * @module
10
+ */
11
+ import { anyApi, componentsGeneric } from "convex/server";
12
+ /**
13
+ * A utility for referencing Convex functions in your app's API.
14
+ *
15
+ * Usage:
16
+ * ```js
17
+ * const myFunctionReference = api.myModule.myFunction;
18
+ * ```
19
+ */
20
+ export const api = anyApi;
21
+ export const internal = anyApi;
22
+ export const components = componentsGeneric();
23
+ /* prettier-ignore-end */
24
+ //# sourceMappingURL=api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../../../../src/component/_generated/api.js"],"names":[],"mappings":"AAAA,2BAA2B;AAE3B,oBAAoB;AACpB;;;;;;;GAOG;AAEH,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAE1D;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,GAAG,GAAG,MAAM,CAAC;AAC1B,MAAM,CAAC,MAAM,QAAQ,GAAG,MAAM,CAAC;AAC/B,MAAM,CAAC,MAAM,UAAU,GAAG,iBAAiB,EAAE,CAAC;AAE9C,yBAAyB"}
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Define a query in this Convex app's public API.
3
+ *
4
+ * This function will be allowed to read your Convex database and will be accessible from the client.
5
+ *
6
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
7
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
8
+ */
9
+ export const query: import("convex/server").QueryBuilder<any, "public">;
10
+ /**
11
+ * Define a query that is only accessible from other Convex functions (but not from the client).
12
+ *
13
+ * This function will be allowed to read from your Convex database. It will not be accessible from the client.
14
+ *
15
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
16
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
17
+ */
18
+ export const internalQuery: import("convex/server").QueryBuilder<any, "internal">;
19
+ /**
20
+ * Define a mutation in this Convex app's public API.
21
+ *
22
+ * This function will be allowed to modify your Convex database and will be accessible from the client.
23
+ *
24
+ * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
25
+ * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
26
+ */
27
+ export const mutation: import("convex/server").MutationBuilder<any, "public">;
28
+ /**
29
+ * Define a mutation that is only accessible from other Convex functions (but not from the client).
30
+ *
31
+ * This function will be allowed to modify your Convex database. It will not be accessible from the client.
32
+ *
33
+ * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
34
+ * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
35
+ */
36
+ export const internalMutation: import("convex/server").MutationBuilder<any, "internal">;
37
+ /**
38
+ * Define an action in this Convex app's public API.
39
+ *
40
+ * An action is a function which can execute any JavaScript code, including non-deterministic
41
+ * code and code with side-effects, like calling third-party services.
42
+ * They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
43
+ * They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
44
+ *
45
+ * @param func - The action. It receives an {@link ActionCtx} as its first argument.
46
+ * @returns The wrapped action. Include this as an `export` to name it and make it accessible.
47
+ */
48
+ export const action: import("convex/server").ActionBuilder<any, "public">;
49
+ /**
50
+ * Define an action that is only accessible from other Convex functions (but not from the client).
51
+ *
52
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument.
53
+ * @returns The wrapped function. Include this as an `export` to name it and make it accessible.
54
+ */
55
+ export const internalAction: import("convex/server").ActionBuilder<any, "internal">;
56
+ /**
57
+ * Define a Convex HTTP action.
58
+ *
59
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument, and a `Request` object
60
+ * as its second.
61
+ * @returns The wrapped endpoint function. Route a URL path to this function in `convex/http.js`.
62
+ */
63
+ export const httpAction: (func: (ctx: import("convex/server").GenericActionCtx<import("convex/server").GenericDataModel>, request: Request) => Promise<Response>) => import("convex/server").PublicHttpAction;
64
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../../../src/component/_generated/server.js"],"names":[],"mappings":"AAuBA;;;;;;;GAOG;AACH,wEAAkC;AAElC;;;;;;;GAOG;AACH,kFAAkD;AAElD;;;;;;;GAOG;AACH,8EAAwC;AAExC;;;;;;;GAOG;AACH,wFAAwD;AAExD;;;;;;;;;;GAUG;AACH,0EAAoC;AAEpC;;;;;GAKG;AACH,oFAAoD;AAEpD;;;;;;GAMG;AACH,8MAA4C"}