@expo/entity-database-adapter-knex 0.63.0 → 0.64.0

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.
@@ -76,7 +76,7 @@ export class StubPostgresDatabaseAdapter<
76
76
  ): Promise<object[]> {
77
77
  const objectCollection = this.getObjectCollectionForTable(tableName);
78
78
  const results = StubPostgresDatabaseAdapter.uniqBy(tableTuples, (tuple) =>
79
- tuple.join(':'),
79
+ JSON.stringify(tuple),
80
80
  ).reduce(
81
81
  (acc, tableTuple) => {
82
82
  return acc.concat(
@@ -202,6 +202,30 @@ export class StubPostgresDatabaseAdapter<
202
202
  throw new Error('SQL fragments not supported for StubDatabaseAdapter');
203
203
  }
204
204
 
205
+ protected async countByFieldEqualityConjunctionInternalAsync(
206
+ queryInterface: any,
207
+ tableName: string,
208
+ tableFieldSingleValueEqualityOperands: TableFieldSingleValueEqualityCondition[],
209
+ tableFieldMultiValueEqualityOperands: TableFieldMultiValueEqualityCondition[],
210
+ ): Promise<number> {
211
+ const results = await this.fetchManyByFieldEqualityConjunctionInternalAsync(
212
+ queryInterface,
213
+ tableName,
214
+ tableFieldSingleValueEqualityOperands,
215
+ tableFieldMultiValueEqualityOperands,
216
+ { orderBy: undefined, offset: undefined, limit: undefined },
217
+ );
218
+ return results.length;
219
+ }
220
+
221
+ protected countBySQLFragmentInternalAsync(
222
+ _queryInterface: any,
223
+ _tableName: string,
224
+ _sqlFragment: SQLFragment<TFields>,
225
+ ): Promise<number> {
226
+ throw new Error('SQL fragment count not supported for StubDatabaseAdapter');
227
+ }
228
+
205
229
  private generateRandomID(): any {
206
230
  const idSchemaField = this.entityConfiguration2.schema.get(this.entityConfiguration2.idField);
207
231
  invariant(
@@ -3,6 +3,7 @@ import {
3
3
  EntityDatabaseAdapterPaginationCursorInvalidError,
4
4
  EntityMetricsLoadType,
5
5
  getDatabaseFieldForEntityField,
6
+ timeAndLogCountEventAsync,
6
7
  timeAndLogLoadEventAsync,
7
8
  } from '@expo/entity';
8
9
  import assert from 'assert';
@@ -190,6 +191,23 @@ export class EntityKnexDataManager<
190
191
  );
191
192
  }
192
193
 
194
+ async countByFieldEqualityConjunctionAsync<N extends keyof TFields>(
195
+ queryContext: EntityQueryContext,
196
+ fieldEqualityOperands: readonly FieldEqualityCondition<TFields, N>[],
197
+ ): Promise<number> {
198
+ return await timeAndLogCountEventAsync(
199
+ this.metricsAdapter,
200
+ EntityMetricsLoadType.COUNT_EQUALITY_CONJUNCTION,
201
+ this.entityClassName,
202
+ queryContext,
203
+ )(
204
+ this.databaseAdapter.countByFieldEqualityConjunctionAsync(
205
+ queryContext,
206
+ fieldEqualityOperands,
207
+ ),
208
+ );
209
+ }
210
+
193
211
  async loadManyBySQLFragmentAsync(
194
212
  queryContext: EntityQueryContext,
195
213
  sqlFragment: SQLFragment<TFields>,
@@ -211,6 +229,18 @@ export class EntityKnexDataManager<
211
229
  );
212
230
  }
213
231
 
232
+ async countBySQLFragmentAsync(
233
+ queryContext: EntityQueryContext,
234
+ sqlFragment: SQLFragment<TFields>,
235
+ ): Promise<number> {
236
+ return await timeAndLogCountEventAsync(
237
+ this.metricsAdapter,
238
+ EntityMetricsLoadType.COUNT_SQL,
239
+ this.entityClassName,
240
+ queryContext,
241
+ )(this.databaseAdapter.countBySQLFragmentAsync(queryContext, sqlFragment));
242
+ }
243
+
214
244
  /**
215
245
  * Load a page of objects using cursor-based pagination with unified pagination specification.
216
246
  *
@@ -450,17 +480,16 @@ export class EntityKnexDataManager<
450
480
  baseWhere: SQLFragment<TFields> | undefined,
451
481
  cursorCondition: SQLFragment<TFields> | null,
452
482
  ): SQLFragment<TFields> {
453
- const conditions = [baseWhere, cursorCondition].filter((it) => !!it);
454
- if (conditions.length === 0) {
455
- return sql`TRUE`;
483
+ if (!baseWhere) {
484
+ return cursorCondition ?? sql`TRUE`;
456
485
  }
457
- if (conditions.length === 1) {
458
- return conditions[0]!;
486
+
487
+ if (!cursorCondition) {
488
+ return baseWhere;
459
489
  }
460
- // Wrap baseWhere in parens if combining with cursor condition
461
- // We know we have exactly 2 conditions at this point
462
- const [first, second] = conditions;
463
- return sql`(${first}) AND ${second}`;
490
+
491
+ // Wrap baseWhere in parens when combining with cursor condition
492
+ return sql`(${baseWhere}) AND ${cursorCondition}`;
464
493
  }
465
494
 
466
495
  private augmentOrderByIfNecessary(