mongoose-currency-convert 0.1.3 → 0.1.5

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
@@ -10,7 +10,7 @@ A lightweight Mongoose plugin for automatic currency conversion at save time - f
10
10
 
11
11
  - **Automatic currency conversion** for specified fields when saving documents or updating them.
12
12
  - **Customizable exchange rate logic** via a user-provided function or extension plugin.
13
- - **Support for nested paths and array elements** in documents (e.g. `items.0.price`).
13
+ - **Support for nested paths** in documents.
14
14
  - **Pluggable rounding function** (default: round to 2 decimals).
15
15
  - **Simple in-memory cache** for exchange rates (optional).
16
16
  - **Error handling and rollback** on conversion failure.
@@ -42,12 +42,6 @@ const ProductSchema = new Schema({
42
42
  currency: String,
43
43
  date: Date,
44
44
  },
45
- items: [
46
- {
47
- price: Number,
48
- currency: String,
49
- }
50
- ]
51
45
  });
52
46
 
53
47
  ProductSchema.plugin(currencyConversionPlugin, {
@@ -59,12 +53,6 @@ ProductSchema.plugin(currencyConversionPlugin, {
59
53
  targetPath: 'priceConversion',
60
54
  toCurrency: 'EUR',
61
55
  },
62
- {
63
- sourcePath: 'items.0.price',
64
- currencyPath: 'items.0.currency',
65
- targetPath: 'items.0.converted',
66
- toCurrency: 'EUR',
67
- }
68
56
  ],
69
57
  getRate: async (from, to, date) => {
70
58
  // Implement your logic to fetch the exchange rate
@@ -108,9 +96,9 @@ You can use the built-in in-memory cache (`SimpleCache` in `src/utils/cache.ts`)
108
96
 
109
97
  ### Using the Internal SimpleCache
110
98
  ```ts
111
- import { SimpleCache } from 'mongoose-currency-convert';
99
+ import { SimpleCache } from 'mongoose-currency-convert/cache';
112
100
 
113
- const cache = new SimpleCache();
101
+ const cache = new SimpleCache<number>();
114
102
 
115
103
  ProductSchema.plugin(currencyConversionPlugin, {
116
104
  fields: [/* ... */],
@@ -124,7 +112,7 @@ You can implement the `CurrencyRateCache` interface to use any external service:
124
112
 
125
113
  ```ts
126
114
  import { createClient } from 'redis';
127
- import type { CurrencyRateCache } from 'mongoose-currency-convert';
115
+ import type { CurrencyRateCache } from 'mongoose-currency-convert/types';
128
116
 
129
117
  class RedisCache implements CurrencyRateCache<number> {
130
118
  private client = createClient({ url: 'redis://localhost:6379' });
package/dist/index.cjs CHANGED
@@ -1,18 +1,126 @@
1
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
2
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./plugin"), exports);
3
+ exports.currencyConversionPlugin = currencyConversionPlugin;
4
+ const helpers_1 = require("./utils/helpers");
5
+ function currencyConversionPlugin(schema, options) {
6
+ const { fields, getRate, round = helpers_1.defaultRound, allowedCurrencyCodes, onError, fallbackRate, rollbackOnError, cache, dateTransform, } = options;
7
+ if (!fields || !Array.isArray(fields) || fields.length === 0) {
8
+ throw new Error('[mongoose-currency-convert] option "fields" must be a non-empty array');
9
+ }
10
+ if (typeof getRate !== "function") {
11
+ throw new Error('[mongoose-currency-convert] option "getRate" must be a function');
12
+ }
13
+ async function applyCurrencyConversion(doc) {
14
+ const convertedFields = [];
15
+ for (const field of fields) {
16
+ const { sourcePath, currencyPath, datePath, targetPath, toCurrency } = field;
17
+ if (!targetPath) {
18
+ console.warn(`[mongoose-currency-convert] WARNING: 'targetPath' is required in field config`);
19
+ continue;
20
+ }
21
+ if (!schema.path(`${targetPath}.amount`)) {
22
+ console.warn(`[mongoose-currency-convert] WARNING: targetPath '${targetPath}' does not exist in schema`);
23
+ continue;
24
+ }
25
+ const amount = (0, helpers_1.getNestedValue)(doc, sourcePath);
26
+ if (amount == null)
27
+ continue;
28
+ const fromCurrency = (0, helpers_1.getNestedValue)(doc, currencyPath);
29
+ if (typeof fromCurrency !== "string" || !fromCurrency) {
30
+ console.warn(`[mongoose-currency-convert] Missing or invalid source currency at path: ${currencyPath}`);
31
+ continue;
32
+ }
33
+ if (!(0, helpers_1.isValidCurrencyCode)(fromCurrency, allowedCurrencyCodes)) {
34
+ console.warn(`[mongoose-currency-convert] Invalid source currency code: ${fromCurrency}`);
35
+ continue;
36
+ }
37
+ if (!(0, helpers_1.isValidCurrencyCode)(toCurrency, allowedCurrencyCodes)) {
38
+ console.warn(`[mongoose-currency-convert] Invalid target currency code: ${toCurrency}`);
39
+ continue;
40
+ }
41
+ const dateValue = datePath ? (0, helpers_1.getNestedValue)(doc, datePath) : undefined;
42
+ let conversionDate = dateValue &&
43
+ (typeof dateValue === "string" ||
44
+ typeof dateValue === "number" ||
45
+ dateValue instanceof Date)
46
+ ? new Date(dateValue)
47
+ : new Date();
48
+ if (dateTransform)
49
+ conversionDate = dateTransform(conversionDate);
50
+ let rate;
51
+ const cacheKey = `${fromCurrency}_${toCurrency}_${conversionDate.toISOString().slice(0, 10)}`;
52
+ try {
53
+ if (cache)
54
+ rate = await cache.get(cacheKey);
55
+ if (rate === undefined) {
56
+ rate = await getRate(fromCurrency, toCurrency, conversionDate);
57
+ if (cache && rate !== undefined && !Number.isNaN(rate)) {
58
+ await cache.set(cacheKey, rate);
59
+ }
60
+ }
61
+ if (!rate || Number.isNaN(rate)) {
62
+ if (typeof fallbackRate === "number") {
63
+ rate = fallbackRate;
64
+ }
65
+ else {
66
+ throw new Error("Invalid rate");
67
+ }
68
+ }
69
+ const convertedValue = {
70
+ amount: round(Number(amount) * rate),
71
+ currency: toCurrency,
72
+ date: conversionDate,
73
+ };
74
+ (0, helpers_1.setNestedValue)(doc, targetPath, convertedValue);
75
+ convertedFields.push(targetPath);
76
+ }
77
+ catch (err) {
78
+ if (onError) {
79
+ onError({
80
+ field: sourcePath,
81
+ fromCurrency: fromCurrency,
82
+ toCurrency,
83
+ date: conversionDate,
84
+ error: err,
85
+ });
86
+ }
87
+ else {
88
+ console.error(`[mongoose-currency-convert] Error converting ${sourcePath}:`, err);
89
+ }
90
+ if (rollbackOnError) {
91
+ for (const f of fields) {
92
+ if (f.targetPath)
93
+ (0, helpers_1.setNestedValue)(doc, f.targetPath, undefined);
94
+ }
95
+ break;
96
+ }
97
+ }
98
+ }
99
+ }
100
+ schema.pre("save", async function () {
101
+ const doc = this.toObject({ depopulate: true });
102
+ await applyCurrencyConversion(doc);
103
+ Object.assign(this, doc);
104
+ });
105
+ async function handleUpdateMiddleware(next) {
106
+ const update = this.getUpdate();
107
+ if (!update)
108
+ return next();
109
+ const updateAny = update;
110
+ let doc;
111
+ if (typeof updateAny.$set === "object" && updateAny.$set !== null) {
112
+ doc = { ...updateAny.$set };
113
+ await applyCurrencyConversion(doc);
114
+ updateAny.$set = doc;
115
+ }
116
+ else {
117
+ doc = { ...updateAny };
118
+ await applyCurrencyConversion(doc);
119
+ Object.assign(updateAny, doc);
120
+ }
121
+ next();
122
+ }
123
+ schema.pre("findOneAndUpdate", handleUpdateMiddleware);
124
+ schema.pre("updateOne", handleUpdateMiddleware);
125
+ }
18
126
  //# sourceMappingURL=index.js.map
package/dist/index.d.ts CHANGED
@@ -1 +1,10 @@
1
- export * from "./plugin";
1
+ import type { Schema } from "mongoose";
2
+ import type { CurrencyPluginErrorContext, CurrencyPluginOptions, CurrencyRateCache } from "./types";
3
+ export declare function currencyConversionPlugin(schema: Schema, options: CurrencyPluginOptions & {
4
+ allowedCurrencyCodes?: string[];
5
+ onError?: (ctx: CurrencyPluginErrorContext) => void;
6
+ fallbackRate?: number;
7
+ rollbackOnError?: boolean;
8
+ cache?: CurrencyRateCache<number>;
9
+ dateTransform?: (date: Date) => Date;
10
+ }): void;
@@ -14,10 +14,22 @@ function currencyConversionPlugin(schema, options) {
14
14
  const convertedFields = [];
15
15
  for (const field of fields) {
16
16
  const { sourcePath, currencyPath, datePath, targetPath, toCurrency } = field;
17
+ if (!targetPath) {
18
+ console.warn(`[mongoose-currency-convert] WARNING: 'targetPath' is required in field config`);
19
+ continue;
20
+ }
21
+ if (!schema.path(`${targetPath}.amount`)) {
22
+ console.warn(`[mongoose-currency-convert] WARNING: targetPath '${targetPath}' does not exist in schema`);
23
+ continue;
24
+ }
17
25
  const amount = (0, helpers_1.getNestedValue)(doc, sourcePath);
26
+ if (amount == null)
27
+ continue;
18
28
  const fromCurrency = (0, helpers_1.getNestedValue)(doc, currencyPath);
19
- if (amount == null || typeof fromCurrency !== "string" || !fromCurrency)
29
+ if (typeof fromCurrency !== "string" || !fromCurrency) {
30
+ console.warn(`[mongoose-currency-convert] Missing or invalid source currency at path: ${currencyPath}`);
20
31
  continue;
32
+ }
21
33
  if (!(0, helpers_1.isValidCurrencyCode)(fromCurrency, allowedCurrencyCodes)) {
22
34
  console.warn(`[mongoose-currency-convert] Invalid source currency code: ${fromCurrency}`);
23
35
  continue;
@@ -76,8 +88,9 @@ function currencyConversionPlugin(schema, options) {
76
88
  console.error(`[mongoose-currency-convert] Error converting ${sourcePath}:`, err);
77
89
  }
78
90
  if (rollbackOnError) {
79
- for (const path of convertedFields) {
80
- (0, helpers_1.setNestedValue)(doc, path, undefined);
91
+ for (const f of fields) {
92
+ if (f.targetPath)
93
+ (0, helpers_1.setNestedValue)(doc, f.targetPath, undefined);
81
94
  }
82
95
  break;
83
96
  }
@@ -110,4 +123,4 @@ function currencyConversionPlugin(schema, options) {
110
123
  schema.pre("findOneAndUpdate", handleUpdateMiddleware);
111
124
  schema.pre("updateOne", handleUpdateMiddleware);
112
125
  }
113
- //# sourceMappingURL=plugin.js.map
126
+ //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAKA,4DAgKC;AAlKD,6CAAoG;AAEpG,SAAgB,wBAAwB,CACtC,MAAc,EACd,OAOC;IAED,MAAM,EACJ,MAAM,EACN,OAAO,EACP,KAAK,GAAG,sBAAY,EACpB,oBAAoB,EACpB,OAAO,EACP,YAAY,EACZ,eAAe,EACf,KAAK,EACL,aAAa,GACd,GAAG,OAAO,CAAC;IAEZ,IAAI,CAAC,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC7D,MAAM,IAAI,KAAK,CAAC,uEAAuE,CAAC,CAAC;IAC3F,CAAC;IAED,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE,CAAC;QAClC,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAC;IACrF,CAAC;IAED,KAAK,UAAU,uBAAuB,CAAC,GAA4B;QACjE,MAAM,eAAe,GAAa,EAAE,CAAC;QAErC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,MAAM,EAAE,UAAU,EAAE,YAAY,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,KAAK,CAAC;YAE7E,IAAI,CAAC,UAAU,EAAE,CAAC;gBAChB,OAAO,CAAC,IAAI,CACV,+EAA+E,CAChF,CAAC;gBACF,SAAS;YACX,CAAC;YAED,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,UAAU,SAAS,CAAC,EAAE,CAAC;gBACzC,OAAO,CAAC,IAAI,CACV,oDAAoD,UAAU,4BAA4B,CAC3F,CAAC;gBACF,SAAS;YACX,CAAC;YAED,MAAM,MAAM,GAAG,IAAA,wBAAc,EAAC,GAAG,E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AAE,CAAC;IACT,CAAC;IAED,MAAM,CAAC,GAAG,CAAC,kBAAkB,EAAE,sBAAsB,CAAC,CAAC;IACvD,MAAM,CAAC,GAAG,CAAC,WAAW,EAAE,sBAAsB,CAAC,CAAC;AAClD,CAAC"}
package/dist/types.d.ts CHANGED
@@ -9,6 +9,7 @@ export interface CurrencyPluginOptions {
9
9
  fields: CurrencyFieldConfig[];
10
10
  getRate: (from: string, to: string, date?: Date) => Promise<number>;
11
11
  round?: (value: number) => number;
12
+ cache?: CurrencyRateCache<number>;
12
13
  }
13
14
  export interface CurrencyPluginErrorContext {
14
15
  field: string;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SimpleCache = void 0;
4
+ class SimpleCache {
5
+ store = new Map();
6
+ ttl;
7
+ constructor(ttlMinutes = 60) {
8
+ this.ttl = ttlMinutes * 60 * 1000;
9
+ }
10
+ isExpired(entry) {
11
+ return Date.now() > entry.expiresAt;
12
+ }
13
+ get(key) {
14
+ const entry = this.store.get(key);
15
+ if (!entry)
16
+ return undefined;
17
+ if (this.isExpired(entry)) {
18
+ this.store.delete(key);
19
+ return undefined;
20
+ }
21
+ return entry.value;
22
+ }
23
+ set(key, value) {
24
+ this.store.set(key, {
25
+ value,
26
+ expiresAt: Date.now() + this.ttl,
27
+ });
28
+ }
29
+ delete(key) {
30
+ this.store.delete(key);
31
+ }
32
+ clear() {
33
+ this.store.clear();
34
+ }
35
+ }
36
+ exports.SimpleCache = SimpleCache;
37
+ //# sourceMappingURL=cache.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongoose-currency-convert",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "A lightweight Mongoose plugin for automatic currency conversion at save time - flexible, extensible, and service-agnostic.",
5
5
  "keywords": [
6
6
  "mongoose",
@@ -31,10 +31,10 @@
31
31
  "require": "./dist/index.cjs",
32
32
  "types": "./dist/index.d.ts"
33
33
  },
34
- "./utils": {
35
- "import": "./dist/utils/helpers.js",
36
- "require": "./dist/utils/helpers.cjs",
37
- "types": "./dist/utils/helpers.d.ts"
34
+ "./cache": {
35
+ "import": "./dist/utils/cache.js",
36
+ "require": "./dist/utils/cache.cjs",
37
+ "types": "./dist/utils/cache.d.ts"
38
38
  },
39
39
  "./types": {
40
40
  "import": "./dist/types.d.ts",
@@ -46,7 +46,7 @@
46
46
  ],
47
47
  "scripts": {
48
48
  "build:esm": "tsc -p tsconfig.json",
49
- "build:cjs": "tsc -p tsconfig.cjs.json --outDir dist --declaration false && for f in dist/*.js; do mv \"$f\" \"${f%.js}.cjs\"; done",
49
+ "build:cjs": "tsc -p tsconfig.cjs.json --outDir dist --declaration false && if [ -f dist/index.js ]; then cp dist/index.js dist/index.cjs; fi && if [ -f dist/utils/cache.js ]; then cp dist/utils/cache.js dist/utils/cache.cjs; fi",
50
50
  "build": "pnpm build:esm && pnpm build:cjs",
51
51
  "clean": "rm -rf dist",
52
52
  "prepare": "husky",
package/dist/plugin.d.ts DELETED
@@ -1,10 +0,0 @@
1
- import type { Schema } from "mongoose";
2
- import type { CurrencyPluginErrorContext, CurrencyPluginOptions, CurrencyRateCache } from "./types";
3
- export declare function currencyConversionPlugin(schema: Schema, options: CurrencyPluginOptions & {
4
- allowedCurrencyCodes?: string[];
5
- onError?: (ctx: CurrencyPluginErrorContext) => void;
6
- fallbackRate?: number;
7
- rollbackOnError?: boolean;
8
- cache?: CurrencyRateCache<number>;
9
- dateTransform?: (date: Date) => Date;
10
- }): void;
@@ -1 +0,0 @@
1
- {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;AAKA,4DA4IC;AA9ID,6CAAoG;AAEpG,SAAgB,wBAAwB,CACtC,MAAc,EACd,OAOC;IAED,MAAM,EACJ,MAAM,EACN,OAAO,EACP,KAAK,GAAG,sBAAY,EACpB,oBAAoB,EACpB,OAAO,EACP,YAAY,EACZ,eAAe,E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