@leoribeiroweb/nest-mongo 0.0.0-stage → 0.0.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.
- package/README.md +88 -2
- package/dist/collation/case-insensitive-collation.d.ts +17 -0
- package/dist/collation/case-insensitive-collation.js +20 -0
- package/dist/connection/mongoose-root-options.d.ts +29 -0
- package/dist/connection/mongoose-root-options.js +32 -0
- package/dist/counter/counter.module.d.ts +2 -0
- package/dist/counter/counter.module.js +25 -0
- package/dist/counter/counter.schema.d.ts +38 -0
- package/dist/counter/counter.schema.js +28 -0
- package/dist/counter/counter.service.d.ts +7 -0
- package/dist/counter/counter.service.js +37 -0
- package/dist/errors/duplicate-key.d.ts +11 -0
- package/dist/errors/duplicate-key.js +19 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +23 -0
- package/dist/uri/build-mongo-uri.d.ts +46 -0
- package/dist/uri/build-mongo-uri.js +49 -0
- package/package.json +52 -4
package/README.md
CHANGED
|
@@ -1,3 +1,89 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @leoribeiroweb/nest-mongo
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Mongo wiring every NestJS service of the platform repeats: the connection
|
|
4
|
+
URI from `MONGODB_*`, the `MongooseModule` root options with a bounded close,
|
|
5
|
+
the shared `counters` sequence module, a case-insensitive collation and the
|
|
6
|
+
duplicate-key check.
|
|
7
|
+
|
|
8
|
+
This package is part of the shared framework behind my own services. It is
|
|
9
|
+
published publicly so builds need no registry credentials, but it is **not an
|
|
10
|
+
open-source project**: there is no support commitment, and the API changes to
|
|
11
|
+
suit the services that consume it. Pin a major version.
|
|
12
|
+
|
|
13
|
+
It does not abstract the driver and has no repository base class.
|
|
14
|
+
|
|
15
|
+
## Requirements
|
|
16
|
+
|
|
17
|
+
Peers, never dependencies (a second copy of either would split the logger or
|
|
18
|
+
the models):
|
|
19
|
+
|
|
20
|
+
| Peer | Range |
|
|
21
|
+
| --- | --- |
|
|
22
|
+
| `@leoribeiroweb/nest-core` | `>=0.8.0 <1.0.0` |
|
|
23
|
+
| `@nestjs/common` | `^11` |
|
|
24
|
+
| `@nestjs/mongoose` | `^11` |
|
|
25
|
+
| `mongoose` | `^9.9.1` |
|
|
26
|
+
|
|
27
|
+
Node `>=20.19.0`, the floor of `mongoose` 9.
|
|
28
|
+
|
|
29
|
+
## Connection
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
MongooseModule.forRootAsync({
|
|
33
|
+
imports: [ConfigModule],
|
|
34
|
+
inject: [ConfigService],
|
|
35
|
+
useFactory: (config: ConfigService) => mongooseRootOptions(config),
|
|
36
|
+
})
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
`mongooseRootOptions(config, { autoIndex? })` returns
|
|
40
|
+
`{ uri, autoIndex, connectionFactory }`:
|
|
41
|
+
|
|
42
|
+
- `uri` is `buildMongoUri(config)`.
|
|
43
|
+
- `autoIndex` defaults to `true`, what every service sets today.
|
|
44
|
+
- `connectionFactory` wraps the connection with the core package's
|
|
45
|
+
`boundMongooseClose`, logging under the `Shutdown` context, so a silent
|
|
46
|
+
mongod cannot hold the shutdown for ~40 s.
|
|
47
|
+
|
|
48
|
+
`buildMongoUri(config)` reads, through `config.get`:
|
|
49
|
+
|
|
50
|
+
| Variable | Default | Use |
|
|
51
|
+
| --- | --- | --- |
|
|
52
|
+
| `MONGODB_URI` | — | When set and non-empty, returned unchanged; nothing else is read |
|
|
53
|
+
| `MONGODB_URL` | `localhost:27017` | Host (and port, or a comma-separated host list) |
|
|
54
|
+
| `MONGODB_DATABASE` | `affiliate-api` | Database. Every service shares this one |
|
|
55
|
+
| `MONGODB_USER`, `MONGODB_PASS` | — | Only when **both** are set: URI-encoded credentials and `?authSource=` |
|
|
56
|
+
| `MONGODB_AUTH_DB` | `admin` | The `authSource`, used only with credentials |
|
|
57
|
+
|
|
58
|
+
Defaults are applied by the reader's two-argument `get`, so a `ConfigService`
|
|
59
|
+
keeps its own rule: only `undefined` takes the default, an empty string is
|
|
60
|
+
kept. `config` is any object with `ConfigService`'s `get` shape; the package
|
|
61
|
+
does not depend on `@nestjs/config`.
|
|
62
|
+
|
|
63
|
+
## Counters
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
@Module({ imports: [CounterModule] })
|
|
67
|
+
export class InvoiceModule {}
|
|
68
|
+
|
|
69
|
+
const number = await counterService.nextSequence('nfse');
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`nextSequence(name)` is one `findOneAndUpdate({ _id: name }, { $inc: { seq: 1 } },
|
|
73
|
+
{ returnDocument: 'after', upsert: true })`: atomic, starting at 1, each number
|
|
74
|
+
handed out once under concurrency.
|
|
75
|
+
|
|
76
|
+
**The collection is shared in production** by `affiliate-api`, `social-api`
|
|
77
|
+
and `nfse-api`, and invoice and post numbers come from it. The collection name
|
|
78
|
+
(`counters`), the document shape (`{ _id: <name>, seq: <number>, __v: 0 }`)
|
|
79
|
+
and the update are pinned by `counter.contract.spec.ts` and
|
|
80
|
+
`counter.mongo.spec.ts`. Changing any of them is a Mongo migration in three
|
|
81
|
+
services and a major release.
|
|
82
|
+
|
|
83
|
+
## Helpers
|
|
84
|
+
|
|
85
|
+
- `CASE_INSENSITIVE_COLLATION` — `{ locale: 'en', strength: 2 }`. Use the same
|
|
86
|
+
object for the index and for every query that should use it.
|
|
87
|
+
- `isDuplicateKeyError(error)` — `true` for any object whose `code` is `11000`
|
|
88
|
+
(`DUPLICATE_KEY_ERROR_CODE`). Structural, so it works across two copies of the
|
|
89
|
+
driver.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Case-insensitive collation (`strength: 2` ignores case and accents-as-case).
|
|
3
|
+
*
|
|
4
|
+
* Use the SAME object for both the index definition and every query that should
|
|
5
|
+
* hit that index — MongoDB only uses a collated index when the query's
|
|
6
|
+
* collation matches the index's collation. Pairing this with an equality match
|
|
7
|
+
* lets a case-insensitive lookup use a B-tree index instead of a `$regex`/`i`
|
|
8
|
+
* COLLSCAN (see leoribeiro2/affiliate-api#338, utmContent.0 on the conversions
|
|
9
|
+
* collection).
|
|
10
|
+
*
|
|
11
|
+
* Moved from `src/common/mongo/case-insensitive-collation.ts`, identical in
|
|
12
|
+
* `affiliate-api` and `social-api`.
|
|
13
|
+
*/
|
|
14
|
+
export declare const CASE_INSENSITIVE_COLLATION: {
|
|
15
|
+
readonly locale: "en";
|
|
16
|
+
readonly strength: 2;
|
|
17
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CASE_INSENSITIVE_COLLATION = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Case-insensitive collation (`strength: 2` ignores case and accents-as-case).
|
|
6
|
+
*
|
|
7
|
+
* Use the SAME object for both the index definition and every query that should
|
|
8
|
+
* hit that index — MongoDB only uses a collated index when the query's
|
|
9
|
+
* collation matches the index's collation. Pairing this with an equality match
|
|
10
|
+
* lets a case-insensitive lookup use a B-tree index instead of a `$regex`/`i`
|
|
11
|
+
* COLLSCAN (see leoribeiro2/affiliate-api#338, utmContent.0 on the conversions
|
|
12
|
+
* collection).
|
|
13
|
+
*
|
|
14
|
+
* Moved from `src/common/mongo/case-insensitive-collation.ts`, identical in
|
|
15
|
+
* `affiliate-api` and `social-api`.
|
|
16
|
+
*/
|
|
17
|
+
exports.CASE_INSENSITIVE_COLLATION = {
|
|
18
|
+
locale: 'en',
|
|
19
|
+
strength: 2,
|
|
20
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { MongooseModuleOptions } from '@nestjs/mongoose';
|
|
2
|
+
import { type MongoUriConfig } from '../uri/build-mongo-uri';
|
|
3
|
+
/** The logger context of the line `boundMongooseClose` writes when a close is abandoned. */
|
|
4
|
+
export declare const MONGOOSE_CLOSE_LOGGER_CONTEXT = "Shutdown";
|
|
5
|
+
export interface MongooseRootOptionsOverrides {
|
|
6
|
+
/**
|
|
7
|
+
* Build the schemas' indexes at connection time. Defaults to `true`, what
|
|
8
|
+
* all four services set today.
|
|
9
|
+
*/
|
|
10
|
+
readonly autoIndex?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The options `MongooseModule.forRootAsync`'s `useFactory` returns in every
|
|
14
|
+
* service: the URI from `buildMongoUri`, `autoIndex`, and a `connectionFactory`
|
|
15
|
+
* that bounds `connection.close()` with the core package's
|
|
16
|
+
* `boundMongooseClose` (a silent mongod otherwise holds the shutdown ~40 s,
|
|
17
|
+
* `leoribeiro2/affiliate-api#545`).
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* MongooseModule.forRootAsync({
|
|
21
|
+
* imports: [ConfigModule],
|
|
22
|
+
* inject: [ConfigService],
|
|
23
|
+
* useFactory: (config: ConfigService) => mongooseRootOptions(config),
|
|
24
|
+
* })
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* A new object is returned on every call; nothing is cached or shared.
|
|
28
|
+
*/
|
|
29
|
+
export declare function mongooseRootOptions(config: MongoUriConfig, overrides?: MongooseRootOptionsOverrides): MongooseModuleOptions;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MONGOOSE_CLOSE_LOGGER_CONTEXT = void 0;
|
|
4
|
+
exports.mongooseRootOptions = mongooseRootOptions;
|
|
5
|
+
const nest_core_1 = require("@leoribeiroweb/nest-core");
|
|
6
|
+
const build_mongo_uri_1 = require("../uri/build-mongo-uri");
|
|
7
|
+
/** The logger context of the line `boundMongooseClose` writes when a close is abandoned. */
|
|
8
|
+
exports.MONGOOSE_CLOSE_LOGGER_CONTEXT = 'Shutdown';
|
|
9
|
+
/**
|
|
10
|
+
* The options `MongooseModule.forRootAsync`'s `useFactory` returns in every
|
|
11
|
+
* service: the URI from `buildMongoUri`, `autoIndex`, and a `connectionFactory`
|
|
12
|
+
* that bounds `connection.close()` with the core package's
|
|
13
|
+
* `boundMongooseClose` (a silent mongod otherwise holds the shutdown ~40 s,
|
|
14
|
+
* `leoribeiro2/affiliate-api#545`).
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* MongooseModule.forRootAsync({
|
|
18
|
+
* imports: [ConfigModule],
|
|
19
|
+
* inject: [ConfigService],
|
|
20
|
+
* useFactory: (config: ConfigService) => mongooseRootOptions(config),
|
|
21
|
+
* })
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* A new object is returned on every call; nothing is cached or shared.
|
|
25
|
+
*/
|
|
26
|
+
function mongooseRootOptions(config, overrides = {}) {
|
|
27
|
+
return {
|
|
28
|
+
uri: (0, build_mongo_uri_1.buildMongoUri)(config),
|
|
29
|
+
autoIndex: overrides.autoIndex ?? true,
|
|
30
|
+
connectionFactory: (connection) => (0, nest_core_1.boundMongooseClose)(connection, (0, nest_core_1.createLogger)(exports.MONGOOSE_CLOSE_LOGGER_CONTEXT)),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.CounterModule = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
const mongoose_1 = require("@nestjs/mongoose");
|
|
12
|
+
const counter_schema_1 = require("./counter.schema");
|
|
13
|
+
const counter_service_1 = require("./counter.service");
|
|
14
|
+
let CounterModule = class CounterModule {
|
|
15
|
+
};
|
|
16
|
+
exports.CounterModule = CounterModule;
|
|
17
|
+
exports.CounterModule = CounterModule = __decorate([
|
|
18
|
+
(0, common_1.Module)({
|
|
19
|
+
imports: [
|
|
20
|
+
mongoose_1.MongooseModule.forFeature([{ name: counter_schema_1.Counter.name, schema: counter_schema_1.CounterSchema }]),
|
|
21
|
+
],
|
|
22
|
+
providers: [counter_service_1.CounterService],
|
|
23
|
+
exports: [counter_service_1.CounterService],
|
|
24
|
+
})
|
|
25
|
+
], CounterModule);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { HydratedDocument } from 'mongoose';
|
|
2
|
+
export declare class Counter {
|
|
3
|
+
_id: string;
|
|
4
|
+
seq: number;
|
|
5
|
+
}
|
|
6
|
+
export type CounterDocument = HydratedDocument<Counter>;
|
|
7
|
+
export declare const CounterSchema: import("mongoose").Schema<Counter, import("mongoose").Model<Counter, any, any, any, any, any, Counter & Required<{
|
|
8
|
+
_id: string;
|
|
9
|
+
}> & {
|
|
10
|
+
__v: number;
|
|
11
|
+
}>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, Counter, import("mongoose").Document<unknown, {}, Counter, {
|
|
12
|
+
id: string;
|
|
13
|
+
}, import("mongoose").DefaultSchemaOptions> & Omit<Counter & Required<{
|
|
14
|
+
_id: string;
|
|
15
|
+
}> & {
|
|
16
|
+
__v: number;
|
|
17
|
+
}, "id"> & import("mongoose").HydratedDocumentOverrides<{
|
|
18
|
+
id: string;
|
|
19
|
+
}>, {
|
|
20
|
+
_id?: import("mongoose").SchemaDefinitionProperty<string, Counter, import("mongoose").Document<unknown, {}, Counter, {
|
|
21
|
+
id: string;
|
|
22
|
+
}, import("mongoose").DefaultSchemaOptions> & Omit<Counter & Required<{
|
|
23
|
+
_id: string;
|
|
24
|
+
}> & {
|
|
25
|
+
__v: number;
|
|
26
|
+
}, "id"> & import("mongoose").HydratedDocumentOverrides<{
|
|
27
|
+
id: string;
|
|
28
|
+
}>> | undefined;
|
|
29
|
+
seq?: import("mongoose").SchemaDefinitionProperty<number, Counter, import("mongoose").Document<unknown, {}, Counter, {
|
|
30
|
+
id: string;
|
|
31
|
+
}, import("mongoose").DefaultSchemaOptions> & Omit<Counter & Required<{
|
|
32
|
+
_id: string;
|
|
33
|
+
}> & {
|
|
34
|
+
__v: number;
|
|
35
|
+
}, "id"> & import("mongoose").HydratedDocumentOverrides<{
|
|
36
|
+
id: string;
|
|
37
|
+
}>> | undefined;
|
|
38
|
+
}, Counter>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.CounterSchema = exports.Counter = void 0;
|
|
13
|
+
const mongoose_1 = require("@nestjs/mongoose");
|
|
14
|
+
let Counter = class Counter {
|
|
15
|
+
};
|
|
16
|
+
exports.Counter = Counter;
|
|
17
|
+
__decorate([
|
|
18
|
+
(0, mongoose_1.Prop)({ required: true }),
|
|
19
|
+
__metadata("design:type", String)
|
|
20
|
+
], Counter.prototype, "_id", void 0);
|
|
21
|
+
__decorate([
|
|
22
|
+
(0, mongoose_1.Prop)({ required: true, default: 0 }),
|
|
23
|
+
__metadata("design:type", Number)
|
|
24
|
+
], Counter.prototype, "seq", void 0);
|
|
25
|
+
exports.Counter = Counter = __decorate([
|
|
26
|
+
(0, mongoose_1.Schema)({ collection: 'counters', _id: false })
|
|
27
|
+
], Counter);
|
|
28
|
+
exports.CounterSchema = mongoose_1.SchemaFactory.createForClass(Counter);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.CounterService = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const mongoose_1 = require("@nestjs/mongoose");
|
|
18
|
+
const mongoose_2 = require("mongoose");
|
|
19
|
+
const counter_schema_1 = require("./counter.schema");
|
|
20
|
+
let CounterService = class CounterService {
|
|
21
|
+
constructor(counterModel) {
|
|
22
|
+
this.counterModel = counterModel;
|
|
23
|
+
}
|
|
24
|
+
async nextSequence(name) {
|
|
25
|
+
const result = await this.counterModel.findOneAndUpdate({ _id: name }, { $inc: { seq: 1 } }, { returnDocument: 'after', upsert: true });
|
|
26
|
+
if (!result) {
|
|
27
|
+
throw new Error(`Counter "${name}" returned null after upsert — check write concern`);
|
|
28
|
+
}
|
|
29
|
+
return result.seq;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
exports.CounterService = CounterService;
|
|
33
|
+
exports.CounterService = CounterService = __decorate([
|
|
34
|
+
(0, common_1.Injectable)(),
|
|
35
|
+
__param(0, (0, mongoose_1.InjectModel)(counter_schema_1.Counter.name)),
|
|
36
|
+
__metadata("design:paramtypes", [mongoose_2.Model])
|
|
37
|
+
], CounterService);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** The server error code of a unique-index violation (`E11000 duplicate key error`). */
|
|
2
|
+
export declare const DUPLICATE_KEY_ERROR_CODE = 11000;
|
|
3
|
+
/**
|
|
4
|
+
* Whether `error` is a MongoDB duplicate-key error: any object whose `code` is
|
|
5
|
+
* `11000`, which covers the driver's `MongoServerError` and the
|
|
6
|
+
* `MongoBulkWriteError` of an unordered insert.
|
|
7
|
+
*
|
|
8
|
+
* Structural on purpose, like the copies in `social-api` it replaces: an
|
|
9
|
+
* `instanceof` check would fail across two copies of the `mongodb` driver.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isDuplicateKeyError(error: unknown): boolean;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DUPLICATE_KEY_ERROR_CODE = void 0;
|
|
4
|
+
exports.isDuplicateKeyError = isDuplicateKeyError;
|
|
5
|
+
/** The server error code of a unique-index violation (`E11000 duplicate key error`). */
|
|
6
|
+
exports.DUPLICATE_KEY_ERROR_CODE = 11000;
|
|
7
|
+
/**
|
|
8
|
+
* Whether `error` is a MongoDB duplicate-key error: any object whose `code` is
|
|
9
|
+
* `11000`, which covers the driver's `MongoServerError` and the
|
|
10
|
+
* `MongoBulkWriteError` of an unordered insert.
|
|
11
|
+
*
|
|
12
|
+
* Structural on purpose, like the copies in `social-api` it replaces: an
|
|
13
|
+
* `instanceof` check would fail across two copies of the `mongodb` driver.
|
|
14
|
+
*/
|
|
15
|
+
function isDuplicateKeyError(error) {
|
|
16
|
+
return (typeof error === 'object' &&
|
|
17
|
+
error !== null &&
|
|
18
|
+
error.code === exports.DUPLICATE_KEY_ERROR_CODE);
|
|
19
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from './uri/build-mongo-uri';
|
|
2
|
+
export * from './connection/mongoose-root-options';
|
|
3
|
+
export * from './counter/counter.schema';
|
|
4
|
+
export * from './counter/counter.service';
|
|
5
|
+
export * from './counter/counter.module';
|
|
6
|
+
export * from './collation/case-insensitive-collation';
|
|
7
|
+
export * from './errors/duplicate-key';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./uri/build-mongo-uri"), exports);
|
|
18
|
+
__exportStar(require("./connection/mongoose-root-options"), exports);
|
|
19
|
+
__exportStar(require("./counter/counter.schema"), exports);
|
|
20
|
+
__exportStar(require("./counter/counter.service"), exports);
|
|
21
|
+
__exportStar(require("./counter/counter.module"), exports);
|
|
22
|
+
__exportStar(require("./collation/case-insensitive-collation"), exports);
|
|
23
|
+
__exportStar(require("./errors/duplicate-key"), exports);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The part of `@nestjs/config`'s `ConfigService` that `buildMongoUri` reads.
|
|
3
|
+
*
|
|
4
|
+
* Declared structurally so this package does not peer-depend on
|
|
5
|
+
* `@nestjs/config`: a `ConfigService`, a `Pick<ConfigService, 'get'>` (what
|
|
6
|
+
* `affiliate-api`'s offline scripts pass) and any test double with the same
|
|
7
|
+
* two call shapes are all accepted.
|
|
8
|
+
*
|
|
9
|
+
* The two-argument form is load-bearing: a default is applied by the reader,
|
|
10
|
+
* not here, so `ConfigService`'s own rule decides when it applies (only for an
|
|
11
|
+
* `undefined` value: an empty string is returned as is). That is exactly what
|
|
12
|
+
* the four copies this function replaces did.
|
|
13
|
+
*/
|
|
14
|
+
export interface MongoUriConfig {
|
|
15
|
+
get<T = string>(propertyPath: string): T | undefined;
|
|
16
|
+
get<T = string>(propertyPath: string, defaultValue: T): T;
|
|
17
|
+
}
|
|
18
|
+
/** `MONGODB_URL` when it is unset: a local mongod on the default port. */
|
|
19
|
+
export declare const DEFAULT_MONGODB_HOST = "localhost:27017";
|
|
20
|
+
/** `MONGODB_AUTH_DB` when it is unset, used only with credentials. */
|
|
21
|
+
export declare const DEFAULT_MONGODB_AUTH_DB = "admin";
|
|
22
|
+
/**
|
|
23
|
+
* `MONGODB_DATABASE` when it is unset.
|
|
24
|
+
*
|
|
25
|
+
* Not a leftover: every service that reads this database (`affiliate-api`,
|
|
26
|
+
* `social-api`, `nfse-api`, `browser-worker`) shares the one named after
|
|
27
|
+
* `affiliate-api`, and changing the default would silently point a service at
|
|
28
|
+
* an empty database.
|
|
29
|
+
*/
|
|
30
|
+
export declare const DEFAULT_MONGODB_DATABASE = "affiliate-api";
|
|
31
|
+
/**
|
|
32
|
+
* Build the MongoDB connection URI from the environment.
|
|
33
|
+
*
|
|
34
|
+
* - `MONGODB_URI`, when set (non-empty), is returned unchanged and everything
|
|
35
|
+
* else is ignored.
|
|
36
|
+
* - Otherwise the URI is assembled from `MONGODB_URL` (host and port),
|
|
37
|
+
* `MONGODB_DATABASE`, and, only when **both** `MONGODB_USER` and
|
|
38
|
+
* `MONGODB_PASS` are set, URI-encoded credentials plus
|
|
39
|
+
* `?authSource=<MONGODB_AUTH_DB>`.
|
|
40
|
+
*
|
|
41
|
+
* One implementation of the function that `affiliate-api`
|
|
42
|
+
* (`src/config/mongo-uri.ts`), `social-api`, `nfse-api` and `browser-worker`
|
|
43
|
+
* (`src/app.module.ts`) each carried. Their bodies were identical; the output
|
|
44
|
+
* of this one is pinned against all of them by `build-mongo-uri.spec.ts`.
|
|
45
|
+
*/
|
|
46
|
+
export declare function buildMongoUri(config: MongoUriConfig): string;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_MONGODB_DATABASE = exports.DEFAULT_MONGODB_AUTH_DB = exports.DEFAULT_MONGODB_HOST = void 0;
|
|
4
|
+
exports.buildMongoUri = buildMongoUri;
|
|
5
|
+
/** `MONGODB_URL` when it is unset: a local mongod on the default port. */
|
|
6
|
+
exports.DEFAULT_MONGODB_HOST = 'localhost:27017';
|
|
7
|
+
/** `MONGODB_AUTH_DB` when it is unset, used only with credentials. */
|
|
8
|
+
exports.DEFAULT_MONGODB_AUTH_DB = 'admin';
|
|
9
|
+
/**
|
|
10
|
+
* `MONGODB_DATABASE` when it is unset.
|
|
11
|
+
*
|
|
12
|
+
* Not a leftover: every service that reads this database (`affiliate-api`,
|
|
13
|
+
* `social-api`, `nfse-api`, `browser-worker`) shares the one named after
|
|
14
|
+
* `affiliate-api`, and changing the default would silently point a service at
|
|
15
|
+
* an empty database.
|
|
16
|
+
*/
|
|
17
|
+
exports.DEFAULT_MONGODB_DATABASE = 'affiliate-api';
|
|
18
|
+
/**
|
|
19
|
+
* Build the MongoDB connection URI from the environment.
|
|
20
|
+
*
|
|
21
|
+
* - `MONGODB_URI`, when set (non-empty), is returned unchanged and everything
|
|
22
|
+
* else is ignored.
|
|
23
|
+
* - Otherwise the URI is assembled from `MONGODB_URL` (host and port),
|
|
24
|
+
* `MONGODB_DATABASE`, and, only when **both** `MONGODB_USER` and
|
|
25
|
+
* `MONGODB_PASS` are set, URI-encoded credentials plus
|
|
26
|
+
* `?authSource=<MONGODB_AUTH_DB>`.
|
|
27
|
+
*
|
|
28
|
+
* One implementation of the function that `affiliate-api`
|
|
29
|
+
* (`src/config/mongo-uri.ts`), `social-api`, `nfse-api` and `browser-worker`
|
|
30
|
+
* (`src/app.module.ts`) each carried. Their bodies were identical; the output
|
|
31
|
+
* of this one is pinned against all of them by `build-mongo-uri.spec.ts`.
|
|
32
|
+
*/
|
|
33
|
+
function buildMongoUri(config) {
|
|
34
|
+
const mongoUri = config.get('MONGODB_URI');
|
|
35
|
+
if (mongoUri) {
|
|
36
|
+
return mongoUri;
|
|
37
|
+
}
|
|
38
|
+
const host = config.get('MONGODB_URL', exports.DEFAULT_MONGODB_HOST);
|
|
39
|
+
const user = config.get('MONGODB_USER');
|
|
40
|
+
const pass = config.get('MONGODB_PASS');
|
|
41
|
+
const authDb = config.get('MONGODB_AUTH_DB', exports.DEFAULT_MONGODB_AUTH_DB);
|
|
42
|
+
const database = config.get('MONGODB_DATABASE', exports.DEFAULT_MONGODB_DATABASE);
|
|
43
|
+
const withCredentials = Boolean(user && pass);
|
|
44
|
+
const credentials = withCredentials
|
|
45
|
+
? `${encodeURIComponent(user)}:${encodeURIComponent(pass)}@`
|
|
46
|
+
: '';
|
|
47
|
+
const authSource = withCredentials ? `?authSource=${authDb}` : '';
|
|
48
|
+
return `mongodb://${credentials}${host}/${database}${authSource}`;
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leoribeiroweb/nest-mongo",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Shared Mongo wiring for NestJS services: the MONGODB_* connection URI, the MongooseModule root options with a bounded close, the shared `counters` sequence module, a case-insensitive collation and the duplicate-key check.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/leoribeiro2/nest-core.git",
|
|
9
|
+
"directory": "packages/nest-mongo"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/leoribeiro2/nest-core/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/leoribeiro2/nest-core#readme",
|
|
15
|
+
"main": "dist/index.js",
|
|
16
|
+
"types": "dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"default": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./package.json": "./package.json"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=20.19.0"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsc -p tsconfig.build.json",
|
|
35
|
+
"prepublishOnly": "npm run build"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@leoribeiroweb/nest-core": ">=0.8.0 <1.0.0",
|
|
39
|
+
"@nestjs/common": "^11",
|
|
40
|
+
"@nestjs/mongoose": "^11",
|
|
41
|
+
"mongoose": "^9.9.1"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@leoribeiroweb/nest-core": "*",
|
|
45
|
+
"@nestjs/common": "^11.1.28",
|
|
46
|
+
"@nestjs/config": "^4.0.4",
|
|
47
|
+
"@nestjs/core": "^11.1.28",
|
|
48
|
+
"@nestjs/mongoose": "^11.0.0",
|
|
49
|
+
"@nestjs/testing": "^11.1.28",
|
|
50
|
+
"mongodb-memory-server": "^11.1.0",
|
|
51
|
+
"mongoose": "^9.12.1",
|
|
52
|
+
"reflect-metadata": "^0.2.2"
|
|
53
|
+
}
|
|
54
|
+
}
|