@tstdl/base 0.93.179 → 0.93.180

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.
@@ -25,6 +25,5 @@ export type OrmModuleOptions = {
25
25
  * @param options - Configuration options including connection details, repository settings, and the encryption secret.
26
26
  */
27
27
  export declare function configureOrm({ injector, ...options }?: OrmModuleOptions & {
28
- encryptionSecret?: Uint8Array;
29
28
  injector?: Injector;
30
29
  }): void;
@@ -1,7 +1,6 @@
1
1
  import { Injector } from '../../injector/injector.js';
2
2
  import { isDefined } from '../../utils/type-guards.js';
3
3
  import { EntityRepositoryConfig } from './repository-config.js';
4
- import { ENCRYPTION_SECRET } from './tokens.js';
5
4
  /**
6
5
  * Configuration class for the database connection.
7
6
  */
@@ -20,7 +19,4 @@ export function configureOrm({ injector, ...options } = {}) {
20
19
  if (isDefined(options.repositoryConfig)) {
21
20
  targetInjector.register(EntityRepositoryConfig, { useValue: options.repositoryConfig });
22
21
  }
23
- if (isDefined(options.encryptionSecret)) {
24
- targetInjector.register(ENCRYPTION_SECRET, { useValue: options.encryptionSecret });
25
- }
26
22
  }
@@ -1,6 +1,7 @@
1
1
  /** biome-ignore-all lint/nursery/noExcessiveClassesPerFile: <explanation> */
2
2
  import { SQL, type SQLWrapper } from 'drizzle-orm';
3
3
  import type { AnyPgTable, PgColumn, PgInsertValue, PgSelectBuilder, PgUpdateSetSource, SelectedFields } from 'drizzle-orm/pg-core';
4
+ import { type DerivedKey } from '../../cryptography/index.js';
4
5
  import { afterResolve, resolveArgumentType, type Resolvable } from '../../injector/interfaces.js';
5
6
  import type { DeepPartial, Function, OneOrMany, Record, SimplifyObject, Type, TypedOmit } from '../../types/index.js';
6
7
  import { Entity, type BaseEntity, type EntityMetadataAttributes, type EntityType } from '../entity.js';
@@ -16,7 +17,7 @@ type EntityRepositoryContext = {
16
17
  table: PgTableFromType;
17
18
  columnDefinitions: ColumnDefinition[];
18
19
  columnDefinitionsMap: Map<string, ColumnDefinition>;
19
- encryptionSecret: Uint8Array<ArrayBuffer> | undefined;
20
+ encryptionKey: DerivedKey | undefined;
20
21
  transformContext: TransformContext | Promise<TransformContext> | undefined;
21
22
  };
22
23
  export type InferSelect<T extends BaseEntity = BaseEntity> = PgTableFromType<EntityType<T>>['$inferSelect'];
@@ -8,7 +8,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  import { and, asc, count, desc, eq, getTableName, inArray, isNotNull as isSqlNotNull, isNull as isSqlNull, isSQLWrapper, lte, or, SQL, sql } from 'drizzle-orm';
9
9
  import { match, P } from 'ts-pattern';
10
10
  import { CancellationSignal } from '../../cancellation/token.js';
11
- import { importSymmetricKey } from '../../cryptography/index.js';
11
+ import { injectDerivedCryptoKey } from '../../cryptography/index.js';
12
12
  import { NotFoundError } from '../../errors/not-found.error.js';
13
13
  import { Singleton } from '../../injector/decorators.js';
14
14
  import { inject, injectArgument } from '../../injector/inject.js';
@@ -30,7 +30,6 @@ import { getInheritanceMetadata, isChildEntity } from '../utils.js';
30
30
  import { getColumnDefinitions, getColumnDefinitionsMap, getDrizzleTableFromType, getPrimaryKeyColumnDefinitions, getPrimaryKeyColumns, getTableColumnDefinitions, isTableOwning } from './drizzle/schema-converter.js';
31
31
  import { convertQuery, getTsQuery, getTsVector, resolveTargetColumn } from './query-converter.js';
32
32
  import { EntityRepositoryConfig } from './repository-config.js';
33
- import { ENCRYPTION_SECRET } from './tokens.js';
34
33
  import { injectTransactional, injectTransactionalAsync, isInTransactionalContext, Transactional, tryGetTransactionalContextData } from './transactional.js';
35
34
  const searchScoreColumn = '__tsl_score';
36
35
  const searchDistanceColumn = '__tsl_distance';
@@ -40,7 +39,7 @@ export const repositoryType = Symbol('repositoryType');
40
39
  const entityTypeToken = Symbol('EntityType');
41
40
  let EntityRepository = class EntityRepository extends Transactional {
42
41
  #context = (isInTransactionalContext() ? tryGetTransactionalContextData(this) : undefined) ?? {};
43
- #encryptionSecret = isInTransactionalContext() ? this.#context.encryptionSecret : inject(ENCRYPTION_SECRET, undefined, { optional: true });
42
+ #encryptionKey = isInTransactionalContext() ? this.#context.encryptionKey : injectDerivedCryptoKey('orm:repository-encryption', { name: 'AES-GCM', length: 256 }, ['encrypt', 'decrypt']);
44
43
  #cancellationSignal = isInTransactionalContext() ? undefined : inject(CancellationSignal);
45
44
  #transformContext = this.#context.transformContext;
46
45
  type = assertDefinedPass(this.#context.type ?? this.constructor[entityTypeToken] ?? injectArgument(this, { optional: true }), 'Missing entity type.');
@@ -137,7 +136,7 @@ let EntityRepository = class EntityRepository extends Transactional {
137
136
  table: this.#table,
138
137
  columnDefinitions: this.#columnDefinitions,
139
138
  columnDefinitionsMap: this.#columnDefinitionsMap,
140
- encryptionSecret: this.#encryptionSecret,
139
+ encryptionKey: this.#encryptionKey,
141
140
  transformContext: this.#transformContext,
142
141
  };
143
142
  return context;
@@ -1745,11 +1744,11 @@ let EntityRepository = class EntityRepository extends Transactional {
1745
1744
  }
1746
1745
  async getTransformContext() {
1747
1746
  if (isUndefined(this.#transformContext)) {
1748
- if (isUndefined(this.#encryptionSecret)) {
1747
+ if (isUndefined(this.#encryptionKey)) {
1749
1748
  this.#transformContext = {};
1750
1749
  return this.#transformContext;
1751
1750
  }
1752
- this.#transformContext = importSymmetricKey('raw', { name: 'AES-GCM', length: 256 }, this.#encryptionSecret, false).then((encryptionKey) => ({ encryptionKey }));
1751
+ this.#transformContext = this.#encryptionKey.getKey().then((encryptionKey) => ({ encryptionKey }));
1753
1752
  this.#transformContext = await this.#transformContext;
1754
1753
  }
1755
1754
  return this.#transformContext; // eslint-disable-line @typescript-eslint/return-await
@@ -17,7 +17,6 @@ import { toArrayAsync } from '../../utils/async-iterable-helpers/to-array.js';
17
17
  import { ChildEntity, Column, Inheritance, Table } from '../decorators.js';
18
18
  import { BaseEntity, Entity } from '../entity.js';
19
19
  import { getRepository } from '../server/repository.js';
20
- import { ENCRYPTION_SECRET } from '../server/tokens.js';
21
20
  describe('ORM Repository Extra Coverage', () => {
22
21
  let injector;
23
22
  let database;
@@ -80,7 +79,6 @@ describe('ORM Repository Extra Coverage', () => {
80
79
  ], PlainItem);
81
80
  beforeAll(async () => {
82
81
  ({ injector, database } = await setupIntegrationTest({ orm: { schema } }));
83
- injector.register(ENCRYPTION_SECRET, { useValue: new Uint8Array(32).fill(1) });
84
82
  baseItemRepo = injector.resolve(getRepository(BaseItem));
85
83
  premiumItemRepo = injector.resolve(getRepository(PremiumItem));
86
84
  simpleItemRepo = injector.resolve(getRepository(SimpleItem));
@@ -17,7 +17,6 @@ import { Column, EmbeddedProperty, EncryptedProperty, Reference, Table } from '.
17
17
  import { Entity } from '../entity.js';
18
18
  import { JsonProperty, NumericDateProperty } from '../schemas/index.js';
19
19
  import { getRepository } from '../server/index.js';
20
- import { ENCRYPTION_SECRET } from '../server/tokens.js';
21
20
  describe('ORM Repository Regression (Integration)', () => {
22
21
  let injector;
23
22
  let db;
@@ -101,11 +100,9 @@ describe('ORM Repository Regression (Integration)', () => {
101
100
  Table('posts', { schema })
102
101
  ], Post);
103
102
  beforeAll(async () => {
104
- const encryptionSecret = new Uint8Array(32).fill(1);
105
103
  ({ injector, database: db } = await setupIntegrationTest({
106
104
  orm: { schema },
107
105
  }));
108
- injector.register(ENCRYPTION_SECRET, { useValue: encryptionSecret });
109
106
  standardRepository = injector.resolve(getRepository(StandardEntity));
110
107
  postRepository = injector.resolve(getRepository(Post));
111
108
  await db.execute(sql `CREATE SCHEMA IF NOT EXISTS ${sql.identifier(schema)}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.93.179",
3
+ "version": "0.93.180",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -130,13 +130,13 @@ export async function setupIsolatedIntegrationTest(options = {}) {
130
130
  ...options.dbConfig,
131
131
  };
132
132
  const schema = options.orm?.schema ?? 'test';
133
+ configureSecrets({ key: 'tstdl-unit-tests' });
133
134
  // 4. Configure ORM
134
135
  // We disable autoMigrate here because APPLICATION_INITIALIZER is not used in integration tests
135
136
  // We manually run migrations via bootstrapOrm below
136
137
  configureOrm({
137
138
  repositoryConfig: { schema },
138
139
  connection: dbConfig,
139
- encryptionSecret: options.orm?.encryptionSecret ?? new Uint8Array(32),
140
140
  injector,
141
141
  });
142
142
  // 5. Database Resolution
@@ -1 +0,0 @@
1
- export declare const ENCRYPTION_SECRET: import("../../injector/token.js").InjectionToken<Uint8Array<ArrayBuffer>, never>;
@@ -1,2 +0,0 @@
1
- import { injectionToken } from '../../injector/token.js';
2
- export const ENCRYPTION_SECRET = injectionToken('EncryptionSecret');