@kikiutils/mongoose 5.2.0 → 6.0.1
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/dist/assertions.d.ts +7 -0
- package/dist/assertions.d.ts.map +1 -0
- package/dist/assertions.js +10 -0
- package/dist/assertions.js.map +1 -0
- package/dist/builders.d.ts +13 -0
- package/dist/builders.d.ts.map +1 -0
- package/dist/builders.js +26 -0
- package/dist/builders.js.map +1 -0
- package/dist/constants.d.ts +0 -8
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +0 -7
- package/dist/constants.js.map +1 -1
- package/dist/helpers.d.ts +7 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +10 -0
- package/dist/helpers.js.map +1 -0
- package/dist/options.d.ts +1 -10
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +0 -7
- package/dist/options.js.map +1 -1
- package/dist/plugins/assertions.d.ts +12 -0
- package/dist/plugins/assertions.d.ts.map +1 -0
- package/dist/plugins/assertions.js +12 -0
- package/dist/plugins/assertions.js.map +1 -0
- package/dist/plugins/normalize.js +13 -11
- package/dist/plugins/normalize.js.map +1 -1
- package/dist/schema-builders/date.d.ts +1 -1
- package/dist/schema-builders/date.js.map +1 -1
- package/dist/schema-builders/decimal128.d.ts +1 -1
- package/dist/schema-builders/decimal128.js.map +1 -1
- package/dist/schema-builders/number.d.ts +1 -1
- package/dist/schema-builders/number.d.ts.map +1 -1
- package/dist/schema-builders/number.js.map +1 -1
- package/dist/schema-builders/object-id.d.ts +1 -1
- package/dist/schema-builders/object-id.js.map +1 -1
- package/dist/schema-builders/ref.d.ts +1 -1
- package/dist/schema-builders/ref.js.map +1 -1
- package/dist/schema-builders/string.d.ts +1 -1
- package/dist/schema-builders/string.js.map +1 -1
- package/dist/types/{utils.d.ts → _internals.d.ts} +2 -2
- package/dist/types/_internals.d.ts.map +1 -0
- package/dist/types/aggregate-paginate.d.ts +62 -68
- package/dist/types/aggregate-paginate.d.ts.map +1 -1
- package/dist/types/data.d.ts +12 -45
- package/dist/types/data.d.ts.map +1 -1
- package/dist/types/index.d.ts +18 -76
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/options.d.ts +2 -50
- package/dist/types/options.d.ts.map +1 -1
- package/dist/types/paginate.d.ts +85 -102
- package/dist/types/paginate.d.ts.map +1 -1
- package/package.json +10 -6
- package/src/assertions.ts +16 -0
- package/src/builders.ts +60 -0
- package/src/constants.ts +0 -7
- package/src/helpers.ts +5 -0
- package/src/options.ts +0 -10
- package/src/plugins/assertions.ts +40 -0
- package/src/plugins/normalize.ts +29 -13
- package/src/schema-builders/date.ts +1 -1
- package/src/schema-builders/decimal128.ts +1 -1
- package/src/schema-builders/number.ts +1 -1
- package/src/schema-builders/object-id.ts +1 -1
- package/src/schema-builders/ref.ts +1 -1
- package/src/schema-builders/string.ts +1 -1
- package/src/types/aggregate-paginate.ts +71 -83
- package/src/types/data.ts +20 -63
- package/src/types/index.ts +45 -110
- package/src/types/options.ts +1 -53
- package/src/types/paginate.ts +119 -136
- package/dist/types/utils.d.ts.map +0 -1
- package/dist/utils.d.ts +0 -81
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -102
- package/dist/utils.js.map +0 -1
- package/src/utils.ts +0 -160
- /package/src/types/{utils.ts → _internals.ts} +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paginate.d.ts","names":[],"sources":["../../src/types/paginate.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"paginate.d.ts","names":[],"sources":["../../src/types/paginate.ts"],"sourcesContent":[],"mappings":";;;;KAYY,uDAAuD,4BAA4B,OAC3F,WACA,OACI,iBACA;;AAJR,CAAA,EAKQ,MALI,CAMA,CANA,CAAA,kBAAgB,CAAA,EAOhB,CAPgB,GAAA;EAAA,CAAA,GAAA,EAAA,MAAA,CAAA,EAAA,GAAA;GAQhB,KAGR,gBAX+D,CAW9C,CAX8C,EAW3C,Q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EtC,eA9EsC,CAAA,CAAA,OAAA,CAAA,EA8EX,CA9EW,CAAA,EA8EP,OA9EO,CA+E7C,cA/E6C,CA+E9B,gBA/E8B,CA+Eb,UA/Ea,EA+ED,gBA/EC,EA+EiB,QA/EjB,EA+E2B,CA/E3B,CAAA,CAAA,CAAA;MAAU,CAAA,WAkF/C,UAlF+C,EAAA,UAkFzB,eAlFyB,GAkFP,eAlFO,CAAA,CAAA,OAAA,CAAA,EAkFoB,CAlFpB,CAAA,EAkFwB,OAlFxB,CAmFvD,cAnFuD,CAmFxC,gBAnFwC,CAmFvB,QAnFuB,EAmFb,gBAnFa,EAmFK,QAnFL,EAmFe,CAnFf,CAAA,CAAA,CAAA;MAAe,CAAA,WAsF9D,UAtF8D,CAAA,CAAA,OAAA,CAAA,EAsFxC,eAtFwC,CAAA,EAsFtB,OAtFsB,CAuFtE,cAvFsE,CAuFvD,gBAvFuD,CAuFtC,QAvFsC,EAuF5B,gBAvF4B,EAuFV,QAvFU,EAuFA,eAvFA,CAAA,CAAA,CAAA;IAApD,CAAA;;;iBA6FjB,CAAA,CArHoE,MAAA,EAqH1D,MArH0D,CAAA,EAAA,IAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "6.0.1",
|
|
5
5
|
"description": "A Mongoose plugin for enhanced JSON normalization and common schema creation, with built-in support for pagination and automatic Decimal128 conversion.",
|
|
6
6
|
"author": "kiki-kanri",
|
|
7
7
|
"license": "MIT",
|
|
@@ -43,6 +43,11 @@
|
|
|
43
43
|
"import": null,
|
|
44
44
|
"require": null
|
|
45
45
|
},
|
|
46
|
+
"./types/_internals": {
|
|
47
|
+
"types": null,
|
|
48
|
+
"import": null,
|
|
49
|
+
"require": null
|
|
50
|
+
},
|
|
46
51
|
"./types/index": {
|
|
47
52
|
"types": null,
|
|
48
53
|
"import": null,
|
|
@@ -75,19 +80,18 @@
|
|
|
75
80
|
"mongoose": "^8.18.1",
|
|
76
81
|
"mongoose-aggregate-paginate-v2": "^1.1.4",
|
|
77
82
|
"mongoose-paginate-v2": "^1.9.1",
|
|
78
|
-
"type-fest": "^5.0.
|
|
83
|
+
"type-fest": "^5.0.1"
|
|
79
84
|
},
|
|
80
85
|
"devDependencies": {
|
|
81
86
|
"@kikiutils/changelogen": "^0.9.0",
|
|
82
87
|
"@kikiutils/eslint-config": "^3.0.1",
|
|
83
88
|
"@kikiutils/tsconfigs": "^5.0.5",
|
|
84
|
-
"@types/node": "^24.
|
|
89
|
+
"@types/node": "^24.5.2",
|
|
85
90
|
"@vitest/coverage-v8": "^3.2.4",
|
|
86
91
|
"depcheck": "^1.4.7",
|
|
87
|
-
"publint": "^0.3.
|
|
92
|
+
"publint": "^0.3.13",
|
|
88
93
|
"ts-unused-exports": "^11.0.1",
|
|
89
|
-
"
|
|
90
|
-
"tsdown": "^0.15.1",
|
|
94
|
+
"tsdown": "^0.15.2",
|
|
91
95
|
"typescript": "^5.9.2",
|
|
92
96
|
"unplugin-unused": "^0.5.3",
|
|
93
97
|
"vitest": "^3.2.4"
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Document,
|
|
3
|
+
mongo,
|
|
4
|
+
} from 'mongoose';
|
|
5
|
+
|
|
6
|
+
export async function assertMongooseUpdateSuccess<T extends Document>(
|
|
7
|
+
updatePromise: Promise<mongo.UpdateResult<T>>,
|
|
8
|
+
expectedModifiedCount = 1,
|
|
9
|
+
) {
|
|
10
|
+
const updateResult = await updatePromise;
|
|
11
|
+
if (!updateResult.acknowledged) throw new Error('Update was not acknowledged');
|
|
12
|
+
if (updateResult.modifiedCount < expectedModifiedCount) {
|
|
13
|
+
// eslint-disable-next-line style/max-len
|
|
14
|
+
throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
package/src/builders.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
import type { Schema } from 'mongoose';
|
|
3
|
+
import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
|
|
4
|
+
import mongoosePaginate from 'mongoose-paginate-v2';
|
|
5
|
+
|
|
6
|
+
import { customMongooseOptions } from './_internals';
|
|
7
|
+
import { mongooseConnections } from './constants';
|
|
8
|
+
import { mongooseAssertionsPlugin } from './plugins/assertions';
|
|
9
|
+
import { mongooseNormalizePlugin } from './plugins/normalize';
|
|
10
|
+
import type { MongooseNormalizePluginOptions } from './plugins/normalize';
|
|
11
|
+
import type { BaseMongoosePaginateModel } from './types';
|
|
12
|
+
import type { BuildMongooseModelOptions } from './types/options';
|
|
13
|
+
|
|
14
|
+
export type DoNotRemoveOrUseThisType = typeof mongooseAssertionsPlugin;
|
|
15
|
+
|
|
16
|
+
export function buildMongooseModel<
|
|
17
|
+
DocType,
|
|
18
|
+
Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,
|
|
19
|
+
InstanceMethodsAndOverrides = object,
|
|
20
|
+
QueryHelpers = object,
|
|
21
|
+
>(
|
|
22
|
+
collection: string,
|
|
23
|
+
name: string,
|
|
24
|
+
schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,
|
|
25
|
+
options?: BuildMongooseModelOptions,
|
|
26
|
+
): Model {
|
|
27
|
+
if (options?.enableNormalizePlugin !== false) {
|
|
28
|
+
registerMongooseNormalizePlugin(schema, options?.normalizePluginRecursive, options?.normalizePluginOptions);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
schema.plugin(mongooseAggregatePaginate);
|
|
32
|
+
schema.plugin(mongooseAssertionsPlugin);
|
|
33
|
+
schema.plugin(mongoosePaginate);
|
|
34
|
+
schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);
|
|
35
|
+
customMongooseOptions.beforeModelBuild?.(schema);
|
|
36
|
+
const connection =
|
|
37
|
+
options?.connection
|
|
38
|
+
|| (
|
|
39
|
+
mongooseConnections.default ||= mongoose.createConnection(
|
|
40
|
+
process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017',
|
|
41
|
+
)
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);
|
|
45
|
+
}
|
|
46
|
+
export function registerMongooseNormalizePlugin<
|
|
47
|
+
DocType,
|
|
48
|
+
Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,
|
|
49
|
+
InstanceMethodsAndOverrides = object,
|
|
50
|
+
QueryHelpers = object,
|
|
51
|
+
>(
|
|
52
|
+
schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,
|
|
53
|
+
recursive: boolean = true,
|
|
54
|
+
options?: MongooseNormalizePluginOptions,
|
|
55
|
+
) {
|
|
56
|
+
schema.plugin(mongooseNormalizePlugin, options);
|
|
57
|
+
if (recursive) {
|
|
58
|
+
schema.childSchemas.forEach(({ schema }) => registerMongooseNormalizePlugin(schema, recursive, options));
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/constants.ts
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
1
|
import type { MongooseConnections } from './types';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* Mongoose connections.
|
|
5
|
-
*
|
|
6
|
-
* This constant is a record of named Mongoose connections, allowing multiple
|
|
7
|
-
* database connections to be managed within the application. An optional `default`
|
|
8
|
-
* connection can also be specified for use when a specific connection is not provided.
|
|
9
|
-
*/
|
|
10
3
|
export const mongooseConnections: MongooseConnections = {};
|
package/src/helpers.ts
ADDED
package/src/options.ts
CHANGED
|
@@ -1,16 +1,6 @@
|
|
|
1
1
|
import { customMongooseOptions } from './_internals';
|
|
2
2
|
import type { CustomMongooseOptions } from './types/options';
|
|
3
|
-
import type { buildMongooseModel } from './utils';
|
|
4
3
|
|
|
5
|
-
export type DoNotRemoveOrUseThisType = typeof buildMongooseModel;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Sets custom options.
|
|
9
|
-
*
|
|
10
|
-
* For example, you can set {@link CustomMongooseOptions.beforeModelBuild} to define a function that will be called
|
|
11
|
-
* every time {@link buildMongooseModel} is used. Inside this function, you can modify
|
|
12
|
-
* the schema, allowing you to apply common settings to all global schemas.
|
|
13
|
-
*/
|
|
14
4
|
export function setCustomMongooseOptions<K extends keyof CustomMongooseOptions>(
|
|
15
5
|
key: K,
|
|
16
6
|
value: CustomMongooseOptions[K],
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
QueryOptions,
|
|
3
|
+
Schema,
|
|
4
|
+
UpdateQuery,
|
|
5
|
+
UpdateWithAggregationPipeline,
|
|
6
|
+
} from 'mongoose';
|
|
7
|
+
|
|
8
|
+
import { assertMongooseUpdateSuccess } from '../assertions';
|
|
9
|
+
|
|
10
|
+
declare module 'mongoose' {
|
|
11
|
+
export interface Document<
|
|
12
|
+
T = unknown,
|
|
13
|
+
TQueryHelpers = any,
|
|
14
|
+
DocType = any,
|
|
15
|
+
TVirtuals = Record<string, any>,
|
|
16
|
+
// eslint-disable-next-line ts/no-empty-object-type
|
|
17
|
+
TSchemaOptions = {},
|
|
18
|
+
> {
|
|
19
|
+
assertUpdateSuccess: (
|
|
20
|
+
update?:
|
|
21
|
+
| UpdateQuery<Document<T, TQueryHelpers, DocType, TVirtuals, TSchemaOptions>>
|
|
22
|
+
| UpdateWithAggregationPipeline,
|
|
23
|
+
options?: null | QueryOptions,
|
|
24
|
+
expectedModifiedCount?: number,
|
|
25
|
+
) => Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function mongooseAssertionsPlugin<S extends Schema>(schema: S) {
|
|
30
|
+
schema.method(
|
|
31
|
+
'assertUpdateSuccess',
|
|
32
|
+
function (
|
|
33
|
+
update?: UpdateQuery<any> | UpdateWithAggregationPipeline,
|
|
34
|
+
options?: null | QueryOptions,
|
|
35
|
+
expectedModifiedCount?: number,
|
|
36
|
+
) {
|
|
37
|
+
return assertMongooseUpdateSuccess(this.updateOne(update, options), expectedModifiedCount);
|
|
38
|
+
},
|
|
39
|
+
);
|
|
40
|
+
}
|
package/src/plugins/normalize.ts
CHANGED
|
@@ -41,17 +41,34 @@ export function mongooseNormalizePlugin<S extends Schema>(
|
|
|
41
41
|
schema: S,
|
|
42
42
|
pluginOptions?: MongooseNormalizePluginOptions,
|
|
43
43
|
) {
|
|
44
|
+
// Collect special paths (Decimal128 & private)
|
|
45
|
+
const decimalPaths: string[] = [];
|
|
46
|
+
const privatePaths: string[] = [];
|
|
47
|
+
for (const [path, schemaType] of Object.entries(schema.paths)) {
|
|
48
|
+
if (schemaType?.options?.private) privatePaths.push(path);
|
|
49
|
+
else if (schemaType?.instance === 'Decimal128') decimalPaths.push(path);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Get original toJSON configuration
|
|
44
53
|
const toJson = schema.get('toJSON');
|
|
45
54
|
const toJsonTransform = toJson?.transform;
|
|
55
|
+
|
|
56
|
+
// Override toJSON with custom transform
|
|
46
57
|
schema.set(
|
|
47
58
|
'toJSON',
|
|
48
59
|
{
|
|
49
60
|
...toJson,
|
|
50
61
|
transform(doc, ret, options) {
|
|
51
|
-
//
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
62
|
+
// Copy object and remove __v
|
|
63
|
+
const copiedRet = { ...ret };
|
|
64
|
+
// @ts-expect-error Ignore this error
|
|
65
|
+
delete copiedRet.__v;
|
|
66
|
+
|
|
67
|
+
// Normalize _id (convert to hex and/or move to id)
|
|
68
|
+
let _id = copiedRet._id;
|
|
69
|
+
delete copiedRet._id;
|
|
70
|
+
if (pluginOptions?.toHexIdIfObjectId !== false && _id instanceof Types.ObjectId) {
|
|
71
|
+
_id = _id.toHexString();
|
|
55
72
|
}
|
|
56
73
|
|
|
57
74
|
if (_id !== undefined) {
|
|
@@ -59,22 +76,21 @@ export function mongooseNormalizePlugin<S extends Schema>(
|
|
|
59
76
|
else copiedRet._id = _id;
|
|
60
77
|
}
|
|
61
78
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
unsetProp(copiedRet, path);
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
79
|
+
// Remove private fields
|
|
80
|
+
for (const path of privatePaths) unsetProp(copiedRet, path);
|
|
67
81
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
82
|
+
// Convert Decimal128 fields to string
|
|
83
|
+
for (const path of decimalPaths) {
|
|
84
|
+
const value = getProp(copiedRet, path) as Types.Decimal128 | undefined;
|
|
85
|
+
if (value) setProp(copiedRet, path, value.toString());
|
|
72
86
|
}
|
|
73
87
|
|
|
88
|
+
// Run original toJSON transform
|
|
74
89
|
if (toJsonTransform && typeof toJsonTransform !== 'boolean') {
|
|
75
90
|
return toJsonTransform(doc as any, copiedRet as any, options as any);
|
|
76
91
|
}
|
|
77
92
|
|
|
93
|
+
// Return normalized object
|
|
78
94
|
return copiedRet;
|
|
79
95
|
},
|
|
80
96
|
},
|
|
@@ -11,7 +11,7 @@ import type {
|
|
|
11
11
|
} from 'mongoose';
|
|
12
12
|
import type { Merge } from 'type-fest';
|
|
13
13
|
|
|
14
|
-
import type { Readonlyable } from '../types/
|
|
14
|
+
import type { Readonlyable } from '../types/_internals';
|
|
15
15
|
|
|
16
16
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
17
17
|
|
|
@@ -6,100 +6,88 @@
|
|
|
6
6
|
// Used with mongoose-paginate, redefined and renamed here to avoid conflicts.
|
|
7
7
|
|
|
8
8
|
import type {
|
|
9
|
-
|
|
9
|
+
Aggregate,
|
|
10
|
+
Model,
|
|
11
|
+
PipelineStage,
|
|
10
12
|
Schema,
|
|
11
13
|
} from 'mongoose';
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
type PrePaginatePipelineStage = '__PREPAGINATE__' | PipelineStage;
|
|
15
|
+
type PrePaginatePipelineStage = '__PREPAGINATE__' | PipelineStage;
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
interface AggregatePaginateModel<
|
|
31
|
-
RawDocType,
|
|
32
|
-
QueryHelpers = object,
|
|
33
|
-
InstanceMethodsAndOverrides = object,
|
|
34
|
-
> extends Model<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> {
|
|
35
|
-
aggregatePaginate: <T>(
|
|
36
|
-
query?: Aggregate<T[]> | PrePaginatePipelineStage[],
|
|
37
|
-
options?: AggregatePaginateOptions,
|
|
38
|
-
callback?: (err: any, result: AggregatePaginateResult<T>) => void,
|
|
39
|
-
) => Promise<AggregatePaginateResult<T>>;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
interface AggregatePaginateOptions {
|
|
43
|
-
allowDiskUse?: boolean;
|
|
44
|
-
countQuery?: object;
|
|
45
|
-
customLabels?: AggregateCustomLabels;
|
|
46
|
-
limit?: number;
|
|
47
|
-
offset?: number;
|
|
48
|
-
page?: number;
|
|
17
|
+
export interface AggregateCustomLabels<T = boolean | string | undefined> {
|
|
18
|
+
docs?: T;
|
|
19
|
+
hasNextPage?: T;
|
|
20
|
+
hasPrevPage?: T;
|
|
21
|
+
limit?: T;
|
|
22
|
+
meta?: T;
|
|
23
|
+
nextPage?: T;
|
|
24
|
+
page?: T;
|
|
25
|
+
pagingCounter?: T;
|
|
26
|
+
prevPage?: T;
|
|
27
|
+
totalDocs?: T;
|
|
28
|
+
totalPages?: T;
|
|
29
|
+
}
|
|
49
30
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
31
|
+
export interface AggregatePaginateModel<
|
|
32
|
+
RawDocType,
|
|
33
|
+
QueryHelpers = object,
|
|
34
|
+
InstanceMethodsAndOverrides = object,
|
|
35
|
+
> extends Model<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> {
|
|
36
|
+
aggregatePaginate: <T>(
|
|
37
|
+
query?: Aggregate<T[]> | PrePaginatePipelineStage[],
|
|
38
|
+
options?: AggregatePaginateOptions,
|
|
39
|
+
callback?: (err: any, result: AggregatePaginateResult<T>) => void,
|
|
40
|
+
) => Promise<AggregatePaginateResult<T>>;
|
|
41
|
+
}
|
|
59
42
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
/** space delimited path(s) to populate */
|
|
68
|
-
path: string;
|
|
69
|
-
/** deep populate */
|
|
70
|
-
populate?: AggregatePaginateQueryPopulateOptions | AggregatePaginateQueryPopulateOptions[];
|
|
71
|
-
/** optional fields to select */
|
|
72
|
-
select?: any;
|
|
73
|
-
}
|
|
43
|
+
export interface AggregatePaginateOptions {
|
|
44
|
+
allowDiskUse?: boolean;
|
|
45
|
+
countQuery?: object;
|
|
46
|
+
customLabels?: AggregateCustomLabels;
|
|
47
|
+
limit?: number;
|
|
48
|
+
offset?: number;
|
|
49
|
+
page?: number;
|
|
74
50
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
page?: number;
|
|
84
|
-
pagingCounter: number;
|
|
85
|
-
prevPage?: null | number;
|
|
86
|
-
totalDocs: number;
|
|
87
|
-
totalPages: number;
|
|
88
|
-
}
|
|
51
|
+
/**
|
|
52
|
+
* If pagination is set to `false`, it will return all docs without adding limit condition.
|
|
53
|
+
*
|
|
54
|
+
* @default true
|
|
55
|
+
*/
|
|
56
|
+
pagination?: boolean;
|
|
57
|
+
sort?: object | string;
|
|
58
|
+
useFacet?: boolean;
|
|
89
59
|
}
|
|
90
60
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
61
|
+
export interface AggregatePaginateQueryPopulateOptions {
|
|
62
|
+
/** optional query conditions to match */
|
|
63
|
+
match?: any;
|
|
64
|
+
/** optional model to use for population */
|
|
65
|
+
model?: Model<any> | string;
|
|
66
|
+
/** optional query options like sort, limit, etc */
|
|
67
|
+
options?: any;
|
|
68
|
+
/** space delimited path(s) to populate */
|
|
69
|
+
path: string;
|
|
70
|
+
/** deep populate */
|
|
71
|
+
populate?: AggregatePaginateQueryPopulateOptions | AggregatePaginateQueryPopulateOptions[];
|
|
72
|
+
/** optional fields to select */
|
|
73
|
+
select?: any;
|
|
96
74
|
}
|
|
97
75
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
76
|
+
export interface AggregatePaginateResult<T> {
|
|
77
|
+
[customLabel: string]: boolean | null | number | T[] | undefined;
|
|
78
|
+
docs: T[];
|
|
79
|
+
hasNextPage: boolean;
|
|
80
|
+
hasPrevPage: boolean;
|
|
81
|
+
limit: number;
|
|
82
|
+
meta?: any;
|
|
83
|
+
nextPage?: null | number;
|
|
84
|
+
page?: number;
|
|
85
|
+
pagingCounter: number;
|
|
86
|
+
prevPage?: null | number;
|
|
87
|
+
totalDocs: number;
|
|
88
|
+
totalPages: number;
|
|
101
89
|
}
|
|
102
90
|
|
|
103
|
-
|
|
91
|
+
declare function _(schema: Schema): void;
|
|
104
92
|
|
|
105
|
-
export default
|
|
93
|
+
export default _;
|
package/src/types/data.ts
CHANGED
|
@@ -1,68 +1,25 @@
|
|
|
1
1
|
import type { SetFieldType } from 'type-fest';
|
|
2
2
|
|
|
3
|
-
import type { IfElse } from './
|
|
3
|
+
import type { IfElse } from './_internals';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
|
|
10
|
-
* @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
11
|
-
*/
|
|
12
|
-
type BaseMongooseModelData<
|
|
13
|
-
CreatedAtField extends boolean = true,
|
|
14
|
-
UpdatedAtField extends boolean = true,
|
|
15
|
-
> =
|
|
16
|
-
& IfElse<CreatedAtField, { createdAt: string }, unknown>
|
|
17
|
-
& IfElse<UpdatedAtField, { updatedAt: string }, unknown>
|
|
18
|
-
& { id: string };
|
|
5
|
+
import type {
|
|
6
|
+
BaseMongooseDocType,
|
|
7
|
+
SetFieldsToObjectId,
|
|
8
|
+
} from './';
|
|
19
9
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
CreatedAtField extends boolean = true,
|
|
25
|
-
UpdatedAtField extends boolean = true,
|
|
26
|
-
> = BaseMongooseDocType<
|
|
27
|
-
SetFieldsToObjectId<SetFieldType<T, DateFields, Date>, ObjectIdFields>,
|
|
28
|
-
CreatedAtField,
|
|
29
|
-
UpdatedAtField
|
|
30
|
-
>;
|
|
10
|
+
export type BaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> =
|
|
11
|
+
& IfElse<CreatedAtField, { createdAt: string }, unknown>
|
|
12
|
+
& IfElse<UpdatedAtField, { updatedAt: string }, unknown>
|
|
13
|
+
& { id: string };
|
|
31
14
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
* and other specified fields (`K`) from a given type `T`.
|
|
44
|
-
*
|
|
45
|
-
* @template T - The type from which to omit fields.
|
|
46
|
-
* @template K - Additional fields to omit, specified as a union of string, number, or symbol keys.
|
|
47
|
-
*/
|
|
48
|
-
type OmitMongooseTimestampAndOtherFields<
|
|
49
|
-
T,
|
|
50
|
-
K extends number | string | symbol = never,
|
|
51
|
-
> = Omit<
|
|
52
|
-
T,
|
|
53
|
-
'createdAt' | 'updatedAt' | K
|
|
54
|
-
>;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Type definition for including timestamp fields conditionally based on the provided flags.
|
|
58
|
-
*
|
|
59
|
-
* @template CreatedAt - A boolean flag indicating whether the `createdAt` field should be included.
|
|
60
|
-
* @template UpdatedAt - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
61
|
-
*/
|
|
62
|
-
type WithMongooseTimestampFieldsData<
|
|
63
|
-
CreatedAt extends boolean = true,
|
|
64
|
-
UpdatedAt extends boolean = true,
|
|
65
|
-
> =
|
|
66
|
-
& IfElse<CreatedAt, { createdAt: string }, object>
|
|
67
|
-
& IfElse<UpdatedAt, { updatedAt: string }, object>;
|
|
68
|
-
}
|
|
15
|
+
export type DataToBaseMongooseDocType<
|
|
16
|
+
T,
|
|
17
|
+
ObjectIdFields extends keyof T = never,
|
|
18
|
+
DateFields extends Exclude<keyof T, ObjectIdFields> = never,
|
|
19
|
+
CreatedAtField extends boolean = true,
|
|
20
|
+
UpdatedAtField extends boolean = true,
|
|
21
|
+
> = BaseMongooseDocType<
|
|
22
|
+
SetFieldsToObjectId<SetFieldType<T, DateFields, Date>, ObjectIdFields>,
|
|
23
|
+
CreatedAtField,
|
|
24
|
+
UpdatedAtField
|
|
25
|
+
>;
|