@togatherlabs/event-sdk 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.
- package/README.md +200 -0
- package/gen/ts/user/create/v1/user_pb.d.ts +38 -0
- package/gen/ts/user/create/v1/user_pb.js +19 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
# togather-shared-events
|
|
2
|
+
|
|
3
|
+
This repository contains **event schemas** for the **toGather event-driven backend system**. We use **Protocol Buffers (Protobuf)** as the messaging format for events. It provides **type-safe code** generation for **TypeScript** and **Python**, enabling services in different languages to share a consistent event schema.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Table of Contents
|
|
7
|
+
|
|
8
|
+
1. [Directory Structure](#directory-structure)
|
|
9
|
+
2. [Development](#development)
|
|
10
|
+
|
|
11
|
+
* [Adding New Proto Files](#adding-new-proto-files)
|
|
12
|
+
* [Linting](#linting)
|
|
13
|
+
* [Code Generation](#code-generation)
|
|
14
|
+
3. [Using Generated Code](#using-generated-code)
|
|
15
|
+
|
|
16
|
+
* [TypeScript](#typescript)
|
|
17
|
+
* [Python](#python)
|
|
18
|
+
4. [Best Practices](#best-practices)
|
|
19
|
+
5. [Troubleshooting](#troubleshooting)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## Directory Structure
|
|
23
|
+
|
|
24
|
+
Protobuf files should follow a **domain/version-based structure**:
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
proto/
|
|
28
|
+
└── <domain>/
|
|
29
|
+
└── <version>/
|
|
30
|
+
└── <event>.proto
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Example:
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
proto/user/create/v1/user.proto
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
* **Domain**: The feature or module (e.g., `user`)
|
|
40
|
+
* **Version**: The event schema version (e.g., `v1`)
|
|
41
|
+
|
|
42
|
+
> This structure ensures forward-compatibility and makes schema evolution manageable.
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
## Development
|
|
47
|
+
|
|
48
|
+
### Adding New Proto Files
|
|
49
|
+
|
|
50
|
+
1. Place your `.proto` file in the appropriate `proto/<domain>/<version>` folder.
|
|
51
|
+
2. Define the `package` in the `.proto` file to match the folder structure.
|
|
52
|
+
|
|
53
|
+
Example for `user.create.v1`:
|
|
54
|
+
|
|
55
|
+
```proto
|
|
56
|
+
syntax = "proto3";
|
|
57
|
+
|
|
58
|
+
package user.create.v1;
|
|
59
|
+
|
|
60
|
+
message UserCreated {
|
|
61
|
+
string id = 1;
|
|
62
|
+
string name = 2;
|
|
63
|
+
string email = 3;
|
|
64
|
+
int64 created_at = 4;
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Linting
|
|
69
|
+
|
|
70
|
+
Lint your proto files to ensure consistency and adherence to standards:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
pnpm buf:lint
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
> This checks for things like missing packages, incorrect directory structure, and other style issues.
|
|
77
|
+
|
|
78
|
+
### Code Generation
|
|
79
|
+
|
|
80
|
+
Generate TypeScript and Python code from your proto files:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pnpm buf:generate
|
|
84
|
+
```
|
|
85
|
+
The generated code is placed in the `gen/` directory:
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
gen/ts/...
|
|
89
|
+
gen/python/...
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
> **Tip:** Keep `buf.gen.yaml` updated if you add new languages or plugins.
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
## Using Generated Code
|
|
96
|
+
|
|
97
|
+
### TypeScript
|
|
98
|
+
|
|
99
|
+
**Dependencies:**
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
pnpm add @bufbuild/protobuf@^2.9.0
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
**Example usage:**
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { UserCreated, UserCreatedSchema } from '../gen/ts/user/create/v1/user_pb';
|
|
109
|
+
import { create, toBinary, fromBinary, toJson } from '@bufbuild/protobuf';
|
|
110
|
+
|
|
111
|
+
// Create a new message
|
|
112
|
+
const newUser: UserCreated = create(UserCreatedSchema, {
|
|
113
|
+
id: "1",
|
|
114
|
+
name: "Abhiram",
|
|
115
|
+
email: "abhiram.ars@gmail.com",
|
|
116
|
+
createdAt: BigInt(Date.now()),
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// Serialize to binary
|
|
120
|
+
const buffer = toBinary(UserCreatedSchema, newUser);
|
|
121
|
+
|
|
122
|
+
// Deserialize from binary
|
|
123
|
+
const decoded = fromBinary(UserCreatedSchema, buffer);
|
|
124
|
+
|
|
125
|
+
// Convert to JSON
|
|
126
|
+
const json = toJson(UserCreatedSchema, decoded);
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
> **Tip:** Use `toBinary`/`fromBinary` for Kafka or network transmission. Use `toJson`/`fromJson` for logging or debugging.
|
|
130
|
+
|
|
131
|
+
### Python
|
|
132
|
+
|
|
133
|
+
**Dependencies:**
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
pip install protobuf
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
**Example usage:**
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
from gen.python.user.create.v1 import user_pb2
|
|
143
|
+
|
|
144
|
+
# Create a new message
|
|
145
|
+
msg = user_pb2.UserCreated(
|
|
146
|
+
id="1",
|
|
147
|
+
name="Abhiram",
|
|
148
|
+
email="abhiram@gmail.com",
|
|
149
|
+
created_at=int(1728512345123),
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
# Serialize to bytes
|
|
153
|
+
binary_data = msg.SerializeToString()
|
|
154
|
+
|
|
155
|
+
# Deserialize from bytes
|
|
156
|
+
decoded = user_pb2.UserCreated()
|
|
157
|
+
decoded.ParseFromString(binary_data)
|
|
158
|
+
|
|
159
|
+
print(decoded)
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
> **Tip:** Python type hints can be added with `mypy-protobuf` or community plugins for full static type checking.
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
## Best Practices
|
|
167
|
+
|
|
168
|
+
1. **Package and folder alignment**
|
|
169
|
+
|
|
170
|
+
* `package` in `.proto` must match folder structure (`proto/<domain>/<version>`).
|
|
171
|
+
|
|
172
|
+
2. **Versioning**
|
|
173
|
+
|
|
174
|
+
* Always increment the version folder (`v1`, `v2`, etc.) when changing message formats to avoid breaking consumers.
|
|
175
|
+
|
|
176
|
+
3. **Type safety**
|
|
177
|
+
|
|
178
|
+
* Use the generated schemas rather than manually constructing messages.
|
|
179
|
+
* In TypeScript, always use `create()`; in Python, use the generated `UserCreated` class.
|
|
180
|
+
|
|
181
|
+
4. **Schema evolution**
|
|
182
|
+
|
|
183
|
+
* Avoid renaming or deleting existing fields; mark them deprecated instead.
|
|
184
|
+
* Use `int64` for timestamps, `string` for IDs/emails, etc.
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
## Troubleshooting
|
|
188
|
+
|
|
189
|
+
* **ModuleNotFoundError in Python**
|
|
190
|
+
|
|
191
|
+
* Ensure `gen/` folder is in your `PYTHONPATH`.
|
|
192
|
+
* Add `__init__.py` files in each subfolder if needed.
|
|
193
|
+
|
|
194
|
+
* **TypeScript errors with missing `$typeName` or `UserCreated`**
|
|
195
|
+
|
|
196
|
+
* Make sure you are importing the **schema** (`UserCreatedSchema`) and using `create()` instead of instantiating the type directly.
|
|
197
|
+
|
|
198
|
+
* **Buf generation issues**
|
|
199
|
+
|
|
200
|
+
* Run `pnpm buf:lint` first to catch package/directory mismatches.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// @generated by protoc-gen-es v2.9.0 with parameter "target=js+dts,import_extension=none"
|
|
2
|
+
// @generated from file user/create/v1/user.proto (package user.create.v1, syntax proto3)
|
|
3
|
+
/* eslint-disable */
|
|
4
|
+
|
|
5
|
+
import type { GenFile, GenMessage } from "@bufbuild/protobuf/codegenv2";
|
|
6
|
+
import type { Message } from "@bufbuild/protobuf";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Describes the file user/create/v1/user.proto.
|
|
10
|
+
*/
|
|
11
|
+
export declare const file_user_create_v1_user: GenFile;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @generated from message user.create.v1.UserCreated
|
|
15
|
+
*/
|
|
16
|
+
export declare type UserCreated = Message<"user.create.v1.UserCreated"> & {
|
|
17
|
+
/**
|
|
18
|
+
* @generated from field: string id = 1;
|
|
19
|
+
*/
|
|
20
|
+
id: string;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @generated from field: string email = 2;
|
|
24
|
+
*/
|
|
25
|
+
email: string;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @generated from field: int64 created_at = 3;
|
|
29
|
+
*/
|
|
30
|
+
createdAt: bigint;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Describes the message user.create.v1.UserCreated.
|
|
35
|
+
* Use `create(UserCreatedSchema)` to create a new message.
|
|
36
|
+
*/
|
|
37
|
+
export declare const UserCreatedSchema: GenMessage<UserCreated>;
|
|
38
|
+
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// @generated by protoc-gen-es v2.9.0 with parameter "target=js+dts,import_extension=none"
|
|
2
|
+
// @generated from file user/create/v1/user.proto (package user.create.v1, syntax proto3)
|
|
3
|
+
/* eslint-disable */
|
|
4
|
+
|
|
5
|
+
import { fileDesc, messageDesc } from "@bufbuild/protobuf/codegenv2";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Describes the file user/create/v1/user.proto.
|
|
9
|
+
*/
|
|
10
|
+
export const file_user_create_v1_user = /*@__PURE__*/
|
|
11
|
+
fileDesc("Chl1c2VyL2NyZWF0ZS92MS91c2VyLnByb3RvEg51c2VyLmNyZWF0ZS52MSI8CgtVc2VyQ3JlYXRlZBIKCgJpZBgBIAEoCRINCgVlbWFpbBgCIAEoCRISCgpjcmVhdGVkX2F0GAMgASgDQnkKEmNvbS51c2VyLmNyZWF0ZS52MUIJVXNlclByb3RvUAGiAgNVQ1iqAg5Vc2VyLkNyZWF0ZS5WMcoCDlVzZXJcQ3JlYXRlXFYx4gIaVXNlclxDcmVhdGVcVjFcR1BCTWV0YWRhdGHqAhBVc2VyOjpDcmVhdGU6OlYxYgZwcm90bzM");
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Describes the message user.create.v1.UserCreated.
|
|
15
|
+
* Use `create(UserCreatedSchema)` to create a new message.
|
|
16
|
+
*/
|
|
17
|
+
export const UserCreatedSchema = /*@__PURE__*/
|
|
18
|
+
messageDesc(file_user_create_v1_user, 0);
|
|
19
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@togatherlabs/event-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Shared Protobuf event schemas and generated code for toGather microservices",
|
|
5
|
+
"main": "./gen/ts/index.js",
|
|
6
|
+
"types": "./gen/ts/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"gen/ts"
|
|
9
|
+
],
|
|
10
|
+
"keywords": [],
|
|
11
|
+
"type": "module",
|
|
12
|
+
"author": "toGather team",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@commitlint/cli": "^20.1.0",
|
|
16
|
+
"@commitlint/config-conventional": "^20.0.0",
|
|
17
|
+
"@commitlint/cz-commitlint": "^20.1.0",
|
|
18
|
+
"commitizen": "^4.3.1",
|
|
19
|
+
"husky": "^9.1.7",
|
|
20
|
+
"inquirer": "^9.3.8"
|
|
21
|
+
},
|
|
22
|
+
"config": {
|
|
23
|
+
"commitizen": {
|
|
24
|
+
"path": "@commitlint/cz-commitlint"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@bufbuild/buf": "^1.58.0",
|
|
29
|
+
"@bufbuild/protobuf": "^2.9.0"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
36
|
+
"commit": "git-cz",
|
|
37
|
+
"buf:lint": "buf lint",
|
|
38
|
+
"buf:generate": "buf generate"
|
|
39
|
+
}
|
|
40
|
+
}
|