@sqrzro/server 4.0.0-alpha.23 → 4.0.0-alpha.25

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.
@@ -1,2 +1,2 @@
1
- export{a as createMFA,e as createReset,g as createSession,j as createUser,h as deleteSession,k as deleteUser,i as deleteUserSessions,c as updatePasswordWithPassword,d as updatePasswordWithToken,b as validateMFA,f as validateReset,l as validateUser}from'../chunk-6AWLN6E3.js';import'../chunk-CESWDV42.js';export{j as ensureSessionUser,i as getSessionUser,h as validateSession}from'../chunk-7LTWI5JO.js';import'../chunk-6LBWOS53.js';import'../chunk-JI2U2EMD.js';import'../chunk-3JA7XHAG.js';import'../chunk-C2XLUXVN.js';import'../chunk-KC7SPWBL.js';//# sourceMappingURL=index.js.map
1
+ export{a as createMFA,e as createReset,g as createSession,j as createUser,h as deleteSession,k as deleteUser,i as deleteUserSessions,c as updatePasswordWithPassword,d as updatePasswordWithToken,b as validateMFA,f as validateReset,l as validateUser}from'../chunk-6AWLN6E3.js';import'../chunk-CESWDV42.js';export{j as ensureSessionUser,i as getSessionUser,h as validateSession}from'../chunk-7LTWI5JO.js';import'../chunk-HTYJSYK5.js';import'../chunk-JI2U2EMD.js';import'../chunk-3JA7XHAG.js';import'../chunk-C2XLUXVN.js';import'../chunk-KC7SPWBL.js';//# sourceMappingURL=index.js.map
2
2
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,2 @@
1
+ function o(n,t){let r=Array.isArray(n)?n[0]:n;return r?.startsWith(t)?r:null}var e=o;function u(n,t){let r=e(n,t);if(!r)throw new Error(`Could not ensure ID. '${n}' is not a valid ID with prefix '${t}'.`);return r}var l=u;async function g(n,t,r){if(!r)throw new Error("");let s=r instanceof Promise?await r:r,i=e(s[n],t);if(!i)throw new Error("");return i}var w=g;export{e as a,l as b,w as c};//# sourceMappingURL=chunk-HTYJSYK5.js.map
2
+ //# sourceMappingURL=chunk-HTYJSYK5.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utility/guard-id.ts","../src/utility/ensure-id.ts","../src/utility/get-id-from-params.ts"],"names":["guardID","id","prefix","value","guard_id_default","ensureID","guardedID","ensure_id_default","getIDFromParams","key","params","awaitedParams","get_id_from_params_default"],"mappings":"AAAA,SAASA,CAAAA,CACLC,EACAC,CAAAA,CACQ,CACR,IAAMC,CAAAA,CAAQ,KAAA,CAAM,QAAQF,CAAE,CAAA,CAAIA,EAAG,CAAC,CAAA,CAAIA,EAE1C,OAAKE,CAAAA,EAAO,WAAWD,CAAM,CAAA,CAItBC,EAHI,IAIf,KAEOC,CAAAA,CAAQJ,ECXf,SAASK,CAAAA,CAA2BJ,CAAAA,CAA0CC,EAAmB,CAC7F,IAAMI,EAAYF,CAAAA,CAAWH,CAAAA,CAAIC,CAAM,CAAA,CAEvC,GAAI,CAACI,CAAAA,CACD,MAAM,IAAI,KAAA,CAAM,CAAA,sBAAA,EAAyBL,CAAE,CAAA,iCAAA,EAAoCC,CAAM,CAAA,EAAA,CAAI,EAG7F,OAAOI,CACX,CAEA,IAAOC,CAAAA,CAAQF,ECVf,eAAeG,CAAAA,CACXC,EACAP,CAAAA,CACAQ,CAAAA,CACU,CACV,GAAI,CAACA,EACD,MAAM,IAAI,MAAM,EAAE,CAAA,CAGtB,IAAMC,CAAAA,CAAgBD,CAAAA,YAAkB,QAAU,MAAMA,CAAAA,CAASA,EAC3DT,CAAAA,CAAKG,CAAAA,CAAWO,EAAcF,CAAG,CAAA,CAAGP,CAAM,CAAA,CAEhD,GAAI,CAACD,CAAAA,CACD,MAAM,IAAI,KAAA,CAAM,EAAE,EAGtB,OAAOA,CACX,CAEA,IAAOW,CAAAA,CAAQJ","file":"chunk-HTYJSYK5.js","sourcesContent":["function guardID<T extends string>(\n id: string[] | string | null | undefined,\n prefix: string\n): T | null {\n const value = Array.isArray(id) ? id[0] : id;\n\n if (!value?.startsWith(prefix)) {\n return null;\n }\n\n return value as T;\n}\n\nexport default guardID;\n","import guardID from './guard-id';\n\nfunction ensureID<T extends string>(id: string[] | string | null | undefined, prefix: string): T {\n const guardedID = guardID<T>(id, prefix);\n\n if (!guardedID) {\n throw new Error(`Could not ensure ID. '${id}' is not a valid ID with prefix '${prefix}'.`);\n }\n\n return guardedID;\n}\n\nexport default ensureID;\n","import guardID from './guard-id';\n\nasync function getIDFromParams<T extends string>(\n key: string,\n prefix: string,\n params?: Promise<Record<string, string>> | Record<string, string> | null\n): Promise<T> {\n if (!params) {\n throw new Error('');\n }\n\n const awaitedParams = params instanceof Promise ? await params : params;\n const id = guardID<T>(awaitedParams[key], prefix);\n\n if (!id) {\n throw new Error('');\n }\n\n return id;\n}\n\nexport default getIDFromParams;\n"]}
@@ -1,6 +1,6 @@
1
1
  import { PgColumn } from 'drizzle-orm/pg-core';
2
- import { P as Paginated } from '../interfaces-BheedaEI.js';
3
2
  import { SQL } from 'drizzle-orm';
3
+ import { P as Paginated } from '../interfaces-BheedaEI.js';
4
4
 
5
5
  type QueryConditions = (SQL | undefined)[];
6
6
  interface QueryConfig {
@@ -54,22 +54,50 @@ type InferUpdate<C> = C extends {
54
54
  data: D;
55
55
  model: M;
56
56
  } : never;
57
-
58
- declare function createRepository<I extends Record<string, PgColumn<any, any, any>>, C extends RepositoryConfig>(repoConfig: C & {
59
- indexes: I;
60
- }): {
61
- create: (data: InferCreate<C>["data"]) => Promise<InferCreate<C>["model"]>;
57
+ type InferRepository<I extends Record<string, PgColumn<any, any, any>>, C extends RepositoryConfig> = (C extends {
58
+ makeCreateMutation: any;
59
+ } ? {
60
+ create: (data: InferCreate<C>['data']) => Promise<InferCreate<C>['model']>;
61
+ } : {}) & (C extends {
62
+ makeQuery: any;
63
+ } ? {
64
+ getAll: (config?: SelectFunctionConfig<InferFilters<C>, InferGuards<C>> | undefined) => Promise<InferQueryModel<C>[]>;
65
+ } & (C extends {
66
+ indexes: any;
67
+ } ? {
62
68
  ensureByID: (id: InferID<C>) => Promise<InferQueryModel<C>>;
63
69
  ensureByIndex: <K extends keyof I>(key: K, value: InferColumnType<I[K]>, config?: SelectFunctionConfig<InferFilters<C>, InferGuards<C>>) => Promise<InferQueryModel<C>>;
64
- getAll: (config?: SelectFunctionConfig<InferFilters<C>, InferGuards<C>>) => Promise<InferQueryModel<C>[]>;
65
70
  getByID: (id: InferID<C>) => Promise<InferQueryModel<C> | null>;
66
71
  getByIndex: <K extends keyof I>(key: K, value: InferColumnType<I[K]>, config?: SelectFunctionConfig<InferFilters<C>, InferGuards<C>>) => Promise<InferQueryModel<C> | null>;
67
- getFromParams: (params?: Promise<Record<string, string>> | null) => Promise<InferQueryModel<C>>;
72
+ } & (C extends {
73
+ prefix: any;
74
+ } ? {
68
75
  getFromSession: () => Promise<InferQueryModel<C>>;
69
- getPaginated: (config?: PaginatedSelectFunctionConfig<InferFilters<C>, InferGuards<C>>) => Promise<Paginated<InferQueryModel<C>>>;
70
- guardID: (id: string[] | string | null | undefined) => InferID<C> | null;
71
- ensureID: (id: string[] | string | null | undefined) => InferID<C>;
72
- update: (data: InferUpdate<C>["data"]) => Promise<InferUpdate<C>["model"]>;
73
- };
76
+ } & (C extends {
77
+ paramKey: any;
78
+ } ? {
79
+ getFromParams: (params?: Record<string, string> | Promise<Record<string, string>> | null | undefined) => Promise<InferQueryModel<C>>;
80
+ } : {}) : {}) : {}) & (C extends {
81
+ makeMetaQuery: any;
82
+ } ? {
83
+ getPaginated: (config?: PaginatedSelectFunctionConfig<InferFilters<C>, InferGuards<C>> | undefined) => Promise<Paginated<InferQueryModel<C>>>;
84
+ } : {}) : {}) & (C extends {
85
+ prefix: any;
86
+ } ? {
87
+ guardID: (id: string | string[] | null | undefined) => InferID<C> | null;
88
+ ensureID: (id: string | string[] | null | undefined) => InferID<C>;
89
+ } & (C extends {
90
+ paramKey: any;
91
+ } ? {
92
+ getIDFromParams: (params?: Record<string, string> | Promise<Record<string, string>> | null | undefined) => Promise<InferID<C>>;
93
+ } : {}) : {}) & (C extends {
94
+ makeUpdateMutation: any;
95
+ } ? {
96
+ update: (data: InferUpdate<C>['data']) => Promise<InferUpdate<C>['model']>;
97
+ } : {});
98
+
99
+ declare function createRepository<I extends Record<string, PgColumn<any, any, any>>, C extends RepositoryConfig>(repoConfig: C & {
100
+ indexes?: I;
101
+ }): InferRepository<I, C>;
74
102
 
75
103
  export { type QueryConditions, type QueryConfig, createRepository };
@@ -1,2 +1,2 @@
1
- import {j}from'../chunk-7LTWI5JO.js';import {c,b,a}from'../chunk-6LBWOS53.js';import'../chunk-JI2U2EMD.js';import'../chunk-C2XLUXVN.js';import'../chunk-KC7SPWBL.js';import {cache}from'react';import {eq}from'drizzle-orm';import {notFound}from'next/navigation';function k(t){async function w(e){if(!t.makeQuery)throw new Error("Could not `getAll`. `makeQuery` function is not defined.");let r=[],n=await t.applyFilters?.(e?.filters??null);r.push(...n||[]);let i=await t.applyGuards?.(e?.guards??null);return r.push(...i||[]),t.makeQuery(r,{limit:e?.limit??-1,offset:e?.offset??0})}async function C(e){if(!t.makeMetaQuery)throw new Error("Could not `getPaginated`. `makeMetaQuery` function is not defined.");if(!t.makeQuery)throw new Error("Could not `getPaginated`. `makeQuery` function is not defined.");let r=[],n=await t.applyFilters?.(e?.filters??null);r.push(...n||[]);let i=await t.applyGuards?.(e?.guards??null);r.push(...i||[]);let u=await t.makeMetaQuery(r);return {data:await t.makeQuery(r,{limit:e?.limit??10,offset:((e?.page??1)-1)*(e?.limit??10)}),meta:{limit:e?.limit??10,page:e?.page??1,total:u.count}}}async function o(e,r,n){if(!t.makeQuery)throw new Error("Could not `getByIndex`. `makeQuery` function is not defined.");if(!t.indexes?.[e])throw new Error(`'Could not \`getByIndex\`. Index ${String(e)} is not defined in the repository configuration.`);let i=[],u=await t.applyFilters?.(n?.filters??null);i.push(...u||[]);let m=await t.applyGuards?.(n?.guards??null);i.push(...m||[]),i.push(eq(t.indexes[e],r));let[F]=await t.makeQuery(i,{limit:1,offset:0});return F??null}async function s(e,r,n){let i=await o(e,r,n);return i||notFound()}async function d(e){return o("id",e)}async function l(e){return s("id",e)}async function h(e){if(!t.paramKey)throw new Error("Could not `getFromParams`. `paramKey` is not defined.");if(!t.prefix)throw new Error("Could not `getFromParams`. `prefix` is not defined.");let r=await c(t.paramKey,t.prefix,e);return l(r)}async function g(){if(!t.prefix)throw new Error("Could not `getFromSession`. `prefix` is not defined.");let e=await j(),r=b(e.id,t.prefix),n=await d(r);if(!n)throw new Error(`Could not \`getFromSession\`. No record with ID '${r}' found.`);return n}async function x(e){if(!t.makeCreateMutation)throw new Error("Could not `create`. `makeCreateMutation` is not defined.");return t.makeCreateMutation(e)}async function Q(e){if(!t.makeUpdateMutation)throw new Error("Could not `update`. `makeUpdateMutation` is not defined.");return t.makeUpdateMutation(e)}function f(e){if(!t.prefix)throw new Error("Could not `guardID`. `prefix` is not defined.");return a(e,t.prefix)}function P(e){let r=f(e);if(!r)throw new Error(`Invalid ID '${e}'. Expected a string starting with '${t.prefix}'.`);return r}return {create:x,ensureByID:cache(l),ensureByIndex:cache(s),getAll:cache(w),getByID:cache(d),getByIndex:cache(o),getFromParams:cache(h),getFromSession:cache(g),getPaginated:cache(C),guardID:f,ensureID:P,update:Q}}var E=k;export{E as createRepository};//# sourceMappingURL=index.js.map
1
+ import {j}from'../chunk-7LTWI5JO.js';import {c,b,a}from'../chunk-HTYJSYK5.js';import'../chunk-JI2U2EMD.js';import'../chunk-C2XLUXVN.js';import'../chunk-KC7SPWBL.js';import {cache}from'react';import {eq}from'drizzle-orm';import {notFound}from'next/navigation';function K(e){async function x(t){if(!e.makeQuery)throw new Error("Could not `getAll`. `makeQuery` function is not defined.");let r=[],a=await e.applyFilters?.(t?.filters??null);r.push(...a||[]);let i=await e.applyGuards?.(t?.guards??null);return r.push(...i||[]),e.makeQuery(r,{limit:t?.limit??-1,offset:t?.offset??0})}async function C(t){if(!e.makeMetaQuery)throw new Error("Could not `getPaginated`. `makeMetaQuery` function is not defined.");if(!e.makeQuery)throw new Error("Could not `getPaginated`. `makeQuery` function is not defined.");let r=[],a=await e.applyFilters?.(t?.filters??null);r.push(...a||[]);let i=await e.applyGuards?.(t?.guards??null);r.push(...i||[]);let u=await e.makeMetaQuery(r);return {data:await e.makeQuery(r,{limit:t?.limit??10,offset:((t?.page??1)-1)*(t?.limit??10)}),meta:{limit:t?.limit??10,page:t?.page??1,total:u.count}}}async function o(t,r,a){if(!e.makeQuery)throw new Error("Could not `getByIndex`. `makeQuery` function is not defined.");if(!e.indexes?.[t])throw new Error(`'Could not \`getByIndex\`. Index ${String(t)} is not defined in the repository configuration.`);let i=[],u=await e.applyFilters?.(a?.filters??null);i.push(...u||[]);let c=await e.applyGuards?.(a?.guards??null);i.push(...c||[]),i.push(eq(e.indexes[t],r));let[M]=await e.makeQuery(i,{limit:1,offset:0});return M??null}async function d(t,r,a){let i=await o(t,r,a);return i||notFound()}async function l(t){return o("id",t)}async function f(t){return d("id",t)}async function m(t){if(!e.paramKey)throw new Error("Could not `getIDFromParams`. `paramKey` is not defined.");if(!e.prefix)throw new Error("Could not `getIDFromParams`. `prefix` is not defined.");return c(e.paramKey,e.prefix,t)}async function P(t){if(!e.paramKey)throw new Error("Could not `getFromParams`. `paramKey` is not defined.");if(!e.prefix)throw new Error("Could not `getFromParams`. `prefix` is not defined.");return f(await m(t))}async function Q(){if(!e.prefix)throw new Error("Could not `getFromSession`. `prefix` is not defined.");let t=await j(),r=b(t.id,e.prefix),a=await l(r);if(!a)throw new Error(`Could not \`getFromSession\`. No record with ID '${r}' found.`);return a}async function F(t){if(!e.makeCreateMutation)throw new Error("Could not `create`. `makeCreateMutation` is not defined.");return e.makeCreateMutation(t)}async function g(t){if(!e.makeUpdateMutation)throw new Error("Could not `update`. `makeUpdateMutation` is not defined.");return e.makeUpdateMutation(t)}function y(t){if(!e.prefix)throw new Error("Could not `guardID`. `prefix` is not defined.");return a(t,e.prefix)}function D(t){let r=y(t);if(!r)throw new Error(`Invalid ID '${t}'. Expected a string starting with '${e.prefix}'.`);return r}let n={};return "makeCreateMutation"in e&&(n.create=F),"makeQuery"in e&&(n.getAll=cache(x),"indexes"in e&&(n.ensureByID=cache(f),n.ensureByIndex=cache(d),n.getByID=cache(l),n.getByIndex=cache(o),"prefix"in e&&(n.getFromSession=cache(Q),"paramKey"in e&&(n.getFromParams=cache(P)))),"makeMetaQuery"in e&&(n.getPaginated=cache(C))),"prefix"in e&&(n.ensureID=D,n.guardID=y,"paramKey"in e&&(n.getIDFromParams=m)),"makeUpdateMutation"in e&&(n.update=g),n}var R=K;export{R as createRepository};//# sourceMappingURL=index.js.map
2
2
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/database/create-repository.ts"],"names":["createRepository","repoConfig","uncached_getAll","config","conditions","filters","guards","uncached_getPaginated","meta","uncached_getByIndex","key","value","eq","result","uncached_ensureByIndex","notFound","uncached_getByID","id","uncached_ensureByID","uncached_getFromParams","params","get_id_from_params_default","uncached_getFromSession","user","ensure_session_user_default","ensure_id_default","create","data","update","guardModelID","guard_id_default","ensureModelID","cache","create_repository_default"],"mappings":"mQA2BA,SAASA,CAAAA,CAGPC,CAAAA,CAAgC,CAC9B,eAAeC,CAAAA,CACXC,CAAAA,CAC6B,CAC7B,GAAI,CAACF,CAAAA,CAAW,SAAA,CACZ,MAAM,IAAI,KAAA,CAAM,0DAA0D,CAAA,CAG9E,IAAMG,CAAAA,CAA8B,EAAC,CAE/BC,CAAAA,CAAU,MAAMJ,CAAAA,CAAW,YAAA,GAAeE,CAAAA,EAAQ,OAAA,EAAW,IAAI,CAAA,CACvEC,CAAAA,CAAW,IAAA,CAAK,GAAIC,CAAAA,EAAW,EAAG,CAAA,CAElC,IAAMC,CAAAA,CAAS,MAAML,CAAAA,CAAW,WAAA,GAAcE,CAAAA,EAAQ,MAAA,EAAU,IAAI,CAAA,CACpE,OAAAC,CAAAA,CAAW,IAAA,CAAK,GAAIE,GAAU,EAAG,CAAA,CAE1BL,CAAAA,CAAW,SAAA,CAAUG,CAAAA,CAAY,CACpC,KAAA,CAAOD,C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AA,CAA6D,CAC/E,GAAI,CAAChB,CAAAA,CAAW,MAAA,CACZ,MAAM,IAAI,KAAA,CAAM,+CAA+C,CAAA,CAGnE,OAAO6B,CAAAA,CAAoBb,CAAAA,CAAIhB,CAAAA,CAAW,MAAM,CACpD,CAEA,SAAS8B,CAAAA,CAAcd,CAAAA,CAAsD,CACzE,IAAMN,CAAAA,CAAQkB,CAAAA,CAAaZ,CAAE,CAAA,CAE7B,GAAI,CAACN,CAAAA,CACD,MAAM,IAAI,KAAA,CACN,CAAA,YAAA,EAAeM,CAAE,CAAA,oCAAA,EAAuChB,CAAAA,CAAW,MAAM,CAAA,EAAA,CAC7E,CAAA,CAGJ,OAAOU,CACX,CAEA,OAAO,CACH,MAAA,CAAAe,CAAAA,CACA,UAAA,CAAYM,KAAAA,CAAMd,CAAmB,CAAA,CACrC,aAAA,CAAec,KAAAA,CAAMlB,CAAsB,EAC3C,MAAA,CAAQkB,KAAAA,CAAM9B,CAAe,CAAA,CAC7B,OAAA,CAAS8B,KAAAA,CAAMhB,CAAgB,CAAA,CAC/B,UAAA,CAAYgB,KAAAA,CAAMvB,CAAmB,CAAA,CACrC,aAAA,CAAeuB,KAAAA,CAAMb,CAAsB,CAAA,CAC3C,cAAA,CAAgBa,KAAAA,CAAMV,CAAuB,CAAA,CAC7C,YAAA,CAAcU,KAAAA,CAAMzB,CAAqB,CAAA,CACzC,OAAA,CAASsB,CAAAA,CACT,QAAA,CAAUE,CAAAA,CACV,MAAA,CAAAH,CACJ,CACJ,KAEOK,CAAAA,CAAQjC","file":"index.js","sourcesContent":["import { cache } from 'react';\n\nimport { eq } from 'drizzle-orm';\nimport type { PgColumn } from 'drizzle-orm/pg-core';\nimport { notFound } from 'next/navigation';\n\nimport ensureSessionUser from '../auth/users/ensure-session-user';\n\nimport ensureID from '../utility/ensure-id';\nimport getIDFromParams from '../utility/get-id-from-params';\nimport guardID from '../utility/guard-id';\nimport type { Paginated } from '../utility/interfaces';\n\nimport type {\n InferColumnType,\n InferCreate,\n InferFilters,\n InferGuards,\n InferID,\n InferQueryModel,\n InferUpdate,\n QueryConditions,\n PaginatedSelectFunctionConfig,\n RepositoryConfig,\n SelectFunctionConfig,\n} from './utility/interfaces';\n\nfunction createRepository<\n I extends Record<string, PgColumn<any, any, any>>,\n C extends RepositoryConfig,\n>(repoConfig: C & { indexes: I }) {\n async function uncached_getAll(\n config?: SelectFunctionConfig<InferFilters<C>, InferGuards<C>>\n ): Promise<InferQueryModel<C>[]> {\n if (!repoConfig.makeQuery) {\n throw new Error('Could not `getAll`. `makeQuery` function is not defined.');\n }\n\n const conditions: QueryConditions = [];\n\n const filters = await repoConfig.applyFilters?.(config?.filters ?? null);\n conditions.push(...(filters || []));\n\n const guards = await repoConfig.applyGuards?.(config?.guards ?? null);\n conditions.push(...(guards || []));\n\n return repoConfig.makeQuery(conditions, {\n limit: config?.limit ?? -1,\n offset: config?.offset ?? 0,\n });\n }\n\n async function uncached_getPaginated(\n config?: PaginatedSelectFunctionConfig<InferFilters<C>, InferGuards<C>>\n ): Promise<Paginated<InferQueryModel<C>>> {\n if (!repoConfig.makeMetaQuery) {\n throw new Error('Could not `getPaginated`. `makeMetaQuery` function is not defined.');\n }\n\n if (!repoConfig.makeQuery) {\n throw new Error('Could not `getPaginated`. `makeQuery` function is not defined.');\n }\n\n const conditions: QueryConditions = [];\n\n const filters = await repoConfig.applyFilters?.(config?.filters ?? null);\n conditions.push(...(filters || []));\n\n const guards = await repoConfig.applyGuards?.(config?.guards ?? null);\n conditions.push(...(guards || []));\n\n const meta = await repoConfig.makeMetaQuery(conditions);\n const data = await repoConfig.makeQuery(conditions, {\n limit: config?.limit ?? 10,\n offset: ((config?.page ?? 1) - 1) * (config?.limit ?? 10),\n });\n\n return {\n data,\n meta: { limit: config?.limit ?? 10, page: config?.page ?? 1, total: meta.count },\n };\n }\n\n async function uncached_getByIndex<K extends keyof I>(\n key: K,\n value: InferColumnType<I[K]>,\n config?: SelectFunctionConfig<InferFilters<C>, InferGuards<C>>\n ): Promise<InferQueryModel<C> | null> {\n if (!repoConfig.makeQuery) {\n throw new Error('Could not `getByIndex`. `makeQuery` function is not defined.');\n }\n\n if (!repoConfig.indexes?.[key]) {\n throw new Error(\n `'Could not \\`getByIndex\\`. Index ${String(key)} is not defined in the repository configuration.`\n );\n }\n\n const conditions: QueryConditions = [];\n\n const filters = await repoConfig.applyFilters?.(config?.filters ?? null);\n conditions.push(...(filters || []));\n\n const guards = await repoConfig.applyGuards?.(config?.guards ?? null);\n conditions.push(...(guards || []));\n\n conditions.push(eq(repoConfig.indexes[key], value));\n\n const [result] = await repoConfig.makeQuery(conditions, { limit: 1, offset: 0 });\n\n return result ?? null;\n }\n\n async function uncached_ensureByIndex<K extends keyof I>(\n key: K,\n value: InferColumnType<I[K]>,\n config?: SelectFunctionConfig<InferFilters<C>, InferGuards<C>>\n ): Promise<InferQueryModel<C>> {\n const result = await uncached_getByIndex(key, value, config);\n\n if (!result) {\n return notFound();\n }\n\n return result;\n }\n\n async function uncached_getByID(id: InferID<C>): Promise<InferQueryModel<C> | null> {\n return uncached_getByIndex('id', id);\n }\n\n async function uncached_ensureByID(id: InferID<C>): Promise<InferQueryModel<C>> {\n return uncached_ensureByIndex('id', id);\n }\n\n async function uncached_getFromParams(\n params?: Promise<Record<string, string>> | null\n ): Promise<InferQueryModel<C>> {\n if (!repoConfig.paramKey) {\n throw new Error('Could not `getFromParams`. `paramKey` is not defined.');\n }\n\n if (!repoConfig.prefix) {\n throw new Error('Could not `getFromParams`. `prefix` is not defined.');\n }\n\n const id = await getIDFromParams<InferID<C>>(\n repoConfig.paramKey,\n repoConfig.prefix,\n params\n );\n\n return uncached_ensureByID(id);\n }\n\n async function uncached_getFromSession(): Promise<InferQueryModel<C>> {\n if (!repoConfig.prefix) {\n throw new Error('Could not `getFromSession`. `prefix` is not defined.');\n }\n\n const user = await ensureSessionUser();\n const id = ensureID<InferID<C>>(user.id, repoConfig.prefix);\n const result = await uncached_getByID(id);\n\n if (!result) {\n throw new Error(`Could not \\`getFromSession\\`. No record with ID '${id}' found.`);\n }\n\n return result;\n }\n\n async function create(data: InferCreate<C>['data']): Promise<InferCreate<C>['model']> {\n if (!repoConfig.makeCreateMutation) {\n throw new Error('Could not `create`. `makeCreateMutation` is not defined.');\n }\n\n return repoConfig.makeCreateMutation(data);\n }\n\n async function update(data: InferUpdate<C>['data']): Promise<InferUpdate<C>['model']> {\n if (!repoConfig.makeUpdateMutation) {\n throw new Error('Could not `update`. `makeUpdateMutation` is not defined.');\n }\n\n return repoConfig.makeUpdateMutation(data);\n }\n\n function guardModelID(id: string[] | string | null | undefined): InferID<C> | null {\n if (!repoConfig.prefix) {\n throw new Error('Could not `guardID`. `prefix` is not defined.');\n }\n\n return guardID<InferID<C>>(id, repoConfig.prefix);\n }\n\n function ensureModelID(id: string[] | string | null | undefined): InferID<C> {\n const value = guardModelID(id);\n\n if (!value) {\n throw new Error(\n `Invalid ID '${id}'. Expected a string starting with '${repoConfig.prefix}'.`\n );\n }\n\n return value;\n }\n\n return {\n create,\n ensureByID: cache(uncached_ensureByID),\n ensureByIndex: cache(uncached_ensureByIndex),\n getAll: cache(uncached_getAll),\n getByID: cache(uncached_getByID),\n getByIndex: cache(uncached_getByIndex),\n getFromParams: cache(uncached_getFromParams),\n getFromSession: cache(uncached_getFromSession),\n getPaginated: cache(uncached_getPaginated),\n guardID: guardModelID,\n ensureID: ensureModelID,\n update,\n };\n}\n\nexport default createRepository;\n"]}
1
+ {"version":3,"sources":["../../src/database/create-repository.ts"],"names":["createRepository","repoConfig","uncached_getAll","config","conditions","filters","guards","uncached_getPaginated","meta","uncached_getByIndex","key","value","eq","result","uncached_ensureByIndex","notFound","uncached_getByID","id","uncached_ensureByID","uncached_getIDFromParams","params","get_id_from_params_default","uncached_getFromParams","uncached_getFromSession","user","ensure_session_user_default","ensure_id_default","create","data","update","guardModelID","guard_id_default","ensureModelID","repository","cache","create_repository_default"],"mappings":"mQA4BA,SAASA,CAAAA,CAGPC,EAAwD,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A,GACZgC,CAAAA,CAAmB,UAAA,CAAaC,KAAAA,CAAMhB,CAAmB,CAAA,CACzDe,EAAmB,aAAA,CAAgBC,KAAAA,CAAMpB,CAAsB,CAAA,CAC/DmB,CAAAA,CAAmB,OAAA,CAAUC,KAAAA,CAAMlB,CAAgB,EACnDiB,CAAAA,CAAmB,UAAA,CAAaC,KAAAA,CAAMzB,CAAmB,EAEtD,QAAA,GAAYR,CAAAA,GACXgC,CAAAA,CAAmB,cAAA,CAAiBC,MAAMX,CAAuB,CAAA,CAE9D,UAAA,GAActB,CAAAA,GACbgC,CAAAA,CAAmB,aAAA,CAAgBC,KAAAA,CAAMZ,CAAsB,KAKxE,eAAA,GAAmBrB,CAAAA,GAClBgC,CAAAA,CAAmB,YAAA,CAAeC,KAAAA,CAAM3B,CAAqB,CAAA,CAAA,CAAA,CAIlE,QAAA,GAAYN,IACXgC,CAAAA,CAAmB,QAAA,CAAWD,CAAAA,CAC9BC,CAAAA,CAAmB,OAAA,CAAUH,CAAAA,CAE1B,UAAA,GAAc7B,CAAAA,GACbgC,EAAmB,eAAA,CAAkBd,CAAAA,CAAAA,CAAAA,CAI1C,oBAAA,GAAwBlB,CAAAA,GACvBgC,EAAmB,MAAA,CAASJ,CAAAA,CAAAA,CAG1BI,CACX,KAEOE,CAAAA,CAAQnC","file":"index.js","sourcesContent":["import { cache } from 'react';\n\nimport { eq } from 'drizzle-orm';\nimport type { PgColumn } from 'drizzle-orm/pg-core';\nimport { notFound } from 'next/navigation';\n\nimport ensureSessionUser from '../auth/users/ensure-session-user';\n\nimport ensureID from '../utility/ensure-id';\nimport getIDFromParams from '../utility/get-id-from-params';\nimport guardID from '../utility/guard-id';\nimport type { Paginated } from '../utility/interfaces';\n\nimport type {\n InferColumnType,\n InferCreate,\n InferFilters,\n InferGuards,\n InferID,\n InferQueryModel,\n InferRepository,\n InferUpdate,\n QueryConditions,\n PaginatedSelectFunctionConfig,\n RepositoryConfig,\n SelectFunctionConfig,\n} from './utility/interfaces';\n\nfunction createRepository<\n I extends Record<string, PgColumn<any, any, any>>,\n C extends RepositoryConfig,\n>(repoConfig: C & { indexes?: I }): InferRepository<I, C> {\n async function uncached_getAll(\n config?: SelectFunctionConfig<InferFilters<C>, InferGuards<C>>\n ): Promise<InferQueryModel<C>[]> {\n if (!repoConfig.makeQuery) {\n throw new Error('Could not `getAll`. `makeQuery` function is not defined.');\n }\n\n const conditions: QueryConditions = [];\n\n const filters = await repoConfig.applyFilters?.(config?.filters ?? null);\n conditions.push(...(filters || []));\n\n const guards = await repoConfig.applyGuards?.(config?.guards ?? null);\n conditions.push(...(guards || []));\n\n return repoConfig.makeQuery(conditions, {\n limit: config?.limit ?? -1,\n offset: config?.offset ?? 0,\n });\n }\n\n async function uncached_getPaginated(\n config?: PaginatedSelectFunctionConfig<InferFilters<C>, InferGuards<C>>\n ): Promise<Paginated<InferQueryModel<C>>> {\n if (!repoConfig.makeMetaQuery) {\n throw new Error('Could not `getPaginated`. `makeMetaQuery` function is not defined.');\n }\n\n if (!repoConfig.makeQuery) {\n throw new Error('Could not `getPaginated`. `makeQuery` function is not defined.');\n }\n\n const conditions: QueryConditions = [];\n\n const filters = await repoConfig.applyFilters?.(config?.filters ?? null);\n conditions.push(...(filters || []));\n\n const guards = await repoConfig.applyGuards?.(config?.guards ?? null);\n conditions.push(...(guards || []));\n\n const meta = await repoConfig.makeMetaQuery(conditions);\n const data = await repoConfig.makeQuery(conditions, {\n limit: config?.limit ?? 10,\n offset: ((config?.page ?? 1) - 1) * (config?.limit ?? 10),\n });\n\n return {\n data,\n meta: { limit: config?.limit ?? 10, page: config?.page ?? 1, total: meta.count },\n };\n }\n\n async function uncached_getByIndex<K extends keyof I>(\n key: K,\n value: InferColumnType<I[K]>,\n config?: SelectFunctionConfig<InferFilters<C>, InferGuards<C>>\n ): Promise<InferQueryModel<C> | null> {\n if (!repoConfig.makeQuery) {\n throw new Error('Could not `getByIndex`. `makeQuery` function is not defined.');\n }\n\n if (!repoConfig.indexes?.[key]) {\n throw new Error(\n `'Could not \\`getByIndex\\`. Index ${String(key)} is not defined in the repository configuration.`\n );\n }\n\n const conditions: QueryConditions = [];\n\n const filters = await repoConfig.applyFilters?.(config?.filters ?? null);\n conditions.push(...(filters || []));\n\n const guards = await repoConfig.applyGuards?.(config?.guards ?? null);\n conditions.push(...(guards || []));\n\n conditions.push(eq(repoConfig.indexes[key], value));\n\n const [result] = await repoConfig.makeQuery(conditions, { limit: 1, offset: 0 });\n\n return result ?? null;\n }\n\n async function uncached_ensureByIndex<K extends keyof I>(\n key: K,\n value: InferColumnType<I[K]>,\n config?: SelectFunctionConfig<InferFilters<C>, InferGuards<C>>\n ): Promise<InferQueryModel<C>> {\n const result = await uncached_getByIndex(key, value, config);\n\n if (!result) {\n return notFound();\n }\n\n return result;\n }\n\n async function uncached_getByID(id: InferID<C>): Promise<InferQueryModel<C> | null> {\n return uncached_getByIndex('id', id);\n }\n\n async function uncached_ensureByID(id: InferID<C>): Promise<InferQueryModel<C>> {\n return uncached_ensureByIndex('id', id);\n }\n\n async function uncached_getIDFromParams(\n params?: Promise<Record<string, string>> | Record<string, string> | null\n ) {\n if (!repoConfig.paramKey) {\n throw new Error('Could not `getIDFromParams`. `paramKey` is not defined.');\n }\n\n if (!repoConfig.prefix) {\n throw new Error('Could not `getIDFromParams`. `prefix` is not defined.');\n }\n\n return getIDFromParams<InferID<C>>(repoConfig.paramKey, repoConfig.prefix, params);\n }\n\n async function uncached_getFromParams(\n params?: Promise<Record<string, string>> | Record<string, string> | null\n ): Promise<InferQueryModel<C>> {\n if (!repoConfig.paramKey) {\n throw new Error('Could not `getFromParams`. `paramKey` is not defined.');\n }\n\n if (!repoConfig.prefix) {\n throw new Error('Could not `getFromParams`. `prefix` is not defined.');\n }\n\n return uncached_ensureByID(await uncached_getIDFromParams(params));\n }\n\n async function uncached_getFromSession(): Promise<InferQueryModel<C>> {\n if (!repoConfig.prefix) {\n throw new Error('Could not `getFromSession`. `prefix` is not defined.');\n }\n\n const user = await ensureSessionUser();\n const id = ensureID<InferID<C>>(user.id, repoConfig.prefix);\n const result = await uncached_getByID(id);\n\n if (!result) {\n throw new Error(`Could not \\`getFromSession\\`. No record with ID '${id}' found.`);\n }\n\n return result;\n }\n\n async function create(data: InferCreate<C>['data']): Promise<InferCreate<C>['model']> {\n if (!repoConfig.makeCreateMutation) {\n throw new Error('Could not `create`. `makeCreateMutation` is not defined.');\n }\n\n return repoConfig.makeCreateMutation(data);\n }\n\n async function update(data: InferUpdate<C>['data']): Promise<InferUpdate<C>['model']> {\n if (!repoConfig.makeUpdateMutation) {\n throw new Error('Could not `update`. `makeUpdateMutation` is not defined.');\n }\n\n return repoConfig.makeUpdateMutation(data);\n }\n\n function guardModelID(id: string[] | string | null | undefined): InferID<C> | null {\n if (!repoConfig.prefix) {\n throw new Error('Could not `guardID`. `prefix` is not defined.');\n }\n\n return guardID<InferID<C>>(id, repoConfig.prefix);\n }\n\n function ensureModelID(id: string[] | string | null | undefined): InferID<C> {\n const value = guardModelID(id);\n\n if (!value) {\n throw new Error(\n `Invalid ID '${id}'. Expected a string starting with '${repoConfig.prefix}'.`\n );\n }\n\n return value;\n }\n\n const repository = {} as InferRepository<I, C>;\n\n if ('makeCreateMutation' in repoConfig) {\n (repository as any).create = create;\n }\n\n if ('makeQuery' in repoConfig) {\n (repository as any).getAll = cache(uncached_getAll);\n\n if ('indexes' in repoConfig) {\n (repository as any).ensureByID = cache(uncached_ensureByID);\n (repository as any).ensureByIndex = cache(uncached_ensureByIndex);\n (repository as any).getByID = cache(uncached_getByID);\n (repository as any).getByIndex = cache(uncached_getByIndex);\n\n if ('prefix' in repoConfig) {\n (repository as any).getFromSession = cache(uncached_getFromSession);\n\n if ('paramKey' in repoConfig) {\n (repository as any).getFromParams = cache(uncached_getFromParams);\n }\n }\n }\n\n if ('makeMetaQuery' in repoConfig) {\n (repository as any).getPaginated = cache(uncached_getPaginated);\n }\n }\n\n if ('prefix' in repoConfig) {\n (repository as any).ensureID = ensureModelID;\n (repository as any).guardID = guardModelID;\n\n if ('paramKey' in repoConfig) {\n (repository as any).getIDFromParams = uncached_getIDFromParams;\n }\n }\n\n if ('makeUpdateMutation' in repoConfig) {\n (repository as any).update = update;\n }\n\n return repository;\n}\n\nexport default createRepository;\n"]}
@@ -1,2 +1,2 @@
1
- import'../chunk-6AWLN6E3.js';import'../chunk-CESWDV42.js';import {h}from'../chunk-7LTWI5JO.js';import'../chunk-6LBWOS53.js';import'../chunk-JI2U2EMD.js';import'../chunk-3JA7XHAG.js';import'../chunk-C2XLUXVN.js';import'../chunk-KC7SPWBL.js';import {NextResponse}from'next/server';async function x(e,o){for(let r of o){let t=await r(e);if(t)return NextResponse.redirect(new URL(t.redirect,e.url))}return NextResponse.next()}var u=x;async function a(e,o){let r=await h(e.cookies);for(let t of o){let n=await t(r,e);if(n)return NextResponse.redirect(new URL(n.redirect,e.url))}return NextResponse.next()}var c=a;export{u as handleProxy,c as handleSessionProxy};//# sourceMappingURL=index.js.map
1
+ import'../chunk-6AWLN6E3.js';import'../chunk-CESWDV42.js';import {h}from'../chunk-7LTWI5JO.js';import'../chunk-HTYJSYK5.js';import'../chunk-JI2U2EMD.js';import'../chunk-3JA7XHAG.js';import'../chunk-C2XLUXVN.js';import'../chunk-KC7SPWBL.js';import {NextResponse}from'next/server';async function x(e,o){for(let r of o){let t=await r(e);if(t)return NextResponse.redirect(new URL(t.redirect,e.url))}return NextResponse.next()}var u=x;async function a(e,o){let r=await h(e.cookies);for(let t of o){let n=await t(r,e);if(n)return NextResponse.redirect(new URL(n.redirect,e.url))}return NextResponse.next()}var c=a;export{u as handleProxy,c as handleSessionProxy};//# sourceMappingURL=index.js.map
2
2
  //# sourceMappingURL=index.js.map
@@ -4,7 +4,7 @@ declare function ensureID<T extends string>(id: string[] | string | null | undef
4
4
 
5
5
  declare function generateID<T extends string = string>(prefix?: string | null, length?: number): T;
6
6
 
7
- declare function getIDFromParams<T extends string>(key: string, prefix: string, params?: Promise<Record<string, string>> | null): Promise<T>;
7
+ declare function getIDFromParams<T extends string>(key: string, prefix: string, params?: Promise<Record<string, string>> | Record<string, string> | null): Promise<T>;
8
8
 
9
9
  declare function guardID<T extends string>(id: string[] | string | null | undefined, prefix: string): T | null;
10
10
 
@@ -1,2 +1,2 @@
1
- export{a as generateID}from'../chunk-CESWDV42.js';export{b as ensureID,c as getIDFromParams,a as guardID}from'../chunk-6LBWOS53.js';export{a as log}from'../chunk-KC7SPWBL.js';//# sourceMappingURL=index.js.map
1
+ export{a as generateID}from'../chunk-CESWDV42.js';export{b as ensureID,c as getIDFromParams,a as guardID}from'../chunk-HTYJSYK5.js';export{a as log}from'../chunk-KC7SPWBL.js';//# sourceMappingURL=index.js.map
2
2
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sqrzro/server",
3
- "version": "4.0.0-alpha.23",
3
+ "version": "4.0.0-alpha.25",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,2 +0,0 @@
1
- function o(r,t){let n=Array.isArray(r)?r[0]:r;return n?.startsWith(t)?n:null}var e=o;function u(r,t){let n=e(r,t);if(!n)throw new Error(`Could not ensure ID. '${r}' is not a valid ID with prefix '${t}'.`);return n}var l=u;async function a(r,t,n){if(!n)throw new Error("");let s=await n,i=e(s[r],t);if(!i)throw new Error("");return i}var w=a;export{e as a,l as b,w as c};//# sourceMappingURL=chunk-6LBWOS53.js.map
2
- //# sourceMappingURL=chunk-6LBWOS53.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utility/guard-id.ts","../src/utility/ensure-id.ts","../src/utility/get-id-from-params.ts"],"names":["guardID","id","prefix","value","guard_id_default","ensureID","guardedID","ensure_id_default","getIDFromParams","key","params","awaitedParams","get_id_from_params_default"],"mappings":"AAAA,SAASA,EACLC,CAAAA,CACAC,CAAAA,CACQ,CACR,IAAMC,CAAAA,CAAQ,MAAM,OAAA,CAAQF,CAAE,EAAIA,CAAAA,CAAG,CAAC,EAAIA,CAAAA,CAE1C,OAAKE,GAAO,UAAA,CAAWD,CAAM,EAItBC,CAAAA,CAHI,IAIf,CAEA,IAAOC,CAAAA,CAAQJ,ECXf,SAASK,CAAAA,CAA2BJ,EAA0CC,CAAAA,CAAmB,CAC7F,IAAMI,CAAAA,CAAYF,CAAAA,CAAWH,EAAIC,CAAM,CAAA,CAEvC,GAAI,CAACI,CAAAA,CACD,MAAM,IAAI,KAAA,CAAM,yBAAyBL,CAAE,CAAA,iCAAA,EAAoCC,CAAM,CAAA,EAAA,CAAI,CAAA,CAG7F,OAAOI,CACX,KAEOC,CAAAA,CAAQF,ECVf,eAAeG,CAAAA,CACXC,CAAAA,CACAP,EACAQ,CAAAA,CACU,CACV,GAAI,CAACA,CAAAA,CACD,MAAM,IAAI,KAAA,CAAM,EAAE,CAAA,CAGtB,IAAMC,EAAgB,MAAMD,CAAAA,CACtBT,EAAKG,CAAAA,CAAWO,CAAAA,CAAcF,CAAG,CAAA,CAAGP,CAAM,EAEhD,GAAI,CAACD,EACD,MAAM,IAAI,MAAM,EAAE,CAAA,CAGtB,OAAOA,CACX,KAEOW,CAAAA,CAAQJ","file":"chunk-6LBWOS53.js","sourcesContent":["function guardID<T extends string>(\n id: string[] | string | null | undefined,\n prefix: string\n): T | null {\n const value = Array.isArray(id) ? id[0] : id;\n\n if (!value?.startsWith(prefix)) {\n return null;\n }\n\n return value as T;\n}\n\nexport default guardID;\n","import guardID from './guard-id';\n\nfunction ensureID<T extends string>(id: string[] | string | null | undefined, prefix: string): T {\n const guardedID = guardID<T>(id, prefix);\n\n if (!guardedID) {\n throw new Error(`Could not ensure ID. '${id}' is not a valid ID with prefix '${prefix}'.`);\n }\n\n return guardedID;\n}\n\nexport default ensureID;\n","import guardID from './guard-id';\n\nasync function getIDFromParams<T extends string>(\n key: string,\n prefix: string,\n params?: Promise<Record<string, string>> | null\n): Promise<T> {\n if (!params) {\n throw new Error('');\n }\n\n const awaitedParams = await params;\n const id = guardID<T>(awaitedParams[key], prefix);\n\n if (!id) {\n throw new Error('');\n }\n\n return id;\n}\n\nexport default getIDFromParams;\n"]}