@tofound/billing 0.1.0 → 0.2.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.
Files changed (45) hide show
  1. package/CHANGELOG.md +76 -0
  2. package/README.md +58 -13
  3. package/dist/catalog/catalog.d.ts +115 -0
  4. package/dist/catalog/catalog.js +201 -0
  5. package/dist/domain/types.d.ts +3 -1
  6. package/dist/drizzle/index.d.ts +2 -0
  7. package/dist/drizzle/index.js +2 -0
  8. package/dist/entitlement/resolve.d.ts +76 -0
  9. package/dist/entitlement/resolve.js +122 -0
  10. package/dist/index.d.ts +10 -0
  11. package/dist/index.js +9 -0
  12. package/dist/providers/polar/events.d.ts +17 -0
  13. package/dist/providers/polar/events.js +213 -0
  14. package/dist/providers/polar/index.d.ts +50 -0
  15. package/dist/providers/polar/index.js +271 -0
  16. package/dist/providers/polar/signature.d.ts +23 -0
  17. package/dist/providers/polar/signature.js +52 -0
  18. package/dist/providers/port.d.ts +6 -0
  19. package/dist/renewal/renewal.d.ts +69 -0
  20. package/dist/renewal/renewal.js +198 -0
  21. package/dist/stores/testing.d.ts +4 -2
  22. package/dist/stores/testing.js +4 -2
  23. package/dist/terms/conformance.d.ts +17 -0
  24. package/dist/terms/conformance.js +198 -0
  25. package/dist/terms/decide.d.ts +97 -0
  26. package/dist/terms/decide.js +176 -0
  27. package/dist/terms/drizzle.d.ts +12 -0
  28. package/dist/terms/drizzle.js +131 -0
  29. package/dist/terms/memory.d.ts +23 -0
  30. package/dist/terms/memory.js +126 -0
  31. package/dist/terms/plans.d.ts +85 -0
  32. package/dist/terms/plans.js +331 -0
  33. package/dist/terms/ports.d.ts +55 -0
  34. package/dist/terms/ports.js +1 -0
  35. package/dist/terms/rules.d.ts +11 -0
  36. package/dist/terms/rules.js +36 -0
  37. package/dist/terms/schema.d.ts +711 -0
  38. package/dist/terms/schema.js +70 -0
  39. package/dist/terms/snapshot.d.ts +20 -0
  40. package/dist/terms/snapshot.js +134 -0
  41. package/dist/terms/types.d.ts +85 -0
  42. package/dist/terms/types.js +4 -0
  43. package/docs/API.md +223 -4
  44. package/docs/SCHEMA.md +57 -2
  45. package/package.json +6 -1
package/docs/SCHEMA.md CHANGED
@@ -35,7 +35,7 @@ package's `engines` floor).
35
35
  | `prefix` | `billing_` | lower snake case; prefixes tables, indexes and constraints |
36
36
  | `accountId` | — | the host-typed subject column (`uuid`, `text` …), NOT NULL, optionally an FK to the host's subject table |
37
37
 
38
- ## Tables (0.1)
38
+ ## Tables (0.1, 0.2)
39
39
 
40
40
  `<prefix>accounts` — one row per account, created on first posting.
41
41
 
@@ -66,12 +66,67 @@ Inbox, checkout-session, purchase, term and account-token tables are declared
66
66
  by their own modules through `billingTableBuilder(config)` — same schema, same
67
67
  prefix, same rules — and a host re-exports them beside these.
68
68
 
69
- `createIngestTables(config)` (P1) adds `<prefix>inbox` (UNIQUE provider +
69
+ Every factory returns an object of Drizzle tables. drizzle-kit only sees a
70
+ table that is a **named export** of a file its `schema` glob covers. So a host
71
+ destructures and exports every key a factory returns; a key it leaves out is
72
+ a table that never gets a migration:
73
+
74
+ | Factory (`@tofound/billing/drizzle`) | Returned keys → tables |
75
+ |---|---|
76
+ | `createBillingTables(config)` | `billingAccounts` → `<prefix>accounts`, `billingEntries` → `<prefix>entries` |
77
+ | `createIngestTables(config)` | `billingInbox` → `<prefix>inbox`, `billingCheckoutSessions` → `<prefix>checkout_sessions`, `billingPurchases` → `<prefix>purchases` |
78
+ | `createTermTables(config)` (0.2) | `billingTerms` → `<prefix>terms`, `billingTermAccounts` → `<prefix>term_accounts` |
79
+
80
+ ```ts
81
+ export const { billingInbox, billingCheckoutSessions, billingPurchases } = createIngestTables(config)
82
+ export const { billingTerms, billingTermAccounts } = createTermTables(config)
83
+ ```
84
+
85
+ Pass the same objects to the stores (`createDrizzleIngestStore({ db, tables:
86
+ { billingInbox, billingCheckoutSessions, billingPurchases } })`,
87
+ `createDrizzleTermStore({ db, tables: { billingTerms, billingTermAccounts }
88
+ })`).
89
+
90
+ `createIngestTables(config)` (0.1) returns `{ billingInbox,
91
+ billingCheckoutSessions, billingPurchases }`: `<prefix>inbox` (UNIQUE provider +
70
92
  event_id; due index on status + next_attempt_at), `<prefix>checkout_sessions`
71
93
  (CHECK amount > 0, credits > 0) and `<prefix>purchases` (UNIQUE provider +
72
94
  environment + external_id). `ingestTableNames(prefix)` lists them for
73
95
  `checkAdditiveDdl`.
74
96
 
97
+ `createTermTables(config)` (0.2) returns `{ billingTerms, billingTermAccounts }`:
98
+ `<prefix>terms`, one row per term of any source, and `<prefix>term_accounts`:
99
+
100
+ | Column | Type | |
101
+ |---|---|---|
102
+ | `id` | varchar(64) PK | |
103
+ | `account_id` | host type | |
104
+ | `source`, `environment`, `external_id` | varchar(16), varchar(16), text | UNIQUE (`source`, `environment`, `external_id`); NULL for credit terms |
105
+ | `plan_key`, `next_plan_key` | varchar(64) | catalog keys |
106
+ | `status` | varchar(16) not null | scheduled, active, grace, past_due, expired, superseded, revoked |
107
+ | `market` | varchar(64) | |
108
+ | `starts_at`, `current_period_start`, `current_period_end` | timestamptz not null | |
109
+ | `interval_months` | integer not null | CHECK > 0 |
110
+ | `auto_renew`, `cancel_at_period_end` | boolean not null default false | |
111
+ | `grace_until` | timestamptz | |
112
+ | `price_minor`, `price_unit` | bigint, varchar(16) | |
113
+ | `period_items`, `meta` | jsonb | |
114
+ | `superseded_by` | varchar(64) | |
115
+ | `last_event_at`, `last_event_ref` | timestamptz, text | provider watermark |
116
+ | `created_at`, `updated_at` | timestamptz not null | |
117
+
118
+ Partial unique indexes keep one open credit term (`source = 'credit'` and
119
+ status active / grace / past_due) and one `scheduled` term per account;
120
+ `(account_id, status)` serves the account lock and lists; three partial
121
+ indexes (`current_period_end` of active credit terms, `starts_at` of
122
+ scheduled, `grace_until` of past_due) bound `listDue` by the due rows.
123
+
124
+ `<prefix>term_accounts` (`account_id` host type, PK; `created_at`) holds one
125
+ guard row per account. `lockAccountTerms` upserts and locks it before the
126
+ account's term rows, so two first writers of an account with no terms yet (a
127
+ credit purchase and a provider snapshot) still serialize.
128
+ `termTableNames(prefix)` lists both for `checkAdditiveDdl`.
129
+
75
130
  ## Rules: additive only, never a table rewrite
76
131
 
77
132
  Hosts deploy by migrating the live database before rolling the container, so
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tofound/billing",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Shared subscription and credit library by 2found: credit ledger, quote/charge waterfall, Drizzle schema factory.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -22,6 +22,11 @@
22
22
  "import": "./dist/providers/sepay/index.js",
23
23
  "default": "./dist/providers/sepay/index.js"
24
24
  },
25
+ "./providers/polar": {
26
+ "types": "./dist/providers/polar/index.d.ts",
27
+ "import": "./dist/providers/polar/index.js",
28
+ "default": "./dist/providers/polar/index.js"
29
+ },
25
30
  "./testing": {
26
31
  "types": "./dist/stores/testing.d.ts",
27
32
  "import": "./dist/stores/testing.js",