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

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-HTYJSYK5.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-YQP5GLE4.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 s(n,t){let r=Array.isArray(n)?n[0]:n;return r?.startsWith(t)?r:null}var e=s;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 a=u;async function o(n,t,r){if(!r)return null;let i=r instanceof Promise?await r:r;return e(i[n],t)}var c=o;export{e as a,a as b,c};//# sourceMappingURL=chunk-YQP5GLE4.js.map
2
+ //# sourceMappingURL=chunk-YQP5GLE4.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,EACLC,CAAAA,CACAC,CAAAA,CACQ,CACR,IAAMC,EAAQ,KAAA,CAAM,OAAA,CAAQF,CAAE,CAAA,CAAIA,EAAG,CAAC,CAAA,CAAIA,EAE1C,OAAKE,CAAAA,EAAO,WAAWD,CAAM,CAAA,CAItBC,CAAAA,CAHI,IAIf,CAEA,IAAOC,CAAAA,CAAQJ,ECXf,SAASK,EAA2BJ,CAAAA,CAA0CC,CAAAA,CAAmB,CAC7F,IAAMI,EAAYF,CAAAA,CAAWH,CAAAA,CAAIC,CAAM,CAAA,CAEvC,GAAI,CAACI,CAAAA,CACD,MAAM,IAAI,KAAA,CAAM,yBAAyBL,CAAE,CAAA,iCAAA,EAAoCC,CAAM,CAAA,EAAA,CAAI,EAG7F,OAAOI,CACX,CAEA,IAAOC,EAAQF,ECVf,eAAeG,EACXC,CAAAA,CACAP,CAAAA,CACAQ,EACiB,CACjB,GAAI,CAACA,CAAAA,CACD,OAAO,IAAA,CAGX,IAAMC,EAAgBD,CAAAA,YAAkB,OAAA,CAAU,MAAMA,CAAAA,CAASA,CAAAA,CACjE,OAAON,CAAAA,CAAWO,EAAcF,CAAG,CAAA,CAAGP,CAAM,CAChD,KAEOU,CAAAA,CAAQJ","file":"chunk-YQP5GLE4.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 | null> {\n if (!params) {\n return null;\n }\n\n const awaitedParams = params instanceof Promise ? await params : params;\n return guardID<T>(awaitedParams[key], prefix);\n}\n\nexport default getIDFromParams;\n"]}
@@ -72,11 +72,12 @@ type InferRepository<I extends Record<string, PgColumn<any, any, any>>, C extend
72
72
  } & (C extends {
73
73
  prefix: any;
74
74
  } ? {
75
- getFromSession: () => Promise<InferQueryModel<C>>;
75
+ ensureFromSession: () => Promise<InferQueryModel<C>>;
76
76
  } & (C extends {
77
77
  paramKey: any;
78
78
  } ? {
79
- getFromParams: (params?: Record<string, string> | Promise<Record<string, string>> | null | undefined) => Promise<InferQueryModel<C>>;
79
+ ensureFromParams: (params?: Record<string, string> | Promise<Record<string, string>> | null | undefined) => Promise<InferQueryModel<C>>;
80
+ getFromParams: (params?: Record<string, string> | Promise<Record<string, string>> | null | undefined) => Promise<InferQueryModel<C> | null>;
80
81
  } : {}) : {}) : {}) & (C extends {
81
82
  makeMetaQuery: any;
82
83
  } ? {
@@ -84,12 +85,13 @@ type InferRepository<I extends Record<string, PgColumn<any, any, any>>, C extend
84
85
  } : {}) : {}) & (C extends {
85
86
  prefix: any;
86
87
  } ? {
87
- guardID: (id: string | string[] | null | undefined) => InferID<C> | null;
88
88
  ensureID: (id: string | string[] | null | undefined) => InferID<C>;
89
+ guardID: (id: string | string[] | null | undefined) => InferID<C> | null;
89
90
  } & (C extends {
90
91
  paramKey: any;
91
92
  } ? {
92
- getIDFromParams: (params?: Record<string, string> | Promise<Record<string, string>> | null | undefined) => Promise<InferID<C>>;
93
+ ensureIDFromParams: (params?: Record<string, string> | Promise<Record<string, string>> | null | undefined) => Promise<InferID<C>>;
94
+ getIDFromParams: (params?: Record<string, string> | Promise<Record<string, string>> | null | undefined) => Promise<InferID<C> | null>;
93
95
  } : {}) : {}) & (C extends {
94
96
  makeUpdateMutation: any;
95
97
  } ? {
@@ -1,2 +1,2 @@
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
1
+ import {j}from'../chunk-7LTWI5JO.js';import {a,b,c}from'../chunk-YQP5GLE4.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';async function E(e,u,o){let d=await c(e,u,o);return b(d,u)}var D=E;function S(e){function u(r){if(!e.prefix)throw new Error("Could not `guardID`. `prefix` is not defined.");return a(r,e.prefix)}function o(r){if(!e.prefix)throw new Error("Could not `ensureID`. `prefix` is not defined.");return b(r,e.prefix)}async function d(r){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,r)}async function I(r){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 D(e.paramKey,e.prefix,r)}async function x(r){if(!e.makeQuery)throw new Error("Could not `getAll`. `makeQuery` function is not defined.");let n=[],s=await e.applyFilters?.(r?.filters??null);n.push(...s||[]);let a=await e.applyGuards?.(r?.guards??null);return n.push(...a||[]),e.makeQuery(n,{limit:r?.limit??-1,offset:r?.offset??0})}async function F(r){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 n=[],s=await e.applyFilters?.(r?.filters??null);n.push(...s||[]);let a=await e.applyGuards?.(r?.guards??null);n.push(...a||[]);let y=await e.makeMetaQuery(n);return {data:await e.makeQuery(n,{limit:r?.limit??10,offset:((r?.page??1)-1)*(r?.limit??10)}),meta:{limit:r?.limit??10,page:r?.page??1,total:y.count}}}async function f(r,n,s){if(!e.makeQuery)throw new Error("Could not `getByIndex`. `makeQuery` function is not defined.");if(!e.indexes?.[r])throw new Error(`'Could not \`getByIndex\`. Index ${String(r)} is not defined in the repository configuration.`);let a=[],y=await e.applyFilters?.(s?.filters??null);a.push(...y||[]);let w=await e.applyGuards?.(s?.guards??null);a.push(...w||[]),a.push(eq(e.indexes[r],n));let[K]=await e.makeQuery(a,{limit:1,offset:0});return K??null}async function p(r,n,s){let a=await f(r,n,s);return a||notFound()}async function P(r){return f("id",r)}async function c$1(r){return p("id",r)}async function C(r){let n=await d(r);return n?P(n):null}async function Q(r){let n=await I(r);return c$1(n)}async function k(){let r=await j(),n=o(r.id);return c$1(n)}async function M(r){if(!e.makeCreateMutation)throw new Error("Could not `create`. `makeCreateMutation` is not defined.");return e.makeCreateMutation(r)}async function R(r){if(!e.makeUpdateMutation)throw new Error("Could not `update`. `makeUpdateMutation` is not defined.");return e.makeUpdateMutation(r)}let t={};return "makeCreateMutation"in e&&(t.create=M),"makeQuery"in e&&(t.getAll=cache(x),"indexes"in e&&(t.ensureByID=cache(c$1),t.ensureByIndex=cache(p),t.getByID=cache(P),t.getByIndex=cache(f),"prefix"in e&&(t.ensureFromSession=cache(k),"paramKey"in e&&(t.ensureFromParams=cache(Q),t.getFromParams=cache(C)))),"makeMetaQuery"in e&&(t.getPaginated=cache(F))),"prefix"in e&&(t.ensureID=o,t.guardID=u,"paramKey"in e&&(t.ensureIDFromParams=I,t.getIDFromParams=d)),"makeUpdateMutation"in e&&(t.update=R),t}var G=S;export{G 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_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,CACtD,eAAeC,CAAAA,CACXC,CAAAA,CAC6B,CAC7B,GAAI,CAACF,CAAAA,CAAW,UACZ,MAAM,IAAI,KAAA,CAAM,0DAA0D,CAAA,CAG9E,IAAMG,CAAAA,CAA8B,GAE9BC,CAAAA,CAAU,MAAMJ,CAAAA,CAAW,YAAA,GAAeE,CAAAA,EAAQ,OAAA,EAAW,IAAI,CAAA,CACvEC,EAAW,IAAA,CAAK,GAAIC,CAAAA,EAAW,EAAG,CAAA,CAElC,IAAMC,CAAAA,CAAS,MAAML,CAAAA,CAAW,WAAA,GA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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
+ {"version":3,"sources":["../../src/utility/ensure-id-from-params.ts","../../src/database/create-repository.ts"],"names":["ensureIDFromParams","key","prefix","params","id","get_id_from_params_default","ensure_id_default","ensure_id_from_params_default","createRepository","repoConfig","uncached_guardID","guard_id_default","uncached_ensureID","uncached_getIDFromParams","uncached_ensureIDFromParams","uncached_getAll","config","conditions","filters","guards","uncached_getPaginated","meta","uncached_getByIndex","value","eq","result","uncached_ensureByIndex","notFound","uncached_getByID","uncached_ensureByID","uncached_getFromParams","uncached_ensureFromParams","uncached_ensureFromSession","user","ensure_session_user_default","create","data","update","repository","cache","create_repository_default"],"mappings":"mQAGA,eAAeA,CAAAA,CACXC,CAAAA,CACAC,EACAC,CAAAA,CACU,CACV,IAAMC,CAAAA,CAAK,MAAMC,CAAAA,CAAmBJ,CAAAA,CAAKC,CAAAA,CAAQC,CAAM,CAAA,CACvD,OAAOG,CAAAA,CAAYF,CAAAA,CAAIF,CAAM,CACjC,CAEA,IAAOK,CAAAA,CAAQP,ECiBf,SAASQ,CAAAA,CAGPC,CAAAA,CAAwD,CAKtD,SAASC,CAAAA,CAAiBN,CAAAA,CAA6D,CACnF,GAAI,CAACK,CAAAA,CAAW,MAAA,CACZ,MAAM,IAAI,KAAA,CAAM,+CAA+C,CAAA,CAGnE,OAAOE,EAAoBP,CAAAA,CAAIK,CAAAA,CAAW,MAAM,CACpD,CAEA,SAASG,CAAAA,CAAkBR,CAAAA,CAAsD,CAC7E,GAAI,CAACK,CAAAA,CAAW,MAAA,CACZ,MAAM,IAAI,KAAA,CAAM,gDAAgD,CAAA,CAGpE,OAAOH,EAAqBF,CAAAA,CAAIK,CAAAA,CAAW,MAAM,CACrD,CAEA,eAAeI,CAAAA,CACXV,CAAAA,CACF,CACE,GAAI,CAACM,CAAAA,CAAW,QAAA,CACZ,MAAM,IAAI,KAAA,CAAM,yDAAyD,CAAA,CAG7E,GAAI,CAACA,CAAAA,CAAW,MAAA,CACZ,MAAM,IAAI,KAAA,CAAM,uDAAuD,CAAA,CAG3E,OAAOJ,EAA4BI,CAAAA,CAAW,QAAA,CAAUA,CAAAA,CAAW,MAAA,CAAQN,CAAM,CACrF,CAEA,eAAeW,CAAAA,CACXX,EACF,CACE,GAAI,CAACM,CAAAA,CAAW,QAAA,CACZ,MAAM,IAAI,KAAA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EAAmB,eAAA,CAAkBzB,CAAAA,CAAAA,CAAAA,CAI1C,oBAAA,GAAwBJ,CAAAA,GACvB6B,EAAmB,MAAA,CAASD,CAAAA,CAAAA,CAG1BC,CACX,KAEOE,CAAAA,CAAQhC","file":"index.js","sourcesContent":["import ensureID from './ensure-id';\nimport getIDFromParams from './get-id-from-params';\n\nasync function ensureIDFromParams<T extends string>(\n key: string,\n prefix: string,\n params?: Promise<Record<string, string>> | Record<string, string> | null\n): Promise<T> {\n const id = await getIDFromParams<T>(key, prefix, params);\n return ensureID<T>(id, prefix);\n}\n\nexport default ensureIDFromParams;\n","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';\nimport ensureIDFromParams from '../utility/ensure-id-from-params';\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 // ----------------------------------------------------------------------------------\n // Guard Functions\n // ----------------------------------------------------------------------------------\n\n function uncached_guardID(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 uncached_ensureID(id: string[] | string | null | undefined): InferID<C> {\n if (!repoConfig.prefix) {\n throw new Error('Could not `ensureID`. `prefix` is not defined.');\n }\n\n return ensureID<InferID<C>>(id, repoConfig.prefix);\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_ensureIDFromParams(\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 ensureIDFromParams<InferID<C>>(repoConfig.paramKey, repoConfig.prefix, params);\n }\n\n // ----------------------------------------------------------------------------------\n // List Functions\n // ----------------------------------------------------------------------------------\n\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 // ----------------------------------------------------------------------------------\n // Index Functions\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>> | Record<string, string> | null\n ): Promise<InferQueryModel<C> | null> {\n const id = await uncached_getIDFromParams(params);\n return id ? uncached_getByID(id) : null;\n }\n\n async function uncached_ensureFromParams(\n params?: Promise<Record<string, string>> | Record<string, string> | null\n ): Promise<InferQueryModel<C>> {\n const id = await uncached_ensureIDFromParams(params);\n return uncached_ensureByID(id);\n }\n\n async function uncached_ensureFromSession(): Promise<InferQueryModel<C>> {\n const user = await ensureSessionUser();\n const id = uncached_ensureID(user.id);\n\n return uncached_ensureByID(id);\n }\n\n // ----------------------------------------------------------------------------------\n // Mutation Functions\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 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).ensureFromSession = cache(uncached_ensureFromSession);\n\n if ('paramKey' in repoConfig) {\n (repository as any).ensureFromParams = cache(uncached_ensureFromParams);\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 = uncached_ensureID;\n (repository as any).guardID = uncached_guardID;\n\n if ('paramKey' in repoConfig) {\n (repository as any).ensureIDFromParams = uncached_ensureIDFromParams;\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-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
1
+ import'../chunk-6AWLN6E3.js';import'../chunk-CESWDV42.js';import {h}from'../chunk-7LTWI5JO.js';import'../chunk-YQP5GLE4.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>> | 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 | null>;
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-HTYJSYK5.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-YQP5GLE4.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.25",
3
+ "version": "4.0.0-alpha.26",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,2 +0,0 @@
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
@@ -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,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"]}