@sqb/nestjs 6.0.9 → 6.0.10

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/README.md CHANGED
@@ -123,5 +123,5 @@ SQB is available under [MIT](LICENSE) license.
123
123
  [downloads-url]: https://npmjs.org/package/@sqb/nestjs
124
124
  [ci-test-image]: https://github.com/panates/sqb/actions/workflows/test.yml/badge.svg
125
125
  [ci-test-url]: https://github.com/panates/sqb/actions/workflows/test.yml
126
- [coveralls-image]: https://coveralls.io/repos/github/sqbjs/sqb/badge.svg?branch=master
126
+ [coveralls-image]: https://coveralls.io/repos/github/panates/sqb/badge.svg?branch=dev
127
127
  [coveralls-url]: https://coveralls.io/github/sqbjs/sqb?branch=master
package/constants.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ /** This package's own version, kept in step with its `package.json` by `rman version`
2
+ * (see `.rmanrc` `version.stamp`) - never edit it by hand. */
3
+ export declare const version = "6.0.10";
package/constants.js ADDED
@@ -0,0 +1,3 @@
1
+ /** This package's own version, kept in step with its `package.json` by `rman version`
2
+ * (see `.rmanrc` `version.stamp`) - never edit it by hand. */
3
+ export const version = '6.0.10';
@@ -1,2 +1,14 @@
1
1
  import type { SqbClientConnectionOptions } from './sqb.interface.js';
2
+ /**
3
+ * Fills in any connection option not already set on `moduleOptions` from
4
+ * the corresponding `<prefix><NAME>` environment variable (e.g.
5
+ * `SQB_HOST`, `SQB_POOL_MAX`), returning a new, cloned options object -
6
+ * `moduleOptions` itself is left untouched. Explicitly-given options
7
+ * always take precedence over the environment.
8
+ *
9
+ * @param moduleOptions - the options given to `SqbModule.forRoot()`/
10
+ * `forRootAsync()` (or resolved from its `useFactory`)
11
+ * @param prefix - environment variable prefix, e.g. `'SQB_'` for `SQB_HOST`
12
+ * @returns a new options object with environment-sourced defaults applied
13
+ */
2
14
  export declare function getSqbConfig(moduleOptions: SqbClientConnectionOptions, prefix?: string): SqbClientConnectionOptions;
package/get-sqb-config.js CHANGED
@@ -1,6 +1,18 @@
1
1
  import process from 'node:process';
2
2
  import { clone } from '@jsopen/objects';
3
3
  import { toBoolean, toInt } from 'putil-varhelpers';
4
+ /**
5
+ * Fills in any connection option not already set on `moduleOptions` from
6
+ * the corresponding `<prefix><NAME>` environment variable (e.g.
7
+ * `SQB_HOST`, `SQB_POOL_MAX`), returning a new, cloned options object -
8
+ * `moduleOptions` itself is left untouched. Explicitly-given options
9
+ * always take precedence over the environment.
10
+ *
11
+ * @param moduleOptions - the options given to `SqbModule.forRoot()`/
12
+ * `forRootAsync()` (or resolved from its `useFactory`)
13
+ * @param prefix - environment variable prefix, e.g. `'SQB_'` for `SQB_HOST`
14
+ * @returns a new options object with environment-sourced defaults applied
15
+ */
4
16
  export function getSqbConfig(moduleOptions, prefix = 'SQB_') {
5
17
  const options = clone(moduleOptions);
6
18
  const env = process.env;
package/index.d.ts CHANGED
@@ -1,3 +1,9 @@
1
+ /**
2
+ * @sqb/nestjs - NestJS integration module for `@sqb/connect`, exposing a
3
+ * `SqbClient` (or a custom injection token) as an injectable NestJS
4
+ * provider via {@link SqbModule}.
5
+ */
6
+ export * from './constants.js';
1
7
  export * from './sqb.interface.js';
2
8
  export * from './sqb.module.js';
3
9
  export { SqbClient } from '@sqb/connect';
package/index.js CHANGED
@@ -1,3 +1,9 @@
1
+ /**
2
+ * @sqb/nestjs - NestJS integration module for `@sqb/connect`, exposing a
3
+ * `SqbClient` (or a custom injection token) as an injectable NestJS
4
+ * provider via {@link SqbModule}.
5
+ */
6
+ export * from './constants.js';
1
7
  export * from './sqb.interface.js';
2
8
  export * from './sqb.module.js';
3
9
  export { SqbClient } from '@sqb/connect';
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@sqb/nestjs",
3
3
  "description": "Nestjs module for data connection using SQB",
4
- "version": "6.0.9",
4
+ "version": "6.0.10",
5
5
  "author": "Panates",
6
6
  "license": "BSD-3-Clause",
7
7
  "homepage": "https://www.sqbjs.org",
8
8
  "dependencies": {
9
- "@jsopen/objects": "^2.3.1",
9
+ "@jsopen/objects": "^2.3.3",
10
10
  "ansi-colors": "^4.1.3",
11
11
  "putil-varhelpers": "^1.7.0",
12
12
  "tslib": "^2.8.1"
@@ -14,8 +14,8 @@
14
14
  "peerDependencies": {
15
15
  "@nestjs/common": ">=7.4.0",
16
16
  "@nestjs/core": ">=7.4.0",
17
- "@sqb/builder": "^6.0.9",
18
- "@sqb/connect": "^6.0.9",
17
+ "@sqb/builder": "^6.0.10",
18
+ "@sqb/connect": "^6.0.10",
19
19
  "reflect-metadata": "^0.2.2",
20
20
  "rxjs": ">=6.6.0"
21
21
  },
@@ -34,7 +34,9 @@
34
34
  },
35
35
  "contributors": [
36
36
  "Eray Hanoglu <e.hanoglu@panates.com>",
37
- "Ilker Gurelli <i.gurelli@panates.com>"
37
+ "Ilker Gurelli <i.gurelli@panates.com>",
38
+ "Onur Tokel <o.tokel@panates.com>",
39
+ "Bircan Yuruk <b.yuruk@panates.com>"
38
40
  ],
39
41
  "repository": {
40
42
  "type": "git",
@@ -49,4 +51,4 @@
49
51
  "publishConfig": {
50
52
  "access": "public"
51
53
  }
52
- }
54
+ }
@@ -1,14 +1,44 @@
1
1
  import { type DynamicModule, Logger, type OnApplicationBootstrap, type OnApplicationShutdown } from '@nestjs/common';
2
2
  import { SqbClient } from '@sqb/connect';
3
3
  import type { SqbClientConnectionOptions, SqbModuleAsyncOptions, SqbModuleOptions } from './sqb.interface.js';
4
+ /**
5
+ * The NestJS dynamic module that actually registers `@sqb/nestjs`'s
6
+ * providers - {@link SqbModule} is a thin public wrapper around this one.
7
+ * Registered `@Global()` so the `SqbClient` provider need not be
8
+ * re-imported into every feature module that injects it.
9
+ */
4
10
  export declare class SqbCoreModule implements OnApplicationBootstrap, OnApplicationShutdown {
5
11
  protected readonly client: SqbClient;
6
12
  private readonly connectionOptions;
7
13
  private logger;
14
+ /** Registers `@sqb/nestjs` with connection options given directly (merged with environment-variable defaults via `getSqbConfig`). */
8
15
  static forRoot(moduleOptions?: SqbModuleOptions): DynamicModule;
16
+ /**
17
+ * Registers `@sqb/nestjs` with connection options produced by an
18
+ * injected factory (`asyncOptions.useFactory`), merged with
19
+ * environment-variable defaults via `getSqbConfig`.
20
+ *
21
+ * @throws {Error} if `asyncOptions.useFactory` is not given
22
+ */
9
23
  static forRootAsync(asyncOptions: SqbModuleAsyncOptions): DynamicModule;
24
+ /**
25
+ * Builds the {@link DynamicModule} shared by `forRoot`/`forRootAsync`:
26
+ * registers the `SqbClient` provider (under the caller-chosen `token`,
27
+ * defaulting to the `SqbClient` class) built from whatever provider
28
+ * `metadata` resolves `SQB_CONNECTION_OPTIONS`, plus the fixed
29
+ * {@link CLIENT_TOKEN} alias, a per-registration `SQB_MODULE_ID`, and a
30
+ * `Logger` provider (the caller's, or a new one scoped `'SQB'`).
31
+ */
10
32
  private static _createDynamicModule;
11
33
  constructor(client: SqbClient, connectionOptions: SqbClientConnectionOptions, logger: Logger);
34
+ /**
35
+ * Connects to the database (unless `lazyConnect` is set) as soon as the
36
+ * NestJS application finishes bootstrapping, logging a "waiting to
37
+ * connect" notice if it takes more than a second, and rethrowing (after
38
+ * logging) any connection failure so application startup fails loudly
39
+ * rather than serving traffic against a database that isn't reachable.
40
+ */
12
41
  onApplicationBootstrap(): Promise<void> | undefined;
42
+ /** Closes the `SqbClient` connection as the NestJS application shuts down, waiting up to `connectionOptions.shutdownWaitMs` for it. */
13
43
  onApplicationShutdown(): Promise<void>;
14
44
  }
@@ -7,11 +7,25 @@ import colors from 'ansi-colors';
7
7
  import * as crypto from 'crypto';
8
8
  import { getSqbConfig } from './get-sqb-config.js';
9
9
  import { SQB_CONNECTION_OPTIONS, SQB_MODULE_ID, SQB_MODULE_OPTIONS, } from './sqb.constants.js';
10
+ /**
11
+ * Fixed injection token the `SqbClient` provider is always additionally
12
+ * registered under (via `useExisting`), regardless of the caller-chosen
13
+ * `token` option - lets this module's own lifecycle hooks
14
+ * (`onApplicationBootstrap`/`onApplicationShutdown`) resolve the client
15
+ * without needing to know what token the caller picked.
16
+ */
10
17
  const CLIENT_TOKEN = Symbol('CLIENT_TOKEN');
18
+ /**
19
+ * The NestJS dynamic module that actually registers `@sqb/nestjs`'s
20
+ * providers - {@link SqbModule} is a thin public wrapper around this one.
21
+ * Registered `@Global()` so the `SqbClient` provider need not be
22
+ * re-imported into every feature module that injects it.
23
+ */
11
24
  let SqbCoreModule = SqbCoreModule_1 = class SqbCoreModule {
12
25
  client;
13
26
  connectionOptions;
14
27
  logger;
28
+ /** Registers `@sqb/nestjs` with connection options given directly (merged with environment-variable defaults via `getSqbConfig`). */
15
29
  static forRoot(moduleOptions = {}) {
16
30
  const connectionOptions = getSqbConfig(moduleOptions.useValue || {}, moduleOptions.envPrefix);
17
31
  return this._createDynamicModule(moduleOptions, {
@@ -24,6 +38,13 @@ let SqbCoreModule = SqbCoreModule_1 = class SqbCoreModule {
24
38
  ],
25
39
  });
26
40
  }
41
+ /**
42
+ * Registers `@sqb/nestjs` with connection options produced by an
43
+ * injected factory (`asyncOptions.useFactory`), merged with
44
+ * environment-variable defaults via `getSqbConfig`.
45
+ *
46
+ * @throws {Error} if `asyncOptions.useFactory` is not given
47
+ */
27
48
  static forRootAsync(asyncOptions) {
28
49
  assert.ok(asyncOptions.useFactory, 'useFactory is required');
29
50
  return this._createDynamicModule(asyncOptions, {
@@ -40,6 +61,14 @@ let SqbCoreModule = SqbCoreModule_1 = class SqbCoreModule {
40
61
  ],
41
62
  });
42
63
  }
64
+ /**
65
+ * Builds the {@link DynamicModule} shared by `forRoot`/`forRootAsync`:
66
+ * registers the `SqbClient` provider (under the caller-chosen `token`,
67
+ * defaulting to the `SqbClient` class) built from whatever provider
68
+ * `metadata` resolves `SQB_CONNECTION_OPTIONS`, plus the fixed
69
+ * {@link CLIENT_TOKEN} alias, a per-registration `SQB_MODULE_ID`, and a
70
+ * `Logger` provider (the caller's, or a new one scoped `'SQB'`).
71
+ */
43
72
  static _createDynamicModule(opts, metadata) {
44
73
  const token = opts.token || SqbClient;
45
74
  const providers = [
@@ -78,6 +107,13 @@ let SqbCoreModule = SqbCoreModule_1 = class SqbCoreModule {
78
107
  this.connectionOptions = connectionOptions;
79
108
  this.logger = logger;
80
109
  }
110
+ /**
111
+ * Connects to the database (unless `lazyConnect` is set) as soon as the
112
+ * NestJS application finishes bootstrapping, logging a "waiting to
113
+ * connect" notice if it takes more than a second, and rethrowing (after
114
+ * logging) any connection failure so application startup fails loudly
115
+ * rather than serving traffic against a database that isn't reachable.
116
+ */
81
117
  onApplicationBootstrap() {
82
118
  if (this.connectionOptions.lazyConnect)
83
119
  return;
@@ -97,6 +133,7 @@ let SqbCoreModule = SqbCoreModule_1 = class SqbCoreModule {
97
133
  this.logger?.log(`Database connection established`);
98
134
  });
99
135
  }
136
+ /** Closes the `SqbClient` connection as the NestJS application shuts down, waiting up to `connectionOptions.shutdownWaitMs` for it. */
100
137
  async onApplicationShutdown() {
101
138
  await this.client.close(this.connectionOptions.shutdownWaitMs);
102
139
  }
@@ -1,3 +1,6 @@
1
+ /** Injection token for the raw {@link SqbModuleOptions}/`SqbModuleAsyncOptions` object `SqbModule.forRoot()`/`forRootAsync()` was called with. */
1
2
  export declare const SQB_MODULE_OPTIONS: unique symbol;
3
+ /** Injection token for the resolved {@link SqbClientConnectionOptions} (env-defaults applied by `getSqbConfig`), used to construct the `SqbClient` provider. */
2
4
  export declare const SQB_CONNECTION_OPTIONS: unique symbol;
5
+ /** Injection token for a random id unique to each `SqbCoreModule.forRoot()`/`forRootAsync()` call, letting multiple registrations of the module coexist. */
3
6
  export declare const SQB_MODULE_ID: unique symbol;
package/sqb.constants.js CHANGED
@@ -1,3 +1,6 @@
1
+ /** Injection token for the raw {@link SqbModuleOptions}/`SqbModuleAsyncOptions` object `SqbModule.forRoot()`/`forRootAsync()` was called with. */
1
2
  export const SQB_MODULE_OPTIONS = Symbol('SQB_MODULE_OPTIONS');
3
+ /** Injection token for the resolved {@link SqbClientConnectionOptions} (env-defaults applied by `getSqbConfig`), used to construct the `SqbClient` provider. */
2
4
  export const SQB_CONNECTION_OPTIONS = Symbol('SQB_CONNECTION_OPTIONS');
5
+ /** Injection token for a random id unique to each `SqbCoreModule.forRoot()`/`forRootAsync()` call, letting multiple registrations of the module coexist. */
3
6
  export const SQB_MODULE_ID = Symbol('SQB_MODULE_ID');
@@ -1,6 +1,10 @@
1
1
  import type { InjectionToken, LoggerService } from '@nestjs/common';
2
2
  import type { ModuleMetadata } from '@nestjs/common/interfaces/index.js';
3
3
  import type { ClientConfiguration } from '@sqb/connect';
4
+ /**
5
+ * `@sqb/connect` {@link ClientConfiguration} plus the NestJS-specific
6
+ * options `@sqb/nestjs` reads on top of it.
7
+ */
4
8
  export interface SqbClientConnectionOptions extends ClientConfiguration {
5
9
  /**
6
10
  * Number of ms to wait closing connection on shutdown
@@ -13,17 +17,25 @@ export interface SqbClientConnectionOptions extends ClientConfiguration {
13
17
  */
14
18
  lazyConnect?: boolean;
15
19
  }
20
+ /** Options common to both the synchronous and factory-based module registration forms. */
16
21
  interface BaseModuleOptions {
22
+ /** Injection token the `SqbClient` provider is registered under. Defaults to the `SqbClient` class itself. */
17
23
  token?: InjectionToken;
24
+ /** Prefix used to read connection options from environment variables (e.g. `SQB_HOST`) for any option not given explicitly. Defaults to `'SQB_'`. */
18
25
  envPrefix?: string;
19
26
  logger?: LoggerService | string;
27
+ /** Registers the module as a NestJS global module (available everywhere without re-importing). */
20
28
  global?: boolean;
21
29
  }
30
+ /** Options for {@link SqbModule.forRoot}: connection options are given directly as `useValue`. */
22
31
  export interface SqbModuleOptions extends BaseModuleOptions {
23
32
  useValue?: SqbClientConnectionOptions;
24
33
  }
34
+ /** Options for {@link SqbModule.forRootAsync}: connection options are produced by an injected factory function, e.g. to read them from a NestJS `ConfigService`. */
25
35
  export interface SqbModuleAsyncOptions extends BaseModuleOptions, Partial<Pick<ModuleMetadata, 'imports'>> {
36
+ /** Providers to inject as arguments into `useFactory`. */
26
37
  inject?: any[];
38
+ /** Produces the connection options, optionally asynchronously, from the providers listed in `inject`. */
27
39
  useFactory?: (...args: any[]) => Promise<SqbClientConnectionOptions> | SqbClientConnectionOptions;
28
40
  }
29
41
  export {};
package/sqb.module.d.ts CHANGED
@@ -1,6 +1,20 @@
1
1
  import { type DynamicModule } from '@nestjs/common';
2
2
  import type { SqbModuleAsyncOptions, SqbModuleOptions } from './sqb.interface.js';
3
+ /**
4
+ * NestJS module registering an `@sqb/connect` `SqbClient` (or a custom
5
+ * injection token) as an injectable provider, connecting it on application
6
+ * bootstrap and closing it on shutdown. A thin public wrapper delegating
7
+ * to {@link SqbCoreModule} for the actual provider setup.
8
+ */
3
9
  export declare class SqbModule {
10
+ /**
11
+ * Registers `@sqb/nestjs` with connection options given directly, e.g.
12
+ * `SqbModule.forRoot({ useValue: { dialect: 'postgres', host: '...' } })`.
13
+ */
4
14
  static forRoot(options?: SqbModuleOptions): DynamicModule;
15
+ /**
16
+ * Registers `@sqb/nestjs` with connection options produced by an
17
+ * injected factory, e.g. to read them from a NestJS `ConfigService`.
18
+ */
5
19
  static forRootAsync(options: SqbModuleAsyncOptions): DynamicModule;
6
20
  }
package/sqb.module.js CHANGED
@@ -2,13 +2,27 @@ var SqbModule_1;
2
2
  import { __decorate } from "tslib";
3
3
  import { Module } from '@nestjs/common';
4
4
  import { SqbCoreModule } from './sqb-core.module.js';
5
+ /**
6
+ * NestJS module registering an `@sqb/connect` `SqbClient` (or a custom
7
+ * injection token) as an injectable provider, connecting it on application
8
+ * bootstrap and closing it on shutdown. A thin public wrapper delegating
9
+ * to {@link SqbCoreModule} for the actual provider setup.
10
+ */
5
11
  let SqbModule = SqbModule_1 = class SqbModule {
12
+ /**
13
+ * Registers `@sqb/nestjs` with connection options given directly, e.g.
14
+ * `SqbModule.forRoot({ useValue: { dialect: 'postgres', host: '...' } })`.
15
+ */
6
16
  static forRoot(options) {
7
17
  return {
8
18
  module: SqbModule_1,
9
19
  imports: [SqbCoreModule.forRoot(options)],
10
20
  };
11
21
  }
22
+ /**
23
+ * Registers `@sqb/nestjs` with connection options produced by an
24
+ * injected factory, e.g. to read them from a NestJS `ConfigService`.
25
+ */
12
26
  static forRootAsync(options) {
13
27
  return {
14
28
  module: SqbModule_1,