@noormdev/sdk 1.0.0-alpha.23 → 1.0.0-alpha.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -3696,7 +3696,7 @@ export declare class NoormOps {
3696
3696
  * the impersonated principal. Mirrors Context's query surface without
3697
3697
  * lifecycle or noorm ops.
3698
3698
  */
3699
- export interface ImpersonatedScope<DB = unknown, Procs = object, Funcs = object> {
3699
+ export interface ImpersonatedScope<DB = unknown, Procs = object, Funcs = object, Tvfs = object> {
3700
3700
  kysely: Kysely<DB>;
3701
3701
  proc: <T = unknown, N extends keyof Procs & string = keyof Procs & string>(name: N, ...args: Procs[N] extends void ? [
3702
3702
  ] : [
@@ -3708,6 +3708,10 @@ export interface ImpersonatedScope<DB = unknown, Procs = object, Funcs = object>
3708
3708
  params: Funcs[N],
3709
3709
  column: string
3710
3710
  ]) => Promise<T>;
3711
+ tvf: <T = unknown, N extends keyof Tvfs & string = keyof Tvfs & string>(name: N, ...args: Tvfs[N] extends void ? [
3712
+ ] : [
3713
+ params: Tvfs[N]
3714
+ ]) => Promise<T[]>;
3711
3715
  transaction: <T>(fn: (trx: Transaction<DB>) => Promise<T>) => Promise<T>;
3712
3716
  revert: () => Promise<void>;
3713
3717
  }
@@ -3743,7 +3747,7 @@ export declare class ImpersonationError extends Error {
3743
3747
  * await ctx.disconnect()
3744
3748
  * ```
3745
3749
  */
3746
- export declare class Context<DB = unknown, Procs = object, Funcs = object> {
3750
+ export declare class Context<DB = unknown, Procs = object, Funcs = object, Tvfs = object> {
3747
3751
  #private;
3748
3752
  constructor(config: Config, settings: Settings, identity: Identity, options: CreateContextOptions, projectRoot: string);
3749
3753
  get dialect(): Dialect;
@@ -3835,6 +3839,29 @@ export declare class Context<DB = unknown, Procs = object, Funcs = object> {
3835
3839
  params: Funcs[N],
3836
3840
  column: string
3837
3841
  ]): Promise<T>;
3842
+ /**
3843
+ * Call a table-valued function and return the result set.
3844
+ *
3845
+ * Generates SELECT * FROM name(...) with dialect-specific parameter
3846
+ * syntax. Returns multiple rows, unlike func() which returns a scalar.
3847
+ * Only supported on MSSQL and PostgreSQL — MySQL and SQLite throw.
3848
+ *
3849
+ * @example
3850
+ * ```typescript
3851
+ * // Named params
3852
+ * const sessions = await ctx.tvf<Session>('validate_session', { session_key: key });
3853
+ *
3854
+ * // Positional params
3855
+ * const results = await ctx.tvf<Result>('search_products', ['widget', 100]);
3856
+ *
3857
+ * // No params
3858
+ * const items = await ctx.tvf<Item>('get_active_items');
3859
+ * ```
3860
+ */
3861
+ tvf<T = unknown, N extends keyof Tvfs & string = keyof Tvfs & string>(name: N, ...args: Tvfs[N] extends void ? [
3862
+ ] : [
3863
+ params: Tvfs[N]
3864
+ ]): Promise<T[]>;
3838
3865
  /**
3839
3866
  * Execute queries as a specific database principal.
3840
3867
  *
@@ -3855,8 +3882,8 @@ export declare class Context<DB = unknown, Procs = object, Funcs = object> {
3855
3882
  * await scope.revert();
3856
3883
  * ```
3857
3884
  */
3858
- impersonate<T>(username: string, fn: (scope: ImpersonatedScope<DB, Procs, Funcs>) => Promise<T>): Promise<T>;
3859
- impersonate(username: string): Promise<ImpersonatedScope<DB, Procs, Funcs>>;
3885
+ impersonate<T>(username: string, fn: (scope: ImpersonatedScope<DB, Procs, Funcs, Tvfs>) => Promise<T>): Promise<T>;
3886
+ impersonate(username: string): Promise<ImpersonatedScope<DB, Procs, Funcs, Tvfs>>;
3860
3887
  }
3861
3888
  /**
3862
3889
  * Error thrown when requireTest is enabled but config.isTest is false.
@@ -3925,7 +3952,7 @@ export declare class ProtectedConfigError extends Error {
3925
3952
  * const ctx = await createContext()
3926
3953
  * ```
3927
3954
  */
3928
- export declare function createContext<DB = unknown, Procs = object, Funcs = object>(options?: CreateContextOptions): Promise<Context<DB, Procs, Funcs>>;
3955
+ export declare function createContext<DB = unknown, Procs = object, Funcs = object, Tvfs = object>(options?: CreateContextOptions): Promise<Context<DB, Procs, Funcs, Tvfs>>;
3929
3956
 
3930
3957
  export {
3931
3958
  Lock$1 as Lock,
package/dist/index.js CHANGED
@@ -16828,6 +16828,27 @@ function buildFuncCall(dialect, name, column, params) {
16828
16828
  const joined = sql.join(values.map((v) => sql`${v}`));
16829
16829
  return sql`SELECT ${rawName}(${joined}) AS ${rawCol}`;
16830
16830
  }
16831
+ function buildTvfCall(dialect, name, params) {
16832
+ if (dialect === "sqlite") {
16833
+ throw new Error("SQLite does not support table-valued functions.");
16834
+ }
16835
+ if (dialect === "mysql") {
16836
+ throw new Error("MySQL does not support table-valued functions.");
16837
+ }
16838
+ const rawName = sql.raw(name);
16839
+ if (!params || Array.isArray(params) && params.length === 0 || !Array.isArray(params) && Object.keys(params).length === 0) {
16840
+ return sql`SELECT * FROM ${rawName}()`;
16841
+ }
16842
+ if (dialect === "postgres" && !Array.isArray(params)) {
16843
+ const parts = Object.entries(params).map(
16844
+ ([key, val]) => sql`${sql.raw(key)} => ${val}`
16845
+ );
16846
+ return sql`SELECT * FROM ${rawName}(${sql.join(parts)})`;
16847
+ }
16848
+ const values = Array.isArray(params) ? params : Object.values(params);
16849
+ const joined = sql.join(values.map((v) => sql`${v}`));
16850
+ return sql`SELECT * FROM ${rawName}(${joined})`;
16851
+ }
16831
16852
  function buildMssqlProc(rawName, params) {
16832
16853
  if (Array.isArray(params)) {
16833
16854
  const joined = sql.join(params.map((v) => sql`${v}`));
@@ -27218,6 +27239,12 @@ function buildScope(db, revertFn, dialect) {
27218
27239
  const result = await query.execute(db);
27219
27240
  return result.rows?.[0] ?? null;
27220
27241
  },
27242
+ async tvf(name, ...args) {
27243
+ const params = args[0];
27244
+ const query = buildTvfCall(dialect, name, params);
27245
+ const result = await query.execute(db);
27246
+ return result.rows ?? [];
27247
+ },
27221
27248
  async transaction(fn) {
27222
27249
  return db.transaction().execute(fn);
27223
27250
  },
@@ -27385,6 +27412,40 @@ var Context = class {
27385
27412
  const result = await query.execute(this.kysely);
27386
27413
  return result.rows?.[0] ?? null;
27387
27414
  }
27415
+ // ─────────────────────────────────────────────────────────
27416
+ // Table-Valued Functions
27417
+ // ─────────────────────────────────────────────────────────
27418
+ /**
27419
+ * Call a table-valued function and return the result set.
27420
+ *
27421
+ * Generates SELECT * FROM name(...) with dialect-specific parameter
27422
+ * syntax. Returns multiple rows, unlike func() which returns a scalar.
27423
+ * Only supported on MSSQL and PostgreSQL — MySQL and SQLite throw.
27424
+ *
27425
+ * @example
27426
+ * ```typescript
27427
+ * // Named params
27428
+ * const sessions = await ctx.tvf<Session>('validate_session', { session_key: key });
27429
+ *
27430
+ * // Positional params
27431
+ * const results = await ctx.tvf<Result>('search_products', ['widget', 100]);
27432
+ *
27433
+ * // No params
27434
+ * const items = await ctx.tvf<Item>('get_active_items');
27435
+ * ```
27436
+ */
27437
+ async tvf(name, ...args) {
27438
+ if (this.dialect === "sqlite") {
27439
+ throw new Error("SQLite does not support table-valued functions.");
27440
+ }
27441
+ if (this.dialect === "mysql") {
27442
+ throw new Error("MySQL does not support table-valued functions.");
27443
+ }
27444
+ const params = args[0];
27445
+ const query = buildTvfCall(this.dialect, name, params);
27446
+ const result = await query.execute(this.kysely);
27447
+ return result.rows ?? [];
27448
+ }
27388
27449
  async impersonate(username, fn) {
27389
27450
  const strategy = dialectStrategy[this.dialect];
27390
27451
  if (!strategy) {