@tstdl/base 0.92.122 → 0.92.123

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.
@@ -77,7 +77,9 @@ export declare class EntityRepository<T extends Entity | EntityWithoutMetadata =
77
77
  hardDeleteManyByQuery(query: Query<T>): Promise<T[]>;
78
78
  getColumn(pathOrColumn: Paths<UntaggedDeep<T>> | ColumnDefinition): PgColumn;
79
79
  convertOrderBy(order: Order<T>): SQL[];
80
- convertQuery(query: Query<T>): SQL;
80
+ convertQuery(query: Query<T>, options?: {
81
+ withDeleted?: boolean;
82
+ }): SQL;
81
83
  mapManyToEntity(columns: InferSelect[]): Promise<T[]>;
82
84
  mapToEntity(columns: InferSelect): Promise<T>;
83
85
  mapManyToColumns(objects: (DeepPartial<T> | NewEntity<T>)[]): Promise<PgInsertValue<PgTableFromType>[]>;
@@ -8,7 +8,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
- import { asc, count, desc, eq, inArray, isSQLWrapper, SQL, sql } from 'drizzle-orm';
11
+ import { and, asc, count, desc, eq, inArray, isNull, isSQLWrapper, SQL, sql } from 'drizzle-orm';
12
12
  import { PgTransaction as DrizzlePgTransaction } from 'drizzle-orm/pg-core';
13
13
  import { createContextProvider } from '../../context/context.js';
14
14
  import { NotFoundError } from '../../errors/not-found.error.js';
@@ -167,9 +167,11 @@ let EntityRepository = class EntityRepository {
167
167
  yield* entities;
168
168
  }
169
169
  async count() {
170
+ const sqlQuery = this.convertQuery({});
170
171
  const dbQuery = this.session
171
172
  .select({ count: count() })
172
- .from(this.#table);
173
+ .from(this.#table)
174
+ .where(sqlQuery);
173
175
  const [result] = await dbQuery;
174
176
  return assertDefinedPass(result).count;
175
177
  }
@@ -194,9 +196,11 @@ let EntityRepository = class EntityRepository {
194
196
  return assertDefinedPass(result).exists;
195
197
  }
196
198
  async hasAll(ids) {
199
+ const sqlQuery = this.convertQuery({});
197
200
  const result = await this.session
198
201
  .select({ contains: sql `array_agg(${this.#table.id}) @> ${ids}`.as('contains') })
199
- .from(this.#table);
202
+ .from(this.#table)
203
+ .where(sqlQuery);
200
204
  return assertDefinedPass(result[0]).contains;
201
205
  }
202
206
  /**
@@ -270,11 +274,12 @@ let EntityRepository = class EntityRepository {
270
274
  return entity;
271
275
  }
272
276
  async tryUpdate(id, update) {
277
+ const sqlQuery = this.convertQuery(eq(this.#table.id, id));
273
278
  const mappedUpdate = await this.mapUpdate(update);
274
279
  const [row] = await this.session
275
280
  .update(this.#table)
276
281
  .set(mappedUpdate)
277
- .where(eq(this.#table.id, id))
282
+ .where(sqlQuery)
278
283
  .returning();
279
284
  if (isUndefined(row)) {
280
285
  return undefined;
@@ -325,13 +330,14 @@ let EntityRepository = class EntityRepository {
325
330
  if (!this.hasMetadata) {
326
331
  return this.tryHardDelete(id);
327
332
  }
333
+ const sqlQuery = this.convertQuery(eq(this.#table.id, id));
328
334
  const [row] = await this.session
329
335
  .update(this.#tableWithMetadata)
330
336
  .set({
331
337
  deleteTimestamp: TRANSACTION_TIMESTAMP,
332
338
  attributes: this.getAttributesUpdate(metadataUpdate?.attributes)
333
339
  })
334
- .where(eq(this.#table.id, id))
340
+ .where(sqlQuery)
335
341
  .returning();
336
342
  if (isUndefined(row)) {
337
343
  return undefined;
@@ -389,9 +395,10 @@ let EntityRepository = class EntityRepository {
389
395
  return result;
390
396
  }
391
397
  async tryHardDelete(id) {
398
+ const sqlQuery = this.convertQuery(eq(this.#table.id, id));
392
399
  const [row] = await this.session
393
400
  .delete(this.#table)
394
- .where(eq(this.#table.id, id))
401
+ .where(sqlQuery)
395
402
  .returning();
396
403
  if (isUndefined(row)) {
397
404
  return undefined;
@@ -453,8 +460,12 @@ let EntityRepository = class EntityRepository {
453
460
  return direction == 'asc' ? asc(column) : desc(column);
454
461
  });
455
462
  }
456
- convertQuery(query) {
457
- return convertQuery(query, this.#table, this.#columnDefinitionsMap);
463
+ convertQuery(query, options) {
464
+ let sql = convertQuery(query, this.#table, this.#columnDefinitionsMap);
465
+ if (!this.hasMetadata || (options?.withDeleted == true)) {
466
+ return sql;
467
+ }
468
+ return and(isNull(this.#tableWithMetadata.deleteTimestamp), sql);
458
469
  }
459
470
  async mapManyToEntity(columns) {
460
471
  const transformContext = await this.getTransformContext();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.92.122",
3
+ "version": "0.92.123",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"