@bb-labs/convex-helpers 0.0.1 → 0.0.2
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/.turbo/turbo-build.log +11 -0
- package/README.md +23 -47
- package/dist/fns/create-table.d.ts +4 -4
- package/dist/fns/create-table.js +4 -4
- package/package.json +14 -24
- package/src/fns/create-table.ts +39 -0
- package/src/index.ts +1 -0
- package/tsconfig.json +11 -0
package/README.md
CHANGED
|
@@ -1,67 +1,43 @@
|
|
|
1
1
|
## Introduction
|
|
2
2
|
|
|
3
|
-
A
|
|
4
|
-
|
|
5
|
-
- ✅ Recursively sorting **object keys**
|
|
6
|
-
- ✅ Recursively sorting **arrays** using **SuperJSON** for deterministic comparison
|
|
7
|
-
- ✅ Preserving non-plain objects (`Date`, `Map`, `Set`, `BigInt`, class instances, etc.)
|
|
8
|
-
- ⚡ Perfect for generating cache keys, hashing structured data, or stabilizing query arguments
|
|
9
|
-
|
|
10
|
-
The goal:
|
|
11
|
-
**Identical structures always produce identical normalized output — even when key order or array order differs.**
|
|
12
|
-
|
|
13
|
-
---
|
|
3
|
+
A lightweight utility library for [Convex](https://convex.dev) containing helpers.
|
|
14
4
|
|
|
15
5
|
## Installation
|
|
16
6
|
|
|
17
7
|
```bash
|
|
18
|
-
npm install @
|
|
8
|
+
npm install @bb-labs/convex-helpers
|
|
19
9
|
# or
|
|
20
|
-
|
|
10
|
+
bun add @bb-labs/convex-helpers
|
|
21
11
|
# or
|
|
22
|
-
|
|
12
|
+
yarn add @bb-labs/convex-helpers
|
|
23
13
|
```
|
|
24
14
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
## API
|
|
28
|
-
|
|
29
|
-
### `deepSort(value: unknown): unknown`
|
|
30
|
-
|
|
31
|
-
Returns a **fully normalized, deeply sorted copy** of any JSON-compatible data structure.
|
|
15
|
+
## Helpers
|
|
32
16
|
|
|
33
|
-
|
|
17
|
+
### `createTable`
|
|
34
18
|
|
|
35
|
-
|
|
36
|
-
- **Arrays** are deeply normalized, then **sorted** using SuperJSON’s structural encoding
|
|
37
|
-
- Non-plain objects (Dates, Maps, Sets, etc.) are **preserved**, and naturally handled by SuperJSON
|
|
19
|
+
A helper function that provides a declarative way to define Convex tables with schemas and indexes in a single, readable configuration object.
|
|
38
20
|
|
|
39
|
-
```
|
|
40
|
-
import {
|
|
21
|
+
```typescript
|
|
22
|
+
import { createTable } from "@bb-labs/convex-helpers";
|
|
23
|
+
import { v } from "convex/values";
|
|
41
24
|
|
|
42
|
-
|
|
43
|
-
|
|
25
|
+
const UserSchema = {
|
|
26
|
+
email: v.string(),
|
|
27
|
+
name: v.string(),
|
|
28
|
+
role: v.string(),
|
|
29
|
+
createdAt: v.number(),
|
|
30
|
+
};
|
|
44
31
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
// → normalized deeply, sorted deterministically
|
|
32
|
+
const usersTable = createTable({
|
|
33
|
+
schema: UserSchema,
|
|
34
|
+
indexes: [
|
|
35
|
+
{ name: "by_email", fields: ["email"] },
|
|
36
|
+
{ name: "by_role", fields: ["role", "createdAt"] },
|
|
37
|
+
],
|
|
38
|
+
});
|
|
53
39
|
```
|
|
54
40
|
|
|
55
|
-
---
|
|
56
|
-
|
|
57
|
-
## Caveats
|
|
58
|
-
|
|
59
|
-
- Sorting arrays by SuperJSON string is deterministic but may be expensive for very large/nested data.
|
|
60
|
-
- Circular references are **not supported** (SuperJSON cannot serialize them).
|
|
61
|
-
- This is intended for **cache key normalization**, **equality pre-normalization**, and **stable hashing** — not for mutating live data structuress.
|
|
62
|
-
|
|
63
|
-
---
|
|
64
|
-
|
|
65
41
|
## License
|
|
66
42
|
|
|
67
43
|
MIT
|
|
@@ -13,11 +13,11 @@ type CreateTableOptions<T extends SchemaDefinition> = {
|
|
|
13
13
|
* Helper to define a Convex table with a declarative syntax.
|
|
14
14
|
*
|
|
15
15
|
* @example
|
|
16
|
-
*
|
|
17
|
-
* schema:
|
|
16
|
+
* const usersTable = createTable({
|
|
17
|
+
* schema: UserSchema,
|
|
18
18
|
* indexes: [
|
|
19
|
-
* { name: "
|
|
20
|
-
* { name: "
|
|
19
|
+
* { name: "by_email", fields: ["email"] },
|
|
20
|
+
* { name: "by_role", fields: ["role", "createdAt"] },
|
|
21
21
|
* ],
|
|
22
22
|
* });
|
|
23
23
|
*/
|
package/dist/fns/create-table.js
CHANGED
|
@@ -3,11 +3,11 @@ import { defineTable } from "convex/server";
|
|
|
3
3
|
* Helper to define a Convex table with a declarative syntax.
|
|
4
4
|
*
|
|
5
5
|
* @example
|
|
6
|
-
*
|
|
7
|
-
* schema:
|
|
6
|
+
* const usersTable = createTable({
|
|
7
|
+
* schema: UserSchema,
|
|
8
8
|
* indexes: [
|
|
9
|
-
* { name: "
|
|
10
|
-
* { name: "
|
|
9
|
+
* { name: "by_email", fields: ["email"] },
|
|
10
|
+
* { name: "by_role", fields: ["role", "createdAt"] },
|
|
11
11
|
* ],
|
|
12
12
|
* });
|
|
13
13
|
*/
|
package/package.json
CHANGED
|
@@ -1,36 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bb-labs/convex-helpers",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"author": "Beepbop",
|
|
3
|
+
"version": "0.0.2",
|
|
5
4
|
"repository": {
|
|
6
5
|
"type": "git",
|
|
7
|
-
"url": "https://github.com/beepbop-labs/
|
|
6
|
+
"url": "https://github.com/beepbop-labs/libraries.git"
|
|
8
7
|
},
|
|
9
8
|
"main": "dist/index.js",
|
|
10
|
-
"devDependencies": {
|
|
11
|
-
"@types/bun": "latest"
|
|
12
|
-
},
|
|
13
|
-
"peerDependencies": {
|
|
14
|
-
"typescript": "^5"
|
|
15
|
-
},
|
|
16
|
-
"description": "A library for convex helpers",
|
|
17
|
-
"files": [
|
|
18
|
-
"dist",
|
|
19
|
-
"README.md"
|
|
20
|
-
],
|
|
21
|
-
"homepage": "https://github.com/beepbop-labs/convex-helpers",
|
|
22
|
-
"keywords": [
|
|
23
|
-
"convex-helpers",
|
|
24
|
-
"convex",
|
|
25
|
-
"helpers"
|
|
26
|
-
],
|
|
27
|
-
"license": "MIT",
|
|
28
9
|
"scripts": {
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
10
|
+
"build": "bldr",
|
|
11
|
+
"dev": "bldr -w",
|
|
12
|
+
"clean": "rm -rf node_modules bun.lock dist"
|
|
32
13
|
},
|
|
33
14
|
"dependencies": {
|
|
15
|
+
"@bb-labs/bldr": "^0.0.16",
|
|
16
|
+
"@bb-labs/tsconfigs": "^0.0.4",
|
|
34
17
|
"convex": "^1.31.2"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/bun": "latest",
|
|
21
|
+
"tsc-alias": "^1.8.16"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"typescript": "^5"
|
|
35
25
|
}
|
|
36
26
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { defineTable, type TableDefinition, type GenericTableIndexes } from "convex/server";
|
|
2
|
+
import type { GenericValidator, ObjectType, VObject } from "convex/values";
|
|
3
|
+
|
|
4
|
+
type SchemaDefinition = Record<string, GenericValidator>;
|
|
5
|
+
|
|
6
|
+
type IndexDefinition<T extends SchemaDefinition> = {
|
|
7
|
+
name: string;
|
|
8
|
+
fields: [keyof T & string, ...(keyof T & string)[]];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type CreateTableOptions<T extends SchemaDefinition> = {
|
|
12
|
+
schema: T;
|
|
13
|
+
indexes?: IndexDefinition<T>[];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Helper to define a Convex table with a declarative syntax.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const usersTable = createTable({
|
|
21
|
+
* schema: UserSchema,
|
|
22
|
+
* indexes: [
|
|
23
|
+
* { name: "by_email", fields: ["email"] },
|
|
24
|
+
* { name: "by_role", fields: ["role", "createdAt"] },
|
|
25
|
+
* ],
|
|
26
|
+
* });
|
|
27
|
+
*/
|
|
28
|
+
export function createTable<T extends SchemaDefinition>({
|
|
29
|
+
schema,
|
|
30
|
+
indexes = [],
|
|
31
|
+
}: CreateTableOptions<T>): TableDefinition<VObject<ObjectType<T>, T>, GenericTableIndexes> {
|
|
32
|
+
let table: TableDefinition = defineTable(schema);
|
|
33
|
+
|
|
34
|
+
for (const index of indexes) {
|
|
35
|
+
table = table.index(index.name, index.fields);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return table as TableDefinition<VObject<ObjectType<T>, T>, GenericTableIndexes>;
|
|
39
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./fns/create-table";
|