@kikiutils/mongoose 1.0.0-rc.2 → 1.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 +43 -1
- package/package.json +1 -1
- package/utils.d.ts +6 -6
- package/utils.mjs +13 -11
package/README.md
CHANGED
|
@@ -41,7 +41,49 @@ npm i @kikiutils/mongoose
|
|
|
41
41
|
- Mongoose v8
|
|
42
42
|
- NodeJS 18 or higher
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
Example code is provided in the examples folder. Follow the steps below to understand how to use the package:
|
|
47
|
+
|
|
48
|
+
1. Navigate to the examples folder
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
cd examples
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
2. Install the dependencies
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
pnpm i
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
3. Copy the .env.example file to .env
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
cp .env.example .env
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
4. Set the MONGODB_URI in the .env file with your MongoDB connection string.
|
|
67
|
+
5. Run the example code
|
|
68
|
+
|
|
69
|
+
- If you use bun, run this command directly
|
|
70
|
+
```bash
|
|
71
|
+
bun --watch run ./src/index.ts
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
- Use node
|
|
75
|
+
```bash
|
|
76
|
+
pnpm run build
|
|
77
|
+
MONGODB_URI="mongodb://127.0.0.1:27017/kikiutils-mongoose-test?directConnection=true" node ./dist/index.mjs
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
This will execute the example code and demonstrate how to use the @kikiutils/mongoose package with your MongoDB setup.
|
|
81
|
+
|
|
82
|
+
You can modify the code in examples/src/index.ts and run it to test other functions.
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
[MIT License](./LICENSE)
|
|
45
87
|
|
|
46
88
|
<!-- Badges -->
|
|
47
89
|
[npm-version-src]: https://img.shields.io/npm/v/@kikiutils/mongoose/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
|
package/package.json
CHANGED
package/utils.d.ts
CHANGED
|
@@ -52,7 +52,7 @@ import type { BaseSchemaAttribute, CreateCommonMongooseSchemasOptions, MongooseO
|
|
|
52
52
|
*
|
|
53
53
|
* @returns The created Mongoose model.
|
|
54
54
|
*/
|
|
55
|
-
export declare
|
|
55
|
+
export declare function buildMongooseModel<DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(collection: string, name: string, schema: mongoose.Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>, options?: BuildMongooseModelOptions): Model;
|
|
56
56
|
/**
|
|
57
57
|
* Creates common Mongoose schema definitions with optional custom schemas and settings.
|
|
58
58
|
*
|
|
@@ -67,7 +67,7 @@ export declare const buildMongooseModel: <DocType, Model extends BaseMongoosePag
|
|
|
67
67
|
*
|
|
68
68
|
* @returns The merged common and custom schemas.
|
|
69
69
|
*/
|
|
70
|
-
export declare
|
|
70
|
+
export declare function createCommonMongooseSchemas<T extends {}>(customSchemas?: T, options?: CreateCommonMongooseSchemasOptions): {
|
|
71
71
|
readonly boolean: {
|
|
72
72
|
readonly defaultFalse: {
|
|
73
73
|
readonly nonRequired: {
|
|
@@ -242,7 +242,7 @@ export declare const createCommonMongooseSchemas: <T extends {}>(customSchemas?:
|
|
|
242
242
|
*
|
|
243
243
|
* @returns The generated ObjectId reference schema definition.
|
|
244
244
|
*/
|
|
245
|
-
export declare
|
|
245
|
+
export declare function createMongooseObjectIdRefSchema<R extends string, T extends BaseSchemaAttribute[]>(refModelName: R, ...attributes: T): MongooseObjectIdRefSchema<R, T>;
|
|
246
246
|
/**
|
|
247
247
|
* Creates a Mongoose schema definition for a string field with specified attributes.
|
|
248
248
|
*
|
|
@@ -256,7 +256,7 @@ export declare const createMongooseObjectIdRefSchema: <R extends string, T exten
|
|
|
256
256
|
*
|
|
257
257
|
* @returns The generated string schema definition.
|
|
258
258
|
*/
|
|
259
|
-
export declare
|
|
259
|
+
export declare function createMongooseStringSchema<T extends MongooseStringSchemaAttribute[]>(...attributes: T): MongooseStringSchema<T>;
|
|
260
260
|
/**
|
|
261
261
|
* Converts a document or ObjectId to a document using the specified model.
|
|
262
262
|
*
|
|
@@ -274,7 +274,7 @@ export declare const createMongooseStringSchema: <T extends MongooseStringSchema
|
|
|
274
274
|
*
|
|
275
275
|
* @returns The corresponding document or null if not found.
|
|
276
276
|
*/
|
|
277
|
-
export declare
|
|
277
|
+
export declare function mongooseDocumentOrObjectIdToDocument<D extends MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>, DocType, InstanceMethodsAndOverrides, QueryHelpers>(documentOrObjectId: MongooseDocumentOrObjectId<D>, model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, selectFields?: string[]): Promise<D | null>;
|
|
278
278
|
/**
|
|
279
279
|
* Sets up a getter to convert Decimal128 fields to strings in a Mongoose schema.
|
|
280
280
|
*
|
|
@@ -289,4 +289,4 @@ export declare const mongooseDocumentOrObjectIdToDocument: <D extends mongoose.I
|
|
|
289
289
|
* @param schema - The Mongoose schema to modify.
|
|
290
290
|
* @param fields - The Decimal128 fields to add the getter to.
|
|
291
291
|
*/
|
|
292
|
-
export declare
|
|
292
|
+
export declare function setupDecimal128FieldsToStringGetter<DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(schema: mongoose.Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>, ...fields: string[]): void;
|
package/utils.mjs
CHANGED
|
@@ -30,14 +30,14 @@ import mongooseNormalizePlugin from "./plugins/normalize.mjs";
|
|
|
30
30
|
*
|
|
31
31
|
* @returns The created Mongoose model.
|
|
32
32
|
*/
|
|
33
|
-
export
|
|
33
|
+
export function buildMongooseModel(collection, name, schema, options) {
|
|
34
34
|
if (options?.enableNormalizePlugin !== false)
|
|
35
35
|
schema.plugin(mongooseNormalizePlugin);
|
|
36
36
|
schema.plugin(mongooseAggregatePaginate);
|
|
37
37
|
schema.plugin(mongoosePaginate);
|
|
38
38
|
schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
|
|
39
39
|
return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://localhost:27017'))).model(name, schema, collection);
|
|
40
|
-
}
|
|
40
|
+
}
|
|
41
41
|
/**
|
|
42
42
|
* Creates common Mongoose schema definitions with optional custom schemas and settings.
|
|
43
43
|
*
|
|
@@ -52,7 +52,7 @@ export const buildMongooseModel = (collection, name, schema, options) => {
|
|
|
52
52
|
*
|
|
53
53
|
* @returns The merged common and custom schemas.
|
|
54
54
|
*/
|
|
55
|
-
export
|
|
55
|
+
export function createCommonMongooseSchemas(customSchemas, options) {
|
|
56
56
|
const autoRoundAndToFixedDecimal128Places = options?.autoRoundAndToFixedDecimal128?.places || 2;
|
|
57
57
|
const autoRoundAndToFixedDecimal128Rounding = options?.autoRoundAndToFixedDecimal128?.rounding || Decimal.ROUND_DOWN;
|
|
58
58
|
const baseAutoRoundAndToFixedDecimal128 = {
|
|
@@ -164,7 +164,7 @@ export const createCommonMongooseSchemas = (customSchemas, options) => {
|
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
}, customSchemas);
|
|
167
|
-
}
|
|
167
|
+
}
|
|
168
168
|
/**
|
|
169
169
|
* Creates a Mongoose schema definition for an ObjectId reference with specified attributes.
|
|
170
170
|
*
|
|
@@ -179,11 +179,11 @@ export const createCommonMongooseSchemas = (customSchemas, options) => {
|
|
|
179
179
|
*
|
|
180
180
|
* @returns The generated ObjectId reference schema definition.
|
|
181
181
|
*/
|
|
182
|
-
export
|
|
182
|
+
export function createMongooseObjectIdRefSchema(refModelName, ...attributes) {
|
|
183
183
|
const schema = { ref: refModelName, type: mongoose.Schema.Types.ObjectId };
|
|
184
184
|
new Set(attributes).forEach((attribute) => (schema[attribute] = true));
|
|
185
185
|
return schema;
|
|
186
|
-
}
|
|
186
|
+
}
|
|
187
187
|
/**
|
|
188
188
|
* Creates a Mongoose schema definition for a string field with specified attributes.
|
|
189
189
|
*
|
|
@@ -197,7 +197,7 @@ export const createMongooseObjectIdRefSchema = (refModelName, ...attributes) =>
|
|
|
197
197
|
*
|
|
198
198
|
* @returns The generated string schema definition.
|
|
199
199
|
*/
|
|
200
|
-
export
|
|
200
|
+
export function createMongooseStringSchema(...attributes) {
|
|
201
201
|
const schema = { type: String };
|
|
202
202
|
new Set(attributes).forEach((attribute) => {
|
|
203
203
|
if (attribute === 'short')
|
|
@@ -205,7 +205,7 @@ export const createMongooseStringSchema = (...attributes) => {
|
|
|
205
205
|
schema[attribute] = true;
|
|
206
206
|
});
|
|
207
207
|
return schema;
|
|
208
|
-
}
|
|
208
|
+
}
|
|
209
209
|
/**
|
|
210
210
|
* Converts a document or ObjectId to a document using the specified model.
|
|
211
211
|
*
|
|
@@ -223,11 +223,11 @@ export const createMongooseStringSchema = (...attributes) => {
|
|
|
223
223
|
*
|
|
224
224
|
* @returns The corresponding document or null if not found.
|
|
225
225
|
*/
|
|
226
|
-
export
|
|
226
|
+
export async function mongooseDocumentOrObjectIdToDocument(documentOrObjectId, model, selectFields) {
|
|
227
227
|
if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId)
|
|
228
228
|
return (await model.findById(documentOrObjectId).select(selectFields || []));
|
|
229
229
|
return documentOrObjectId;
|
|
230
|
-
}
|
|
230
|
+
}
|
|
231
231
|
/**
|
|
232
232
|
* Sets up a getter to convert Decimal128 fields to strings in a Mongoose schema.
|
|
233
233
|
*
|
|
@@ -242,4 +242,6 @@ export const mongooseDocumentOrObjectIdToDocument = async (documentOrObjectId, m
|
|
|
242
242
|
* @param schema - The Mongoose schema to modify.
|
|
243
243
|
* @param fields - The Decimal128 fields to add the getter to.
|
|
244
244
|
*/
|
|
245
|
-
export
|
|
245
|
+
export function setupDecimal128FieldsToStringGetter(schema, ...fields) {
|
|
246
|
+
fields.forEach((field) => schema.path(field).get((value) => value?.toString()));
|
|
247
|
+
}
|