faire-auth 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (200) hide show
  1. package/dist/{access-Crt-IJ2P.cjs → access-BZ7-AbTM.cjs} +1 -1
  2. package/dist/{adapter-factory-DT-_2aHJ.cjs → adapter-factory-D1I7joki.cjs} +2 -2
  3. package/dist/adapters/drizzle-adapter/index.cjs +5 -5
  4. package/dist/adapters/drizzle-adapter/index.d.cts +1 -1
  5. package/dist/adapters/drizzle-adapter/index.d.mts +2 -2
  6. package/dist/adapters/drizzle-adapter/index.mjs +3 -3
  7. package/dist/adapters/index.cjs +4 -4
  8. package/dist/adapters/index.d.cts +1 -1
  9. package/dist/adapters/index.d.mts +2 -2
  10. package/dist/adapters/index.mjs +3 -3
  11. package/dist/adapters/kysely-adapter/index.cjs +5 -5
  12. package/dist/adapters/kysely-adapter/index.d.cts +1 -1
  13. package/dist/adapters/kysely-adapter/index.d.mts +2 -2
  14. package/dist/adapters/kysely-adapter/index.mjs +4 -4
  15. package/dist/adapters/memory-adapter/index.cjs +5 -5
  16. package/dist/adapters/memory-adapter/index.d.cts +1 -1
  17. package/dist/adapters/memory-adapter/index.d.mts +2 -2
  18. package/dist/adapters/memory-adapter/index.mjs +4 -4
  19. package/dist/adapters/mongodb-adapter/index.cjs +294 -6
  20. package/dist/adapters/mongodb-adapter/index.d.cts +34 -2
  21. package/dist/adapters/mongodb-adapter/index.d.mts +35 -3
  22. package/dist/adapters/mongodb-adapter/index.mjs +291 -4
  23. package/dist/adapters/prisma-adapter/index.cjs +5 -5
  24. package/dist/adapters/prisma-adapter/index.d.cts +1 -1
  25. package/dist/adapters/prisma-adapter/index.d.mts +2 -2
  26. package/dist/adapters/prisma-adapter/index.mjs +3 -3
  27. package/dist/adapters/test.cjs +3 -3
  28. package/dist/adapters/test.d.cts +1 -1
  29. package/dist/adapters/test.d.mts +2 -2
  30. package/dist/adapters/test.mjs +2 -2
  31. package/dist/api/index.cjs +15 -15
  32. package/dist/api/index.d.cts +1 -1
  33. package/dist/api/index.d.mts +2 -2
  34. package/dist/api/index.mjs +8 -8
  35. package/dist/api/middleware/index.cjs +8 -8
  36. package/dist/api/middleware/index.d.cts +3 -3
  37. package/dist/api/middleware/index.d.mts +4 -4
  38. package/dist/api/middleware/index.mjs +3 -3
  39. package/dist/{api-_B7qO28y.mjs → api-B4RDwBty.mjs} +7 -7
  40. package/dist/{api-Bm89BLgN.cjs → api-CaRC1Gio.cjs} +12 -12
  41. package/dist/{bearer-CHeREcGT.cjs → bearer-DDzrp5M7.cjs} +2 -2
  42. package/dist/{bearer-0SeEmaS4.mjs → bearer-WZcs7gxL.mjs} +1 -1
  43. package/dist/{bun-sqlite-dialect-wuaC2-gh.cjs → bun-sqlite-dialect-DPgTzc0w.cjs} +1 -1
  44. package/dist/{chunk-Ceigfy4P.mjs → chunk-BFnQceUY.mjs} +1 -13
  45. package/dist/{chunk-oakRccI5.cjs → chunk-fPmz2rbM.cjs} +0 -9
  46. package/dist/client/index.cjs +26 -7
  47. package/dist/client/index.d.cts +27 -4
  48. package/dist/client/index.d.mts +28 -5
  49. package/dist/client/index.mjs +25 -6
  50. package/dist/client/lynx/index.cjs +5 -5
  51. package/dist/client/lynx/index.d.cts +15 -15
  52. package/dist/client/lynx/index.d.mts +27 -27
  53. package/dist/client/lynx/index.mjs +3 -3
  54. package/dist/client/plugins/index.d.cts +1 -1
  55. package/dist/client/plugins/index.d.mts +2 -2
  56. package/dist/client/react/index.cjs +5 -5
  57. package/dist/client/react/index.d.cts +26 -26
  58. package/dist/client/react/index.d.mts +16 -16
  59. package/dist/client/react/index.mjs +3 -3
  60. package/dist/client/solid/index.cjs +5 -5
  61. package/dist/client/solid/index.d.cts +16 -16
  62. package/dist/client/solid/index.d.mts +17 -17
  63. package/dist/client/solid/index.mjs +3 -3
  64. package/dist/client/svelte/index.cjs +4 -4
  65. package/dist/client/svelte/index.d.cts +11 -11
  66. package/dist/client/svelte/index.d.mts +16 -16
  67. package/dist/client/svelte/index.mjs +3 -3
  68. package/dist/client/vue/index.cjs +5 -5
  69. package/dist/client/vue/index.d.cts +27 -27
  70. package/dist/client/vue/index.d.mts +28 -28
  71. package/dist/client/vue/index.mjs +3 -3
  72. package/dist/{cookies-B5xeA6i8.cjs → cookies-BTl72THv.cjs} +3 -3
  73. package/dist/{cookies-Ch1JfQIz.mjs → cookies-BTs1o992.mjs} +2 -2
  74. package/dist/crypto/index.cjs +1 -1
  75. package/dist/crypto/index.d.cts +2 -2
  76. package/dist/crypto/index.d.mts +3 -3
  77. package/dist/crypto/index.mjs +1 -1
  78. package/dist/{crypto-CyQRFtkq.mjs → crypto-DH2Aa7jN.mjs} +1 -1
  79. package/dist/{crypto-CcTxYH82.cjs → crypto-DpSCnrTp.cjs} +1 -1
  80. package/dist/db/index.cjs +10 -10
  81. package/dist/db/index.d.cts +1 -1
  82. package/dist/db/index.d.mts +2 -2
  83. package/dist/db/index.mjs +6 -6
  84. package/dist/{db-xiJtoDgz.mjs → db-4tUsQJXd.mjs} +3 -3
  85. package/dist/{db-BTKyYu-_.cjs → db-ChMbb_U-.cjs} +8 -8
  86. package/dist/{email-verification-CWhj2qfb.mjs → email-verification-C_lBu_JY.mjs} +3 -3
  87. package/dist/{email-verification-C-AsfJsb.cjs → email-verification-DI_0H4JC.cjs} +5 -5
  88. package/dist/{hono-Cdukwu5K.cjs → hono-C14rpOEa.cjs} +1 -1
  89. package/dist/{hono-Covz10jU.mjs → hono-CO-zsqI-.mjs} +2 -2
  90. package/dist/{hono-BEq1NYVX.cjs → hono-DnW5crl1.cjs} +4 -4
  91. package/dist/{index-BVHscLQl.d.cts → index-4PEF72Mq.d.cts} +20 -20
  92. package/dist/{index-B-WeOMzg.d.cts → index-BCzB3uDD.d.cts} +833 -833
  93. package/dist/{index-B09P9p5_.d.cts → index-Bw8dyZTc.d.cts} +6 -6
  94. package/dist/{index-Qu-iJGNr.d.mts → index-C0b8eC8h.d.mts} +16 -16
  95. package/dist/{index-jlessPI0.d.mts → index-CrmLTEj8.d.mts} +382 -382
  96. package/dist/index-Dfgobqa-.d.mts +33 -0
  97. package/dist/index.cjs +721 -27
  98. package/dist/index.d.cts +1 -1
  99. package/dist/index.d.mts +2 -2
  100. package/dist/index.mjs +711 -16
  101. package/dist/integrations/next-js.cjs +3 -3
  102. package/dist/integrations/next-js.d.cts +5 -5
  103. package/dist/integrations/next-js.d.mts +6 -6
  104. package/dist/integrations/next-js.mjs +2 -2
  105. package/dist/integrations/node.d.cts +1 -1
  106. package/dist/integrations/node.d.mts +2 -2
  107. package/dist/integrations/react-start.cjs +3 -3
  108. package/dist/integrations/react-start.d.cts +5 -5
  109. package/dist/integrations/react-start.d.mts +5 -5
  110. package/dist/integrations/react-start.mjs +2 -2
  111. package/dist/integrations/svelte-kit.cjs +3 -3
  112. package/dist/integrations/svelte-kit.d.cts +5 -5
  113. package/dist/integrations/svelte-kit.d.mts +6 -6
  114. package/dist/integrations/svelte-kit.mjs +2 -2
  115. package/dist/{ip-B5SwXaMB.cjs → ip-C17Qdq-G.cjs} +2 -2
  116. package/dist/{json-CcJ2HjGo.cjs → json--tl7Grvs.cjs} +1 -1
  117. package/dist/{kysely-adapter-DRuoT_X8.mjs → kysely-adapter-CtIdoHa8.mjs} +3 -3
  118. package/dist/{kysely-adapter-Bxi7sPdG.cjs → kysely-adapter-QZTx077T.cjs} +4 -4
  119. package/dist/{logged-DQK3C64b.cjs → logged-Dv3st5qc.cjs} +1 -1
  120. package/dist/{memory-adapter-BlnDIAAX.cjs → memory-adapter-17RZd5df.cjs} +2 -2
  121. package/dist/{memory-adapter-Db3tsJTn.mjs → memory-adapter-Dp4XdCZK.mjs} +1 -1
  122. package/dist/{node-sqlite-dialect-mJWC8LWQ.cjs → node-sqlite-dialect-DP11CB0y.cjs} +1 -1
  123. package/dist/oauth2/index.cjs +11 -11
  124. package/dist/oauth2/index.d.cts +1 -1
  125. package/dist/oauth2/index.d.mts +2 -2
  126. package/dist/oauth2/index.mjs +6 -6
  127. package/dist/{oauth2-BLeOEi7Y.cjs → oauth2-5RL4cVNS.cjs} +3 -3
  128. package/dist/{oauth2-B8M0lW9l.mjs → oauth2-C56wz-n9.mjs} +2 -2
  129. package/dist/{origin-check-id9lTnqZ.cjs → origin-check-DjLTuZQ8.cjs} +2 -2
  130. package/dist/{passkey-B6N20LSS.cjs → passkey-BTT-v5c-.cjs} +4 -4
  131. package/dist/{passkey-DJsR_GX3.mjs → passkey-CzY_W2us.mjs} +2 -2
  132. package/dist/plugins/access/index.cjs +1 -1
  133. package/dist/plugins/access/index.d.cts +1 -1
  134. package/dist/plugins/access/index.d.mts +1 -1
  135. package/dist/plugins/access/index.mjs +1 -1
  136. package/dist/plugins/bearer/index.cjs +4 -4
  137. package/dist/plugins/bearer/index.d.cts +2 -2
  138. package/dist/plugins/bearer/index.d.mts +3 -3
  139. package/dist/plugins/bearer/index.mjs +3 -3
  140. package/dist/plugins/index.cjs +5215 -31
  141. package/dist/plugins/index.d.cts +351 -351
  142. package/dist/plugins/index.d.mts +268 -268
  143. package/dist/plugins/index.mjs +5202 -19
  144. package/dist/plugins/passkey/index.cjs +10 -10
  145. package/dist/plugins/passkey/index.d.cts +2 -2
  146. package/dist/plugins/passkey/index.d.mts +3 -3
  147. package/dist/plugins/passkey/index.mjs +5 -5
  148. package/dist/{schema-D-qjygfE.cjs → schema-CirKflVs.cjs} +2 -2
  149. package/dist/{session-rH8ougDl.mjs → session-CM3rmOYF.mjs} +1 -1
  150. package/dist/{session-De9POJTW.cjs → session-Cwrj4GfV.cjs} +5 -5
  151. package/dist/social-providers/index.cjs +1 -1
  152. package/dist/social-providers/index.d.cts +1 -1
  153. package/dist/social-providers/index.d.mts +1 -1
  154. package/dist/social-providers/index.mjs +1 -1
  155. package/dist/{social-providers-DoYJhBe3.mjs → social-providers-B8RK5yqI.mjs} +1 -1
  156. package/dist/{social-providers-DcFNUT0S.cjs → social-providers-BmFOIIPk.cjs} +1 -1
  157. package/dist/{state-CqJfCIl9.mjs → state-CQghgH9I.mjs} +1 -1
  158. package/dist/{state--f7fwIGh.cjs → state-CvSihlVT.cjs} +2 -2
  159. package/dist/test-utils/bun-sqlite-dialect-BSWhhKNs.mjs +155 -0
  160. package/dist/test-utils/bun-sqlite-dialect-BZBvHchL.cjs +156 -0
  161. package/dist/test-utils/index.cjs +8927 -482
  162. package/dist/test-utils/index.d.cts +8395 -337
  163. package/dist/test-utils/index.d.mts +8394 -334
  164. package/dist/test-utils/index.mjs +8712 -265
  165. package/dist/test-utils/node-sqlite-dialect-BgJqjBOu.cjs +156 -0
  166. package/dist/test-utils/node-sqlite-dialect-DAQXiobH.mjs +155 -0
  167. package/dist/types/index.d.cts +1 -1
  168. package/dist/types/index.d.mts +2 -2
  169. package/dist/{url-D2yvKaM7.cjs → url-Ce7CVaQD.cjs} +1 -1
  170. package/dist/utils/cookies.cjs +4 -4
  171. package/dist/utils/cookies.d.cts +1 -1
  172. package/dist/utils/cookies.d.mts +2 -2
  173. package/dist/utils/cookies.mjs +2 -2
  174. package/package.json +2 -2
  175. package/dist/auth-Bo9PDt5j.cjs +0 -739
  176. package/dist/auth-Ds4AG4h4.mjs +0 -709
  177. package/dist/index-C2-2QDyS.d.cts +0 -35
  178. package/dist/index-CWAqWfoZ.d.cts +0 -27
  179. package/dist/index-CYSShrFx.d.mts +0 -27
  180. package/dist/index-ppgcFe12.d.mts +0 -33
  181. package/dist/index-to0RO38U.d.mts +0 -35
  182. package/dist/mongodb-adapter-C_qGQED5.mjs +0 -291
  183. package/dist/mongodb-adapter-C_qfK87x.cjs +0 -297
  184. package/dist/plugins-C1lm2Wj3.cjs +0 -5254
  185. package/dist/plugins-Dai1QkRU.mjs +0 -5199
  186. package/dist/vanilla-Bu2PmFzE.cjs +0 -30
  187. package/dist/vanilla-TfHqeiC7.mjs +0 -25
  188. /package/dist/{access-B5bBI4uT.mjs → access-D0DRqUKC.mjs} +0 -0
  189. /package/dist/{adapter-factory-BXei1QRX.mjs → adapter-factory-lKIOf2OR.mjs} +0 -0
  190. /package/dist/{bun-sqlite-dialect-CfkBo2v5.mjs → bun-sqlite-dialect-6NqeV3px.mjs} +0 -0
  191. /package/dist/{defu-B0Svx_x4.cjs → defu-BmOYq4PU.cjs} +0 -0
  192. /package/dist/{defu-SZkpHhbR.mjs → defu-D9ZTXqlo.mjs} +0 -0
  193. /package/dist/{hono-DuE14mmj.d.mts → hono-D0VqxVy1.d.mts} +0 -0
  194. /package/dist/{index-B5_0QTLg.d.mts → index-BDhhaE6Y.d.cts} +0 -0
  195. /package/dist/{index-B7SKyR_a.d.cts → index-CF9iq55a.d.mts} +0 -0
  196. /package/dist/{index-CTwrVluH.d.mts → index-CRt_fppq.d.mts} +0 -0
  197. /package/dist/{index-cs4Sh0En.d.cts → index-CdLyn7qP.d.cts} +0 -0
  198. /package/dist/{node-sqlite-dialect-BfBzyOAg.mjs → node-sqlite-dialect-w4DAg0PH.mjs} +0 -0
  199. /package/dist/{parser-CeAer-RM.mjs → parser-B25ylT5u.mjs} +0 -0
  200. /package/dist/{parser-CdncAl9i.cjs → parser-B_wxvaAr.cjs} +0 -0
@@ -1,35 +0,0 @@
1
- import { I as FaireAuthOptions } from "./index-B-WeOMzg.cjs";
2
- import { DBAdapter, DBAdapterDebugLogOption } from "@faire-auth/core/db/adapter";
3
- import { Db, MongoClient } from "mongodb";
4
-
5
- //#region src/adapters/mongodb-adapter/mongodb-adapter.d.ts
6
- interface MongoDBAdapterConfig {
7
- /**
8
- * MongoDB client instance
9
- * If not provided, Database transactions won't be enabled.
10
- */
11
- client?: MongoClient;
12
- /**
13
- * Enable debug logs for the adapter
14
- *
15
- * @default false
16
- */
17
- debugLogs?: DBAdapterDebugLogOption;
18
- /**
19
- * Use plural table names
20
- *
21
- * @default false
22
- */
23
- usePlural?: boolean;
24
- /**
25
- * Whether to execute multiple operations in a transaction.
26
- *
27
- * If the database doesn't support transactions,
28
- * set this to `false` and operations will be executed sequentially.
29
- * @default false
30
- */
31
- transaction?: boolean;
32
- }
33
- declare const mongodbAdapter: (db: Db, config?: MongoDBAdapterConfig) => (options: FaireAuthOptions) => DBAdapter<FaireAuthOptions>;
34
- //#endregion
35
- export { mongodbAdapter as n, MongoDBAdapterConfig as t };
@@ -1,27 +0,0 @@
1
- import { I as FaireAuthOptions, _t as FaireAuthPlugin } from "./index-B-WeOMzg.cjs";
2
- import { BetterFetch, BetterFetchError, BetterFetchOption } from "@better-fetch/fetch";
3
- import { PreinitializedWritableAtom } from "nanostores";
4
-
5
- //#region src/client/query.d.ts
6
- declare const useAuthQuery: <T>(initializedAtom: PreinitializedWritableAtom<any> | PreinitializedWritableAtom<any>[], path: string, $fetch: BetterFetch, options?: ((value: {
7
- data: null | T;
8
- error: BetterFetchError | null;
9
- isPending: boolean;
10
- }) => BetterFetchOption) | BetterFetchOption) => PreinitializedWritableAtom<{
11
- data: null | T;
12
- error: BetterFetchError | null;
13
- isPending: boolean;
14
- isRefetching: boolean;
15
- refetch: () => void;
16
- }> & object;
17
- //#endregion
18
- //#region src/client/index.d.ts
19
- declare const InferPlugin: <T extends FaireAuthPlugin>() => {
20
- id: "infer-server-plugin";
21
- $InferServerPlugin: T;
22
- };
23
- declare function InferAuth<O extends {
24
- options: FaireAuthOptions;
25
- }>(): O["options"];
26
- //#endregion
27
- export { InferPlugin as n, useAuthQuery as r, InferAuth as t };
@@ -1,27 +0,0 @@
1
- import { I as FaireAuthOptions, _t as FaireAuthPlugin } from "./index-jlessPI0.mjs";
2
- import { BetterFetch, BetterFetchError, BetterFetchOption } from "@better-fetch/fetch";
3
- import { PreinitializedWritableAtom } from "nanostores";
4
-
5
- //#region src/client/query.d.ts
6
- declare const useAuthQuery: <T>(initializedAtom: PreinitializedWritableAtom<any> | PreinitializedWritableAtom<any>[], path: string, $fetch: BetterFetch, options?: ((value: {
7
- data: null | T;
8
- error: BetterFetchError | null;
9
- isPending: boolean;
10
- }) => BetterFetchOption) | BetterFetchOption) => PreinitializedWritableAtom<{
11
- data: null | T;
12
- error: BetterFetchError | null;
13
- isPending: boolean;
14
- isRefetching: boolean;
15
- refetch: () => void;
16
- }> & object;
17
- //#endregion
18
- //#region src/client/index.d.ts
19
- declare const InferPlugin: <T extends FaireAuthPlugin>() => {
20
- id: "infer-server-plugin";
21
- $InferServerPlugin: T;
22
- };
23
- declare function InferAuth<O extends {
24
- options: FaireAuthOptions;
25
- }>(): O["options"];
26
- //#endregion
27
- export { InferPlugin as n, useAuthQuery as r, InferAuth as t };
@@ -1,33 +0,0 @@
1
- import { t as ContextVars } from "./hono-DuE14mmj.mjs";
2
- import * as hono44 from "hono";
3
- import * as _faire_auth_core_types39 from "@faire-auth/core/types";
4
-
5
- //#region src/plugins/bearer/index.d.ts
6
- interface BearerOptions {
7
- /**
8
- * If true, only signed tokens
9
- * will be converted to session
10
- * cookies
11
- *
12
- * @default false
13
- */
14
- requireSignature?: boolean;
15
- }
16
- /**
17
- * Converts bearer token to session cookie
18
- */
19
- declare const bearer: (options?: BearerOptions) => {
20
- id: "bearer";
21
- hooks: {
22
- before: {
23
- matcher: (ctx: hono44.Context<ContextVars<any>, any, {}>) => boolean;
24
- handler: (_opts: _faire_auth_core_types39.FaireAuthOptions<any>) => (ctx: hono44.Context<ContextVars<object>, string, {}>) => Promise<void>;
25
- }[];
26
- after: {
27
- matcher: (_ctx: hono44.Context<ContextVars<any>, any, {}>) => true;
28
- handler: (_opts: _faire_auth_core_types39.FaireAuthOptions<any>) => (ctx: hono44.Context<ContextVars<object>, string, {}>) => Promise<void>;
29
- }[];
30
- };
31
- };
32
- //#endregion
33
- export { bearer as t };
@@ -1,35 +0,0 @@
1
- import { I as FaireAuthOptions } from "./index-jlessPI0.mjs";
2
- import { DBAdapter, DBAdapterDebugLogOption } from "@faire-auth/core/db/adapter";
3
- import { Db, MongoClient } from "mongodb";
4
-
5
- //#region src/adapters/mongodb-adapter/mongodb-adapter.d.ts
6
- interface MongoDBAdapterConfig {
7
- /**
8
- * MongoDB client instance
9
- * If not provided, Database transactions won't be enabled.
10
- */
11
- client?: MongoClient;
12
- /**
13
- * Enable debug logs for the adapter
14
- *
15
- * @default false
16
- */
17
- debugLogs?: DBAdapterDebugLogOption;
18
- /**
19
- * Use plural table names
20
- *
21
- * @default false
22
- */
23
- usePlural?: boolean;
24
- /**
25
- * Whether to execute multiple operations in a transaction.
26
- *
27
- * If the database doesn't support transactions,
28
- * set this to `false` and operations will be executed sequentially.
29
- * @default false
30
- */
31
- transaction?: boolean;
32
- }
33
- declare const mongodbAdapter: (db: Db, config?: MongoDBAdapterConfig) => (options: FaireAuthOptions) => DBAdapter<FaireAuthOptions>;
34
- //#endregion
35
- export { mongodbAdapter as n, MongoDBAdapterConfig as t };
@@ -1,291 +0,0 @@
1
- import { t as createAdapterFactory } from "./adapter-factory-BXei1QRX.mjs";
2
- import { ObjectId } from "mongodb";
3
-
4
- //#region src/adapters/mongodb-adapter/mongodb-adapter.ts
5
- const mongodbAdapter = (db, config) => {
6
- let lazyOptions;
7
- const createCustomAdapter = (db$1, session) => ({ options, getFieldName, schema, getDefaultModelName }) => {
8
- function serializeID({ field, value, model }) {
9
- model = getDefaultModelName(model);
10
- if (field === "id" || field === "_id" || schema[model].fields[field]?.references?.field === "id") {
11
- if (typeof value !== "string") {
12
- if (value instanceof ObjectId) return value;
13
- if (Array.isArray(value)) return value.map((v) => {
14
- if (typeof v === "string") try {
15
- return new ObjectId(v);
16
- } catch (e) {
17
- return v;
18
- }
19
- if (v instanceof ObjectId) return v;
20
- throw new Error("Invalid id value");
21
- });
22
- throw new Error("Invalid id value");
23
- }
24
- try {
25
- return new ObjectId(value);
26
- } catch (e) {
27
- return value;
28
- }
29
- }
30
- return value;
31
- }
32
- function convertWhereClause({ where, model }) {
33
- if (!where.length) return {};
34
- const conditions = where.map((w) => {
35
- const { field: field_, value, operator = "eq", connector = "AND" } = w;
36
- let condition;
37
- let field = getFieldName({
38
- model,
39
- field: field_
40
- });
41
- if (field === "id") field = "_id";
42
- switch (operator.toLowerCase()) {
43
- case "eq":
44
- condition = { [field]: serializeID({
45
- field,
46
- value,
47
- model
48
- }) };
49
- break;
50
- case "in":
51
- condition = { [field]: { $in: Array.isArray(value) ? value.map((v) => serializeID({
52
- field,
53
- value: v,
54
- model
55
- })) : [serializeID({
56
- field,
57
- value,
58
- model
59
- })] } };
60
- break;
61
- case "not_in":
62
- condition = { [field]: { $nin: Array.isArray(value) ? value.map((v) => serializeID({
63
- field,
64
- value: v,
65
- model
66
- })) : [serializeID({
67
- field,
68
- value,
69
- model
70
- })] } };
71
- break;
72
- case "gt":
73
- condition = { [field]: { $gt: value } };
74
- break;
75
- case "gte":
76
- condition = { [field]: { $gte: value } };
77
- break;
78
- case "lt":
79
- condition = { [field]: { $lt: value } };
80
- break;
81
- case "lte":
82
- condition = { [field]: { $lte: value } };
83
- break;
84
- case "ne":
85
- condition = { [field]: { $ne: value } };
86
- break;
87
- case "contains":
88
- condition = { [field]: { $regex: `.*${escapeForMongoRegex(value)}.*` } };
89
- break;
90
- case "starts_with":
91
- condition = { [field]: { $regex: `^${escapeForMongoRegex(value)}` } };
92
- break;
93
- case "ends_with":
94
- condition = { [field]: { $regex: `${escapeForMongoRegex(value)}$` } };
95
- break;
96
- default: throw new Error(`Unsupported operator: ${operator}`);
97
- }
98
- return {
99
- condition,
100
- connector
101
- };
102
- });
103
- if (conditions.length === 1) return conditions[0].condition;
104
- const andConditions = conditions.filter((c) => c.connector === "AND").map((c) => c.condition);
105
- const orConditions = conditions.filter((c) => c.connector === "OR").map((c) => c.condition);
106
- let clause = {};
107
- if (andConditions.length) clause = {
108
- ...clause,
109
- $and: andConditions
110
- };
111
- if (orConditions.length) clause = {
112
- ...clause,
113
- $or: orConditions
114
- };
115
- return clause;
116
- }
117
- return {
118
- async create({ model, data: values }) {
119
- return {
120
- _id: (await db$1.collection(model).insertOne(values, { ...session && { session } })).insertedId.toString(),
121
- ...values
122
- };
123
- },
124
- async findOne({ model, where, select }) {
125
- const clause = convertWhereClause({
126
- where,
127
- model
128
- });
129
- const projection = select ? Object.fromEntries(select.map((field) => [getFieldName({
130
- field,
131
- model
132
- }), 1])) : void 0;
133
- const res = await db$1.collection(model).findOne(clause, {
134
- ...session && { session },
135
- ...projection && { projection }
136
- });
137
- if (!res) return null;
138
- return res;
139
- },
140
- async findMany({ model, where, limit, offset, sortBy }) {
141
- const clause = where ? convertWhereClause({
142
- where,
143
- model
144
- }) : {};
145
- const cursor = db$1.collection(model).find(clause, { ...session && { session } });
146
- if (limit) cursor.limit(limit);
147
- if (offset) cursor.skip(offset);
148
- if (sortBy) cursor.sort(getFieldName({
149
- field: sortBy.field,
150
- model
151
- }), sortBy.direction === "desc" ? -1 : 1);
152
- return await cursor.toArray();
153
- },
154
- async count({ model, where }) {
155
- const clause = where ? convertWhereClause({
156
- where,
157
- model
158
- }) : {};
159
- return await db$1.collection(model).countDocuments(clause, { ...session && { session } });
160
- },
161
- async update({ model, where, update: values }) {
162
- const clause = convertWhereClause({
163
- where,
164
- model
165
- });
166
- const res = await db$1.collection(model).findOneAndUpdate(clause, { $set: values }, {
167
- ...session && { session },
168
- returnDocument: "after"
169
- });
170
- if (!res) return null;
171
- return res;
172
- },
173
- async updateMany({ model, where, update: values }) {
174
- const clause = convertWhereClause({
175
- where,
176
- model
177
- });
178
- return (await db$1.collection(model).updateMany(clause, { $set: values }, { ...session && { session } })).modifiedCount;
179
- },
180
- async delete({ model, where }) {
181
- const clause = convertWhereClause({
182
- where,
183
- model
184
- });
185
- await db$1.collection(model).deleteOne(clause, { ...session && { session } });
186
- },
187
- async deleteMany({ model, where }) {
188
- const clause = convertWhereClause({
189
- where,
190
- model
191
- });
192
- return (await db$1.collection(model).deleteMany(clause, { ...session && { session } })).deletedCount;
193
- }
194
- };
195
- };
196
- let lazyAdapter = null;
197
- let adapterOptions = null;
198
- adapterOptions = {
199
- config: {
200
- adapterId: "mongodb-adapter",
201
- adapterName: "MongoDB Adapter",
202
- usePlural: config?.usePlural ?? false,
203
- debugLogs: config?.debugLogs ?? false,
204
- mapKeysTransformInput: { id: "_id" },
205
- mapKeysTransformOutput: { _id: "id" },
206
- supportsNumericIds: false,
207
- transaction: config?.client && (config?.transaction ?? true) ? async (cb) => {
208
- if (!config.client) return cb(lazyAdapter(lazyOptions));
209
- const session = config.client.startSession();
210
- try {
211
- session.startTransaction();
212
- const result = await cb(createAdapterFactory({
213
- config: adapterOptions.config,
214
- adapter: createCustomAdapter(db, session)
215
- })(lazyOptions));
216
- await session.commitTransaction();
217
- return result;
218
- } catch (err) {
219
- await session.abortTransaction();
220
- throw err;
221
- } finally {
222
- await session.endSession();
223
- }
224
- } : false,
225
- customTransformInput({ action, data, field, fieldAttributes, schema, model, options }) {
226
- if (field === "_id" || fieldAttributes.references?.field === "id") {
227
- if (action === "update") {
228
- if (typeof data === "string") try {
229
- return new ObjectId(data);
230
- } catch (error) {
231
- return data;
232
- }
233
- return data;
234
- }
235
- if (Array.isArray(data)) return data.map((v) => {
236
- if (typeof v === "string") try {
237
- return new ObjectId(v);
238
- } catch (error) {
239
- return v;
240
- }
241
- return v;
242
- });
243
- if (typeof data === "string") try {
244
- return new ObjectId(data);
245
- } catch (error) {
246
- return new ObjectId();
247
- }
248
- if (data === null && fieldAttributes.references?.field === "id") return null;
249
- return new ObjectId();
250
- }
251
- return data;
252
- },
253
- customTransformOutput({ data, field, fieldAttributes }) {
254
- if (field === "id" || fieldAttributes.references?.field === "id") {
255
- if (data instanceof ObjectId) return data.toHexString();
256
- if (Array.isArray(data)) return data.map((v) => {
257
- if (v instanceof ObjectId) return v.toHexString();
258
- return v;
259
- });
260
- return data;
261
- }
262
- return data;
263
- },
264
- customIdGenerator(props) {
265
- return new ObjectId().toString();
266
- }
267
- },
268
- adapter: createCustomAdapter(db)
269
- };
270
- lazyAdapter = createAdapterFactory(adapterOptions);
271
- return (options) => {
272
- lazyOptions = options;
273
- return lazyAdapter(options);
274
- };
275
- };
276
- /**
277
- * Safely escape user input for use in a MongoDB regex.
278
- * This ensures the resulting pattern is treated as literal text,
279
- * and not as a regex with special syntax.
280
- *
281
- * @param input - The input string to escape. Any type that isn't a string will be converted to an empty string.
282
- * @param maxLength - The maximum length of the input string to escape. Defaults to 256. This is to prevent DOS attacks.
283
- * @returns The escaped string.
284
- */
285
- function escapeForMongoRegex(input, maxLength = 256) {
286
- if (typeof input !== "string") return "";
287
- return input.slice(0, maxLength).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
288
- }
289
-
290
- //#endregion
291
- export { mongodbAdapter as t };