@chatman-media/storage 0.0.0 → 1.4.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Alexander Kireev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,155 @@
1
+ # @chatman-media/storage
2
+
3
+ [![npm](https://img.shields.io/npm/v/@chatman-media/storage)](https://www.npmjs.com/package/@chatman-media/storage)
4
+ [![CI](https://github.com/chatman-media/lead-engine/actions/workflows/ci.yml/badge.svg)](https://github.com/chatman-media/lead-engine/actions/workflows/ci.yml)
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-strict-3178C6?logo=typescript&logoColor=white)](https://www.typescriptlang.org/)
6
+ [![Bun](https://img.shields.io/badge/runtime-Bun-fbf0df?logo=bun&logoColor=black)](https://bun.sh)
7
+ [![License: MIT](https://img.shields.io/badge/license-MIT-green)](LICENSE)
8
+ [![used by @chatman-media/sales](https://img.shields.io/badge/used%20by-@chatman--media%2Fsales-6366f1)](https://github.com/chatman-media/lead-engine/tree/main/packages/sales)
9
+
10
+ **PostgreSQL storage adapters for [@chatman-media/sales](https://github.com/chatman-media/lead-engine/tree/main/packages/sales).** Drizzle ORM implementations of all engine repository interfaces — users, conversations, leads, skills, ELO ratings, self-play matches, pairwise comparisons, and shadow evaluations.
11
+
12
+ Built from the production persistence layer of the Lead Engine platform — multi-tenant SaaS with Postgres RLS.
13
+
14
+ ---
15
+
16
+ ## What's inside
17
+
18
+ | Repo class | Interface | What it does |
19
+ |------------|-----------|-------------|
20
+ | `PgUsersRepo` | `IUsersRepo` | Upsert Telegram user accounts by `telegramId` |
21
+ | `PgConversationsRepo` | `IConversationsRepo` | Create conversations linked to a user + style slug |
22
+ | `PgLeadsRepo` | `ILeadsRepo` | Create sales leads within a conversation |
23
+ | `PgSkillsRepo` | `ISkillsRepo` | Fetch all skill definitions for a given style |
24
+ | `PgSkillOutcomesRepo` | `ISkillOutcomesRepo` | Record per-lead skill outcomes; aggregate win/loss/draw counts |
25
+ | `PgStyleRatingsRepo` | `IStyleRatingsRepo` | Get/set ELO ratings per style (default 1500, upserted on change) |
26
+ | `PgSelfPlayMatchesRepo` | `ISelfPlayMatchesRepo` | Insert & query self-play match records with full transcripts |
27
+ | `PgPairwiseMatchesRepo` | `IPairwiseMatchesRepo` | Record head-to-head comparisons between two self-play matches |
28
+ | `PgShadowEvaluationsRepo` | `IShadowEvaluationsRepo` | Track evaluation run state (status, decision, pair counts, error) |
29
+
30
+ ---
31
+
32
+ ## Install
33
+
34
+ ```bash
35
+ bun add @chatman-media/storage # Bun
36
+ npm install @chatman-media/storage # npm / pnpm / yarn
37
+ ```
38
+
39
+ Peer dependencies:
40
+
41
+ ```bash
42
+ bun add drizzle-orm postgres @chatman-media/sales
43
+ ```
44
+
45
+ ---
46
+
47
+ ## Quick start
48
+
49
+ ```typescript
50
+ import postgres from "postgres";
51
+ import { drizzle } from "drizzle-orm/postgres-js";
52
+ import { createPgRepos, schema } from "@chatman-media/storage";
53
+
54
+ const client = postgres(process.env.DATABASE_URL!);
55
+ const db = drizzle(client, { schema });
56
+
57
+ const { users, conversations, leads, skills, outcomes, ratings, matches, pairwise, shadowEvals } =
58
+ createPgRepos(db);
59
+
60
+ // upsert a Telegram user
61
+ const { id: userId } = await users.upsert({ telegramId: 123456789, username: "alice" });
62
+
63
+ // start a conversation and a lead
64
+ const { id: convId } = await conversations.create({ userId, styleSlug: "marina-prime-v1" });
65
+ const { id: leadId } = await leads.create({ conversationId: convId });
66
+
67
+ // record a skill outcome
68
+ await outcomes.record({ skillSlug: "mirroring", styleSlug: "marina-prime-v1", leadId, outcome: "won", source: "judge" });
69
+
70
+ // read ELO rating
71
+ const elo = await ratings.getRating(42); // → 1500 (default)
72
+ ```
73
+
74
+ ## Storage interfaces
75
+
76
+ All classes implement interfaces from `@chatman-media/sales` — swap the implementation without touching engine code:
77
+
78
+ ```typescript
79
+ import type { ISelfPlayMatchesRepo } from "@chatman-media/sales";
80
+
81
+ // Implement for any backend (SQLite, in-memory, …):
82
+ class MyMatchesRepo implements ISelfPlayMatchesRepo {
83
+ async insert(match) { /* ... */ }
84
+ async byId(id) { /* ... */ }
85
+ async list(opts) { /* ... */ }
86
+ }
87
+ ```
88
+
89
+ ---
90
+
91
+ ## Database schema
92
+
93
+ | Table | Description |
94
+ |-------|-------------|
95
+ | `users` | Telegram user accounts (`telegram_id` unique) |
96
+ | `conversations` | Conversations linked to a user and a style slug |
97
+ | `leads` | Sales leads within a conversation |
98
+ | `skill_outcomes` | Per-lead skill performance records (`won` / `lost` / `draw`) |
99
+ | `style_ratings` | ELO ratings per style (default 1500), upserted on change |
100
+ | `skills` | Skill definitions — slug, family, prompt fragment, applicable stages (JSONB) |
101
+ | `self_play_matches` | Single-style match records with full transcript (JSONB) |
102
+ | `pairwise_matches` | Head-to-head comparisons between two self-play matches |
103
+ | `shadow_evaluations` | Evaluation run state — status, decision, pair counts, error |
104
+
105
+ ---
106
+
107
+ ## Architecture
108
+
109
+ ```
110
+ @chatman-media/storage
111
+ ├── schema.ts 9 Drizzle table definitions (pgTable)
112
+ ├── pg/
113
+ │ └── index.ts All repo classes + createPgRepos()
114
+ └── index.ts Public exports
115
+ ```
116
+
117
+ Depends on [`@chatman-media/sales`](https://github.com/chatman-media/lead-engine/tree/main/packages/sales) for repo interfaces and domain types (`SelfPlayMatchRecord`, `SkillAggregate`, `SkillRow`, …).
118
+
119
+ ---
120
+
121
+ ## Environment variables
122
+
123
+ | Variable | Description |
124
+ |----------|-------------|
125
+ | `DATABASE_URL` | PostgreSQL connection string — used by drizzle-kit CLI commands |
126
+
127
+ ## Commands
128
+
129
+ | Command | Description |
130
+ |---------|-------------|
131
+ | `npm run build` | Bundle `src/index.ts` → `dist/` with type declarations |
132
+ | `npm run typecheck` | TypeScript type check without emitting |
133
+ | `npm run check` | Lint source with Biome |
134
+ | `npm run format` | Auto-format source with Biome |
135
+ | `npm run db:generate` | Generate Drizzle migration files from schema changes |
136
+ | `npm run db:migrate` | Apply pending migrations to the database |
137
+ | `npm test` | Run tests with bun |
138
+
139
+ ## Migrations
140
+
141
+ ```bash
142
+ # After changing src/schema.ts, generate a new migration:
143
+ npm run db:generate
144
+
145
+ # Apply all pending migrations:
146
+ DATABASE_URL=postgres://... npm run db:migrate
147
+ ```
148
+
149
+ Migration files are written to `./migrations/` and tracked in version control.
150
+
151
+ ---
152
+
153
+ ## License
154
+
155
+ [MIT](LICENSE) — Alexander Kireev / [chatman-media](https://github.com/chatman-media)
@@ -0,0 +1,4 @@
1
+ export * as schema from "./schema.ts";
2
+ export * from "./schema.ts";
3
+ export { applyAllMigrations, createIsolatedDb, dropIsolatedDb, tryConnectToPg, } from "./integration-helpers.ts";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,cAAc,aAAa,CAAC;AAI5B,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,cAAc,EACd,cAAc,GACf,MAAM,0BAA0B,CAAC"}