@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.
- package/CHANGELOG.md +76 -0
- package/README.md +58 -13
- package/dist/catalog/catalog.d.ts +115 -0
- package/dist/catalog/catalog.js +201 -0
- package/dist/domain/types.d.ts +3 -1
- package/dist/drizzle/index.d.ts +2 -0
- package/dist/drizzle/index.js +2 -0
- package/dist/entitlement/resolve.d.ts +76 -0
- package/dist/entitlement/resolve.js +122 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +9 -0
- package/dist/providers/polar/events.d.ts +17 -0
- package/dist/providers/polar/events.js +213 -0
- package/dist/providers/polar/index.d.ts +50 -0
- package/dist/providers/polar/index.js +271 -0
- package/dist/providers/polar/signature.d.ts +23 -0
- package/dist/providers/polar/signature.js +52 -0
- package/dist/providers/port.d.ts +6 -0
- package/dist/renewal/renewal.d.ts +69 -0
- package/dist/renewal/renewal.js +198 -0
- package/dist/stores/testing.d.ts +4 -2
- package/dist/stores/testing.js +4 -2
- package/dist/terms/conformance.d.ts +17 -0
- package/dist/terms/conformance.js +198 -0
- package/dist/terms/decide.d.ts +97 -0
- package/dist/terms/decide.js +176 -0
- package/dist/terms/drizzle.d.ts +12 -0
- package/dist/terms/drizzle.js +131 -0
- package/dist/terms/memory.d.ts +23 -0
- package/dist/terms/memory.js +126 -0
- package/dist/terms/plans.d.ts +85 -0
- package/dist/terms/plans.js +331 -0
- package/dist/terms/ports.d.ts +55 -0
- package/dist/terms/ports.js +1 -0
- package/dist/terms/rules.d.ts +11 -0
- package/dist/terms/rules.js +36 -0
- package/dist/terms/schema.d.ts +711 -0
- package/dist/terms/schema.js +70 -0
- package/dist/terms/snapshot.d.ts +20 -0
- package/dist/terms/snapshot.js +134 -0
- package/dist/terms/types.d.ts +85 -0
- package/dist/terms/types.js +4 -0
- package/docs/API.md +223 -4
- package/docs/SCHEMA.md +57 -2
- 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
|
-
|
|
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.
|
|
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",
|