@zenstackhq/runtime 3.0.0-alpha.6 → 3.0.0-alpha.8

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.
@@ -716,6 +716,40 @@ type ClientContract<Schema extends SchemaDef> = {
716
716
  * The client options.
717
717
  */
718
718
  readonly $options: ClientOptions<Schema>;
719
+ /**
720
+ * Executes a prepared raw query and returns the number of affected rows.
721
+ * @example
722
+ * ```
723
+ * const result = await client.$executeRaw`UPDATE User SET cool = ${true} WHERE email = ${'user@email.com'};`
724
+ * ```
725
+ */
726
+ $executeRaw(query: TemplateStringsArray, ...values: any[]): Promise<number>;
727
+ /**
728
+ * Executes a raw query and returns the number of affected rows.
729
+ * This method is susceptible to SQL injections.
730
+ * @example
731
+ * ```
732
+ * const result = await client.$executeRawUnsafe('UPDATE User SET cool = $1 WHERE email = $2 ;', true, 'user@email.com')
733
+ * ```
734
+ */
735
+ $executeRawUnsafe(query: string, ...values: any[]): Promise<number>;
736
+ /**
737
+ * Performs a prepared raw query and returns the `SELECT` data.
738
+ * @example
739
+ * ```
740
+ * const result = await client.$queryRaw`SELECT * FROM User WHERE id = ${1} OR email = ${'user@email.com'};`
741
+ * ```
742
+ */
743
+ $queryRaw<T = unknown>(query: TemplateStringsArray, ...values: any[]): Promise<T>;
744
+ /**
745
+ * Performs a raw query and returns the `SELECT` data.
746
+ * This method is susceptible to SQL injections.
747
+ * @example
748
+ * ```
749
+ * const result = await client.$queryRawUnsafe('SELECT * FROM User WHERE id = $1 OR email = $2;', 1, 'user@email.com')
750
+ * ```
751
+ */
752
+ $queryRawUnsafe<T = unknown>(query: string, ...values: any[]): Promise<T>;
719
753
  /**
720
754
  * The current user identity.
721
755
  */
@@ -716,6 +716,40 @@ type ClientContract<Schema extends SchemaDef> = {
716
716
  * The client options.
717
717
  */
718
718
  readonly $options: ClientOptions<Schema>;
719
+ /**
720
+ * Executes a prepared raw query and returns the number of affected rows.
721
+ * @example
722
+ * ```
723
+ * const result = await client.$executeRaw`UPDATE User SET cool = ${true} WHERE email = ${'user@email.com'};`
724
+ * ```
725
+ */
726
+ $executeRaw(query: TemplateStringsArray, ...values: any[]): Promise<number>;
727
+ /**
728
+ * Executes a raw query and returns the number of affected rows.
729
+ * This method is susceptible to SQL injections.
730
+ * @example
731
+ * ```
732
+ * const result = await client.$executeRawUnsafe('UPDATE User SET cool = $1 WHERE email = $2 ;', true, 'user@email.com')
733
+ * ```
734
+ */
735
+ $executeRawUnsafe(query: string, ...values: any[]): Promise<number>;
736
+ /**
737
+ * Performs a prepared raw query and returns the `SELECT` data.
738
+ * @example
739
+ * ```
740
+ * const result = await client.$queryRaw`SELECT * FROM User WHERE id = ${1} OR email = ${'user@email.com'};`
741
+ * ```
742
+ */
743
+ $queryRaw<T = unknown>(query: TemplateStringsArray, ...values: any[]): Promise<T>;
744
+ /**
745
+ * Performs a raw query and returns the `SELECT` data.
746
+ * This method is susceptible to SQL injections.
747
+ * @example
748
+ * ```
749
+ * const result = await client.$queryRawUnsafe('SELECT * FROM User WHERE id = $1 OR email = $2;', 1, 'user@email.com')
750
+ * ```
751
+ */
752
+ $queryRawUnsafe<T = unknown>(query: string, ...values: any[]): Promise<T>;
719
753
  /**
720
754
  * The current user identity.
721
755
  */
package/dist/index.cjs CHANGED
@@ -2550,8 +2550,8 @@ var BaseOperationHandler = class {
2550
2550
  try {
2551
2551
  result = await query.execute();
2552
2552
  } catch (err) {
2553
- const { sql: sql10, parameters } = query.compile();
2554
- throw new QueryError(`Failed to execute query: ${err}, sql: ${sql10}, parameters: ${parameters}`);
2553
+ const { sql: sql11, parameters } = query.compile();
2554
+ throw new QueryError(`Failed to execute query: ${err}, sql: ${sql11}, parameters: ${parameters}`);
2555
2555
  }
2556
2556
  if (inMemoryDistinct) {
2557
2557
  const distinctResult = [];
@@ -3142,8 +3142,8 @@ var BaseOperationHandler = class {
3142
3142
  return result;
3143
3143
  }
3144
3144
  } catch (err) {
3145
- const { sql: sql10, parameters } = query.compile();
3146
- throw new QueryError(`Error during updateMany: ${err}, sql: ${sql10}, parameters: ${parameters}`);
3145
+ const { sql: sql11, parameters } = query.compile();
3146
+ throw new QueryError(`Error during updateMany: ${err}, sql: ${sql11}, parameters: ${parameters}`);
3147
3147
  }
3148
3148
  }
3149
3149
  buildIdFieldRefs(kysely, model) {
@@ -5189,7 +5189,8 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely13
5189
5189
  ])
5190
5190
  };
5191
5191
  }
5192
- const result = await this.proceedQueryWithKyselyInterceptors(queryNode, queryId);
5192
+ const queryParams = compiledQuery.$raw ? compiledQuery.parameters : void 0;
5193
+ const result = await this.proceedQueryWithKyselyInterceptors(queryNode, queryParams, queryId);
5193
5194
  await this.callAfterQueryInterceptionFilters(result, queryNode, mutationInterceptionInfo);
5194
5195
  if (oldQueryNode !== queryNode) {
5195
5196
  }
@@ -5197,8 +5198,8 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely13
5197
5198
  }, "task");
5198
5199
  return this.executeWithTransaction(task, !!mutationInterceptionInfo?.useTransactionForMutation);
5199
5200
  }
5200
- proceedQueryWithKyselyInterceptors(queryNode, queryId) {
5201
- let proceed = /* @__PURE__ */ __name((q) => this.proceedQuery(q, queryId), "proceed");
5201
+ proceedQueryWithKyselyInterceptors(queryNode, parameters, queryId) {
5202
+ let proceed = /* @__PURE__ */ __name((q) => this.proceedQuery(q, parameters, queryId), "proceed");
5202
5203
  const makeTx = /* @__PURE__ */ __name((p) => (callback) => {
5203
5204
  return this.executeWithTransaction(() => callback(p));
5204
5205
  }, "makeTx");
@@ -5218,9 +5219,15 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely13
5218
5219
  }
5219
5220
  return proceed(queryNode);
5220
5221
  }
5221
- async proceedQuery(query, queryId) {
5222
+ async proceedQuery(query, parameters, queryId) {
5222
5223
  const finalQuery = this.nameMapper.transformNode(query);
5223
- const compiled = this.compileQuery(finalQuery);
5224
+ let compiled = this.compileQuery(finalQuery);
5225
+ if (parameters) {
5226
+ compiled = {
5227
+ ...compiled,
5228
+ parameters
5229
+ };
5230
+ }
5224
5231
  try {
5225
5232
  return this.driver.txConnection ? await super.withConnectionProvider(new import_kysely13.SingleConnectionProvider(this.driver.txConnection)).executeQuery(compiled, queryId) : await super.executeQuery(compiled, queryId);
5226
5233
  } catch (err) {
@@ -5967,6 +5974,39 @@ var ClientImpl = class _ClientImpl {
5967
5974
  get $auth() {
5968
5975
  return this.auth;
5969
5976
  }
5977
+ $executeRaw(query, ...values) {
5978
+ return createDeferredPromise(async () => {
5979
+ const result = await (0, import_kysely16.sql)(query, ...values).execute(this.kysely);
5980
+ return Number(result.numAffectedRows ?? 0);
5981
+ });
5982
+ }
5983
+ $executeRawUnsafe(query, ...values) {
5984
+ return createDeferredPromise(async () => {
5985
+ const compiledQuery = this.createRawCompiledQuery(query, values);
5986
+ const result = await this.kysely.executeQuery(compiledQuery);
5987
+ return Number(result.numAffectedRows ?? 0);
5988
+ });
5989
+ }
5990
+ $queryRaw(query, ...values) {
5991
+ return createDeferredPromise(async () => {
5992
+ const result = await (0, import_kysely16.sql)(query, ...values).execute(this.kysely);
5993
+ return result.rows;
5994
+ });
5995
+ }
5996
+ $queryRawUnsafe(query, ...values) {
5997
+ return createDeferredPromise(async () => {
5998
+ const compiledQuery = this.createRawCompiledQuery(query, values);
5999
+ const result = await this.kysely.executeQuery(compiledQuery);
6000
+ return result.rows;
6001
+ });
6002
+ }
6003
+ createRawCompiledQuery(query, values) {
6004
+ const q = import_kysely16.CompiledQuery.raw(query, values);
6005
+ return {
6006
+ ...q,
6007
+ $raw: true
6008
+ };
6009
+ }
5970
6010
  };
5971
6011
  function createClientProxy(client) {
5972
6012
  const inputValidator = new InputValidator(client.$schema);