@zonastery/bursar 0.0.2 → 1.0.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/README.md +118 -333
- package/dist/billing/auto-recharge-port.d.ts +27 -0
- package/dist/billing/auto-recharge-port.d.ts.map +1 -0
- package/dist/billing/auto-recharge-port.js +2 -0
- package/dist/billing/auto-recharge-port.js.map +1 -0
- package/dist/billing/auto-recharge-service.d.ts +43 -0
- package/dist/billing/auto-recharge-service.d.ts.map +1 -0
- package/dist/billing/auto-recharge-service.js +218 -0
- package/dist/billing/auto-recharge-service.js.map +1 -0
- package/dist/billing/billing-service.d.ts +71 -0
- package/dist/billing/billing-service.d.ts.map +1 -0
- package/dist/billing/billing-service.js +172 -0
- package/dist/billing/billing-service.js.map +1 -0
- package/dist/billing/billing-store.d.ts +45 -13
- package/dist/billing/billing-store.d.ts.map +1 -1
- package/dist/billing/billing-store.js.map +1 -1
- package/dist/billing/contracts.d.ts +149 -0
- package/dist/billing/contracts.d.ts.map +1 -0
- package/dist/billing/contracts.js +2 -0
- package/dist/billing/contracts.js.map +1 -0
- package/dist/billing/event-handlers.d.ts +57 -0
- package/dist/billing/event-handlers.d.ts.map +1 -0
- package/dist/billing/event-handlers.js +684 -0
- package/dist/billing/event-handlers.js.map +1 -0
- package/dist/billing/event-processor.d.ts +20 -0
- package/dist/billing/event-processor.d.ts.map +1 -0
- package/dist/billing/event-processor.js +131 -0
- package/dist/billing/event-processor.js.map +1 -0
- package/dist/billing/financial-event-handlers.d.ts +26 -0
- package/dist/billing/financial-event-handlers.d.ts.map +1 -0
- package/dist/billing/financial-event-handlers.js +241 -0
- package/dist/billing/financial-event-handlers.js.map +1 -0
- package/dist/billing/index.d.ts +9 -5
- package/dist/billing/index.d.ts.map +1 -1
- package/dist/billing/index.js +4 -4
- package/dist/billing/index.js.map +1 -1
- package/dist/billing/management.d.ts +59 -0
- package/dist/billing/management.d.ts.map +1 -0
- package/dist/billing/management.js +113 -0
- package/dist/billing/management.js.map +1 -0
- package/dist/billing/policy-window.d.ts +24 -0
- package/dist/billing/policy-window.d.ts.map +1 -0
- package/dist/billing/policy-window.js +88 -0
- package/dist/billing/policy-window.js.map +1 -0
- package/dist/billing/postgres/repositories/auto-recharge.d.ts +32 -0
- package/dist/billing/postgres/repositories/auto-recharge.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/auto-recharge.js +180 -0
- package/dist/billing/postgres/repositories/auto-recharge.js.map +1 -0
- package/dist/billing/postgres/repositories/customer.d.ts +12 -0
- package/dist/billing/postgres/repositories/customer.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/customer.js +33 -0
- package/dist/billing/postgres/repositories/customer.js.map +1 -0
- package/dist/billing/postgres/repositories/dispute.d.ts +7 -0
- package/dist/billing/postgres/repositories/dispute.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/dispute.js +18 -0
- package/dist/billing/postgres/repositories/dispute.js.map +1 -0
- package/dist/billing/postgres/repositories/event.d.ts +21 -0
- package/dist/billing/postgres/repositories/event.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/event.js +45 -0
- package/dist/billing/postgres/repositories/event.js.map +1 -0
- package/dist/billing/postgres/repositories/invoice.d.ts +9 -0
- package/dist/billing/postgres/repositories/invoice.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/invoice.js +36 -0
- package/dist/billing/postgres/repositories/invoice.js.map +1 -0
- package/dist/billing/postgres/repositories/offer.d.ts +26 -0
- package/dist/billing/postgres/repositories/offer.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/offer.js +73 -0
- package/dist/billing/postgres/repositories/offer.js.map +1 -0
- package/dist/billing/postgres/repositories/payment.d.ts +32 -0
- package/dist/billing/postgres/repositories/payment.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/payment.js +78 -0
- package/dist/billing/postgres/repositories/payment.js.map +1 -0
- package/dist/billing/postgres/repositories/preferences.d.ts +15 -0
- package/dist/billing/postgres/repositories/preferences.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/preferences.js +23 -0
- package/dist/billing/postgres/repositories/preferences.js.map +1 -0
- package/dist/billing/postgres/repositories/refund.d.ts +7 -0
- package/dist/billing/postgres/repositories/refund.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/refund.js +32 -0
- package/dist/billing/postgres/repositories/refund.js.map +1 -0
- package/dist/billing/postgres/repositories/subscription.d.ts +46 -0
- package/dist/billing/postgres/repositories/subscription.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/subscription.js +187 -0
- package/dist/billing/postgres/repositories/subscription.js.map +1 -0
- package/dist/billing/postgres/repositories/topup.d.ts +27 -0
- package/dist/billing/postgres/repositories/topup.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/topup.js +51 -0
- package/dist/billing/postgres/repositories/topup.js.map +1 -0
- package/dist/billing/postgres/store.d.ts +89 -0
- package/dist/billing/postgres/store.d.ts.map +1 -0
- package/dist/billing/postgres/store.js +542 -0
- package/dist/billing/postgres/store.js.map +1 -0
- package/dist/billing/service-types.d.ts +37 -0
- package/dist/billing/service-types.d.ts.map +1 -0
- package/dist/billing/service-types.js +2 -0
- package/dist/billing/service-types.js.map +1 -0
- package/dist/billing/types/auto-recharge.d.ts +56 -0
- package/dist/billing/types/auto-recharge.d.ts.map +1 -0
- package/dist/billing/types/auto-recharge.js +2 -0
- package/dist/billing/types/auto-recharge.js.map +1 -0
- package/dist/billing/types/catalog.d.ts +47 -0
- package/dist/billing/types/catalog.d.ts.map +1 -0
- package/dist/billing/types/catalog.js +2 -0
- package/dist/billing/types/catalog.js.map +1 -0
- package/dist/billing/types/common.d.ts +16 -0
- package/dist/billing/types/common.d.ts.map +1 -0
- package/dist/billing/types/common.js +2 -0
- package/dist/billing/types/common.js.map +1 -0
- package/dist/billing/types/documents.d.ts +39 -0
- package/dist/billing/types/documents.d.ts.map +1 -0
- package/dist/billing/types/documents.js +2 -0
- package/dist/billing/types/documents.js.map +1 -0
- package/dist/billing/types/events.d.ts +75 -0
- package/dist/billing/types/events.d.ts.map +1 -0
- package/dist/billing/types/events.js +38 -0
- package/dist/billing/types/events.js.map +1 -0
- package/dist/billing/types/index.d.ts +8 -0
- package/dist/billing/types/index.d.ts.map +1 -0
- package/dist/billing/types/index.js +8 -0
- package/dist/billing/types/index.js.map +1 -0
- package/dist/billing/types/preferences.d.ts +13 -0
- package/dist/billing/types/preferences.d.ts.map +1 -0
- package/dist/billing/types/preferences.js +2 -0
- package/dist/billing/types/preferences.js.map +1 -0
- package/dist/billing/types/subscriptions.d.ts +97 -0
- package/dist/billing/types/subscriptions.d.ts.map +1 -0
- package/dist/billing/types/subscriptions.js +12 -0
- package/dist/billing/types/subscriptions.js.map +1 -0
- package/dist/breakdown.d.ts +3 -1
- package/dist/breakdown.d.ts.map +1 -1
- package/dist/breakdown.js +4 -1
- package/dist/breakdown.js.map +1 -1
- package/dist/browser.d.ts +8 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +7 -0
- package/dist/browser.js.map +1 -0
- package/dist/bursar.d.ts +84 -0
- package/dist/bursar.d.ts.map +1 -0
- package/dist/bursar.js +137 -0
- package/dist/bursar.js.map +1 -0
- package/dist/catalog.d.ts +62 -0
- package/dist/catalog.d.ts.map +1 -0
- package/dist/catalog.js +96 -0
- package/dist/catalog.js.map +1 -0
- package/dist/commerce/errors.d.ts +68 -0
- package/dist/commerce/errors.d.ts.map +1 -0
- package/dist/commerce/errors.js +105 -0
- package/dist/commerce/errors.js.map +1 -0
- package/dist/commerce/index.d.ts +6 -0
- package/dist/commerce/index.d.ts.map +1 -0
- package/dist/commerce/index.js +6 -0
- package/dist/commerce/index.js.map +1 -0
- package/dist/commerce/plan-change.d.ts +10 -0
- package/dist/commerce/plan-change.d.ts.map +1 -0
- package/dist/commerce/plan-change.js +42 -0
- package/dist/commerce/plan-change.js.map +1 -0
- package/dist/commerce/provider-registry.d.ts +18 -0
- package/dist/commerce/provider-registry.d.ts.map +1 -0
- package/dist/commerce/provider-registry.js +70 -0
- package/dist/commerce/provider-registry.js.map +1 -0
- package/dist/commerce/service.d.ts +73 -0
- package/dist/commerce/service.d.ts.map +1 -0
- package/dist/commerce/service.js +1016 -0
- package/dist/commerce/service.js.map +1 -0
- package/dist/commerce/types.d.ts +225 -0
- package/dist/commerce/types.d.ts.map +1 -0
- package/dist/commerce/types.js.map +1 -0
- package/dist/config/parse-access.d.ts +6 -0
- package/dist/config/parse-access.d.ts.map +1 -0
- package/dist/config/parse-access.js +72 -0
- package/dist/config/parse-access.js.map +1 -0
- package/dist/config/parse-commerce.d.ts +3 -0
- package/dist/config/parse-commerce.d.ts.map +1 -0
- package/dist/config/parse-commerce.js +236 -0
- package/dist/config/parse-commerce.js.map +1 -0
- package/dist/config/parse-credits.d.ts +3 -0
- package/dist/config/parse-credits.d.ts.map +1 -0
- package/dist/config/parse-credits.js +99 -0
- package/dist/config/parse-credits.js.map +1 -0
- package/dist/config/parse-plans.d.ts +5 -0
- package/dist/config/parse-plans.d.ts.map +1 -0
- package/dist/config/parse-plans.js +142 -0
- package/dist/config/parse-plans.js.map +1 -0
- package/dist/config/parse-pricing.d.ts +4 -0
- package/dist/config/parse-pricing.d.ts.map +1 -0
- package/dist/config/parse-pricing.js +252 -0
- package/dist/config/parse-pricing.js.map +1 -0
- package/dist/config/parse-utils.d.ts +18 -0
- package/dist/config/parse-utils.d.ts.map +1 -0
- package/dist/config/parse-utils.js +118 -0
- package/dist/config/parse-utils.js.map +1 -0
- package/dist/config/types.d.ts +340 -0
- package/dist/config/types.d.ts.map +1 -0
- package/dist/config/types.js +2 -0
- package/dist/config/types.js.map +1 -0
- package/dist/config.d.ts +12 -23
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +141 -377
- package/dist/config.js.map +1 -1
- package/dist/{stores → credits}/events.d.ts +6 -6
- package/dist/credits/events.d.ts.map +1 -0
- package/dist/{stores → credits}/events.js +5 -5
- package/dist/credits/events.js.map +1 -0
- package/dist/credits/lease-workflow.d.ts +90 -0
- package/dist/credits/lease-workflow.d.ts.map +1 -0
- package/dist/credits/lease-workflow.js +378 -0
- package/dist/credits/lease-workflow.js.map +1 -0
- package/dist/credits/low-balance-monitor.d.ts +26 -0
- package/dist/credits/low-balance-monitor.d.ts.map +1 -0
- package/dist/credits/low-balance-monitor.js +110 -0
- package/dist/credits/low-balance-monitor.js.map +1 -0
- package/dist/credits/postgres/repositories/analytics.d.ts +81 -0
- package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/analytics.js +153 -0
- package/dist/credits/postgres/repositories/analytics.js.map +1 -0
- package/dist/credits/postgres/repositories/balance.d.ts +66 -0
- package/dist/credits/postgres/repositories/balance.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/balance.js +139 -0
- package/dist/credits/postgres/repositories/balance.js.map +1 -0
- package/dist/credits/postgres/repositories/bucket.d.ts +25 -0
- package/dist/credits/postgres/repositories/bucket.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/bucket.js +52 -0
- package/dist/credits/postgres/repositories/bucket.js.map +1 -0
- package/dist/credits/postgres/repositories/deduction.d.ts +64 -0
- package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/deduction.js +154 -0
- package/dist/credits/postgres/repositories/deduction.js.map +1 -0
- package/dist/credits/postgres/repositories/lease.d.ts +69 -0
- package/dist/credits/postgres/repositories/lease.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/lease.js +148 -0
- package/dist/credits/postgres/repositories/lease.js.map +1 -0
- package/dist/credits/postgres/repositories/plan.d.ts +110 -0
- package/dist/credits/postgres/repositories/plan.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/plan.js +194 -0
- package/dist/credits/postgres/repositories/plan.js.map +1 -0
- package/dist/credits/postgres/repositories/pricing.d.ts +28 -0
- package/dist/credits/postgres/repositories/pricing.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/pricing.js +76 -0
- package/dist/credits/postgres/repositories/pricing.js.map +1 -0
- package/dist/credits/postgres/repositories/team.d.ts +63 -0
- package/dist/credits/postgres/repositories/team.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/team.js +120 -0
- package/dist/credits/postgres/repositories/team.js.map +1 -0
- package/dist/credits/postgres/store.d.ts +89 -0
- package/dist/credits/postgres/store.d.ts.map +1 -0
- package/dist/credits/postgres/store.js +838 -0
- package/dist/credits/postgres/store.js.map +1 -0
- package/dist/credits/postgres/value-mappers.d.ts +17 -0
- package/dist/credits/postgres/value-mappers.d.ts.map +1 -0
- package/dist/credits/postgres/value-mappers.js +102 -0
- package/dist/credits/postgres/value-mappers.js.map +1 -0
- package/dist/credits/pricing-runtime.d.ts +35 -0
- package/dist/credits/pricing-runtime.d.ts.map +1 -0
- package/dist/credits/pricing-runtime.js +141 -0
- package/dist/credits/pricing-runtime.js.map +1 -0
- package/dist/credits/queries.d.ts +33 -0
- package/dist/credits/queries.d.ts.map +1 -0
- package/dist/credits/queries.js +71 -0
- package/dist/credits/queries.js.map +1 -0
- package/dist/credits/service-errors.d.ts +4 -0
- package/dist/credits/service-errors.d.ts.map +1 -0
- package/dist/credits/service-errors.js +57 -0
- package/dist/credits/service-errors.js.map +1 -0
- package/dist/credits/service-types.d.ts +122 -0
- package/dist/credits/service-types.d.ts.map +1 -0
- package/dist/credits/service-types.js +2 -0
- package/dist/credits/service-types.js.map +1 -0
- package/dist/credits/service.d.ts +220 -0
- package/dist/credits/service.d.ts.map +1 -0
- package/dist/credits/service.js +640 -0
- package/dist/credits/service.js.map +1 -0
- package/dist/credits/store.d.ts +172 -0
- package/dist/credits/store.d.ts.map +1 -0
- package/dist/{stores/credit-store.js → credits/store.js} +27 -43
- package/dist/credits/store.js.map +1 -0
- package/dist/credits/types/account.d.ts +82 -0
- package/dist/credits/types/account.d.ts.map +1 -0
- package/dist/credits/types/account.js +2 -0
- package/dist/credits/types/account.js.map +1 -0
- package/dist/credits/types/admission.d.ts +39 -0
- package/dist/credits/types/admission.d.ts.map +1 -0
- package/dist/credits/types/admission.js +2 -0
- package/dist/credits/types/admission.js.map +1 -0
- package/dist/credits/types/analytics.d.ts +52 -0
- package/dist/credits/types/analytics.d.ts.map +1 -0
- package/dist/credits/types/analytics.js +2 -0
- package/dist/credits/types/analytics.js.map +1 -0
- package/dist/credits/types/catalog.d.ts +71 -0
- package/dist/credits/types/catalog.d.ts.map +1 -0
- package/dist/credits/types/catalog.js +2 -0
- package/dist/credits/types/catalog.js.map +1 -0
- package/dist/credits/types/grants.d.ts +24 -0
- package/dist/credits/types/grants.d.ts.map +1 -0
- package/dist/credits/types/grants.js +2 -0
- package/dist/credits/types/grants.js.map +1 -0
- package/dist/credits/types/index.d.ts +9 -0
- package/dist/credits/types/index.d.ts.map +1 -0
- package/dist/credits/types/index.js +2 -0
- package/dist/credits/types/index.js.map +1 -0
- package/dist/credits/types/ledger.d.ts +87 -0
- package/dist/credits/types/ledger.d.ts.map +1 -0
- package/dist/credits/types/ledger.js +2 -0
- package/dist/credits/types/ledger.js.map +1 -0
- package/dist/credits/types/quotas.d.ts +40 -0
- package/dist/credits/types/quotas.d.ts.map +1 -0
- package/dist/credits/types/quotas.js +2 -0
- package/dist/credits/types/quotas.js.map +1 -0
- package/dist/credits/types/teams.d.ts +44 -0
- package/dist/credits/types/teams.d.ts.map +1 -0
- package/dist/credits/types/teams.js +2 -0
- package/dist/credits/types/teams.js.map +1 -0
- package/dist/engine.d.ts +18 -39
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +169 -145
- package/dist/engine.js.map +1 -1
- package/dist/errors.d.ts +133 -25
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +173 -24
- package/dist/errors.js.map +1 -1
- package/dist/expr/ast.d.ts +46 -0
- package/dist/expr/ast.d.ts.map +1 -0
- package/dist/expr/ast.js +18 -0
- package/dist/expr/ast.js.map +1 -0
- package/dist/expr/evaluator.d.ts +4 -0
- package/dist/expr/evaluator.d.ts.map +1 -0
- package/dist/expr/evaluator.js +144 -0
- package/dist/expr/evaluator.js.map +1 -0
- package/dist/expr/language.d.ts +5 -0
- package/dist/expr/language.d.ts.map +1 -0
- package/dist/expr/language.js +73 -0
- package/dist/expr/language.js.map +1 -0
- package/dist/expr/parser.d.ts +24 -0
- package/dist/expr/parser.d.ts.map +1 -0
- package/dist/expr/parser.js +175 -0
- package/dist/expr/parser.js.map +1 -0
- package/dist/expr/tokenizer.d.ts +7 -0
- package/dist/expr/tokenizer.d.ts.map +1 -0
- package/dist/expr/tokenizer.js +85 -0
- package/dist/expr/tokenizer.js.map +1 -0
- package/dist/expr.d.ts +7 -58
- package/dist/expr.d.ts.map +1 -1
- package/dist/expr.js +41 -640
- package/dist/expr.js.map +1 -1
- package/dist/generated/pricing-config.schema.json +2792 -0
- package/dist/generated/pricing-config.types.d.ts +685 -0
- package/dist/generated/pricing-config.types.d.ts.map +1 -0
- package/dist/generated/pricing-config.types.js +8 -0
- package/dist/generated/pricing-config.types.js.map +1 -0
- package/dist/index.d.ts +23 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -10
- package/dist/index.js.map +1 -1
- package/dist/load-pricing-file.d.ts.map +1 -1
- package/dist/load-pricing-file.js +8 -4
- package/dist/load-pricing-file.js.map +1 -1
- package/dist/metrics.d.ts +6 -16
- package/dist/metrics.d.ts.map +1 -1
- package/dist/metrics.js.map +1 -1
- package/dist/node.d.ts +9 -2
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +6 -2
- package/dist/node.js.map +1 -1
- package/dist/providers/_shared.d.ts +9 -0
- package/dist/providers/_shared.d.ts.map +1 -0
- package/dist/providers/_shared.js +52 -0
- package/dist/providers/_shared.js.map +1 -0
- package/dist/providers/dodo/client-contract.d.ts +106 -0
- package/dist/providers/dodo/client-contract.d.ts.map +1 -0
- package/dist/providers/dodo/client-contract.js +2 -0
- package/dist/providers/dodo/client-contract.js.map +1 -0
- package/dist/providers/dodo/event-mapper.d.ts +5 -3
- package/dist/providers/dodo/event-mapper.d.ts.map +1 -1
- package/dist/providers/dodo/event-mapper.js +213 -38
- package/dist/providers/dodo/event-mapper.js.map +1 -1
- package/dist/providers/dodo/index.d.ts +1 -0
- package/dist/providers/dodo/index.d.ts.map +1 -1
- package/dist/providers/dodo/index.js.map +1 -1
- package/dist/providers/dodo/provider.d.ts +24 -14
- package/dist/providers/dodo/provider.d.ts.map +1 -1
- package/dist/providers/dodo/provider.js +249 -31
- package/dist/providers/dodo/provider.js.map +1 -1
- package/dist/providers/index.d.ts +3 -1
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +1 -0
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/mock/index.js.map +1 -1
- package/dist/providers/mock/provider.d.ts +14 -10
- package/dist/providers/mock/provider.d.ts.map +1 -1
- package/dist/providers/mock/provider.js +78 -10
- package/dist/providers/mock/provider.js.map +1 -1
- package/dist/providers/stripe/event-mapper.d.ts +3 -3
- package/dist/providers/stripe/event-mapper.d.ts.map +1 -1
- package/dist/providers/stripe/event-mapper.js +139 -30
- package/dist/providers/stripe/event-mapper.js.map +1 -1
- package/dist/providers/stripe/index.js.map +1 -1
- package/dist/providers/stripe/provider.d.ts +21 -12
- package/dist/providers/stripe/provider.d.ts.map +1 -1
- package/dist/providers/stripe/provider.js +175 -17
- package/dist/providers/stripe/provider.js.map +1 -1
- package/dist/providers/types.d.ts +128 -32
- package/dist/providers/types.d.ts.map +1 -1
- package/dist/providers/types.js +17 -1
- package/dist/providers/types.js.map +1 -1
- package/dist/retry.d.ts +35 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +70 -0
- package/dist/retry.js.map +1 -0
- package/dist/shared/diagnostics.d.ts +7 -0
- package/dist/shared/diagnostics.d.ts.map +1 -0
- package/dist/shared/diagnostics.js +18 -0
- package/dist/shared/diagnostics.js.map +1 -0
- package/dist/shared/logger.d.ts +10 -0
- package/dist/shared/logger.d.ts.map +1 -0
- package/dist/shared/logger.js +15 -0
- package/dist/shared/logger.js.map +1 -0
- package/dist/shared/postgres-client.d.ts +84 -0
- package/dist/shared/postgres-client.d.ts.map +1 -0
- package/dist/shared/postgres-client.js +337 -0
- package/dist/shared/postgres-client.js.map +1 -0
- package/dist/shared/postgres-errors.d.ts +11 -0
- package/dist/shared/postgres-errors.d.ts.map +1 -0
- package/dist/shared/postgres-errors.js +105 -0
- package/dist/shared/postgres-errors.js.map +1 -0
- package/dist/shared/postgres-types.d.ts +7 -0
- package/dist/shared/postgres-types.d.ts.map +1 -0
- package/dist/shared/postgres-types.js +2 -0
- package/dist/shared/postgres-types.js.map +1 -0
- package/dist/shared/postgres-validation.d.ts +8 -0
- package/dist/shared/postgres-validation.d.ts.map +1 -0
- package/dist/shared/postgres-validation.js +40 -0
- package/dist/shared/postgres-validation.js.map +1 -0
- package/dist/storage/adapters/clickhouse.d.ts +61 -0
- package/dist/storage/adapters/clickhouse.d.ts.map +1 -0
- package/dist/storage/adapters/clickhouse.js +347 -0
- package/dist/storage/adapters/clickhouse.js.map +1 -0
- package/dist/storage/adapters/s3.d.ts +35 -0
- package/dist/storage/adapters/s3.d.ts.map +1 -0
- package/dist/storage/adapters/s3.js +100 -0
- package/dist/storage/adapters/s3.js.map +1 -0
- package/dist/storage/outbox-worker.d.ts +41 -0
- package/dist/storage/outbox-worker.d.ts.map +1 -0
- package/dist/storage/outbox-worker.js +146 -0
- package/dist/storage/outbox-worker.js.map +1 -0
- package/dist/storage/ports.d.ts +78 -0
- package/dist/storage/ports.d.ts.map +1 -0
- package/dist/storage/ports.js +2 -0
- package/dist/storage/ports.js.map +1 -0
- package/dist/storage/postgres-repository.d.ts +14 -0
- package/dist/storage/postgres-repository.d.ts.map +1 -0
- package/dist/storage/postgres-repository.js +140 -0
- package/dist/storage/postgres-repository.js.map +1 -0
- package/dist/storage/runtime.d.ts +77 -0
- package/dist/storage/runtime.d.ts.map +1 -0
- package/dist/storage/runtime.js +369 -0
- package/dist/storage/runtime.js.map +1 -0
- package/package.json +43 -21
- package/dist/allowance.d.ts +0 -48
- package/dist/allowance.d.ts.map +0 -1
- package/dist/allowance.js +0 -140
- package/dist/allowance.js.map +0 -1
- package/dist/billing/billing-manager.d.ts +0 -66
- package/dist/billing/billing-manager.d.ts.map +0 -1
- package/dist/billing/billing-manager.js +0 -560
- package/dist/billing/billing-manager.js.map +0 -1
- package/dist/billing/billing-types.d.ts +0 -203
- package/dist/billing/billing-types.d.ts.map +0 -1
- package/dist/billing/billing-types.js +0 -6
- package/dist/billing/billing-types.js.map +0 -1
- package/dist/billing/memory-billing-store.d.ts +0 -37
- package/dist/billing/memory-billing-store.d.ts.map +0 -1
- package/dist/billing/memory-billing-store.js +0 -197
- package/dist/billing/memory-billing-store.js.map +0 -1
- package/dist/billing/postgres-billing-store.d.ts +0 -35
- package/dist/billing/postgres-billing-store.d.ts.map +0 -1
- package/dist/billing/postgres-billing-store.js +0 -274
- package/dist/billing/postgres-billing-store.js.map +0 -1
- package/dist/billing/supabase-billing-store.d.ts +0 -29
- package/dist/billing/supabase-billing-store.d.ts.map +0 -1
- package/dist/billing/supabase-billing-store.js +0 -250
- package/dist/billing/supabase-billing-store.js.map +0 -1
- package/dist/manager.d.ts +0 -444
- package/dist/manager.d.ts.map +0 -1
- package/dist/manager.js +0 -1153
- package/dist/manager.js.map +0 -1
- package/dist/stores/credit-store.d.ts +0 -198
- package/dist/stores/credit-store.d.ts.map +0 -1
- package/dist/stores/credit-store.js.map +0 -1
- package/dist/stores/events.d.ts.map +0 -1
- package/dist/stores/events.js.map +0 -1
- package/dist/stores/memory-store.d.ts +0 -187
- package/dist/stores/memory-store.d.ts.map +0 -1
- package/dist/stores/memory-store.js +0 -1890
- package/dist/stores/memory-store.js.map +0 -1
- package/dist/stores/postgres-store.d.ts +0 -100
- package/dist/stores/postgres-store.d.ts.map +0 -1
- package/dist/stores/postgres-store.js +0 -891
- package/dist/stores/postgres-store.js.map +0 -1
- package/dist/stores/supabase-store.d.ts +0 -87
- package/dist/stores/supabase-store.d.ts.map +0 -1
- package/dist/stores/supabase-store.js +0 -948
- package/dist/stores/supabase-store.js.map +0 -1
- package/dist/types.d.ts +0 -463
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- /package/dist/{types.js → commerce/types.js} +0 -0
package/dist/retry.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import pRetry from "p-retry";
|
|
2
|
+
import { isRetryableBursarError } from "./errors.js";
|
|
3
|
+
const MAX_TIMER_MS = 2_147_483_647;
|
|
4
|
+
function finiteNumber(value, name, minimum, maximum = Number.MAX_VALUE) {
|
|
5
|
+
if (!Number.isFinite(value) || value < minimum || value > maximum) {
|
|
6
|
+
throw new RangeError(`${name} must be a finite number between ${minimum} and ${maximum}`);
|
|
7
|
+
}
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Execute an operation with bounded, cancellable exponential backoff.
|
|
12
|
+
*
|
|
13
|
+
* Retries are deliberately opt-in at the error level: by default only typed
|
|
14
|
+
* Bursar errors whose `retryable` flag is true are attempted again. The
|
|
15
|
+
* operation must be read-only or idempotent because a transport failure can
|
|
16
|
+
* make a mutation's commit outcome indeterminate.
|
|
17
|
+
*/
|
|
18
|
+
export async function retryBursarOperation(operation, options = {}) {
|
|
19
|
+
if (typeof operation !== "function")
|
|
20
|
+
throw new TypeError("operation must be a function");
|
|
21
|
+
if (options.jitter !== undefined && typeof options.jitter !== "boolean") {
|
|
22
|
+
throw new TypeError("jitter must be a boolean");
|
|
23
|
+
}
|
|
24
|
+
if (options.unref !== undefined && typeof options.unref !== "boolean") {
|
|
25
|
+
throw new TypeError("unref must be a boolean");
|
|
26
|
+
}
|
|
27
|
+
if (options.shouldRetry !== undefined && typeof options.shouldRetry !== "function") {
|
|
28
|
+
throw new TypeError("shouldRetry must be a function");
|
|
29
|
+
}
|
|
30
|
+
if (options.onRetry !== undefined && typeof options.onRetry !== "function") {
|
|
31
|
+
throw new TypeError("onRetry must be a function");
|
|
32
|
+
}
|
|
33
|
+
if (options.signal !== undefined &&
|
|
34
|
+
(typeof options.signal !== "object" ||
|
|
35
|
+
options.signal === null ||
|
|
36
|
+
typeof options.signal.throwIfAborted !== "function")) {
|
|
37
|
+
throw new TypeError("signal must be an AbortSignal");
|
|
38
|
+
}
|
|
39
|
+
const maxAttempts = options.maxAttempts ?? 3;
|
|
40
|
+
if (!Number.isSafeInteger(maxAttempts) || maxAttempts < 1) {
|
|
41
|
+
throw new RangeError("maxAttempts must be a positive safe integer");
|
|
42
|
+
}
|
|
43
|
+
const baseDelayMs = finiteNumber(options.baseDelayMs ?? 250, "baseDelayMs", 0, MAX_TIMER_MS);
|
|
44
|
+
const maxDelayMs = finiteNumber(options.maxDelayMs ?? Math.max(2_000, baseDelayMs), "maxDelayMs", 0, MAX_TIMER_MS);
|
|
45
|
+
if (maxDelayMs < baseDelayMs) {
|
|
46
|
+
throw new RangeError("maxDelayMs must be greater than or equal to baseDelayMs");
|
|
47
|
+
}
|
|
48
|
+
const factor = finiteNumber(options.factor ?? 2, "factor", Number.EPSILON);
|
|
49
|
+
const maxElapsedMs = finiteNumber(options.maxElapsedMs ?? 30_000, "maxElapsedMs", 0, MAX_TIMER_MS);
|
|
50
|
+
return pRetry(operation, {
|
|
51
|
+
retries: maxAttempts - 1,
|
|
52
|
+
minTimeout: baseDelayMs,
|
|
53
|
+
maxTimeout: maxDelayMs,
|
|
54
|
+
factor,
|
|
55
|
+
randomize: options.jitter ?? true,
|
|
56
|
+
maxRetryTime: maxElapsedMs,
|
|
57
|
+
signal: options.signal,
|
|
58
|
+
unref: options.unref ?? false,
|
|
59
|
+
shouldRetry: async ({ error, attemptNumber, retryDelay }) => {
|
|
60
|
+
const retry = options.shouldRetry
|
|
61
|
+
? await options.shouldRetry(error)
|
|
62
|
+
: isRetryableBursarError(error);
|
|
63
|
+
if (!retry)
|
|
64
|
+
return false;
|
|
65
|
+
await options.onRetry?.(error, attemptNumber + 1, retryDelay);
|
|
66
|
+
return true;
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=retry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.js","sourceRoot":"","sources":["../src/retry.ts"],"names":[],"mappings":"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","sourcesContent":["import pRetry from \"p-retry\";\n\nimport { isRetryableBursarError } from \"./errors.js\";\n\nconst MAX_TIMER_MS = 2_147_483_647;\n\nexport interface BursarRetryOptions {\n /** Total attempts, including the first call. Defaults to 3. */\n maxAttempts?: number;\n /** Initial exponential-backoff delay. Defaults to 250 ms. */\n baseDelayMs?: number;\n /** Maximum delay between attempts. Defaults to 2 seconds. */\n maxDelayMs?: number;\n /** Exponential-backoff multiplier. Defaults to 2. */\n factor?: number;\n /** Randomize backoff to prevent a thundering herd. Defaults to true. */\n jitter?: boolean;\n /** Maximum elapsed retry budget. Defaults to 30 seconds. */\n maxElapsedMs?: number;\n /** Abort retries and pending backoff when this signal is aborted. */\n signal?: AbortSignal;\n /** Allow Node.js to exit while waiting to retry. Defaults to false. */\n unref?: boolean;\n /**\n * Override the default `error.retryable` decision. This does not make an\n * operation idempotent; mutation retries must reuse their idempotency key.\n */\n shouldRetry?: (error: Error) => boolean | Promise<boolean>;\n /** Observer invoked immediately before each retry is scheduled. */\n onRetry?: (error: Error, nextAttempt: number, delayMs: number) => void | Promise<void>;\n}\n\nfunction finiteNumber(\n value: number,\n name: string,\n minimum: number,\n maximum = Number.MAX_VALUE,\n): number {\n if (!Number.isFinite(value) || value < minimum || value > maximum) {\n throw new RangeError(`${name} must be a finite number between ${minimum} and ${maximum}`);\n }\n return value;\n}\n\n/**\n * Execute an operation with bounded, cancellable exponential backoff.\n *\n * Retries are deliberately opt-in at the error level: by default only typed\n * Bursar errors whose `retryable` flag is true are attempted again. The\n * operation must be read-only or idempotent because a transport failure can\n * make a mutation's commit outcome indeterminate.\n */\nexport async function retryBursarOperation<T>(\n operation: () => PromiseLike<T> | T,\n options: BursarRetryOptions = {},\n): Promise<T> {\n if (typeof operation !== \"function\") throw new TypeError(\"operation must be a function\");\n if (options.jitter !== undefined && typeof options.jitter !== \"boolean\") {\n throw new TypeError(\"jitter must be a boolean\");\n }\n if (options.unref !== undefined && typeof options.unref !== \"boolean\") {\n throw new TypeError(\"unref must be a boolean\");\n }\n if (options.shouldRetry !== undefined && typeof options.shouldRetry !== \"function\") {\n throw new TypeError(\"shouldRetry must be a function\");\n }\n if (options.onRetry !== undefined && typeof options.onRetry !== \"function\") {\n throw new TypeError(\"onRetry must be a function\");\n }\n if (\n options.signal !== undefined &&\n (typeof options.signal !== \"object\" ||\n options.signal === null ||\n typeof options.signal.throwIfAborted !== \"function\")\n ) {\n throw new TypeError(\"signal must be an AbortSignal\");\n }\n\n const maxAttempts = options.maxAttempts ?? 3;\n if (!Number.isSafeInteger(maxAttempts) || maxAttempts < 1) {\n throw new RangeError(\"maxAttempts must be a positive safe integer\");\n }\n\n const baseDelayMs = finiteNumber(options.baseDelayMs ?? 250, \"baseDelayMs\", 0, MAX_TIMER_MS);\n const maxDelayMs = finiteNumber(\n options.maxDelayMs ?? Math.max(2_000, baseDelayMs),\n \"maxDelayMs\",\n 0,\n MAX_TIMER_MS,\n );\n if (maxDelayMs < baseDelayMs) {\n throw new RangeError(\"maxDelayMs must be greater than or equal to baseDelayMs\");\n }\n const factor = finiteNumber(options.factor ?? 2, \"factor\", Number.EPSILON);\n const maxElapsedMs = finiteNumber(\n options.maxElapsedMs ?? 30_000,\n \"maxElapsedMs\",\n 0,\n MAX_TIMER_MS,\n );\n\n return pRetry(operation, {\n retries: maxAttempts - 1,\n minTimeout: baseDelayMs,\n maxTimeout: maxDelayMs,\n factor,\n randomize: options.jitter ?? true,\n maxRetryTime: maxElapsedMs,\n signal: options.signal,\n unref: options.unref ?? false,\n shouldRetry: async ({ error, attemptNumber, retryDelay }) => {\n const retry = options.shouldRetry\n ? await options.shouldRetry(error)\n : isRetryableBursarError(error);\n if (!retry) return false;\n await options.onRetry?.(error, attemptNumber + 1, retryDelay);\n return true;\n },\n });\n}\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Maximum diagnostic length accepted by the SQL layer. */
|
|
2
|
+
export declare const PERSISTED_DIAGNOSTIC_MAX_CHARACTERS = 8192;
|
|
3
|
+
/** Return trimmed, non-empty text accepted by diagnostic SQL constraints. */
|
|
4
|
+
export declare function boundedDiagnosticMessage(value: unknown, fallback?: string): string;
|
|
5
|
+
/** Normalize an optional diagnostic while preserving an absent value. */
|
|
6
|
+
export declare function optionalBoundedDiagnosticMessage(value: unknown): string | null;
|
|
7
|
+
//# sourceMappingURL=diagnostics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diagnostics.d.ts","sourceRoot":"","sources":["../../src/shared/diagnostics.ts"],"names":[],"mappings":"AAAA,2DAA2D;AAC3D,eAAO,MAAM,mCAAmC,OAAQ,CAAC;AAEzD,6EAA6E;AAC7E,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,SAAqB,GAAG,MAAM,CAS9F;AAED,yEAAyE;AACzE,wBAAgB,gCAAgC,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAE9E"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Maximum diagnostic length accepted by the SQL layer. */
|
|
2
|
+
export const PERSISTED_DIAGNOSTIC_MAX_CHARACTERS = 8_192;
|
|
3
|
+
/** Return trimmed, non-empty text accepted by diagnostic SQL constraints. */
|
|
4
|
+
export function boundedDiagnosticMessage(value, fallback = "operation_failed") {
|
|
5
|
+
let message = value instanceof Error ? value.message : value == null ? "" : String(value);
|
|
6
|
+
message = message.replaceAll("\0", "\uFFFD").trim();
|
|
7
|
+
if (!message && value instanceof Error)
|
|
8
|
+
message = value.name;
|
|
9
|
+
const normalizedFallback = fallback.replaceAll("\0", "\uFFFD").trim() || "operation_failed";
|
|
10
|
+
return Array.from(message || normalizedFallback)
|
|
11
|
+
.slice(0, PERSISTED_DIAGNOSTIC_MAX_CHARACTERS)
|
|
12
|
+
.join("");
|
|
13
|
+
}
|
|
14
|
+
/** Normalize an optional diagnostic while preserving an absent value. */
|
|
15
|
+
export function optionalBoundedDiagnosticMessage(value) {
|
|
16
|
+
return value == null ? null : boundedDiagnosticMessage(value);
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=diagnostics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diagnostics.js","sourceRoot":"","sources":["../../src/shared/diagnostics.ts"],"names":[],"mappings":"AAAA,2DAA2D;AAC3D,MAAM,CAAC,MAAM,mCAAmC,GAAG,KAAK,CAAC;AAEzD,6EAA6E;AAC7E,MAAM,UAAU,wBAAwB,CAAC,KAAc,EAAE,QAAQ,GAAG,kBAAkB;IACpF,IAAI,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC1F,OAAO,GAAG,OAAO,CAAC,UAAU,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,IAAI,EAAE,CAAC;IACpD,IAAI,CAAC,OAAO,IAAI,KAAK,YAAY,KAAK;QAAE,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC;IAE7D,MAAM,kBAAkB,GAAG,QAAQ,CAAC,UAAU,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,IAAI,EAAE,IAAI,kBAAkB,CAAC;IAC5F,OAAO,KAAK,CAAC,IAAI,CAAC,OAAO,IAAI,kBAAkB,CAAC;SAC7C,KAAK,CAAC,CAAC,EAAE,mCAAmC,CAAC;SAC7C,IAAI,CAAC,EAAE,CAAC,CAAC;AACd,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,gCAAgC,CAAC,KAAc;IAC7D,OAAO,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,wBAAwB,CAAC,KAAK,CAAC,CAAC;AAChE,CAAC","sourcesContent":["/** Maximum diagnostic length accepted by the SQL layer. */\nexport const PERSISTED_DIAGNOSTIC_MAX_CHARACTERS = 8_192;\n\n/** Return trimmed, non-empty text accepted by diagnostic SQL constraints. */\nexport function boundedDiagnosticMessage(value: unknown, fallback = \"operation_failed\"): string {\n let message = value instanceof Error ? value.message : value == null ? \"\" : String(value);\n message = message.replaceAll(\"\\0\", \"\\uFFFD\").trim();\n if (!message && value instanceof Error) message = value.name;\n\n const normalizedFallback = fallback.replaceAll(\"\\0\", \"\\uFFFD\").trim() || \"operation_failed\";\n return Array.from(message || normalizedFallback)\n .slice(0, PERSISTED_DIAGNOSTIC_MAX_CHARACTERS)\n .join(\"\");\n}\n\n/** Normalize an optional diagnostic while preserving an absent value. */\nexport function optionalBoundedDiagnosticMessage(value: unknown): string | null {\n return value == null ? null : boundedDiagnosticMessage(value);\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface Logger {
|
|
2
|
+
debug?: (message: string, context?: Record<string, unknown>) => void;
|
|
3
|
+
info?: (message: string, context?: Record<string, unknown>) => void;
|
|
4
|
+
warn?: (message: string, context?: Record<string, unknown>) => void;
|
|
5
|
+
error?: (message: string, context?: Record<string, unknown>) => void;
|
|
6
|
+
}
|
|
7
|
+
export type NormalizedLogger = Required<Logger>;
|
|
8
|
+
export declare const noopLogger: NormalizedLogger;
|
|
9
|
+
export declare function normalizeLogger(logger?: Logger | null): NormalizedLogger;
|
|
10
|
+
//# sourceMappingURL=logger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/shared/logger.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,MAAM;IACrB,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;IACrE,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;IACpE,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;IACpE,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;CACtE;AAED,MAAM,MAAM,gBAAgB,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC;AAEhD,eAAO,MAAM,UAAU,EAAE,gBAKxB,CAAC;AAEF,wBAAgB,eAAe,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,gBAAgB,CAOxE"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const noopLogger = {
|
|
2
|
+
debug: () => { },
|
|
3
|
+
info: () => { },
|
|
4
|
+
warn: () => { },
|
|
5
|
+
error: () => { },
|
|
6
|
+
};
|
|
7
|
+
export function normalizeLogger(logger) {
|
|
8
|
+
return {
|
|
9
|
+
debug: logger?.debug ?? noopLogger.debug,
|
|
10
|
+
info: logger?.info ?? noopLogger.info,
|
|
11
|
+
warn: logger?.warn ?? noopLogger.warn,
|
|
12
|
+
error: logger?.error ?? noopLogger.error,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/shared/logger.ts"],"names":[],"mappings":"AASA,MAAM,CAAC,MAAM,UAAU,GAAqB;IAC1C,KAAK,EAAE,GAAG,EAAE,GAAE,CAAC;IACf,IAAI,EAAE,GAAG,EAAE,GAAE,CAAC;IACd,IAAI,EAAE,GAAG,EAAE,GAAE,CAAC;IACd,KAAK,EAAE,GAAG,EAAE,GAAE,CAAC;CAChB,CAAC;AAEF,MAAM,UAAU,eAAe,CAAC,MAAsB;IACpD,OAAO;QACL,KAAK,EAAE,MAAM,EAAE,KAAK,IAAI,UAAU,CAAC,KAAK;QACxC,IAAI,EAAE,MAAM,EAAE,IAAI,IAAI,UAAU,CAAC,IAAI;QACrC,IAAI,EAAE,MAAM,EAAE,IAAI,IAAI,UAAU,CAAC,IAAI;QACrC,KAAK,EAAE,MAAM,EAAE,KAAK,IAAI,UAAU,CAAC,KAAK;KACzC,CAAC;AACJ,CAAC","sourcesContent":["export interface Logger {\n debug?: (message: string, context?: Record<string, unknown>) => void;\n info?: (message: string, context?: Record<string, unknown>) => void;\n warn?: (message: string, context?: Record<string, unknown>) => void;\n error?: (message: string, context?: Record<string, unknown>) => void;\n}\n\nexport type NormalizedLogger = Required<Logger>;\n\nexport const noopLogger: NormalizedLogger = {\n debug: () => {},\n info: () => {},\n warn: () => {},\n error: () => {},\n};\n\nexport function normalizeLogger(logger?: Logger | null): NormalizedLogger {\n return {\n debug: logger?.debug ?? noopLogger.debug,\n info: logger?.info ?? noopLogger.info,\n warn: logger?.warn ?? noopLogger.warn,\n error: logger?.error ?? noopLogger.error,\n };\n}\n"]}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type BursarError } from "../errors.js";
|
|
2
|
+
import type { QueryFn } from "./postgres-types.js";
|
|
3
|
+
export interface PostgresPoolConfig {
|
|
4
|
+
connectionString: string;
|
|
5
|
+
connectionTimeoutMillis?: number;
|
|
6
|
+
statement_timeout?: number;
|
|
7
|
+
query_timeout?: number;
|
|
8
|
+
idle_in_transaction_session_timeout?: number;
|
|
9
|
+
idleTimeoutMillis?: number;
|
|
10
|
+
max?: number;
|
|
11
|
+
application_name?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface PostgresPool {
|
|
14
|
+
query(text: string, params?: unknown[]): Promise<{
|
|
15
|
+
rows: unknown[];
|
|
16
|
+
}>;
|
|
17
|
+
connect(): Promise<PostgresPoolClient>;
|
|
18
|
+
end(): Promise<void>;
|
|
19
|
+
on?(event: "error", listener: (error: Error) => void): unknown;
|
|
20
|
+
}
|
|
21
|
+
export interface PostgresPoolClient {
|
|
22
|
+
query(text: string, params?: unknown[]): Promise<{
|
|
23
|
+
rows: unknown[];
|
|
24
|
+
}>;
|
|
25
|
+
release(error?: Error | boolean): void;
|
|
26
|
+
}
|
|
27
|
+
export interface PostgresPoolConstructor {
|
|
28
|
+
new (config: PostgresPoolConfig): PostgresPool;
|
|
29
|
+
}
|
|
30
|
+
/** PostgreSQL deadline, pool, and observability controls. */
|
|
31
|
+
export interface PostgresConnectionOptions {
|
|
32
|
+
/** Time allowed to establish or acquire a connection. Defaults to 10 seconds. */
|
|
33
|
+
connectionTimeoutMs?: number;
|
|
34
|
+
/** Server-side statement deadline. Defaults to 30 seconds; set 0 to disable. */
|
|
35
|
+
statementTimeoutMs?: number;
|
|
36
|
+
/** Server-side idle transaction deadline. Defaults to 30 seconds; set 0 to disable. */
|
|
37
|
+
idleTransactionTimeoutMs?: number;
|
|
38
|
+
/** Idle pooled connection lifetime. Uses pg's default when omitted. */
|
|
39
|
+
idleTimeoutMs?: number;
|
|
40
|
+
/** Maximum connections in an SDK-owned pool. Uses pg's default when omitted. */
|
|
41
|
+
maxConnections?: number;
|
|
42
|
+
/** PostgreSQL application_name. Defaults to `bursar-js`. */
|
|
43
|
+
applicationName?: string;
|
|
44
|
+
/** Receives typed errors emitted by idle pool clients. Must not throw. */
|
|
45
|
+
onPoolError?: (error: BursarError) => void;
|
|
46
|
+
}
|
|
47
|
+
export interface PostgresClientOptions extends PostgresConnectionOptions {
|
|
48
|
+
poolConstructor?: PostgresPoolConstructor;
|
|
49
|
+
closedError?: () => Error;
|
|
50
|
+
tenantId?: string;
|
|
51
|
+
usageBackend?: "postgres" | "clickhouse";
|
|
52
|
+
billingPayloadBackend?: "postgres" | "s3";
|
|
53
|
+
}
|
|
54
|
+
/** Build a pg Pool config with finite connection and statement deadlines. */
|
|
55
|
+
export declare function postgresPoolConfig(connectionString: string, options?: PostgresConnectionOptions): PostgresPoolConfig;
|
|
56
|
+
/**
|
|
57
|
+
* Owns the lifecycle and lazy initialization of a PostgreSQL pool.
|
|
58
|
+
*
|
|
59
|
+
* A supplied pool is borrowed and therefore never ended. A connection string
|
|
60
|
+
* creates an owned pool on first use and closes it exactly once.
|
|
61
|
+
*/
|
|
62
|
+
export declare class PostgresClient {
|
|
63
|
+
private pool;
|
|
64
|
+
private poolPromise;
|
|
65
|
+
private poolConstructor;
|
|
66
|
+
private readonly databaseUrl;
|
|
67
|
+
private readonly ownsPool;
|
|
68
|
+
private readonly closedError;
|
|
69
|
+
private readonly tenantId;
|
|
70
|
+
private readonly usageBackend;
|
|
71
|
+
private readonly billingPayloadBackend;
|
|
72
|
+
private readonly connectionOptions;
|
|
73
|
+
private removePoolObserver;
|
|
74
|
+
private closePromise;
|
|
75
|
+
private closed;
|
|
76
|
+
constructor(poolOrUrl: PostgresPool | string, options?: PostgresClientOptions);
|
|
77
|
+
readonly query: QueryFn;
|
|
78
|
+
close(): Promise<void>;
|
|
79
|
+
private closePool;
|
|
80
|
+
private getPool;
|
|
81
|
+
private createPool;
|
|
82
|
+
private getPoolConstructor;
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=postgres-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgres-client.d.ts","sourceRoot":"","sources":["../../src/shared/postgres-client.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import { ImportError as BursarImportError, StoreClosedError } from "../errors.js";
|
|
2
|
+
import { normalizePostgresError } from "./postgres-errors.js";
|
|
3
|
+
const DEFAULT_CONNECTION_TIMEOUT_MS = 10_000;
|
|
4
|
+
const DEFAULT_STATEMENT_TIMEOUT_MS = 30_000;
|
|
5
|
+
const DEFAULT_IDLE_TRANSACTION_TIMEOUT_MS = 30_000;
|
|
6
|
+
const CLIENT_TIMEOUT_GRACE_MS = 1_000;
|
|
7
|
+
const MAX_POSTGRES_TIMEOUT_MS = 2_147_483_647;
|
|
8
|
+
function assertPostgresPool(value) {
|
|
9
|
+
if (typeof value !== "object" ||
|
|
10
|
+
value === null ||
|
|
11
|
+
typeof value.query !== "function" ||
|
|
12
|
+
typeof value.connect !== "function" ||
|
|
13
|
+
typeof value.end !== "function" ||
|
|
14
|
+
(value.on !== undefined &&
|
|
15
|
+
typeof value.on !== "function")) {
|
|
16
|
+
throw new TypeError("postgres pool must provide query(), connect(), and end() methods");
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function integerOption(value, fallback, name, minimum, maximum = MAX_POSTGRES_TIMEOUT_MS) {
|
|
20
|
+
const normalized = value ?? fallback;
|
|
21
|
+
if (normalized === undefined)
|
|
22
|
+
return undefined;
|
|
23
|
+
if (!Number.isSafeInteger(normalized) || normalized < minimum || normalized > maximum) {
|
|
24
|
+
throw new RangeError(`${name} must be a safe integer between ${minimum} and ${maximum}`);
|
|
25
|
+
}
|
|
26
|
+
return normalized;
|
|
27
|
+
}
|
|
28
|
+
function normalizeConnectionOptions(options) {
|
|
29
|
+
if (options.applicationName !== undefined && typeof options.applicationName !== "string") {
|
|
30
|
+
throw new TypeError("applicationName must be a string");
|
|
31
|
+
}
|
|
32
|
+
if (options.onPoolError !== undefined && typeof options.onPoolError !== "function") {
|
|
33
|
+
throw new TypeError("onPoolError must be a function");
|
|
34
|
+
}
|
|
35
|
+
const applicationName = options.applicationName?.trim() || "bursar-js";
|
|
36
|
+
if (applicationName.includes("\0")) {
|
|
37
|
+
throw new TypeError("applicationName must not contain null bytes");
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
connectionTimeoutMs: integerOption(options.connectionTimeoutMs, DEFAULT_CONNECTION_TIMEOUT_MS, "connectionTimeoutMs", 0),
|
|
41
|
+
statementTimeoutMs: integerOption(options.statementTimeoutMs, DEFAULT_STATEMENT_TIMEOUT_MS, "statementTimeoutMs", 0),
|
|
42
|
+
idleTransactionTimeoutMs: integerOption(options.idleTransactionTimeoutMs, DEFAULT_IDLE_TRANSACTION_TIMEOUT_MS, "idleTransactionTimeoutMs", 0),
|
|
43
|
+
idleTimeoutMs: integerOption(options.idleTimeoutMs, undefined, "idleTimeoutMs", 0),
|
|
44
|
+
maxConnections: integerOption(options.maxConnections, undefined, "maxConnections", 1),
|
|
45
|
+
applicationName,
|
|
46
|
+
onPoolError: options.onPoolError,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** Build a pg Pool config with finite connection and statement deadlines. */
|
|
50
|
+
export function postgresPoolConfig(connectionString, options = {}) {
|
|
51
|
+
if (typeof connectionString !== "string") {
|
|
52
|
+
throw new TypeError("postgres connection string must be a string");
|
|
53
|
+
}
|
|
54
|
+
if (!connectionString.trim())
|
|
55
|
+
throw new TypeError("postgres connection string must not be empty");
|
|
56
|
+
const normalized = normalizeConnectionOptions(options);
|
|
57
|
+
const queryTimeout = normalized.statementTimeoutMs === 0
|
|
58
|
+
? 0
|
|
59
|
+
: Math.min(normalized.statementTimeoutMs + CLIENT_TIMEOUT_GRACE_MS, MAX_POSTGRES_TIMEOUT_MS);
|
|
60
|
+
return {
|
|
61
|
+
connectionString,
|
|
62
|
+
connectionTimeoutMillis: normalized.connectionTimeoutMs,
|
|
63
|
+
statement_timeout: normalized.statementTimeoutMs,
|
|
64
|
+
query_timeout: queryTimeout,
|
|
65
|
+
idle_in_transaction_session_timeout: normalized.idleTransactionTimeoutMs,
|
|
66
|
+
application_name: normalized.applicationName,
|
|
67
|
+
...(normalized.idleTimeoutMs !== undefined
|
|
68
|
+
? { idleTimeoutMillis: normalized.idleTimeoutMs }
|
|
69
|
+
: {}),
|
|
70
|
+
...(normalized.maxConnections !== undefined ? { max: normalized.maxConnections } : {}),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const poolObservers = new WeakMap();
|
|
74
|
+
function notifyPoolError(onPoolError, error) {
|
|
75
|
+
try {
|
|
76
|
+
onPoolError?.(error);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// Observability callbacks must never destabilize SDK control flow.
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function observePool(pool, onPoolError) {
|
|
83
|
+
if (!pool.on)
|
|
84
|
+
return () => { };
|
|
85
|
+
const existing = poolObservers.get(pool);
|
|
86
|
+
if (existing) {
|
|
87
|
+
if (onPoolError)
|
|
88
|
+
existing.set(onPoolError, (existing.get(onPoolError) ?? 0) + 1);
|
|
89
|
+
return () => {
|
|
90
|
+
if (!onPoolError)
|
|
91
|
+
return;
|
|
92
|
+
const references = existing.get(onPoolError) ?? 0;
|
|
93
|
+
if (references <= 1)
|
|
94
|
+
existing.delete(onPoolError);
|
|
95
|
+
else
|
|
96
|
+
existing.set(onPoolError, references - 1);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
const observers = new Map();
|
|
100
|
+
if (onPoolError)
|
|
101
|
+
observers.set(onPoolError, 1);
|
|
102
|
+
poolObservers.set(pool, observers);
|
|
103
|
+
pool.on("error", (error) => {
|
|
104
|
+
const normalized = normalizePostgresError(error, { operation: "pool", phase: "pool" });
|
|
105
|
+
for (const observer of observers.keys())
|
|
106
|
+
notifyPoolError(observer, normalized);
|
|
107
|
+
});
|
|
108
|
+
return () => {
|
|
109
|
+
if (!onPoolError)
|
|
110
|
+
return;
|
|
111
|
+
const references = observers.get(onPoolError) ?? 0;
|
|
112
|
+
if (references <= 1)
|
|
113
|
+
observers.delete(onPoolError);
|
|
114
|
+
else
|
|
115
|
+
observers.set(onPoolError, references - 1);
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Owns the lifecycle and lazy initialization of a PostgreSQL pool.
|
|
120
|
+
*
|
|
121
|
+
* A supplied pool is borrowed and therefore never ended. A connection string
|
|
122
|
+
* creates an owned pool on first use and closes it exactly once.
|
|
123
|
+
*/
|
|
124
|
+
export class PostgresClient {
|
|
125
|
+
pool;
|
|
126
|
+
poolPromise = null;
|
|
127
|
+
poolConstructor;
|
|
128
|
+
databaseUrl;
|
|
129
|
+
ownsPool;
|
|
130
|
+
closedError;
|
|
131
|
+
tenantId;
|
|
132
|
+
usageBackend;
|
|
133
|
+
billingPayloadBackend;
|
|
134
|
+
connectionOptions;
|
|
135
|
+
removePoolObserver = null;
|
|
136
|
+
closePromise = null;
|
|
137
|
+
closed = false;
|
|
138
|
+
constructor(poolOrUrl, options = {}) {
|
|
139
|
+
if (typeof poolOrUrl !== "string")
|
|
140
|
+
assertPostgresPool(poolOrUrl);
|
|
141
|
+
if (options.poolConstructor !== undefined && typeof options.poolConstructor !== "function") {
|
|
142
|
+
throw new TypeError("poolConstructor must be a constructor");
|
|
143
|
+
}
|
|
144
|
+
if (options.closedError !== undefined && typeof options.closedError !== "function") {
|
|
145
|
+
throw new TypeError("closedError must be a function");
|
|
146
|
+
}
|
|
147
|
+
if (options.usageBackend !== undefined &&
|
|
148
|
+
options.usageBackend !== "postgres" &&
|
|
149
|
+
options.usageBackend !== "clickhouse") {
|
|
150
|
+
throw new TypeError("usageBackend must be 'postgres' or 'clickhouse'");
|
|
151
|
+
}
|
|
152
|
+
if (options.billingPayloadBackend !== undefined &&
|
|
153
|
+
options.billingPayloadBackend !== "postgres" &&
|
|
154
|
+
options.billingPayloadBackend !== "s3") {
|
|
155
|
+
throw new TypeError("billingPayloadBackend must be 'postgres' or 's3'");
|
|
156
|
+
}
|
|
157
|
+
this.connectionOptions = normalizeConnectionOptions(options);
|
|
158
|
+
this.databaseUrl = typeof poolOrUrl === "string" ? poolOrUrl : null;
|
|
159
|
+
if (this.databaseUrl !== null && !this.databaseUrl.trim()) {
|
|
160
|
+
throw new TypeError("postgres connection string must not be empty");
|
|
161
|
+
}
|
|
162
|
+
this.pool = typeof poolOrUrl === "string" ? null : poolOrUrl;
|
|
163
|
+
this.poolConstructor = options.poolConstructor ?? null;
|
|
164
|
+
this.ownsPool = typeof poolOrUrl === "string";
|
|
165
|
+
this.closedError =
|
|
166
|
+
options.closedError ?? (() => new StoreClosedError("PostgreSQL client has been closed"));
|
|
167
|
+
this.tenantId = options.tenantId === undefined ? null : normalizeTenantId(options.tenantId);
|
|
168
|
+
this.usageBackend = options.usageBackend ?? "postgres";
|
|
169
|
+
this.billingPayloadBackend = options.billingPayloadBackend ?? "postgres";
|
|
170
|
+
if (this.pool) {
|
|
171
|
+
this.removePoolObserver = observePool(this.pool, this.connectionOptions.onPoolError);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
query = async (text, params) => {
|
|
175
|
+
let pool;
|
|
176
|
+
try {
|
|
177
|
+
pool = await this.getPool();
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
if (this.closed)
|
|
181
|
+
throw error;
|
|
182
|
+
throw normalizePostgresError(error, { operation: "query", phase: "connect" });
|
|
183
|
+
}
|
|
184
|
+
if (!this.tenantId) {
|
|
185
|
+
try {
|
|
186
|
+
return (await pool.query(text, params)).rows;
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
throw normalizePostgresError(error, {
|
|
190
|
+
operation: "query",
|
|
191
|
+
phase: "query",
|
|
192
|
+
indeterminate: true,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
let client;
|
|
197
|
+
try {
|
|
198
|
+
client = await pool.connect();
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
throw normalizePostgresError(error, { operation: "query", phase: "connect" });
|
|
202
|
+
}
|
|
203
|
+
let phase = "begin";
|
|
204
|
+
let transactionStarted = false;
|
|
205
|
+
let discardConnection;
|
|
206
|
+
try {
|
|
207
|
+
await client.query("BEGIN");
|
|
208
|
+
transactionStarted = true;
|
|
209
|
+
phase = "configure";
|
|
210
|
+
await client.query(`SELECT set_config('bursar.tenant_id', $1, true), set_config('bursar.usage_backend', $2, true), set_config('bursar.billing_payload_backend', $3, true), set_config('statement_timeout', $4, true), set_config('idle_in_transaction_session_timeout', $5, true)`, [
|
|
211
|
+
this.tenantId,
|
|
212
|
+
this.usageBackend,
|
|
213
|
+
this.billingPayloadBackend,
|
|
214
|
+
String(this.connectionOptions.statementTimeoutMs),
|
|
215
|
+
String(this.connectionOptions.idleTransactionTimeoutMs),
|
|
216
|
+
]);
|
|
217
|
+
phase = "query";
|
|
218
|
+
const result = await client.query(text, params);
|
|
219
|
+
phase = "commit";
|
|
220
|
+
await client.query("COMMIT");
|
|
221
|
+
transactionStarted = false;
|
|
222
|
+
return result.rows;
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
const failedPhase = phase;
|
|
226
|
+
let rollbackFailed = false;
|
|
227
|
+
if (transactionStarted) {
|
|
228
|
+
try {
|
|
229
|
+
phase = "rollback";
|
|
230
|
+
await client.query("ROLLBACK");
|
|
231
|
+
transactionStarted = false;
|
|
232
|
+
}
|
|
233
|
+
catch (rollbackError) {
|
|
234
|
+
rollbackFailed = true;
|
|
235
|
+
discardConnection =
|
|
236
|
+
rollbackError instanceof Error
|
|
237
|
+
? rollbackError
|
|
238
|
+
: new Error("PostgreSQL rollback failed", { cause: rollbackError });
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
const normalized = normalizePostgresError(error, {
|
|
242
|
+
operation: "query",
|
|
243
|
+
phase: failedPhase,
|
|
244
|
+
indeterminate: failedPhase === "query" || failedPhase === "commit",
|
|
245
|
+
rollbackFailed,
|
|
246
|
+
});
|
|
247
|
+
throw normalized;
|
|
248
|
+
}
|
|
249
|
+
finally {
|
|
250
|
+
try {
|
|
251
|
+
client.release(discardConnection);
|
|
252
|
+
}
|
|
253
|
+
catch (releaseError) {
|
|
254
|
+
const normalized = normalizePostgresError(releaseError, {
|
|
255
|
+
operation: "release connection",
|
|
256
|
+
phase: "pool",
|
|
257
|
+
});
|
|
258
|
+
notifyPoolError(this.connectionOptions.onPoolError, normalized);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
close() {
|
|
263
|
+
if (this.closePromise)
|
|
264
|
+
return this.closePromise;
|
|
265
|
+
this.closed = true;
|
|
266
|
+
this.closePromise = this.closePool();
|
|
267
|
+
return this.closePromise;
|
|
268
|
+
}
|
|
269
|
+
async closePool() {
|
|
270
|
+
if (!this.ownsPool) {
|
|
271
|
+
this.removePoolObserver?.();
|
|
272
|
+
this.removePoolObserver = null;
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
try {
|
|
276
|
+
const pool = this.poolPromise ? await this.poolPromise : this.pool;
|
|
277
|
+
if (pool)
|
|
278
|
+
await pool.end();
|
|
279
|
+
}
|
|
280
|
+
catch (error) {
|
|
281
|
+
throw normalizePostgresError(error, { operation: "close", phase: "close" });
|
|
282
|
+
}
|
|
283
|
+
finally {
|
|
284
|
+
this.pool = null;
|
|
285
|
+
this.poolPromise = null;
|
|
286
|
+
this.removePoolObserver?.();
|
|
287
|
+
this.removePoolObserver = null;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
async getPool() {
|
|
291
|
+
if (this.closed)
|
|
292
|
+
throw this.closedError();
|
|
293
|
+
if (this.pool)
|
|
294
|
+
return this.pool;
|
|
295
|
+
if (!this.databaseUrl)
|
|
296
|
+
throw new StoreClosedError("PostgreSQL client has no connection source");
|
|
297
|
+
if (!this.poolPromise) {
|
|
298
|
+
this.poolPromise = this.createPool(this.databaseUrl).catch((error) => {
|
|
299
|
+
this.poolPromise = null;
|
|
300
|
+
throw error;
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
return this.poolPromise;
|
|
304
|
+
}
|
|
305
|
+
async createPool(databaseUrl) {
|
|
306
|
+
const Pool = await this.getPoolConstructor();
|
|
307
|
+
const pool = new Pool(postgresPoolConfig(databaseUrl, this.connectionOptions));
|
|
308
|
+
this.removePoolObserver = observePool(pool, this.connectionOptions.onPoolError);
|
|
309
|
+
this.pool = pool;
|
|
310
|
+
return pool;
|
|
311
|
+
}
|
|
312
|
+
async getPoolConstructor() {
|
|
313
|
+
if (this.poolConstructor)
|
|
314
|
+
return this.poolConstructor;
|
|
315
|
+
let pg;
|
|
316
|
+
try {
|
|
317
|
+
pg = await import("pg");
|
|
318
|
+
}
|
|
319
|
+
catch (cause) {
|
|
320
|
+
throw new BursarImportError("pg is required for PostgreSQL stores: npm install pg", {
|
|
321
|
+
cause,
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
this.poolConstructor = pg.Pool;
|
|
325
|
+
return this.poolConstructor;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
function normalizeTenantId(tenantId) {
|
|
329
|
+
if (typeof tenantId !== "string")
|
|
330
|
+
throw new TypeError("tenantId must be a UUID");
|
|
331
|
+
const normalized = tenantId.trim().toLowerCase();
|
|
332
|
+
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(normalized)) {
|
|
333
|
+
throw new TypeError("tenantId must be a UUID");
|
|
334
|
+
}
|
|
335
|
+
return normalized;
|
|
336
|
+
}
|
|
337
|
+
//# sourceMappingURL=postgres-client.js.map
|