pecunia-core 0.0.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/dist/api/index.d.mts +2 -0
- package/dist/api/index.mjs +5 -0
- package/dist/api-Dnub-EIf.mjs +35 -0
- package/dist/async_hooks-D8vqDJIk.mjs +20 -0
- package/dist/context/index.d.mts +2 -0
- package/dist/context/index.mjs +4 -0
- package/dist/context-_erD9NsC.mjs +164 -0
- package/dist/db/index.d.mts +2 -0
- package/dist/db/index.mjs +6 -0
- package/dist/db-JBwMpkmd.mjs +146 -0
- package/dist/env/index.d.mts +2 -0
- package/dist/env/index.mjs +3 -0
- package/dist/env-C7DHqPmD.mjs +230 -0
- package/dist/errors/index.d.mts +2 -0
- package/dist/errors/index.mjs +3 -0
- package/dist/errors-vpD21Iku.mjs +566 -0
- package/dist/generate-id-D3ZvbbXn.mjs +9 -0
- package/dist/get-payment-tables-0FkEj3dw.mjs +3677 -0
- package/dist/hooks/index.d.mts +2 -0
- package/dist/hooks/index.mjs +4 -0
- package/dist/hooks-PXnNTy74.mjs +1 -0
- package/dist/index-BNj-TC2H.d.mts +10 -0
- package/dist/index-B_V7jOck.d.mts +77 -0
- package/dist/index-CtOgkCz5.d.mts +2021 -0
- package/dist/index-DOO_aiRf.d.mts +6 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +13 -0
- package/dist/types/index.d.mts +2 -0
- package/dist/types/index.mjs +11 -0
- package/dist/types-B0U9sZ8x.mjs +320 -0
- package/dist/utils/index.d.mts +2 -0
- package/dist/utils/index.mjs +5 -0
- package/dist/utils-BNmlJWNJ.mjs +30 -0
- package/package.json +126 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { $ as WebHookEventStatus, $t as KyselyDatabaseDialectType, A as CheckoutSessionStatus, At as RequestState, B as PricingModel, Bt as ErrorDefinition, C as AdapterFactoryOptions, Ct as initGetFieldAttributes, D as AuditAction, Dt as getCurrentDBAdapterAsyncLocalStorage, E as Actor, Et as getCurrentAdapter, F as InvoiceStatus, Ft as hasRequestState, G as ScheduledTaskStatus, Gt as IndexableRegistry, H as ProrationBehavior, Ht as ErrorMap, I as LedgerEntryType, It as runWithRequestState, J as TransactionStatus, Jt as HookEndpointContext, K as ScheduledTaskType, Kt as ProviderErrorCode, L as PaymentIntentStatus, Lt as getCurrentAuthContext, M as Currency, Mt as defineRequestState, N as DiscountType, Nt as getCurrentRequestState, O as BillingInterval, Ot as runWithAdapter, P as ERROR_REGISTRY, Pt as getRequestStateAsyncLocalStorage, Q as WebHookDeliveryTrigger, Qt as createPecuniaEndpoint, R as PaymentProviders, Rt as getCurrentPaymentContextAsyncLocalStorage, S as AdapterFactoryCreator, St as initGetFieldName, T as AccountType, Tt as initGetDefaultFieldName, U as ProviderId, Ut as ErrorResponse, V as ProductType, Vt as ErrorDefinitionResponse, W as ScheduleStatus, Wt as ErrorType, X as UsageAction, Xt as PecuniaEndpoint, Y as TransactionType, Yt as PecuniaPlugin, Z as UsageAggregation, Zt as PecuniaMiddleware, _ as ProviderAdapter, _t as discountSchema, a as useMiddleware, an as DBPrimitive, at as getPaymentTables, b as AdapterFactory, bt as initGetModelName, c as SenlyContext, cn as ModelNames, ct as Subscription, d as DBAdapterDebugLogOption, dn as Primitive, dt as sharedDeletableSchema, en as Awaitable, et as statusCodes, f as DBAdapterFactoryConfig, fn as StandardSchemaV1, ft as Product, g as InternalAdapter, gt as Discount, h as DBTransactionAdapter, ht as priceSchema, i as optionsMiddleware, in as DBFieldType, it as createProviderError, j as CollectionMethod, jt as RequestStateWeakMap, k as CheckoutSessionMode, kt as runWithTransaction, l as CustomAdapter, ln as PecuniaDBSchema, lt as subscriptionSchema, m as DBAdapterSchemaCreation, mt as Price, n as createPaymentEndpoint, nn as DBFieldAttribute, nt as PecuniaError, o as GenericEndpointContext, on as LiteralString, ot as deepmerge, p as DBAdapterInstance, pt as productSchema, q as SubscriptionStatus, qt as PecuniaOptions, r as createPecuniaMiddleware, rn as DBFieldAttributeConfig, rt as ValidationError, s as PecuniaContext, sn as LiteralUnion, st as withApplyDefault, t as PaymentMiddleware, tn as BaseModelNames, tt as ApiError, u as DBAdapter, un as Prettify, ut as sharedCoreSchema, v as RequiredWhere, vt as Customer, w as createAdapterFactory, wt as initGetDefaultModelName, x as AdapterFactoryConfig, xt as initGetIdField, y as Where, yt as customerSchema, z as PriceType, zt as runWithEndpointContext } from "./index-CtOgkCz5.mjs";
|
|
2
|
+
import { _ as isTest, a as TTY_COLORS, c as logger, d as EnvObject, f as env, g as isProduction, h as isDevelopment, i as Logger, l as shouldPublishLog, m as getEnvVar, n as LogHandlerParams, o as createLogger, p as getBooleanEnvVar, r as LogLevel, s as levels, t as InternalLogger, u as ENV, v as nodeENV, y as getColorDepth } from "./index-B_V7jOck.mjs";
|
|
3
|
+
import { t as getAsyncLocalStorage } from "./index-DOO_aiRf.mjs";
|
|
4
|
+
import { n as safeJSONParse, r as generateId, t as capitalizeFirstLetter } from "./index-BNj-TC2H.mjs";
|
|
5
|
+
export { AccountType, Actor, AdapterFactory, AdapterFactoryConfig, AdapterFactoryCreator, AdapterFactoryOptions, ApiError, AuditAction, Awaitable, BaseModelNames, BillingInterval, CheckoutSessionMode, CheckoutSessionStatus, CollectionMethod, Currency, CustomAdapter, Customer, DBAdapter, DBAdapterDebugLogOption, DBAdapterFactoryConfig, DBAdapterInstance, DBAdapterSchemaCreation, DBFieldAttribute, DBFieldAttributeConfig, DBFieldType, DBPrimitive, DBTransactionAdapter, Discount, DiscountType, ENV, ERROR_REGISTRY, EnvObject, ErrorDefinition, ErrorDefinitionResponse, ErrorMap, ErrorResponse, ErrorType, GenericEndpointContext, HookEndpointContext, IndexableRegistry, InternalAdapter, InternalLogger, InvoiceStatus, KyselyDatabaseDialectType, LedgerEntryType, LiteralString, LiteralUnion, LogHandlerParams, LogLevel, Logger, ModelNames, PaymentIntentStatus, PaymentMiddleware, PaymentProviders, PecuniaContext, PecuniaDBSchema, PecuniaEndpoint, PecuniaError, PecuniaMiddleware, PecuniaOptions, PecuniaPlugin, Prettify, Price, PriceType, PricingModel, Primitive, Product, ProductType, ProrationBehavior, ProviderAdapter, ProviderErrorCode, ProviderId, RequestState, RequestStateWeakMap, RequiredWhere, ScheduleStatus, ScheduledTaskStatus, ScheduledTaskType, SenlyContext, StandardSchemaV1, Subscription, SubscriptionStatus, TTY_COLORS, TransactionStatus, TransactionType, UsageAction, UsageAggregation, ValidationError, WebHookDeliveryTrigger, WebHookEventStatus, Where, capitalizeFirstLetter, createAdapterFactory, createLogger, createPaymentEndpoint, createPecuniaEndpoint, createPecuniaMiddleware, createProviderError, customerSchema, deepmerge, defineRequestState, discountSchema, env, generateId, getAsyncLocalStorage, getBooleanEnvVar, getColorDepth, getCurrentAdapter, getCurrentAuthContext, getCurrentDBAdapterAsyncLocalStorage, getCurrentPaymentContextAsyncLocalStorage, getCurrentRequestState, getEnvVar, getPaymentTables, getRequestStateAsyncLocalStorage, hasRequestState, initGetDefaultFieldName, initGetDefaultModelName, initGetFieldAttributes, initGetFieldName, initGetIdField, initGetModelName, isDevelopment, isProduction, isTest, levels, logger, nodeENV, optionsMiddleware, priceSchema, productSchema, runWithAdapter, runWithEndpointContext, runWithRequestState, runWithTransaction, safeJSONParse, sharedCoreSchema, sharedDeletableSchema, shouldPublishLog, statusCodes, subscriptionSchema, useMiddleware, withApplyDefault };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { a as defineRequestState, c as hasRequestState, d as getCurrentPaymentContextAsyncLocalStorage, f as runWithEndpointContext, i as runWithTransaction, l as runWithRequestState, n as getCurrentDBAdapterAsyncLocalStorage, o as getCurrentRequestState, r as runWithAdapter, s as getRequestStateAsyncLocalStorage, t as getCurrentAdapter, u as getCurrentAuthContext } from "./context-_erD9NsC.mjs";
|
|
2
|
+
import { t as getAsyncLocalStorage } from "./async_hooks-D8vqDJIk.mjs";
|
|
3
|
+
import { i as useMiddleware, n as createPecuniaMiddleware, r as optionsMiddleware, t as createPaymentEndpoint } from "./api-Dnub-EIf.mjs";
|
|
4
|
+
import { A as UsageAggregation, C as ScheduleStatus, D as TransactionStatus, E as SubscriptionStatus, M as WebHookEventStatus, N as statusCodes, O as TransactionType, P as ErrorType, S as ProrationBehavior, T as ScheduledTaskType, _ as PaymentIntentStatus, a as AccountType, b as PricingModel, c as BillingInterval, d as CollectionMethod, f as Currency, g as LedgerEntryType, h as InvoiceStatus, i as createProviderError, j as WebHookDeliveryTrigger, k as UsageAction, l as CheckoutSessionMode, m as ERROR_REGISTRY, n as PecuniaError, o as Actor, p as DiscountType, r as ValidationError, s as AuditAction, t as ApiError, u as CheckoutSessionStatus, v as PaymentProviders, w as ScheduledTaskStatus, x as ProductType, y as PriceType } from "./errors-vpD21Iku.mjs";
|
|
5
|
+
import { a as initGetFieldAttributes, c as initGetDefaultModelName, i as initGetModelName, n as deepmerge, o as initGetIdField, r as withApplyDefault, s as initGetDefaultFieldName, t as getPaymentTables } from "./get-payment-tables-0FkEj3dw.mjs";
|
|
6
|
+
import { t as generateId } from "./generate-id-D3ZvbbXn.mjs";
|
|
7
|
+
import { a as customerSchema, c as initGetFieldName, i as discountSchema, n as productSchema, o as sharedCoreSchema, r as priceSchema, s as sharedDeletableSchema, t as subscriptionSchema } from "./db-JBwMpkmd.mjs";
|
|
8
|
+
import { a as shouldPublishLog, c as env, d as isDevelopment, f as isProduction, i as logger, l as getBooleanEnvVar, m as nodeENV, n as createLogger, o as getColorDepth, p as isTest, r as levels, s as ENV, t as TTY_COLORS, u as getEnvVar } from "./env-C7DHqPmD.mjs";
|
|
9
|
+
import "./hooks-PXnNTy74.mjs";
|
|
10
|
+
import { n as safeJSONParse, t as capitalizeFirstLetter } from "./utils-BNmlJWNJ.mjs";
|
|
11
|
+
import { n as createAdapterFactory, t as createPecuniaEndpoint } from "./types-B0U9sZ8x.mjs";
|
|
12
|
+
|
|
13
|
+
export { AccountType, Actor, ApiError, AuditAction, BillingInterval, CheckoutSessionMode, CheckoutSessionStatus, CollectionMethod, Currency, DiscountType, ENV, ERROR_REGISTRY, ErrorType, InvoiceStatus, LedgerEntryType, PaymentIntentStatus, PaymentProviders, PecuniaError, PriceType, PricingModel, ProductType, ProrationBehavior, ScheduleStatus, ScheduledTaskStatus, ScheduledTaskType, SubscriptionStatus, TTY_COLORS, TransactionStatus, TransactionType, UsageAction, UsageAggregation, ValidationError, WebHookDeliveryTrigger, WebHookEventStatus, capitalizeFirstLetter, createAdapterFactory, createLogger, createPaymentEndpoint, createPecuniaEndpoint, createPecuniaMiddleware, createProviderError, customerSchema, deepmerge, defineRequestState, discountSchema, env, generateId, getAsyncLocalStorage, getBooleanEnvVar, getColorDepth, getCurrentAdapter, getCurrentAuthContext, getCurrentDBAdapterAsyncLocalStorage, getCurrentPaymentContextAsyncLocalStorage, getCurrentRequestState, getEnvVar, getPaymentTables, getRequestStateAsyncLocalStorage, hasRequestState, initGetDefaultFieldName, initGetDefaultModelName, initGetFieldAttributes, initGetFieldName, initGetIdField, initGetModelName, isDevelopment, isProduction, isTest, levels, logger, nodeENV, optionsMiddleware, priceSchema, productSchema, runWithAdapter, runWithEndpointContext, runWithRequestState, runWithTransaction, safeJSONParse, sharedCoreSchema, sharedDeletableSchema, shouldPublishLog, statusCodes, subscriptionSchema, useMiddleware, withApplyDefault };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { Bt as ErrorDefinition, C as AdapterFactoryOptions, Gt as IndexableRegistry, Ht as ErrorMap, Jt as HookEndpointContext, Kt as ProviderErrorCode, Qt as createPecuniaEndpoint, S as AdapterFactoryCreator, Ut as ErrorResponse, Vt as ErrorDefinitionResponse, Wt as ErrorType, Xt as PecuniaEndpoint, Yt as PecuniaPlugin, Zt as PecuniaMiddleware, _ as ProviderAdapter, an as DBPrimitive, b as AdapterFactory, c as SenlyContext, cn as ModelNames, d as DBAdapterDebugLogOption, dn as Primitive, en as Awaitable, f as DBAdapterFactoryConfig, fn as StandardSchemaV1, g as InternalAdapter, h as DBTransactionAdapter, in as DBFieldType, l as CustomAdapter, ln as PecuniaDBSchema, m as DBAdapterSchemaCreation, nn as DBFieldAttribute, o as GenericEndpointContext, on as LiteralString, p as DBAdapterInstance, qt as PecuniaOptions, rn as DBFieldAttributeConfig, s as PecuniaContext, sn as LiteralUnion, tn as BaseModelNames, u as DBAdapter, un as Prettify, v as RequiredWhere, w as createAdapterFactory, x as AdapterFactoryConfig, y as Where } from "../index-CtOgkCz5.mjs";
|
|
2
|
+
export { AdapterFactory, AdapterFactoryConfig, AdapterFactoryCreator, AdapterFactoryOptions, Awaitable, BaseModelNames, CustomAdapter, DBAdapter, DBAdapterDebugLogOption, DBAdapterFactoryConfig, DBAdapterInstance, DBAdapterSchemaCreation, DBFieldAttribute, DBFieldAttributeConfig, DBFieldType, DBPrimitive, DBTransactionAdapter, ErrorDefinition, ErrorDefinitionResponse, ErrorMap, ErrorResponse, ErrorType, GenericEndpointContext, HookEndpointContext, IndexableRegistry, InternalAdapter, LiteralString, LiteralUnion, ModelNames, PecuniaContext, PecuniaDBSchema, PecuniaEndpoint, PecuniaMiddleware, PecuniaOptions, PecuniaPlugin, Prettify, Primitive, ProviderAdapter, ProviderErrorCode, RequiredWhere, SenlyContext, StandardSchemaV1, Where, createAdapterFactory, createPecuniaEndpoint };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import "../context-_erD9NsC.mjs";
|
|
2
|
+
import "../async_hooks-D8vqDJIk.mjs";
|
|
3
|
+
import "../api-Dnub-EIf.mjs";
|
|
4
|
+
import { P as ErrorType } from "../errors-vpD21Iku.mjs";
|
|
5
|
+
import "../get-payment-tables-0FkEj3dw.mjs";
|
|
6
|
+
import "../generate-id-D3ZvbbXn.mjs";
|
|
7
|
+
import "../env-C7DHqPmD.mjs";
|
|
8
|
+
import "../utils-BNmlJWNJ.mjs";
|
|
9
|
+
import { n as createAdapterFactory, t as createPecuniaEndpoint } from "../types-B0U9sZ8x.mjs";
|
|
10
|
+
|
|
11
|
+
export { ErrorType, createAdapterFactory, createPecuniaEndpoint };
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import { f as runWithEndpointContext } from "./context-_erD9NsC.mjs";
|
|
2
|
+
import { i as useMiddleware } from "./api-Dnub-EIf.mjs";
|
|
3
|
+
import { n as PecuniaError } from "./errors-vpD21Iku.mjs";
|
|
4
|
+
import { a as initGetFieldAttributes, c as initGetDefaultModelName, i as initGetModelName, o as initGetIdField, r as withApplyDefault, s as initGetDefaultFieldName, t as getPaymentTables } from "./get-payment-tables-0FkEj3dw.mjs";
|
|
5
|
+
import { n as safeJSONParse } from "./utils-BNmlJWNJ.mjs";
|
|
6
|
+
import { createEndpoint } from "better-call";
|
|
7
|
+
|
|
8
|
+
//#region src/types/db/adapter/factory/index.ts
|
|
9
|
+
const createAsIsTransaction = (adapter) => (fn) => fn(adapter);
|
|
10
|
+
const createAdapterFactory = ({ adapter: customAdapter, config: cfg }) => (options) => {
|
|
11
|
+
const config = {
|
|
12
|
+
...cfg,
|
|
13
|
+
supportsBooleans: cfg.supportsBooleans ?? true,
|
|
14
|
+
supportsDates: cfg.supportsDates ?? true,
|
|
15
|
+
supportsJSON: cfg.supportsJSON ?? false,
|
|
16
|
+
adapterName: cfg.adapterName ?? cfg.adapterId,
|
|
17
|
+
supportsNumericIds: cfg.supportsNumericIds ?? true,
|
|
18
|
+
supportsUUIDs: cfg.supportsUUIDs ?? false,
|
|
19
|
+
supportsArrays: cfg.supportsArrays ?? false,
|
|
20
|
+
transaction: cfg.transaction ?? false,
|
|
21
|
+
disableTransformInput: cfg.disableTransformInput ?? false,
|
|
22
|
+
disableTransformOutput: cfg.disableTransformOutput ?? false
|
|
23
|
+
};
|
|
24
|
+
const schema = getPaymentTables(options);
|
|
25
|
+
const debugLog = (..._args) => {};
|
|
26
|
+
const getDefaultModelName = initGetDefaultModelName({
|
|
27
|
+
usePlural: config.usePlural,
|
|
28
|
+
schema
|
|
29
|
+
});
|
|
30
|
+
const getDefaultFieldName = initGetDefaultFieldName({
|
|
31
|
+
usePlural: config.usePlural,
|
|
32
|
+
schema
|
|
33
|
+
});
|
|
34
|
+
const getModelName = initGetModelName({
|
|
35
|
+
usePlural: config.usePlural,
|
|
36
|
+
schema
|
|
37
|
+
});
|
|
38
|
+
const getFieldName = initGetDefaultFieldName({
|
|
39
|
+
schema,
|
|
40
|
+
usePlural: config.usePlural
|
|
41
|
+
});
|
|
42
|
+
const idField = initGetIdField();
|
|
43
|
+
const getFieldAttributes = initGetFieldAttributes({
|
|
44
|
+
schema,
|
|
45
|
+
options,
|
|
46
|
+
usePlural: config.usePlural,
|
|
47
|
+
disableIdGeneration: config.disableIdGeneration,
|
|
48
|
+
customIdGenerator: config.customIdGenerator
|
|
49
|
+
});
|
|
50
|
+
const transformInput = async (data, defaultModelName, action) => {
|
|
51
|
+
const transformedData = {};
|
|
52
|
+
const fields = schema[defaultModelName].fields;
|
|
53
|
+
const newMappedKeys = config.mapKeysTransformInput ?? {};
|
|
54
|
+
fields.id = idField();
|
|
55
|
+
for (const field in fields) {
|
|
56
|
+
let value = data[field];
|
|
57
|
+
const fieldAttributes = fields[field];
|
|
58
|
+
const newFieldName = newMappedKeys[field] || fields[field].fieldName || field;
|
|
59
|
+
if (value === void 0 && (fieldAttributes.defaultValue === void 0 && !fieldAttributes.transform?.input && !(action === "update" && fieldAttributes.onUpdate) || action === "update" && !fieldAttributes.onUpdate)) continue;
|
|
60
|
+
let newValue = withApplyDefault(value, fieldAttributes, action);
|
|
61
|
+
if (fieldAttributes.transform?.input) newValue = await fieldAttributes.transform.input(newValue);
|
|
62
|
+
if (config.supportsJSON === false && typeof newValue === "object" && fieldAttributes.type === "json") newValue = JSON.stringify(newValue);
|
|
63
|
+
else if (config.supportsArrays === false && Array.isArray(newValue) && (fieldAttributes.type === "string[]" || fieldAttributes.type === "number[]")) newValue = JSON.stringify(newValue);
|
|
64
|
+
else if (config.supportsDates === false && newValue instanceof Date && fieldAttributes.type === "date") newValue = newValue.toISOString();
|
|
65
|
+
else if (config.supportsBooleans === false && typeof newValue === "boolean") newValue = newValue ? 1 : 0;
|
|
66
|
+
if (config.customTransformInput) newValue = config.customTransformInput({
|
|
67
|
+
data: newValue,
|
|
68
|
+
action,
|
|
69
|
+
field: newFieldName,
|
|
70
|
+
fieldAttributes,
|
|
71
|
+
model: getModelName(defaultModelName),
|
|
72
|
+
schema,
|
|
73
|
+
options
|
|
74
|
+
});
|
|
75
|
+
if (newValue !== void 0) transformedData[newFieldName] = newValue;
|
|
76
|
+
}
|
|
77
|
+
return transformedData;
|
|
78
|
+
};
|
|
79
|
+
const transformOutput = async (data, unsafe_model, select = []) => {
|
|
80
|
+
const transformSingleOutput = async (data$1, unsafe_model$1, select$1 = []) => {
|
|
81
|
+
if (!data$1) return null;
|
|
82
|
+
const newMappedKeys = config.mapKeysTransformOutput ?? {};
|
|
83
|
+
const transformedData = {};
|
|
84
|
+
const tableSchema = schema[getDefaultModelName(unsafe_model$1)].fields;
|
|
85
|
+
for (const key in tableSchema) {
|
|
86
|
+
if (select$1.length && !select$1.includes(key)) continue;
|
|
87
|
+
const field = tableSchema[key];
|
|
88
|
+
if (!field) continue;
|
|
89
|
+
const originalKey = field.fieldName || key;
|
|
90
|
+
let newValue = data$1[Object.entries(newMappedKeys).find(([_, v]) => v === originalKey)?.[0] || originalKey];
|
|
91
|
+
if (field.transform?.output) newValue = await field.transform.output(newValue);
|
|
92
|
+
const newFieldName = newMappedKeys[key] || key;
|
|
93
|
+
if (originalKey === "id" || field.references?.field === "id") {
|
|
94
|
+
if (typeof newValue !== "undefined" && newValue !== null) newValue = String(newValue);
|
|
95
|
+
} else if (config.supportsJSON === false && typeof newValue === "string" && field.type === "json") newValue = safeJSONParse(newValue);
|
|
96
|
+
else if (config.supportsArrays === false && typeof newValue === "string" && (field.type === "string[]" || field.type === "number[]")) newValue = safeJSONParse(newValue);
|
|
97
|
+
else if (config.supportsDates === false && typeof newValue === "string" && field.type === "date") newValue = new Date(newValue);
|
|
98
|
+
else if (config.supportsBooleans === false && typeof newValue === "number" && field.type === "boolean") newValue = newValue === 1;
|
|
99
|
+
if (config.customTransformOutput) newValue = config.customTransformOutput({
|
|
100
|
+
data: newValue,
|
|
101
|
+
field: newFieldName,
|
|
102
|
+
fieldAttributes: field,
|
|
103
|
+
select: select$1,
|
|
104
|
+
model: getModelName(unsafe_model$1),
|
|
105
|
+
schema,
|
|
106
|
+
options
|
|
107
|
+
});
|
|
108
|
+
transformedData[newFieldName] = newValue;
|
|
109
|
+
}
|
|
110
|
+
return transformedData;
|
|
111
|
+
};
|
|
112
|
+
return await transformSingleOutput(data, unsafe_model, select);
|
|
113
|
+
};
|
|
114
|
+
const transformWhereClause = ({ model, where, action }) => {
|
|
115
|
+
if (!where) return void 0;
|
|
116
|
+
const newMappedKeys = config.mapKeysTransformInput ?? {};
|
|
117
|
+
return where.map((w) => {
|
|
118
|
+
const { field: unsafe_field, value, operator = "eq", connector = "AND" } = w;
|
|
119
|
+
if (operator === "in" && !Array.isArray(value)) throw new PecuniaError("Value must be an array");
|
|
120
|
+
const defaultModelName = getDefaultModelName(model);
|
|
121
|
+
const defaultFieldName = getDefaultFieldName({
|
|
122
|
+
field: unsafe_field,
|
|
123
|
+
model
|
|
124
|
+
});
|
|
125
|
+
const fieldName = newMappedKeys[defaultFieldName] || getFieldName({
|
|
126
|
+
field: defaultFieldName,
|
|
127
|
+
model: defaultModelName
|
|
128
|
+
});
|
|
129
|
+
const fieldAttr = getFieldAttributes({
|
|
130
|
+
field: defaultFieldName,
|
|
131
|
+
model: defaultModelName
|
|
132
|
+
});
|
|
133
|
+
let newValue = value;
|
|
134
|
+
if (fieldAttr.type === "date" && value instanceof Date && !config.supportsDates) newValue = value.toISOString();
|
|
135
|
+
if (fieldAttr.type === "boolean" && typeof value === "boolean" && !config.supportsBooleans) newValue = value ? 1 : 0;
|
|
136
|
+
if (fieldAttr.type === "json" && typeof value === "object" && !config.supportsJSON) newValue = JSON.stringify(value);
|
|
137
|
+
if (config.customTransformInput) newValue = config.customTransformInput({
|
|
138
|
+
data: newValue,
|
|
139
|
+
fieldAttributes: fieldAttr,
|
|
140
|
+
field: fieldName,
|
|
141
|
+
model: getModelName(model),
|
|
142
|
+
schema,
|
|
143
|
+
options,
|
|
144
|
+
action
|
|
145
|
+
});
|
|
146
|
+
return {
|
|
147
|
+
operator,
|
|
148
|
+
connector,
|
|
149
|
+
field: fieldName,
|
|
150
|
+
value: newValue
|
|
151
|
+
};
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
const adapterInstance = customAdapter({
|
|
155
|
+
options,
|
|
156
|
+
schema,
|
|
157
|
+
debugLog,
|
|
158
|
+
getFieldName,
|
|
159
|
+
getModelName,
|
|
160
|
+
getDefaultModelName,
|
|
161
|
+
getDefaultFieldName,
|
|
162
|
+
getFieldAttributes,
|
|
163
|
+
transformInput,
|
|
164
|
+
transformOutput,
|
|
165
|
+
transformWhereClause
|
|
166
|
+
});
|
|
167
|
+
let lazyLoadTransaction = null;
|
|
168
|
+
const adapter = {
|
|
169
|
+
transaction: async (cb) => {
|
|
170
|
+
if (!lazyLoadTransaction) lazyLoadTransaction = config.transaction ? config.transaction : createAsIsTransaction(adapter);
|
|
171
|
+
return lazyLoadTransaction(cb);
|
|
172
|
+
},
|
|
173
|
+
create: async ({ data: unsafeData, model: unsafeModel, select, forceAllowId = false }) => {
|
|
174
|
+
const model = getModelName(unsafeModel);
|
|
175
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
176
|
+
if ("id" in unsafeData && typeof unsafeData.id !== "undefined" && !forceAllowId) unsafeData.id = void 0;
|
|
177
|
+
let data = unsafeData;
|
|
178
|
+
if (!config.disableTransformInput) data = await transformInput(unsafeData, unsafeModel, "create");
|
|
179
|
+
const res = await adapterInstance.create({
|
|
180
|
+
data,
|
|
181
|
+
model
|
|
182
|
+
});
|
|
183
|
+
let transformed = res;
|
|
184
|
+
if (!config.disableTransformOutput) transformed = await transformOutput(res, unsafeModel, select);
|
|
185
|
+
return transformed;
|
|
186
|
+
},
|
|
187
|
+
update: async ({ model: unsafeModel, where: unsafeWhere, update: unsafeData }) => {
|
|
188
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
189
|
+
const model = getModelName(unsafeModel);
|
|
190
|
+
const where = transformWhereClause({
|
|
191
|
+
model: unsafeModel,
|
|
192
|
+
where: unsafeWhere,
|
|
193
|
+
action: "update"
|
|
194
|
+
});
|
|
195
|
+
let data = unsafeData;
|
|
196
|
+
if (!config.disableTransformInput) data = await transformInput(unsafeData, unsafeModel, "update");
|
|
197
|
+
const res = await adapterInstance.update({
|
|
198
|
+
model,
|
|
199
|
+
where,
|
|
200
|
+
update: data
|
|
201
|
+
});
|
|
202
|
+
if (config.disableTransformOutput) return res;
|
|
203
|
+
return await transformOutput(res, unsafeModel, void 0);
|
|
204
|
+
},
|
|
205
|
+
updateMany: async ({ model: unsafeModel, where: unsafeWhere, update: unsafeData }) => {
|
|
206
|
+
const model = getModelName(unsafeModel);
|
|
207
|
+
const where = transformWhereClause({
|
|
208
|
+
model: unsafeModel,
|
|
209
|
+
where: unsafeWhere,
|
|
210
|
+
action: "updateMany"
|
|
211
|
+
});
|
|
212
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
213
|
+
let data = unsafeData;
|
|
214
|
+
if (!config.disableTransformInput) data = await transformInput(unsafeData, unsafeModel, "update");
|
|
215
|
+
return await adapterInstance.updateMany({
|
|
216
|
+
model,
|
|
217
|
+
where,
|
|
218
|
+
update: data
|
|
219
|
+
});
|
|
220
|
+
},
|
|
221
|
+
findOne: async ({ model: unsafeModel, where: unsafeWhere, select }) => {
|
|
222
|
+
const model = getModelName(unsafeModel);
|
|
223
|
+
const where = transformWhereClause({
|
|
224
|
+
model: unsafeModel,
|
|
225
|
+
where: unsafeWhere,
|
|
226
|
+
action: "findOne"
|
|
227
|
+
});
|
|
228
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
229
|
+
const res = await adapterInstance.findOne({
|
|
230
|
+
model,
|
|
231
|
+
where,
|
|
232
|
+
select
|
|
233
|
+
});
|
|
234
|
+
if (config.disableTransformOutput) return res;
|
|
235
|
+
return await transformOutput(res, unsafeModel, select);
|
|
236
|
+
},
|
|
237
|
+
findMany: async ({ model: unsafeModel, where: unsafeWhere, limit: unsafeLimit, sortBy, offset }) => {
|
|
238
|
+
const limit = unsafeLimit ?? 100;
|
|
239
|
+
const model = getModelName(unsafeModel);
|
|
240
|
+
const where = transformWhereClause({
|
|
241
|
+
model: unsafeModel,
|
|
242
|
+
where: unsafeWhere,
|
|
243
|
+
action: "findMany"
|
|
244
|
+
});
|
|
245
|
+
unsafeModel = getDefaultModelName(unsafeModel);
|
|
246
|
+
const res = await adapterInstance.findMany({
|
|
247
|
+
model,
|
|
248
|
+
where,
|
|
249
|
+
limit,
|
|
250
|
+
sortBy,
|
|
251
|
+
offset
|
|
252
|
+
});
|
|
253
|
+
if (config.disableTransformOutput) return res;
|
|
254
|
+
return await Promise.all(res.map(async (r) => {
|
|
255
|
+
return await transformOutput(r, unsafeModel, void 0);
|
|
256
|
+
}));
|
|
257
|
+
},
|
|
258
|
+
delete: async ({ model: unsafeModel, where: unsafeWhere }) => {
|
|
259
|
+
const model = getModelName(unsafeModel);
|
|
260
|
+
const where = transformWhereClause({
|
|
261
|
+
model: unsafeModel,
|
|
262
|
+
where: unsafeWhere,
|
|
263
|
+
action: "delete"
|
|
264
|
+
});
|
|
265
|
+
await adapterInstance.delete({
|
|
266
|
+
model,
|
|
267
|
+
where
|
|
268
|
+
});
|
|
269
|
+
},
|
|
270
|
+
deleteMany: async ({ model: unsafeModel, where: unsafeWhere }) => {
|
|
271
|
+
const model = getModelName(unsafeModel);
|
|
272
|
+
const where = transformWhereClause({
|
|
273
|
+
model: unsafeModel,
|
|
274
|
+
where: unsafeWhere,
|
|
275
|
+
action: "deleteMany"
|
|
276
|
+
});
|
|
277
|
+
return await adapterInstance.deleteMany({
|
|
278
|
+
model,
|
|
279
|
+
where
|
|
280
|
+
});
|
|
281
|
+
},
|
|
282
|
+
count: async ({ model: unsafeModel, where: unsafeWhere }) => {
|
|
283
|
+
const model = getModelName(unsafeModel);
|
|
284
|
+
const where = transformWhereClause({
|
|
285
|
+
model: unsafeModel,
|
|
286
|
+
where: unsafeWhere,
|
|
287
|
+
action: "count"
|
|
288
|
+
});
|
|
289
|
+
return await adapterInstance.count({
|
|
290
|
+
model,
|
|
291
|
+
where
|
|
292
|
+
});
|
|
293
|
+
},
|
|
294
|
+
options: {
|
|
295
|
+
adapterConfig: config,
|
|
296
|
+
...adapterInstance.options ?? {}
|
|
297
|
+
},
|
|
298
|
+
id: config.adapterId
|
|
299
|
+
};
|
|
300
|
+
return adapter;
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
//#endregion
|
|
304
|
+
//#region src/types/middleware/index.ts
|
|
305
|
+
function createPecuniaEndpoint(pathOrOptions, handlerOrOptions, handlerOrNever) {
|
|
306
|
+
const path = typeof pathOrOptions === "string" ? pathOrOptions : void 0;
|
|
307
|
+
const options = typeof handlerOrOptions === "object" ? handlerOrOptions : pathOrOptions;
|
|
308
|
+
const handler = typeof handlerOrOptions === "function" ? handlerOrOptions : handlerOrNever;
|
|
309
|
+
if (path) return createEndpoint(path, {
|
|
310
|
+
...options,
|
|
311
|
+
use: [...options?.use || [], ...useMiddleware]
|
|
312
|
+
}, async (ctx) => runWithEndpointContext(ctx, () => handler(ctx)));
|
|
313
|
+
return createEndpoint({
|
|
314
|
+
...options,
|
|
315
|
+
use: [...options?.use || [], ...useMiddleware]
|
|
316
|
+
}, async (ctx) => runWithEndpointContext(ctx, () => handler(ctx)));
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
//#endregion
|
|
320
|
+
export { createAdapterFactory as n, createPecuniaEndpoint as t };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { i as logger } from "./env-C7DHqPmD.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/json-parse.ts
|
|
4
|
+
function safeJSONParse(data) {
|
|
5
|
+
function reviver(_, value) {
|
|
6
|
+
if (typeof value === "string") {
|
|
7
|
+
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/.test(value)) {
|
|
8
|
+
const date = new Date(value);
|
|
9
|
+
if (!isNaN(date.getTime())) return date;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
if (typeof data !== "string") return data;
|
|
16
|
+
return JSON.parse(data, reviver);
|
|
17
|
+
} catch (e) {
|
|
18
|
+
logger.error("Error parsing JSON", { error: e });
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/utils/capitalize-first-letter.ts
|
|
25
|
+
function capitalizeFirstLetter(val) {
|
|
26
|
+
return val.charAt(0).toUpperCase() + val.slice(1);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
export { safeJSONParse as n, capitalizeFirstLetter as t };
|
package/package.json
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pecunia-core",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.mjs",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.mts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"dev-source": "./src/index.ts",
|
|
11
|
+
"types": "./dist/index.d.mts",
|
|
12
|
+
"default": "./dist/index.mjs"
|
|
13
|
+
},
|
|
14
|
+
"./api": {
|
|
15
|
+
"dev-source": "./src/api/index.ts",
|
|
16
|
+
"types": "./dist/api/index.d.mts",
|
|
17
|
+
"default": "./dist/api/index.mjs"
|
|
18
|
+
},
|
|
19
|
+
"./hooks": {
|
|
20
|
+
"dev-source": "./src/hooks/index.ts",
|
|
21
|
+
"types": "./dist/hooks/index.d.mts",
|
|
22
|
+
"node": "./dist/hooks/index.mjs",
|
|
23
|
+
"deno": "./dist/hooks/index.mjs",
|
|
24
|
+
"bun": "./dist/hooks/index.mjs",
|
|
25
|
+
"edge": "./dist/hooks/pure.index.mjs",
|
|
26
|
+
"workerd": "./dist/hooks/index.mjs",
|
|
27
|
+
"browser": "./dist/hooks/pure.index.mjs",
|
|
28
|
+
"default": "./dist/hooks/index.mjs"
|
|
29
|
+
},
|
|
30
|
+
"./context": {
|
|
31
|
+
"dev-source": "./src/context/index.ts",
|
|
32
|
+
"types": "./dist/context/index.d.mts",
|
|
33
|
+
"default": "./dist/context/index.mjs"
|
|
34
|
+
},
|
|
35
|
+
"./env": {
|
|
36
|
+
"dev-source": "./src/env/index.ts",
|
|
37
|
+
"types": "./dist/env/index.d.mts",
|
|
38
|
+
"default": "./dist/env/index.mjs"
|
|
39
|
+
},
|
|
40
|
+
"./errors": {
|
|
41
|
+
"dev-source": "./src/errors/index.ts",
|
|
42
|
+
"types": "./dist/errors/index.d.mts",
|
|
43
|
+
"default": "./dist/errors/index.mjs"
|
|
44
|
+
},
|
|
45
|
+
"./utils": {
|
|
46
|
+
"dev-source": "./src/utils/index.ts",
|
|
47
|
+
"types": "./dist/utils/index.d.mts",
|
|
48
|
+
"default": "./dist/utils/index.mjs"
|
|
49
|
+
},
|
|
50
|
+
"./db": {
|
|
51
|
+
"dev-source": "./src/db/index.ts",
|
|
52
|
+
"types": "./dist/db/index.d.mts",
|
|
53
|
+
"default": "./dist/db/index.mjs"
|
|
54
|
+
},
|
|
55
|
+
"./types": {
|
|
56
|
+
"dev-source": "./src/types/index.ts",
|
|
57
|
+
"types": "./dist/types/index.d.mts",
|
|
58
|
+
"default": "./dist/types/index.mjs"
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"typesVersions": {
|
|
62
|
+
"*": {
|
|
63
|
+
".": [
|
|
64
|
+
"dist/index.d.mts"
|
|
65
|
+
],
|
|
66
|
+
"api": [
|
|
67
|
+
"dist/api/index.d.mts"
|
|
68
|
+
],
|
|
69
|
+
"hooks": [
|
|
70
|
+
"dist/hooks/index.d.mts"
|
|
71
|
+
],
|
|
72
|
+
"context": [
|
|
73
|
+
"dist/context/index.d.mts"
|
|
74
|
+
],
|
|
75
|
+
"env": [
|
|
76
|
+
"dist/env/index.d.mts"
|
|
77
|
+
],
|
|
78
|
+
"error": [
|
|
79
|
+
"dist/error/index.d.mts"
|
|
80
|
+
],
|
|
81
|
+
"utils": [
|
|
82
|
+
"dist/utils/index.d.mts"
|
|
83
|
+
],
|
|
84
|
+
"db": [
|
|
85
|
+
"dist/db/index.d.mts"
|
|
86
|
+
],
|
|
87
|
+
"types": [
|
|
88
|
+
"dist/types/index.d.mts"
|
|
89
|
+
]
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
"files": [
|
|
93
|
+
"dist"
|
|
94
|
+
],
|
|
95
|
+
"scripts": {
|
|
96
|
+
"build": "tsdown",
|
|
97
|
+
"dev": "tsdown --watch",
|
|
98
|
+
"lint:package": "publint run --strict",
|
|
99
|
+
"lint:types": "attw --profile esm-only --pack .",
|
|
100
|
+
"typecheck": "tsc --project tsconfig.json",
|
|
101
|
+
"test": "vitest",
|
|
102
|
+
"coverage": "vitest run --coverage"
|
|
103
|
+
},
|
|
104
|
+
"devDependencies": {
|
|
105
|
+
"@better-auth/utils": "catalog:",
|
|
106
|
+
"@better-fetch/fetch": "catalog:",
|
|
107
|
+
"better-call": "catalog:",
|
|
108
|
+
"jose": "^6.1.0",
|
|
109
|
+
"kysely": "^0.28.5",
|
|
110
|
+
"nanostores": "^1.0.1",
|
|
111
|
+
"tsdown": "catalog:"
|
|
112
|
+
},
|
|
113
|
+
"dependencies": {
|
|
114
|
+
"@standard-schema/spec": "^1.0.0",
|
|
115
|
+
"uuidv7": "^1.1.0",
|
|
116
|
+
"zod": "^4.1.12"
|
|
117
|
+
},
|
|
118
|
+
"peerDependencies": {
|
|
119
|
+
"@better-auth/utils": "catalog:",
|
|
120
|
+
"@better-fetch/fetch": "catalog:",
|
|
121
|
+
"better-call": "catalog:",
|
|
122
|
+
"jose": "^6.1.0",
|
|
123
|
+
"kysely": "^0.28.5",
|
|
124
|
+
"nanostores": "^1.0.1"
|
|
125
|
+
}
|
|
126
|
+
}
|