@akanjs/nest 0.0.28 → 0.0.30

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akanjs/nest",
3
- "version": "0.0.28",
3
+ "version": "0.0.30",
4
4
  "type": "commonjs",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -21,6 +21,7 @@
21
21
  "@nestjs/passport": "^10.0.3",
22
22
  "@nestjs/platform-express": "^10.4.15",
23
23
  "@nestjs/platform-socket.io": "^10.4.15",
24
+ "@nestjs/schedule": "^4.1.2",
24
25
  "@nestjs/websockets": "^10.4.15",
25
26
  "@socket.io/redis-adapter": "^8.3.0",
26
27
  "@urql/core": "^5.1.0",
@@ -0,0 +1,13 @@
1
+ import "reflect-metadata";
2
+ import { CronExpression } from "@nestjs/schedule";
3
+ export declare const Try: () => (target: any, key: string, descriptor: PropertyDescriptor) => void;
4
+ interface TimerOptions {
5
+ lock?: boolean;
6
+ serverMode?: "federation" | "batch";
7
+ enabled?: boolean;
8
+ }
9
+ export declare const Cron: (cronTime: CronExpression | string, { lock, serverMode, enabled }?: TimerOptions) => (target: any, key: string, descriptor: PropertyDescriptor) => void;
10
+ export declare const Interval: (ms: number, { lock, serverMode, enabled }?: TimerOptions) => (target: any, key: string, descriptor: PropertyDescriptor) => PropertyDescriptor | undefined;
11
+ export declare const Transaction: () => (target: any, key: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
12
+ export declare const Cache: (timeout?: number, getCacheKey?: (...args: any) => string) => MethodDecorator;
13
+ export {};
@@ -0,0 +1,171 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var decorators_exports = {};
19
+ __export(decorators_exports, {
20
+ Cache: () => Cache,
21
+ Cron: () => Cron,
22
+ Interval: () => Interval,
23
+ Transaction: () => Transaction,
24
+ Try: () => Try
25
+ });
26
+ module.exports = __toCommonJS(decorators_exports);
27
+ var import_reflect_metadata = require("reflect-metadata");
28
+ var import_schedule = require("@nestjs/schedule");
29
+ const Try = () => {
30
+ return function(target, key, descriptor) {
31
+ const originMethod = descriptor.value;
32
+ descriptor.value = async function(...args) {
33
+ try {
34
+ const result = await originMethod.apply(this, args);
35
+ return result;
36
+ } catch (e) {
37
+ this.logger?.warn(`${key} action error return: ${e}`);
38
+ }
39
+ };
40
+ };
41
+ };
42
+ const Cron = (cronTime, { lock = true, serverMode, enabled = true } = {}) => {
43
+ return function(target, key, descriptor) {
44
+ const originMethod = descriptor.value;
45
+ let isRunning = false;
46
+ descriptor.value = async function(...args) {
47
+ if (lock && isRunning)
48
+ return this.logger?.warn(`Cronjob-${key} is already running, skipped`);
49
+ try {
50
+ isRunning = true;
51
+ this.logger?.verbose(`Cron Job-${key} started`);
52
+ const res = await originMethod.apply(this, args);
53
+ this.logger?.verbose(`Cron Job-${key} finished`);
54
+ isRunning = false;
55
+ return res;
56
+ } catch (e) {
57
+ this.logger?.error(`Cron Job-${key} error return: ${e}`);
58
+ isRunning = false;
59
+ }
60
+ };
61
+ if (!enabled)
62
+ return;
63
+ if (!serverMode || process.env.SERVER_MODE === "all" || serverMode === process.env.SERVER_MODE)
64
+ (0, import_schedule.Cron)(cronTime)(target, key, descriptor);
65
+ };
66
+ };
67
+ const getIntervalMetaMap = (prototype) => {
68
+ return Reflect.getMetadata("serviceInterval", prototype) ?? /* @__PURE__ */ new Map();
69
+ };
70
+ const setIntervalMetaMap = (prototype, intervalMetaMap) => {
71
+ Reflect.defineMetadata("serviceInterval", intervalMetaMap, prototype);
72
+ };
73
+ const Interval = (ms, { lock = true, serverMode, enabled = true } = {}) => {
74
+ return function(target, key, descriptor) {
75
+ const intervalMetaMap = getIntervalMetaMap(target);
76
+ if (intervalMetaMap.has(key))
77
+ return descriptor;
78
+ intervalMetaMap.set(key, descriptor.value);
79
+ setIntervalMetaMap(target, intervalMetaMap);
80
+ const originMethod = descriptor.value;
81
+ let isRunning = false;
82
+ descriptor.value = async function(...args) {
83
+ if (lock && isRunning)
84
+ return this.logger?.warn(`Cronjob-${key} is already running, skipped`);
85
+ try {
86
+ isRunning = true;
87
+ this.logger?.verbose(`Interval Job-${key} started`);
88
+ const res = await originMethod.apply(this, args);
89
+ this.logger?.verbose(`Interval Job-${key} finished`);
90
+ isRunning = false;
91
+ return res;
92
+ } catch (e) {
93
+ this.logger?.error(`Cronjob-${key} error return: ${e}`);
94
+ isRunning = false;
95
+ }
96
+ };
97
+ if (!enabled)
98
+ return;
99
+ if (!serverMode || process.env.SERVER_MODE === "all" || serverMode === process.env.SERVER_MODE)
100
+ (0, import_schedule.Interval)(ms)(target, key, descriptor);
101
+ };
102
+ };
103
+ const Transaction = () => {
104
+ return function(target, key, descriptor) {
105
+ const originMethod = descriptor.value;
106
+ descriptor.value = function(...args) {
107
+ if (!this.connection)
108
+ throw new Error(`No Connection in function ${key}`);
109
+ return new Promise((resolve, reject) => {
110
+ this.connection.transaction(async () => {
111
+ const res = await originMethod.apply(this, args);
112
+ resolve(res);
113
+ }).catch(reject);
114
+ });
115
+ };
116
+ return descriptor;
117
+ };
118
+ };
119
+ const Cache = (timeout = 1e3, getCacheKey) => {
120
+ return function(target, key, descriptor) {
121
+ const originMethod = descriptor.value;
122
+ const cacheMap = /* @__PURE__ */ new Map();
123
+ const timerMap = /* @__PURE__ */ new Map();
124
+ descriptor.value = async function(...args) {
125
+ const classType = this.__model ? "doc" : this.__databaseModel ? "service" : "class";
126
+ const model = this.__model ?? this.__databaseModel?.__model;
127
+ const cache = this.__cache ?? this.__databaseModel?.__cache;
128
+ const getCacheKeyFn = getCacheKey ?? JSON.stringify;
129
+ const cacheKey = `${classType}:${model.modelName}:${key}:${getCacheKeyFn(...args)}`;
130
+ const getCache = async (cacheKey2) => {
131
+ if (classType === "class")
132
+ return cacheMap.get(cacheKey2);
133
+ const cached = await cache.get(cacheKey2);
134
+ if (cached)
135
+ return JSON.parse(cached);
136
+ return null;
137
+ };
138
+ const setCache = async (cacheKey2, value) => {
139
+ if (classType === "class") {
140
+ const existingTimer = timerMap.get(cacheKey2);
141
+ if (existingTimer)
142
+ clearTimeout(existingTimer);
143
+ cacheMap.set(cacheKey2, value);
144
+ const timer = setTimeout(() => {
145
+ cacheMap.delete(cacheKey2);
146
+ timerMap.delete(cacheKey2);
147
+ }, timeout);
148
+ timerMap.set(cacheKey2, timer);
149
+ } else
150
+ await cache.set(cacheKey2, JSON.stringify(value), { PX: timeout });
151
+ };
152
+ const cachedData = await getCache(cacheKey);
153
+ if (cachedData) {
154
+ this.logger?.trace(`${model.modelName} cache hit: ${cacheKey}`);
155
+ return cachedData;
156
+ }
157
+ const result = await originMethod.apply(this, args);
158
+ await setCache(cacheKey, result);
159
+ this.logger?.trace(`${model.modelName} cache set: ${cacheKey}`);
160
+ return result;
161
+ };
162
+ };
163
+ };
164
+ // Annotate the CommonJS export names for ESM import in node:
165
+ 0 && (module.exports = {
166
+ Cache,
167
+ Cron,
168
+ Interval,
169
+ Transaction,
170
+ Try
171
+ });
package/src/exporter.js CHANGED
@@ -86,7 +86,7 @@ const exportToCsv = async ({ items, path, fields, delimiter, options }) => {
86
86
  writeStream.once(`open`, () => {
87
87
  writeStream.write(header);
88
88
  for (const item of items) {
89
- const data = fields.map((field) => objPath(item, field) || null).map((field) => String(field).replace(/\n/g, "").replace(/,/g, ""));
89
+ const data = fields.map((field) => objPath(item, field) ?? null).map((field) => String(field).replace(/\n/g, "").replace(/,/g, ""));
90
90
  const body = data.reduce((acc, cur) => acc + (delimiter ?? `,`) + cur) + `
91
91
  `;
92
92
  if (options?.append)
package/src/index.d.ts CHANGED
@@ -16,3 +16,4 @@ export * from "./mongoose";
16
16
  export * from "./searchClient";
17
17
  export * from "./cacheClient";
18
18
  export * from "./databaseClient";
19
+ export * from "./decorators";
package/src/index.js CHANGED
@@ -51,6 +51,7 @@ __reExport(src_exports, require("./mongoose"), module.exports);
51
51
  __reExport(src_exports, require("./searchClient"), module.exports);
52
52
  __reExport(src_exports, require("./cacheClient"), module.exports);
53
53
  __reExport(src_exports, require("./databaseClient"), module.exports);
54
+ __reExport(src_exports, require("./decorators"), module.exports);
54
55
  // Annotate the CommonJS export names for ESM import in node:
55
56
  0 && (module.exports = {
56
57
  Exporter,
@@ -70,5 +71,6 @@ __reExport(src_exports, require("./databaseClient"), module.exports);
70
71
  ...require("./mongoose"),
71
72
  ...require("./searchClient"),
72
73
  ...require("./cacheClient"),
73
- ...require("./databaseClient")
74
+ ...require("./databaseClient"),
75
+ ...require("./decorators")
74
76
  });