gencow 0.1.153 → 0.1.155

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/core/index.js CHANGED
@@ -3229,6 +3229,10 @@ async function forEachSetConfig(rls, setOne) {
3229
3229
  }
3230
3230
  }
3231
3231
  var rlsExecClient = new AsyncLocalStorage();
3232
+ function isBunSqlSession(session) {
3233
+ const name = session?.constructor?.name;
3234
+ return typeof name === "string" && name.includes("BunSQLSession");
3235
+ }
3232
3236
  function isDrizzleTransactionDb(db) {
3233
3237
  const d = db;
3234
3238
  if (typeof d?.rollback === "function") {
@@ -3296,40 +3300,80 @@ async function withRlsConnection(session, rls, reuseOuterConnection, fn) {
3296
3300
  }
3297
3301
  return runInner(c);
3298
3302
  }
3299
- function wrapPreparedQuery(pq, session, rls, reuseOuterConnection) {
3303
+ function createClientBoundSession(session, client) {
3304
+ return new Proxy(session, {
3305
+ get(target, prop, receiver) {
3306
+ if (prop === "client") {
3307
+ return client;
3308
+ }
3309
+ const value = Reflect.get(target, prop, receiver);
3310
+ return typeof value === "function" ? value.bind(receiver) : value;
3311
+ }
3312
+ });
3313
+ }
3314
+ async function withPreparedQueryClient(pq, client, fn) {
3315
+ if (!("client" in pq)) {
3316
+ return await fn();
3317
+ }
3318
+ const prevPq = pq.client;
3319
+ pq.client = client;
3320
+ try {
3321
+ return await fn();
3322
+ } finally {
3323
+ pq.client = prevPq;
3324
+ }
3325
+ }
3326
+ function createGenericPreparedQueryClientExecutors(pq) {
3300
3327
  const origExecute = pq.execute.bind(pq);
3301
3328
  const origAll = typeof pq.all === "function" ? pq.all.bind(pq) : null;
3302
- const withPreparedQueryClient = async (client, fn) => {
3303
- if (!("client" in pq)) {
3304
- return fn();
3305
- }
3306
- const prevPq = pq.client;
3307
- pq.client = client;
3308
- try {
3309
- return await fn();
3310
- } finally {
3311
- pq.client = prevPq;
3312
- }
3329
+ return {
3330
+ execute(client, placeholderValues) {
3331
+ return withPreparedQueryClient(pq, client, () => origExecute(placeholderValues));
3332
+ },
3333
+ all: origAll ? (client, placeholderValues) => withPreparedQueryClient(pq, client, () => origAll(placeholderValues)) : void 0
3313
3334
  };
3314
- pq.execute = async (placeholderValues) => {
3315
- const active = rlsExecClient.getStore();
3316
- if (active) {
3317
- return withPreparedQueryClient(active, () => origExecute(placeholderValues));
3318
- }
3319
- return withRlsConnection(session, rls, reuseOuterConnection, async (client) => {
3320
- return withPreparedQueryClient(client, () => origExecute(placeholderValues));
3321
- });
3335
+ }
3336
+ function createBunSqlPreparedQueryClientExecutors(baseSession, prepareQuery, prepareArgs) {
3337
+ const reprepareOnClient = (client) => {
3338
+ const clientBoundSession = createClientBoundSession(baseSession, client);
3339
+ return Reflect.apply(prepareQuery, clientBoundSession, prepareArgs);
3322
3340
  };
3323
- if (origAll) {
3324
- pq.all = async (placeholderValues) => {
3325
- const active = rlsExecClient.getStore();
3326
- if (active) {
3327
- return withPreparedQueryClient(active, () => origAll(placeholderValues));
3341
+ return {
3342
+ execute(client, placeholderValues) {
3343
+ return reprepareOnClient(client).execute(placeholderValues);
3344
+ },
3345
+ all(client, placeholderValues) {
3346
+ const txPreparedQuery = reprepareOnClient(client);
3347
+ if (typeof txPreparedQuery.all === "function") {
3348
+ return txPreparedQuery.all(placeholderValues);
3328
3349
  }
3329
- return withRlsConnection(session, rls, reuseOuterConnection, async (client) => {
3330
- return withPreparedQueryClient(client, () => origAll(placeholderValues));
3331
- });
3332
- };
3350
+ return txPreparedQuery.execute(placeholderValues);
3351
+ }
3352
+ };
3353
+ }
3354
+ function createPreparedQueryClientExecutors(params) {
3355
+ if (isBunSqlSession(params.baseSession)) {
3356
+ return createBunSqlPreparedQueryClientExecutors(params.baseSession, params.prepareQuery, params.prepareArgs);
3357
+ }
3358
+ return createGenericPreparedQueryClientExecutors(params.pq);
3359
+ }
3360
+ async function runPreparedQueryWithRls(session, rls, reuseOuterConnection, runOnClient, placeholderValues) {
3361
+ const active = rlsExecClient.getStore();
3362
+ if (active) {
3363
+ return runOnClient(active, placeholderValues);
3364
+ }
3365
+ return withRlsConnection(session, rls, reuseOuterConnection, (client) => runOnClient(client, placeholderValues));
3366
+ }
3367
+ function wrapPreparedQuery(pq, session, baseSession, prepareQuery, prepareArgs, rls, reuseOuterConnection) {
3368
+ const clientExecutors = createPreparedQueryClientExecutors({
3369
+ pq,
3370
+ baseSession,
3371
+ prepareQuery,
3372
+ prepareArgs
3373
+ });
3374
+ pq.execute = (placeholderValues) => runPreparedQueryWithRls(session, rls, reuseOuterConnection, clientExecutors.execute, placeholderValues);
3375
+ if (clientExecutors.all) {
3376
+ pq.all = (placeholderValues) => runPreparedQueryWithRls(session, rls, reuseOuterConnection, clientExecutors.all, placeholderValues);
3333
3377
  }
3334
3378
  }
3335
3379
  function wrapSession(session, rls, reuseOuterConnection) {
@@ -3342,7 +3386,7 @@ function wrapSession(session, rls, reuseOuterConnection) {
3342
3386
  return (...args) => {
3343
3387
  const prepareQuery = Reflect.get(sTarget, sProp, sRecv);
3344
3388
  const pq = Reflect.apply(prepareQuery, sRecv, args);
3345
- wrapPreparedQuery(pq, sRecv, rls, reuseOuterConnection);
3389
+ wrapPreparedQuery(pq, sRecv, sTarget, prepareQuery, args, rls, reuseOuterConnection);
3346
3390
  return pq;
3347
3391
  };
3348
3392
  }