speedly 1.0.0 → 1.1.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 +280 -0
- package/dist/cjs/auth/auth.js +86 -0
- package/dist/cjs/db/db.d.ts +181 -0
- package/dist/cjs/db/db.js +485 -0
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/index.js +44 -0
- package/dist/cjs/model/translation.d.ts +59 -0
- package/dist/cjs/model/translation.js +13 -0
- package/dist/{modules/auth.d.ts → cjs/uploader/uploader.d.ts} +1 -1
- package/dist/cjs/uploader/uploader.js +150 -0
- package/dist/cjs/util/getConfig.d.ts +4 -0
- package/dist/cjs/util/getConfig.js +21 -0
- package/dist/cjs/util/strToObj.d.ts +2 -0
- package/dist/cjs/util/strToObj.js +9 -0
- package/dist/cjs/util/translator.d.ts +2 -0
- package/dist/cjs/util/translator.js +70 -0
- package/dist/cjs/validator/validator.d.ts +9 -0
- package/dist/cjs/validator/validator.js +32 -0
- package/dist/esm/auth/auth.js +81 -0
- package/dist/esm/db/db.d.ts +181 -0
- package/dist/esm/db/db.js +480 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/model/translation.d.ts +2 -0
- package/dist/esm/model/translation.js +8 -0
- package/dist/{modules/db.d.ts → esm/uploader/uploader.d.ts} +1 -1
- package/dist/esm/uploader/uploader.js +145 -0
- package/dist/esm/util/getConfig.d.ts +4 -0
- package/dist/esm/util/getConfig.js +16 -0
- package/dist/esm/util/strToObj.d.ts +2 -0
- package/dist/esm/util/strToObj.js +7 -0
- package/dist/esm/util/translator.d.ts +2 -0
- package/dist/esm/util/translator.js +65 -0
- package/dist/esm/validator/validator.d.ts +9 -0
- package/dist/esm/validator/validator.js +28 -0
- package/package.json +28 -15
- package/dist/index.d.ts +0 -0
- package/dist/index.js +0 -1
- package/dist/modules/auth.js +0 -68
- package/dist/modules/db/aggregation.d.ts +0 -0
- package/dist/modules/db/aggregation.js +0 -1
- package/dist/modules/db/aggregation.types.d.ts +0 -56
- package/dist/modules/db/aggregation.types.js +0 -2
- package/dist/modules/db/db.d.ts +0 -0
- package/dist/modules/db/db.js +0 -1
- package/dist/modules/db/db.type.js +0 -2
- package/dist/modules/db.js +0 -228
- package/dist/modules/types/db.d.ts +0 -38
- package/dist/modules/types/db.js +0 -1
- package/dist/modules/uploader.js +0 -57
- package/dist/modules/utils.d.ts +0 -0
- package/dist/modules/utils.js +0 -1
- package/dist/modules/validator.d.ts +0 -1
- package/dist/modules/validator.js +0 -47
- package/dist/utils/strToObj.d.ts +0 -0
- package/dist/utils/strToObj.js +0 -8
- /package/dist/{modules/db/db.type.d.ts → cjs/auth/auth.d.ts} +0 -0
- /package/dist/{modules/uploader.d.ts → esm/auth/auth.d.ts} +0 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
// const { path } = require('../../app');
|
|
3
|
+
export default (configField) => {
|
|
4
|
+
const stackPath = new Error().stack?.split('\n')[2].trim().match(/\(.+\)/)?.[0].replace(/([\(\)]|\:\d+\:\d+)/g, '');
|
|
5
|
+
for (let i = 1; i < 4; i++) {
|
|
6
|
+
const filePath = stackPath?.replace(/\\/g, '/').split('/').slice(0, -i).join('/') + '/speedly.config.js';
|
|
7
|
+
if (fs.existsSync(filePath)) {
|
|
8
|
+
const config = require(filePath);
|
|
9
|
+
if (config && config[configField]) {
|
|
10
|
+
return config[configField];
|
|
11
|
+
}
|
|
12
|
+
return {};
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return {};
|
|
16
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import translationModel from "./../model/translation";
|
|
3
|
+
import { Translate } from "translate";
|
|
4
|
+
import getConfig from "./getConfig";
|
|
5
|
+
const configs = { ...getConfig('translate') };
|
|
6
|
+
async function firstSuccessful(promises) {
|
|
7
|
+
return new Promise((resolve, reject) => {
|
|
8
|
+
let rejections = 0;
|
|
9
|
+
const total = promises.length;
|
|
10
|
+
promises.forEach((p) => {
|
|
11
|
+
Promise.resolve(p)
|
|
12
|
+
.then(resolve)
|
|
13
|
+
.catch(() => {
|
|
14
|
+
rejections++;
|
|
15
|
+
if (rejections === total) {
|
|
16
|
+
reject(new Error("همه ترجمهها شکست خوردند"));
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
export default async (text = "unspecified text", lang = "fa") => {
|
|
23
|
+
const translateInstance = Translate({ to: lang, engine: "google", });
|
|
24
|
+
const formattedText = text
|
|
25
|
+
.replaceAll(/[\-\_]/g, " ")
|
|
26
|
+
.replaceAll(/[A-Z]/g, " $&");
|
|
27
|
+
const translationDoc = await translationModel.findOne({ text: formattedText, lang });
|
|
28
|
+
if (translationDoc)
|
|
29
|
+
return translationDoc.translatedText;
|
|
30
|
+
const translationPromises = [];
|
|
31
|
+
translationPromises.push((async () => {
|
|
32
|
+
const res = await axios.get(`https://655.mtis.workers.dev/translate?text=${encodeURIComponent(formattedText)}&source_lang=en&target_lang=${lang}`, { timeout: 2000 });
|
|
33
|
+
if (!res.data?.response?.translated_text)
|
|
34
|
+
throw new Error("Translation failed");
|
|
35
|
+
return res.data.response.translated_text;
|
|
36
|
+
})());
|
|
37
|
+
if (configs.one_api_token)
|
|
38
|
+
translationPromises.push((async () => {
|
|
39
|
+
const res = await axios.post("https://api.one-api.ir/translate/v1/microsoft", {
|
|
40
|
+
text: formattedText,
|
|
41
|
+
target: lang,
|
|
42
|
+
}, {
|
|
43
|
+
timeout: 2000,
|
|
44
|
+
headers: {
|
|
45
|
+
"one-api-token": configs.one_api_token
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
if (res.data.status === 200) {
|
|
49
|
+
return res.data.result;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
throw new Error(res.data.message);
|
|
53
|
+
}
|
|
54
|
+
})());
|
|
55
|
+
translationPromises.push(translateInstance(formattedText));
|
|
56
|
+
try {
|
|
57
|
+
const result = await firstSuccessful(translationPromises);
|
|
58
|
+
await translationModel.create({ text: formattedText, lang, translatedText: result });
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
console.error("translator error:", err);
|
|
63
|
+
return formattedText;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RequestHandler } from "express";
|
|
2
|
+
import * as yup from "yup";
|
|
3
|
+
type ValidationSchema = {
|
|
4
|
+
body?: yup.AnyObjectSchema;
|
|
5
|
+
params?: yup.AnyObjectSchema;
|
|
6
|
+
query?: yup.AnyObjectSchema;
|
|
7
|
+
};
|
|
8
|
+
export declare const validator: <T extends ValidationSchema>(schemas: T) => RequestHandler;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// این تابع validator generic هست
|
|
2
|
+
export const validator = (schemas) => async (req, res, next) => {
|
|
3
|
+
try {
|
|
4
|
+
if (schemas.body) {
|
|
5
|
+
req.body = (await schemas.body.validate(req.body, {
|
|
6
|
+
stripUnknown: true,
|
|
7
|
+
}));
|
|
8
|
+
}
|
|
9
|
+
if (schemas.params) {
|
|
10
|
+
req.params = (await schemas.params.validate(req.params, {
|
|
11
|
+
stripUnknown: true,
|
|
12
|
+
}));
|
|
13
|
+
}
|
|
14
|
+
if (schemas.query) {
|
|
15
|
+
req.query = (await schemas.query.validate(req.query, {
|
|
16
|
+
stripUnknown: true,
|
|
17
|
+
}));
|
|
18
|
+
}
|
|
19
|
+
next();
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
return next({
|
|
23
|
+
status: 405,
|
|
24
|
+
json: { message: err.message },
|
|
25
|
+
section: "validation",
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
};
|
package/package.json
CHANGED
|
@@ -1,25 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "speedly",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"main": "dist/index.js",
|
|
5
|
-
"
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"main": "dist/cjs/index.js",
|
|
5
|
+
"module": "dist/esm/index.js",
|
|
6
|
+
"types": "dist/esm/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/esm/index.js",
|
|
10
|
+
"require": "./dist/cjs/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
6
13
|
"scripts": {
|
|
7
|
-
"
|
|
8
|
-
"build": "tsc"
|
|
14
|
+
"build": "npm run build:cjs && npm run build:esm",
|
|
15
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
16
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
17
|
+
"dev": "ts-node src/index.ts"
|
|
9
18
|
},
|
|
10
|
-
"
|
|
11
|
-
"author": "Mahserin",
|
|
19
|
+
"author": "MAHSERIN",
|
|
12
20
|
"license": "MIT",
|
|
13
|
-
"description": "
|
|
21
|
+
"description": "",
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/express": "^5.0.3",
|
|
24
|
+
"@types/multer": "^2.0.0",
|
|
25
|
+
"@types/node": "^24.5.2",
|
|
26
|
+
"ts-node": "^10.9.2",
|
|
27
|
+
"typescript": "^5.9.2"
|
|
28
|
+
},
|
|
14
29
|
"dependencies": {
|
|
15
|
-
"
|
|
16
|
-
"@types/node": "^22.15.17",
|
|
30
|
+
"axios": "^1.11.0",
|
|
17
31
|
"express": "^5.1.0",
|
|
18
32
|
"jsonwebtoken": "^9.0.2",
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"@types/multer": "^1.4.12"
|
|
33
|
+
"mongoose": "^8.18.2",
|
|
34
|
+
"multer": "^2.0.2",
|
|
35
|
+
"translate": "^3.0.1",
|
|
36
|
+
"yup": "^1.7.1"
|
|
24
37
|
}
|
|
25
38
|
}
|
package/dist/index.d.ts
DELETED
|
File without changes
|
package/dist/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
package/dist/modules/auth.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const jwt = require('jsonwebtoken');
|
|
13
|
-
const adminModel = require('./../models/admin');
|
|
14
|
-
const auth = (config = { admin: { role: 'ADMIN' } }) => {
|
|
15
|
-
let handlerState = {};
|
|
16
|
-
const useAuth = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
18
|
-
console.log('auth 6 ', req.cookies);
|
|
19
|
-
try {
|
|
20
|
-
const nextFunc = (handlers, index = 0) => (errorMessage = '') => {
|
|
21
|
-
if (!handlers.length || !handlers[index + 1])
|
|
22
|
-
return next();
|
|
23
|
-
if (errorMessage)
|
|
24
|
-
return next(errorMessage);
|
|
25
|
-
handlers[index + 1](req, res, nextFunc(handlers, index + 1));
|
|
26
|
-
};
|
|
27
|
-
if (!req.cookies.AT_SECRET) {
|
|
28
|
-
if (!handlerState.user || !((_b = (_a = handlerState.user) === null || _a === void 0 ? void 0 : _a.handlers) === null || _b === void 0 ? void 0 : _b.length))
|
|
29
|
-
return res.status(403).json({ message: 'you dont have access for this section' });
|
|
30
|
-
handlerState.user.handlers[0](req, res, nextFunc(handlerState.user.handlers));
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
if (!handlerState.admin || !((_d = (_c = handlerState.admin) === null || _c === void 0 ? void 0 : _c.handlers) === null || _d === void 0 ? void 0 : _d.length)) {
|
|
34
|
-
if (!handlerState.user || !((_f = (_e = handlerState.user) === null || _e === void 0 ? void 0 : _e.handlers) === null || _f === void 0 ? void 0 : _f.length))
|
|
35
|
-
return res.status(404).json({ message: 'route not found :(' });
|
|
36
|
-
handlerState.user.handlers[0](req, res, nextFunc(handlerState.user.handlers));
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
const tokenPayload = jwt.verify(req.cookies.AT_SECRET, process.env.ACCESS_TOKEN_SECRET);
|
|
40
|
-
const adminDoc = yield adminModel.findById(tokenPayload.id);
|
|
41
|
-
if (!adminDoc)
|
|
42
|
-
return res.status(403).json({ message: 'you dont have access for this section' });
|
|
43
|
-
if (adminDoc.role != 'OWNER' && adminDoc.role != ((_g = config === null || config === void 0 ? void 0 : config.admin) === null || _g === void 0 ? void 0 : _g.role))
|
|
44
|
-
return res.status(403).json({ message: 'you dont have access for this section' });
|
|
45
|
-
req.admin = adminDoc;
|
|
46
|
-
handlerState.admin.handlers[0](req, res, nextFunc(handlerState.admin.handlers));
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
useAuth.admin = (...handlers) => {
|
|
54
|
-
var _a;
|
|
55
|
-
if (!Array.isArray(handlers))
|
|
56
|
-
throw new Error('handlers must be an array');
|
|
57
|
-
handlerState.admin = { role: ((_a = config === null || config === void 0 ? void 0 : config.admin) === null || _a === void 0 ? void 0 : _a.role) || 'admin', handlers };
|
|
58
|
-
return useAuth;
|
|
59
|
-
};
|
|
60
|
-
useAuth.user = (...handlers) => {
|
|
61
|
-
if (!Array.isArray(handlers))
|
|
62
|
-
throw new Error('handlers must be an array');
|
|
63
|
-
handlerState.user = { handlers };
|
|
64
|
-
return useAuth;
|
|
65
|
-
};
|
|
66
|
-
return useAuth;
|
|
67
|
-
};
|
|
68
|
-
module.exports = auth;
|
|
File without changes
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
export interface IAggregationBuilder {
|
|
2
|
-
match(filter: object): this;
|
|
3
|
-
group(id: any, fn: (op: IAggregationOperators) => object): this;
|
|
4
|
-
project(fields: object): this;
|
|
5
|
-
addFields(fields: object): this;
|
|
6
|
-
sort(sort: object): this;
|
|
7
|
-
limit(n: number): this;
|
|
8
|
-
skip(n: number): this;
|
|
9
|
-
unwind(field: string | object): this;
|
|
10
|
-
lookup(config: ILookupConfig): this;
|
|
11
|
-
count(field: string): this;
|
|
12
|
-
sortByCount(field: string): this;
|
|
13
|
-
facet(facets: {
|
|
14
|
-
[key: string]: any[];
|
|
15
|
-
}): this;
|
|
16
|
-
bucket(params: IBucketConfig): this;
|
|
17
|
-
bucketAuto(params: IBucketAutoConfig): this;
|
|
18
|
-
replaceRoot(newRoot: object): this;
|
|
19
|
-
redact(expression: any): this;
|
|
20
|
-
sample(size: number): this;
|
|
21
|
-
merge(output: string | object): this;
|
|
22
|
-
out(collection: string): this;
|
|
23
|
-
raw(stage: object): this;
|
|
24
|
-
toArray(): Promise<any[]>;
|
|
25
|
-
first(): Promise<any | null>;
|
|
26
|
-
}
|
|
27
|
-
export interface IAggregationOperators {
|
|
28
|
-
sum(field: string | number): object;
|
|
29
|
-
avg(field: string | number): object;
|
|
30
|
-
min(field: string | number): object;
|
|
31
|
-
max(field: string | number): object;
|
|
32
|
-
first(field: string): object;
|
|
33
|
-
last(field: string): object;
|
|
34
|
-
push(field: string): object;
|
|
35
|
-
addToSet(field: string): object;
|
|
36
|
-
count(): object;
|
|
37
|
-
}
|
|
38
|
-
export interface ILookupConfig {
|
|
39
|
-
from: string;
|
|
40
|
-
localField?: string;
|
|
41
|
-
foreignField?: string;
|
|
42
|
-
as: string;
|
|
43
|
-
let?: object;
|
|
44
|
-
pipeline?: any[];
|
|
45
|
-
}
|
|
46
|
-
export interface IBucketConfig {
|
|
47
|
-
groupBy: any;
|
|
48
|
-
boundaries: any[];
|
|
49
|
-
default?: any;
|
|
50
|
-
output?: object;
|
|
51
|
-
}
|
|
52
|
-
export interface IBucketAutoConfig {
|
|
53
|
-
groupBy: any;
|
|
54
|
-
buckets: number;
|
|
55
|
-
output?: object;
|
|
56
|
-
}
|
package/dist/modules/db/db.d.ts
DELETED
|
File without changes
|
package/dist/modules/db/db.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
package/dist/modules/db.js
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
let __path = "./models/";
|
|
13
|
-
const path = require("path");
|
|
14
|
-
const strToObj = require("../utils/strToObj");
|
|
15
|
-
const db = (collectionName, config = { path: "../models" }) => {
|
|
16
|
-
let model;
|
|
17
|
-
let queryState = {
|
|
18
|
-
queries: [],
|
|
19
|
-
};
|
|
20
|
-
if (config === null || config === void 0 ? void 0 : config.path)
|
|
21
|
-
__path = config.path;
|
|
22
|
-
model = require(path.join(__path, collectionName));
|
|
23
|
-
const actionHandler = {
|
|
24
|
-
find: (match = {}) => {
|
|
25
|
-
queryState.action = "find";
|
|
26
|
-
queryState.match = match;
|
|
27
|
-
return handler;
|
|
28
|
-
},
|
|
29
|
-
aggregate: (pipelineGenerator) => {
|
|
30
|
-
},
|
|
31
|
-
create: (body = {}) => {
|
|
32
|
-
queryState.action = "create";
|
|
33
|
-
queryState.body = body;
|
|
34
|
-
return handler;
|
|
35
|
-
},
|
|
36
|
-
updateOne: (match = {}, body = {}) => {
|
|
37
|
-
queryState.action = "updateOne";
|
|
38
|
-
queryState.match = match;
|
|
39
|
-
queryState.body = body;
|
|
40
|
-
return handler;
|
|
41
|
-
},
|
|
42
|
-
updateMany: (match = {}, body = {}) => {
|
|
43
|
-
queryState.action = "updateById";
|
|
44
|
-
queryState.match = match;
|
|
45
|
-
queryState.body = body;
|
|
46
|
-
return handler;
|
|
47
|
-
},
|
|
48
|
-
deleteOne: (match = {}) => {
|
|
49
|
-
queryState.action = "deleteOne";
|
|
50
|
-
queryState.match = match;
|
|
51
|
-
return handler;
|
|
52
|
-
},
|
|
53
|
-
deleteMany: (match = {}) => {
|
|
54
|
-
queryState.action = "deleteMany";
|
|
55
|
-
queryState.match = match;
|
|
56
|
-
return handler;
|
|
57
|
-
},
|
|
58
|
-
findOne: (match = {}) => {
|
|
59
|
-
queryState.action = "findOne";
|
|
60
|
-
queryState.match = match;
|
|
61
|
-
return handler;
|
|
62
|
-
},
|
|
63
|
-
findOneAndUpdate: (match = {}, body = {}) => {
|
|
64
|
-
queryState.action = "findOneAndUpdate";
|
|
65
|
-
queryState.match = match;
|
|
66
|
-
queryState.body = body;
|
|
67
|
-
return handler;
|
|
68
|
-
},
|
|
69
|
-
findOneAndDelete: (match = {}) => {
|
|
70
|
-
queryState.action = "findOneAndDelete";
|
|
71
|
-
queryState.match = match;
|
|
72
|
-
return handler;
|
|
73
|
-
},
|
|
74
|
-
findById: (id = "") => {
|
|
75
|
-
queryState.action = "findById";
|
|
76
|
-
queryState.id = id;
|
|
77
|
-
return handler;
|
|
78
|
-
},
|
|
79
|
-
findByIdAndUpdate: (id = "", body = {}) => {
|
|
80
|
-
queryState.action = "findByIdAndUpdate";
|
|
81
|
-
queryState.id = id;
|
|
82
|
-
queryState.body = body;
|
|
83
|
-
return handler;
|
|
84
|
-
},
|
|
85
|
-
findByIdAndDelete: (id = "") => {
|
|
86
|
-
queryState.action = "findByIdAndDelete";
|
|
87
|
-
queryState.id = id;
|
|
88
|
-
return handler;
|
|
89
|
-
},
|
|
90
|
-
};
|
|
91
|
-
const handler = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
92
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
93
|
-
let data;
|
|
94
|
-
let match = {};
|
|
95
|
-
let realTimeQueries = [...queryState.queries];
|
|
96
|
-
const { sort, limit, page, select } = req.query;
|
|
97
|
-
if (req.query.search) {
|
|
98
|
-
const splittedSearch = typeof req.query.search === 'string'
|
|
99
|
-
? req.query.search.trim().split(/\s|/g)
|
|
100
|
-
: [];
|
|
101
|
-
const searchValue = splittedSearch.map(word => `(?=.*${word.split('').join('\\s?')})`).join('');
|
|
102
|
-
match.$or = [
|
|
103
|
-
{ name: { $regex: searchValue } },
|
|
104
|
-
{ title: { $regex: searchValue } },
|
|
105
|
-
{ subTitle: { $regex: searchValue } }
|
|
106
|
-
];
|
|
107
|
-
}
|
|
108
|
-
if (req.query.filters)
|
|
109
|
-
match = Object.assign(Object.assign({}, match), JSON.parse(req.query.filters));
|
|
110
|
-
if (typeof queryState.match == 'function')
|
|
111
|
-
match = Object.assign(Object.assign({}, match), queryState.match(req));
|
|
112
|
-
if (typeof queryState.match == 'object')
|
|
113
|
-
match = Object.assign(Object.assign({}, match), queryState.match);
|
|
114
|
-
{
|
|
115
|
-
if (req.query.sort) {
|
|
116
|
-
const sortQueryIndex = realTimeQueries.findIndex(q => q.type == 'sort');
|
|
117
|
-
const sortObject = strToObj(req.query.sort);
|
|
118
|
-
if (sortQueryIndex == -1)
|
|
119
|
-
realTimeQueries.push({ type: 'sort', value: sortObject });
|
|
120
|
-
else
|
|
121
|
-
realTimeQueries.splice(sortQueryIndex, 1, { type: 'sort', value: Object.assign(Object.assign({}, (typeof realTimeQueries[sortQueryIndex].value === 'object' ? realTimeQueries[sortQueryIndex].value : {})), (typeof sortObject === 'object' ? sortObject : {})) });
|
|
122
|
-
}
|
|
123
|
-
if (req.query.limit) {
|
|
124
|
-
if (realTimeQueries.findIndex(q => q.type == 'limit') == -1)
|
|
125
|
-
realTimeQueries.push({ type: 'limit', value: req.query.limit });
|
|
126
|
-
}
|
|
127
|
-
if (req.query.select) {
|
|
128
|
-
const selectQueryIndex = realTimeQueries.findIndex(q => q.type == 'select');
|
|
129
|
-
const selectObject = strToObj(req.query.select);
|
|
130
|
-
if (selectQueryIndex == -1)
|
|
131
|
-
realTimeQueries.push({ type: 'select', value: selectObject });
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
try {
|
|
135
|
-
if (queryState.action) {
|
|
136
|
-
if (!data)
|
|
137
|
-
switch (queryState.action) {
|
|
138
|
-
case "find":
|
|
139
|
-
data = (_a = model === null || model === void 0 ? void 0 : model[queryState.action]) === null || _a === void 0 ? void 0 : _a.call(model, match);
|
|
140
|
-
break;
|
|
141
|
-
case "create":
|
|
142
|
-
data = (_b = model === null || model === void 0 ? void 0 : model[queryState.action]) === null || _b === void 0 ? void 0 : _b.call(model, Object.assign(Object.assign({}, req.body), (typeof queryState.body == 'function' ? queryState.body(req) : queryState.body)));
|
|
143
|
-
break;
|
|
144
|
-
case "updateOne":
|
|
145
|
-
data = (_c = model === null || model === void 0 ? void 0 : model[queryState.action]) === null || _c === void 0 ? void 0 : _c.call(model, match, Object.assign(Object.assign({}, req.body), (typeof queryState.body == 'function' ? queryState.body(req) : queryState.body)));
|
|
146
|
-
break;
|
|
147
|
-
case "updateMany":
|
|
148
|
-
data = (_d = model === null || model === void 0 ? void 0 : model[queryState.action]) === null || _d === void 0 ? void 0 : _d.call(model, match, Object.assign(Object.assign({}, req.body), (typeof queryState.body == 'function' ? queryState.body(req) : queryState.body)));
|
|
149
|
-
break;
|
|
150
|
-
case "deleteOne":
|
|
151
|
-
data = (_e = model === null || model === void 0 ? void 0 : model[queryState.action]) === null || _e === void 0 ? void 0 : _e.call(model, match);
|
|
152
|
-
break;
|
|
153
|
-
case "deleteMany":
|
|
154
|
-
data = (_f = model === null || model === void 0 ? void 0 : model[queryState.action]) === null || _f === void 0 ? void 0 : _f.call(model, match);
|
|
155
|
-
break;
|
|
156
|
-
case "findOne":
|
|
157
|
-
data = (_g = model === null || model === void 0 ? void 0 : model[queryState.action]) === null || _g === void 0 ? void 0 : _g.call(model, match);
|
|
158
|
-
break;
|
|
159
|
-
case "findOneAndUpdate":
|
|
160
|
-
yield ((_h = model === null || model === void 0 ? void 0 : model[queryState.action]) === null || _h === void 0 ? void 0 : _h.call(model, match, { $set: Object.assign(Object.assign({}, req.body), (typeof queryState.body == 'function' ? queryState.body(req) : queryState.body))
|
|
161
|
-
}));
|
|
162
|
-
data = model.findOne(match);
|
|
163
|
-
break;
|
|
164
|
-
case "findOneAndDelete":
|
|
165
|
-
data = (_j = model === null || model === void 0 ? void 0 : model[queryState.action]) === null || _j === void 0 ? void 0 : _j.call(model, match);
|
|
166
|
-
break;
|
|
167
|
-
case "findById":
|
|
168
|
-
console.log('model', 99, req.body);
|
|
169
|
-
data = (_k = model === null || model === void 0 ? void 0 : model[queryState.action]) === null || _k === void 0 ? void 0 : _k.call(model, queryState.id || req.params.id);
|
|
170
|
-
break;
|
|
171
|
-
case "findByIdAndUpdate":
|
|
172
|
-
req.document = yield model.findById(queryState.id || req.params.id);
|
|
173
|
-
yield ((_l = model === null || model === void 0 ? void 0 : model[queryState.action]) === null || _l === void 0 ? void 0 : _l.call(model, queryState.id || req.params.id, { $set: Object.assign(Object.assign({}, req.body), (typeof queryState.body == 'function' ? queryState.body(req) : queryState.body)) }));
|
|
174
|
-
data = model.findById(queryState.id || req.params.id);
|
|
175
|
-
break;
|
|
176
|
-
case "findByIdAndDelete":
|
|
177
|
-
data = (_m = model === null || model === void 0 ? void 0 : model[queryState.action]) === null || _m === void 0 ? void 0 : _m.call(model, queryState.id || req.params.id);
|
|
178
|
-
break;
|
|
179
|
-
}
|
|
180
|
-
realTimeQueries.forEach((q) => {
|
|
181
|
-
var _a;
|
|
182
|
-
data = (_a = data === null || data === void 0 ? void 0 : data[q.type]) === null || _a === void 0 ? void 0 : _a.call(data, q.value);
|
|
183
|
-
});
|
|
184
|
-
// if(req.query.select) data = data.select(req.query.select)
|
|
185
|
-
data = yield data;
|
|
186
|
-
if (!data) {
|
|
187
|
-
res.status(404).json({
|
|
188
|
-
message: `${collectionName} not found.`,
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
else {
|
|
192
|
-
res.status(200).json({
|
|
193
|
-
content: data,
|
|
194
|
-
message: `${queryState.action.match(/create|update|delete/i) || queryState.action.match(/find/i) || queryState.action} ${collectionName} was successfully`,
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
catch (err) {
|
|
200
|
-
if (((_o = err === null || err === void 0 ? void 0 : err.errorResponse) === null || _o === void 0 ? void 0 : _o.code) == 11000)
|
|
201
|
-
return res.status(403).json({ message: Object.entries(err.errorResponse.keyValue || {})[0].join(' ') + ' is duplicated' });
|
|
202
|
-
console.error("Error : model", err);
|
|
203
|
-
res.status(400).json({ message: err.message });
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
handler.select = (value) => {
|
|
207
|
-
queryState.queries.push({ type: "select", value: typeof value == 'string' ? strToObj(value, 0) : value });
|
|
208
|
-
return handler;
|
|
209
|
-
};
|
|
210
|
-
handler.skip = (value) => {
|
|
211
|
-
queryState.queries.push({ type: "skip", value });
|
|
212
|
-
return handler;
|
|
213
|
-
};
|
|
214
|
-
handler.limit = (value) => {
|
|
215
|
-
queryState.queries.push({ type: "limit", value });
|
|
216
|
-
return handler;
|
|
217
|
-
};
|
|
218
|
-
handler.sort = (value) => {
|
|
219
|
-
queryState.queries.push({ type: "sort", value: typeof value == 'string' ? strToObj(value) : value });
|
|
220
|
-
return handler;
|
|
221
|
-
};
|
|
222
|
-
handler.populate = (value) => {
|
|
223
|
-
queryState.queries.push({ type: "populate", value });
|
|
224
|
-
return handler;
|
|
225
|
-
};
|
|
226
|
-
return actionHandler;
|
|
227
|
-
};
|
|
228
|
-
module.exports = db;
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
type FlagCombination = '' | 'i' | 'g' | 'm' | 's' | 'u' | 'y' | 'ig' | 'im' | 'is' | 'iu' | 'iy' | 'gm' | 'gs' | 'gu' | 'gy' | 'ms' | 'mu' | 'my' | 'su' | 'sy' | 'uy' | 'igm' | 'igs' | 'igu' | 'igy' | 'ims' | 'imu' | 'imy' | 'isu' | 'isy' | 'iuy' | 'gms' | 'gmu' | 'gmy' | 'gsu' | 'gsy' | 'guy' | 'msu' | 'msy' | 'muy' | 'suy' | 'igms' | 'igmu' | 'igmy' | 'igsu' | 'igsy' | 'iguy' | 'imsu' | 'imsy' | 'imuy' | 'isuy' | 'gmsu' | 'gmsy' | 'gmuy' | 'gsuy' | 'msuy' | 'igmsu' | 'igmsy' | 'igmuy' | 'igsuy' | 'imsuy' | 'gmsuy' | 'igmsuy';
|
|
2
|
-
type Types = "double" | "string" | "object" | "array" | "binData" | "undefined" | "objectId" | "bool" | "date" | "null" | "regex" | "dbPointer" | "javascript" | "symbol" | "int" | "timestamp" | "long" | "decimal" | "minKey" | "maxKey";
|
|
3
|
-
interface MatchOperators {
|
|
4
|
-
eq: (value: string | number) => MatchOperators;
|
|
5
|
-
ne: (value: string | number) => MatchOperators;
|
|
6
|
-
gte: (value: number) => MatchOperators;
|
|
7
|
-
gt: (value: number) => MatchOperators;
|
|
8
|
-
lte: (value: number) => MatchOperators;
|
|
9
|
-
lt: (value: number) => MatchOperators;
|
|
10
|
-
nin: (value: (number | string)[]) => MatchOperators;
|
|
11
|
-
in: (value: (number | string)[]) => MatchOperators;
|
|
12
|
-
and: (value: {
|
|
13
|
-
[key: string]: number | string | boolean | MatchOperators;
|
|
14
|
-
}[]) => MatchOperators;
|
|
15
|
-
or: (value: {
|
|
16
|
-
[key: string]: number | string | boolean | MatchOperators;
|
|
17
|
-
}[]) => MatchOperators;
|
|
18
|
-
not: (value: {
|
|
19
|
-
[key: string]: number | string | boolean | MatchOperators;
|
|
20
|
-
}) => MatchOperators;
|
|
21
|
-
nor: (value: {
|
|
22
|
-
[key: string]: number | string | boolean | MatchOperators;
|
|
23
|
-
}[]) => MatchOperators;
|
|
24
|
-
elemMatch: (value: {
|
|
25
|
-
[key: string]: number | string | boolean | MatchOperators;
|
|
26
|
-
}) => MatchOperators;
|
|
27
|
-
all: (value: (number | string | boolean)[]) => MatchOperators;
|
|
28
|
-
size: (value: number) => MatchOperators;
|
|
29
|
-
exist: (value: boolean) => MatchOperators;
|
|
30
|
-
type: (value: Types) => MatchOperators;
|
|
31
|
-
regex: (value: RegExp, option?: FlagCombination) => MatchOperators;
|
|
32
|
-
mod: (number: number, devisor: number) => MatchOperators;
|
|
33
|
-
expr: (value: MatchOperators) => MatchOperators;
|
|
34
|
-
}
|
|
35
|
-
interface ProjectOperators {
|
|
36
|
-
add: (...args: (number | `$${string}`)[]) => ProjectOperators;
|
|
37
|
-
cond: ($if: (operators: MatchOperators) => unknown, $then: (operators: MatchOperators) => unknown, $else: (operators: MatchOperators) => unknown) => ProjectOperators;
|
|
38
|
-
}
|
package/dist/modules/types/db.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|