@drawbridge/drawbridge-utils 0.0.28 → 0.0.29

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/dist/ai.cjs CHANGED
@@ -88,11 +88,12 @@ var recordTransaction = async ({
88
88
  balance,
89
89
  provider,
90
90
  stripeInvoiceId,
91
- stripeEventId
91
+ stripeEventId,
92
+ session
92
93
  }) => {
93
94
  await db.create({
94
95
  authenticated,
95
- collection: "transactions",
96
+ collection: "transaction",
96
97
  data: {
97
98
  userId,
98
99
  billable: {
@@ -105,7 +106,8 @@ var recordTransaction = async ({
105
106
  ...provider && { provider },
106
107
  ...stripeInvoiceId && { stripeInvoiceId },
107
108
  ...stripeEventId && { stripeEventId }
108
- }
109
+ },
110
+ ...session && { options: { session } }
109
111
  });
110
112
  };
111
113
  var InsufficientCreditsError = class extends Error {
@@ -124,7 +126,8 @@ var debit = async ({
124
126
  tags = [],
125
127
  provider,
126
128
  stripeInvoiceId,
127
- stripeEventId
129
+ stripeEventId,
130
+ session
128
131
  }) => {
129
132
  var _a, _b, _c;
130
133
  if (!Number.isInteger(amount) || amount <= 0) {
@@ -132,7 +135,7 @@ var debit = async ({
132
135
  }
133
136
  const result = await db.update({
134
137
  authenticated,
135
- collection: "users",
138
+ collection: "user",
136
139
  query: {
137
140
  id: userId,
138
141
  "balance.ai": {
@@ -143,7 +146,8 @@ var debit = async ({
143
146
  $inc: {
144
147
  "balance.ai": -amount
145
148
  }
146
- }
149
+ },
150
+ ...session && { options: { session } }
147
151
  });
148
152
  const after = ((_b = (_a = result == null ? void 0 : result.value) == null ? void 0 : _a.balance) == null ? void 0 : _b.ai) ?? ((_c = result == null ? void 0 : result.balance) == null ? void 0 : _c.ai);
149
153
  if (typeof after !== "number") {
@@ -163,7 +167,8 @@ var debit = async ({
163
167
  },
164
168
  provider,
165
169
  stripeInvoiceId,
166
- stripeEventId
170
+ stripeEventId,
171
+ session
167
172
  });
168
173
  return {
169
174
  amount: -amount,
package/dist/ai.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  debit
3
- } from "./chunk-RC5E56UB.js";
3
+ } from "./chunk-H735KDYS.js";
4
4
  import {
5
5
  circuit
6
6
  } from "./chunk-6HH36OK6.js";
@@ -9,11 +9,12 @@ var recordTransaction = async ({
9
9
  balance,
10
10
  provider,
11
11
  stripeInvoiceId,
12
- stripeEventId
12
+ stripeEventId,
13
+ session
13
14
  }) => {
14
15
  await db.create({
15
16
  authenticated,
16
- collection: "transactions",
17
+ collection: "transaction",
17
18
  data: {
18
19
  userId,
19
20
  billable: {
@@ -26,7 +27,8 @@ var recordTransaction = async ({
26
27
  ...provider && { provider },
27
28
  ...stripeInvoiceId && { stripeInvoiceId },
28
29
  ...stripeEventId && { stripeEventId }
29
- }
30
+ },
31
+ ...session && { options: { session } }
30
32
  });
31
33
  };
32
34
  var credit = async ({
@@ -37,7 +39,8 @@ var credit = async ({
37
39
  category,
38
40
  tags = [],
39
41
  stripeInvoiceId,
40
- stripeEventId
42
+ stripeEventId,
43
+ session
41
44
  }) => {
42
45
  var _a, _b, _c;
43
46
  if (!Number.isInteger(amount) || amount <= 0) {
@@ -45,7 +48,7 @@ var credit = async ({
45
48
  }
46
49
  const result = await db.update({
47
50
  authenticated,
48
- collection: "users",
51
+ collection: "user",
49
52
  query: {
50
53
  id: userId
51
54
  },
@@ -53,7 +56,8 @@ var credit = async ({
53
56
  $inc: {
54
57
  "balance.ai": amount
55
58
  }
56
- }
59
+ },
60
+ ...session && { options: { session } }
57
61
  });
58
62
  const after = ((_b = (_a = result == null ? void 0 : result.value) == null ? void 0 : _a.balance) == null ? void 0 : _b.ai) ?? ((_c = result == null ? void 0 : result.balance) == null ? void 0 : _c.ai);
59
63
  if (typeof after !== "number") {
@@ -72,7 +76,8 @@ var credit = async ({
72
76
  after
73
77
  },
74
78
  stripeInvoiceId,
75
- stripeEventId
79
+ stripeEventId,
80
+ session
76
81
  });
77
82
  return {
78
83
  balance: {
@@ -97,7 +102,8 @@ var debit = async ({
97
102
  tags = [],
98
103
  provider,
99
104
  stripeInvoiceId,
100
- stripeEventId
105
+ stripeEventId,
106
+ session
101
107
  }) => {
102
108
  var _a, _b, _c;
103
109
  if (!Number.isInteger(amount) || amount <= 0) {
@@ -105,7 +111,7 @@ var debit = async ({
105
111
  }
106
112
  const result = await db.update({
107
113
  authenticated,
108
- collection: "users",
114
+ collection: "user",
109
115
  query: {
110
116
  id: userId,
111
117
  "balance.ai": {
@@ -116,7 +122,8 @@ var debit = async ({
116
122
  $inc: {
117
123
  "balance.ai": -amount
118
124
  }
119
- }
125
+ },
126
+ ...session && { options: { session } }
120
127
  });
121
128
  const after = ((_b = (_a = result == null ? void 0 : result.value) == null ? void 0 : _a.balance) == null ? void 0 : _b.ai) ?? ((_c = result == null ? void 0 : result.balance) == null ? void 0 : _c.ai);
122
129
  if (typeof after !== "number") {
@@ -136,7 +143,8 @@ var debit = async ({
136
143
  },
137
144
  provider,
138
145
  stripeInvoiceId,
139
- stripeEventId
146
+ stripeEventId,
147
+ session
140
148
  });
141
149
  return {
142
150
  amount: -amount,
@@ -34,11 +34,12 @@ var recordTransaction = async ({
34
34
  balance,
35
35
  provider,
36
36
  stripeInvoiceId,
37
- stripeEventId
37
+ stripeEventId,
38
+ session
38
39
  }) => {
39
40
  await db.create({
40
41
  authenticated,
41
- collection: "transactions",
42
+ collection: "transaction",
42
43
  data: {
43
44
  userId,
44
45
  billable: {
@@ -51,7 +52,8 @@ var recordTransaction = async ({
51
52
  ...provider && { provider },
52
53
  ...stripeInvoiceId && { stripeInvoiceId },
53
54
  ...stripeEventId && { stripeEventId }
54
- }
55
+ },
56
+ ...session && { options: { session } }
55
57
  });
56
58
  };
57
59
  var credit = async ({
@@ -62,7 +64,8 @@ var credit = async ({
62
64
  category,
63
65
  tags = [],
64
66
  stripeInvoiceId,
65
- stripeEventId
67
+ stripeEventId,
68
+ session
66
69
  }) => {
67
70
  var _a, _b, _c;
68
71
  if (!Number.isInteger(amount) || amount <= 0) {
@@ -70,7 +73,7 @@ var credit = async ({
70
73
  }
71
74
  const result = await db.update({
72
75
  authenticated,
73
- collection: "users",
76
+ collection: "user",
74
77
  query: {
75
78
  id: userId
76
79
  },
@@ -78,7 +81,8 @@ var credit = async ({
78
81
  $inc: {
79
82
  "balance.ai": amount
80
83
  }
81
- }
84
+ },
85
+ ...session && { options: { session } }
82
86
  });
83
87
  const after = ((_b = (_a = result == null ? void 0 : result.value) == null ? void 0 : _a.balance) == null ? void 0 : _b.ai) ?? ((_c = result == null ? void 0 : result.balance) == null ? void 0 : _c.ai);
84
88
  if (typeof after !== "number") {
@@ -97,7 +101,8 @@ var credit = async ({
97
101
  after
98
102
  },
99
103
  stripeInvoiceId,
100
- stripeEventId
104
+ stripeEventId,
105
+ session
101
106
  });
102
107
  return {
103
108
  balance: {
@@ -122,7 +127,8 @@ var debit = async ({
122
127
  tags = [],
123
128
  provider,
124
129
  stripeInvoiceId,
125
- stripeEventId
130
+ stripeEventId,
131
+ session
126
132
  }) => {
127
133
  var _a, _b, _c;
128
134
  if (!Number.isInteger(amount) || amount <= 0) {
@@ -130,7 +136,7 @@ var debit = async ({
130
136
  }
131
137
  const result = await db.update({
132
138
  authenticated,
133
- collection: "users",
139
+ collection: "user",
134
140
  query: {
135
141
  id: userId,
136
142
  "balance.ai": {
@@ -141,7 +147,8 @@ var debit = async ({
141
147
  $inc: {
142
148
  "balance.ai": -amount
143
149
  }
144
- }
150
+ },
151
+ ...session && { options: { session } }
145
152
  });
146
153
  const after = ((_b = (_a = result == null ? void 0 : result.value) == null ? void 0 : _a.balance) == null ? void 0 : _b.ai) ?? ((_c = result == null ? void 0 : result.balance) == null ? void 0 : _c.ai);
147
154
  if (typeof after !== "number") {
@@ -161,7 +168,8 @@ var debit = async ({
161
168
  },
162
169
  provider,
163
170
  stripeInvoiceId,
164
- stripeEventId
171
+ stripeEventId,
172
+ session
165
173
  });
166
174
  return {
167
175
  amount: -amount,
@@ -1,5 +1,5 @@
1
1
  // Generic ledger primitives — authoritative balance for user.balance.ai lives
2
- // in Mongo, and every change to that balance is recorded in the `transactions`
2
+ // in Mongo, and every change to that balance is recorded in the `transaction`
3
3
  // collection. Domain-specific helpers (e.g., LLM pricing in ./ai.js) call into
4
4
  // credit/debit with pre-computed amounts.
5
5
  //
@@ -14,9 +14,16 @@
14
14
  //
15
15
  // `db` is the wrapper returned by @drawbridge/mongodb — credit/debit duck-type
16
16
  // against its `.create`, `.update` interface so this module has no hard dep.
17
-
18
- // Internal: write a transactions row. The user.balance.ai update happens in the
19
- // callers (credit/debit) so we can keep the atomic gate-and-decrement contract.
17
+ //
18
+ // Both helpers accept an optional `session` so the caller can enroll them in
19
+ // a larger Mongo transaction. When session is omitted, the balance update +
20
+ // transaction insert run as two separate atomic ops (acceptable: balance is
21
+ // always correct, worst case is a missing ledger row on a Mongo blip between
22
+ // the two writes).
23
+
24
+ // Internal: write a transaction row. The user.balance.ai update happens in
25
+ // the callers (credit/debit) so we can keep the atomic gate-and-decrement
26
+ // contract.
20
27
  const recordTransaction = async ({
21
28
  db,
22
29
  authenticated,
@@ -27,12 +34,13 @@ const recordTransaction = async ({
27
34
  balance,
28
35
  provider,
29
36
  stripeInvoiceId,
30
- stripeEventId
37
+ stripeEventId,
38
+ session
31
39
  }) => {
32
40
 
33
41
  await db.create({
34
42
  authenticated,
35
- collection : 'transactions',
43
+ collection : 'transaction',
36
44
  data : {
37
45
  userId,
38
46
  billable : {
@@ -45,14 +53,15 @@ const recordTransaction = async ({
45
53
  ...( provider && { provider }),
46
54
  ...( stripeInvoiceId && { stripeInvoiceId }),
47
55
  ...( stripeEventId && { stripeEventId })
48
- }
56
+ },
57
+ ...( session && { options : { session } })
49
58
  });
50
59
 
51
60
  };
52
61
 
53
62
  // Additive: grant credits to a user. Used by welcome flow, top-up webhook,
54
63
  // admin promo, refunds. If stripeEventId is provided, the unique sparse index
55
- // on the transactions collection makes the insert idempotent against webhook
64
+ // on the transaction collection makes the insert idempotent against webhook
56
65
  // retries — second call no-ops at the Mongo layer.
57
66
  const credit = async ({
58
67
  db,
@@ -62,7 +71,8 @@ const credit = async ({
62
71
  category,
63
72
  tags = [],
64
73
  stripeInvoiceId,
65
- stripeEventId
74
+ stripeEventId,
75
+ session
66
76
  }) => {
67
77
 
68
78
  if( !Number.isInteger( amount ) || amount <= 0 ){
@@ -73,7 +83,7 @@ const credit = async ({
73
83
 
74
84
  const result = await db.update({
75
85
  authenticated,
76
- collection : 'users',
86
+ collection : 'user',
77
87
  query : {
78
88
  id : userId
79
89
  },
@@ -81,7 +91,8 @@ const credit = async ({
81
91
  $inc : {
82
92
  'balance.ai' : amount
83
93
  }
84
- }
94
+ },
95
+ ...( session && { options : { session } })
85
96
  });
86
97
 
87
98
  const after = result?.value?.balance?.ai ?? result?.balance?.ai;
@@ -106,7 +117,8 @@ const credit = async ({
106
117
  after
107
118
  },
108
119
  stripeInvoiceId,
109
- stripeEventId
120
+ stripeEventId,
121
+ session
110
122
  });
111
123
 
112
124
  return {
@@ -144,7 +156,8 @@ const debit = async ({
144
156
  tags = [],
145
157
  provider,
146
158
  stripeInvoiceId,
147
- stripeEventId
159
+ stripeEventId,
160
+ session
148
161
  }) => {
149
162
 
150
163
  if( !Number.isInteger( amount ) || amount <= 0 ){
@@ -155,7 +168,7 @@ const debit = async ({
155
168
 
156
169
  const result = await db.update({
157
170
  authenticated,
158
- collection : 'users',
171
+ collection : 'user',
159
172
  query : {
160
173
  id : userId,
161
174
  'balance.ai' : {
@@ -166,7 +179,8 @@ const debit = async ({
166
179
  $inc : {
167
180
  'balance.ai' : -amount
168
181
  }
169
- }
182
+ },
183
+ ...( session && { options : { session } })
170
184
  });
171
185
 
172
186
  const after = result?.value?.balance?.ai ?? result?.balance?.ai;
@@ -192,7 +206,8 @@ const debit = async ({
192
206
  },
193
207
  provider,
194
208
  stripeInvoiceId,
195
- stripeEventId
209
+ stripeEventId,
210
+ session
196
211
  });
197
212
 
198
213
  return {
@@ -1,5 +1,5 @@
1
1
  // Generic ledger primitives — authoritative balance for user.balance.ai lives
2
- // in Mongo, and every change to that balance is recorded in the `transactions`
2
+ // in Mongo, and every change to that balance is recorded in the `transaction`
3
3
  // collection. Domain-specific helpers (e.g., LLM pricing in ./ai.js) call into
4
4
  // credit/debit with pre-computed amounts.
5
5
  //
@@ -14,9 +14,16 @@
14
14
  //
15
15
  // `db` is the wrapper returned by @drawbridge/mongodb — credit/debit duck-type
16
16
  // against its `.create`, `.update` interface so this module has no hard dep.
17
-
18
- // Internal: write a transactions row. The user.balance.ai update happens in the
19
- // callers (credit/debit) so we can keep the atomic gate-and-decrement contract.
17
+ //
18
+ // Both helpers accept an optional `session` so the caller can enroll them in
19
+ // a larger Mongo transaction. When session is omitted, the balance update +
20
+ // transaction insert run as two separate atomic ops (acceptable: balance is
21
+ // always correct, worst case is a missing ledger row on a Mongo blip between
22
+ // the two writes).
23
+
24
+ // Internal: write a transaction row. The user.balance.ai update happens in
25
+ // the callers (credit/debit) so we can keep the atomic gate-and-decrement
26
+ // contract.
20
27
  const recordTransaction = async ({
21
28
  db,
22
29
  authenticated,
@@ -27,12 +34,13 @@ const recordTransaction = async ({
27
34
  balance,
28
35
  provider,
29
36
  stripeInvoiceId,
30
- stripeEventId
37
+ stripeEventId,
38
+ session
31
39
  }) => {
32
40
 
33
41
  await db.create({
34
42
  authenticated,
35
- collection : 'transactions',
43
+ collection : 'transaction',
36
44
  data : {
37
45
  userId,
38
46
  billable : {
@@ -45,14 +53,15 @@ const recordTransaction = async ({
45
53
  ...( provider && { provider }),
46
54
  ...( stripeInvoiceId && { stripeInvoiceId }),
47
55
  ...( stripeEventId && { stripeEventId })
48
- }
56
+ },
57
+ ...( session && { options : { session } })
49
58
  });
50
59
 
51
60
  };
52
61
 
53
62
  // Additive: grant credits to a user. Used by welcome flow, top-up webhook,
54
63
  // admin promo, refunds. If stripeEventId is provided, the unique sparse index
55
- // on the transactions collection makes the insert idempotent against webhook
64
+ // on the transaction collection makes the insert idempotent against webhook
56
65
  // retries — second call no-ops at the Mongo layer.
57
66
  const credit = async ({
58
67
  db,
@@ -62,7 +71,8 @@ const credit = async ({
62
71
  category,
63
72
  tags = [],
64
73
  stripeInvoiceId,
65
- stripeEventId
74
+ stripeEventId,
75
+ session
66
76
  }) => {
67
77
 
68
78
  if( !Number.isInteger( amount ) || amount <= 0 ){
@@ -73,7 +83,7 @@ const credit = async ({
73
83
 
74
84
  const result = await db.update({
75
85
  authenticated,
76
- collection : 'users',
86
+ collection : 'user',
77
87
  query : {
78
88
  id : userId
79
89
  },
@@ -81,7 +91,8 @@ const credit = async ({
81
91
  $inc : {
82
92
  'balance.ai' : amount
83
93
  }
84
- }
94
+ },
95
+ ...( session && { options : { session } })
85
96
  });
86
97
 
87
98
  const after = result?.value?.balance?.ai ?? result?.balance?.ai;
@@ -106,7 +117,8 @@ const credit = async ({
106
117
  after
107
118
  },
108
119
  stripeInvoiceId,
109
- stripeEventId
120
+ stripeEventId,
121
+ session
110
122
  });
111
123
 
112
124
  return {
@@ -144,7 +156,8 @@ const debit = async ({
144
156
  tags = [],
145
157
  provider,
146
158
  stripeInvoiceId,
147
- stripeEventId
159
+ stripeEventId,
160
+ session
148
161
  }) => {
149
162
 
150
163
  if( !Number.isInteger( amount ) || amount <= 0 ){
@@ -155,7 +168,7 @@ const debit = async ({
155
168
 
156
169
  const result = await db.update({
157
170
  authenticated,
158
- collection : 'users',
171
+ collection : 'user',
159
172
  query : {
160
173
  id : userId,
161
174
  'balance.ai' : {
@@ -166,7 +179,8 @@ const debit = async ({
166
179
  $inc : {
167
180
  'balance.ai' : -amount
168
181
  }
169
- }
182
+ },
183
+ ...( session && { options : { session } })
170
184
  });
171
185
 
172
186
  const after = result?.value?.balance?.ai ?? result?.balance?.ai;
@@ -192,7 +206,8 @@ const debit = async ({
192
206
  },
193
207
  provider,
194
208
  stripeInvoiceId,
195
- stripeEventId
209
+ stripeEventId,
210
+ session
196
211
  });
197
212
 
198
213
  return {
@@ -2,7 +2,7 @@ import {
2
2
  InsufficientCreditsError,
3
3
  credit,
4
4
  debit
5
- } from "./chunk-RC5E56UB.js";
5
+ } from "./chunk-H735KDYS.js";
6
6
  export {
7
7
  InsufficientCreditsError,
8
8
  credit,
package/package.json CHANGED
@@ -103,5 +103,5 @@
103
103
  "build": "tsup && npm publish"
104
104
  },
105
105
  "types": "dist/index.d.ts",
106
- "version": "0.0.28"
106
+ "version": "0.0.29"
107
107
  }