@aeriajs/core 0.0.249 → 0.0.250
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/collection/makePagination.d.ts +1 -1
- package/dist/functions/getAll.d.ts +4 -7
- package/dist/functions/getAll.js +8 -115
- package/dist/functions/getAll.mjs +9 -118
- package/dist/functions/index.d.ts +1 -1
- package/dist/functions/index.js +1 -1
- package/dist/functions/index.mjs +1 -1
- package/dist/functions/unpaginatedGetAll.d.ts +7 -0
- package/dist/functions/unpaginatedGetAll.js +124 -0
- package/dist/functions/unpaginatedGetAll.mjs +123 -0
- package/package.json +8 -8
- package/dist/functions/paginatedGetAll.d.ts +0 -53
- package/dist/functions/paginatedGetAll.js +0 -17
- package/dist/functions/paginatedGetAll.mjs +0 -14
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { Context, SchemaWithId, Description, Pagination, GetAllPayload, CountReturnType } from '@aeriajs/types';
|
|
2
|
-
export declare const makePagination: <TDescription extends Description>(payload: GetAllPayload<SchemaWithId<TDescription>>, documents: unknown[], context: Context<Description, {
|
|
2
|
+
export declare const makePagination: <TDescription extends Description>(payload: GetAllPayload<SchemaWithId<TDescription>>, documents: unknown[], context: Context<Description, undefined | {
|
|
3
3
|
count?: (...args: unknown[]) => Promise<CountReturnType>;
|
|
4
4
|
}>) => Promise<Pagination>;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import type
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
noRegExpEscaping?: boolean;
|
|
6
|
-
};
|
|
7
|
-
export declare const getAll: <TContext extends Context>(payload: GetAllPayload<SchemaWithId<TContext["description"]>> | undefined, context: TContext, options?: GetAllOptions) => Promise<GetAllReturnType<SchemaWithId<TContext["description"]>>>;
|
|
1
|
+
import { type Description, type GetAllPayload, type SchemaWithId, type Context, type CountReturnType, type GetAllReturnType } from '@aeriajs/types';
|
|
2
|
+
export declare const getAll: <TDescription extends Description, TFunctions>(payload: GetAllPayload<SchemaWithId<TDescription>>, context: Context<TDescription, undefined | (TFunctions & {
|
|
3
|
+
count?: (...args: unknown[]) => Promise<CountReturnType>;
|
|
4
|
+
})>) => Promise<GetAllReturnType<SchemaWithId<TDescription>>>;
|
package/dist/functions/getAll.js
CHANGED
|
@@ -1,124 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getAll = void 0;
|
|
4
|
-
const security_1 = require("@aeriajs/security");
|
|
5
4
|
const types_1 = require("@aeriajs/types");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const {
|
|
10
|
-
const filters = payload.filters
|
|
11
|
-
? Object.assign({}, payload.filters)
|
|
12
|
-
: {};
|
|
13
|
-
const $text = payload.filters && '$text' in payload.filters
|
|
14
|
-
? payload.filters.$text
|
|
15
|
-
: undefined;
|
|
16
|
-
if ('$text' in filters) {
|
|
17
|
-
delete filters.$text;
|
|
18
|
-
}
|
|
19
|
-
const pipeline = [];
|
|
20
|
-
const refMap = await (0, index_js_1.getReferences)(context.description.properties, {
|
|
21
|
-
memoize: context.description.$id,
|
|
22
|
-
});
|
|
23
|
-
if ($text) {
|
|
24
|
-
pipeline.push({
|
|
25
|
-
$match: {
|
|
26
|
-
$text,
|
|
27
|
-
},
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
const preferredSort = sort
|
|
31
|
-
? sort
|
|
32
|
-
: context.description.timestamps !== false
|
|
33
|
-
? {
|
|
34
|
-
_id: -1,
|
|
35
|
-
}
|
|
36
|
-
: null;
|
|
37
|
-
if (preferredSort) {
|
|
38
|
-
pipeline.push({
|
|
39
|
-
$sort: preferredSort,
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
const { error: filtersError, result: traversedFilters } = await (0, index_js_1.traverseDocument)(filters, context.description, {
|
|
43
|
-
autoCast: true,
|
|
44
|
-
allowOperators: true,
|
|
45
|
-
noRegExpEscaping: options.noRegExpEscaping,
|
|
46
|
-
context,
|
|
47
|
-
});
|
|
48
|
-
if (filtersError) {
|
|
49
|
-
switch (filtersError) {
|
|
50
|
-
case types_1.ACError.InsecureOperator: return context.error(types_1.HTTPStatus.Forbidden, {
|
|
51
|
-
code: filtersError,
|
|
52
|
-
});
|
|
53
|
-
default: throw new Error;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
if (Object.keys(filters).length > 0) {
|
|
57
|
-
pipeline.push({
|
|
58
|
-
$match: traversedFilters,
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
if (offset > 0) {
|
|
62
|
-
pipeline.push({
|
|
63
|
-
$skip: offset,
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
if (limit) {
|
|
67
|
-
pipeline.push({
|
|
68
|
-
$limit: limit,
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
if (project) {
|
|
72
|
-
const projection = (0, index_js_1.normalizeProjection)(project, context.description);
|
|
73
|
-
if (projection) {
|
|
74
|
-
pipeline.push({
|
|
75
|
-
$project: projection,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
pipeline.push(...(0, index_js_1.buildLookupPipeline)(refMap, {
|
|
80
|
-
memoize: context.description.$id,
|
|
81
|
-
project: payload.populate
|
|
82
|
-
? payload.populate
|
|
83
|
-
: project,
|
|
84
|
-
}));
|
|
85
|
-
if (Object.keys(refMap).length > 0 && preferredSort) {
|
|
86
|
-
pipeline.push({
|
|
87
|
-
$sort: preferredSort,
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
const result = await context.collection.model.aggregate(pipeline).toArray();
|
|
91
|
-
const documents = [];
|
|
92
|
-
for (const doc of result) {
|
|
93
|
-
documents.push((0, common_1.throwIfError)(await (0, index_js_1.traverseDocument)(doc, context.description, {
|
|
94
|
-
context,
|
|
95
|
-
getters: true,
|
|
96
|
-
fromProperties: true,
|
|
97
|
-
recurseReferences: true,
|
|
98
|
-
recurseDeep: true,
|
|
99
|
-
})));
|
|
100
|
-
}
|
|
101
|
-
return types_1.Result.result(documents);
|
|
102
|
-
};
|
|
103
|
-
const getAll = async (payload, context, options = {}) => {
|
|
104
|
-
if (!payload) {
|
|
105
|
-
return internalGetAll({}, context, options);
|
|
106
|
-
}
|
|
107
|
-
if (options.bypassSecurity) {
|
|
108
|
-
return internalGetAll(payload, context, options);
|
|
109
|
-
}
|
|
110
|
-
const security = (0, security_1.useSecurity)(context);
|
|
111
|
-
const { error, result: securedPayload } = await security.secureReadPayload(payload);
|
|
5
|
+
const makePagination_js_1 = require("../collection/makePagination.js");
|
|
6
|
+
const unpaginatedGetAll_js_1 = require("./unpaginatedGetAll.js");
|
|
7
|
+
const getAll = async (payload, context) => {
|
|
8
|
+
const { error, result } = await (0, unpaginatedGetAll_js_1.unpaginatedGetAll)(payload, context);
|
|
112
9
|
if (error) {
|
|
113
|
-
return
|
|
114
|
-
code: error,
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
if (!options.noDefaultLimit) {
|
|
118
|
-
securedPayload.limit ||= context.config.defaultPaginationLimit;
|
|
10
|
+
return types_1.Result.error(error);
|
|
119
11
|
}
|
|
120
|
-
return
|
|
121
|
-
|
|
12
|
+
return types_1.Result.result({
|
|
13
|
+
data: result,
|
|
14
|
+
pagination: await (0, makePagination_js_1.makePagination)(payload, result, context),
|
|
122
15
|
});
|
|
123
16
|
};
|
|
124
17
|
exports.getAll = getAll;
|
|
@@ -1,123 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
normalizeProjection,
|
|
8
|
-
getReferences,
|
|
9
|
-
buildLookupPipeline
|
|
10
|
-
} from "../collection/index.mjs";
|
|
11
|
-
const internalGetAll = async (payload, context, options) => {
|
|
12
|
-
const {
|
|
13
|
-
limit,
|
|
14
|
-
sort,
|
|
15
|
-
project,
|
|
16
|
-
offset = 0
|
|
17
|
-
} = payload;
|
|
18
|
-
const filters = payload.filters ? Object.assign({}, payload.filters) : {};
|
|
19
|
-
const $text = payload.filters && "$text" in payload.filters ? payload.filters.$text : void 0;
|
|
20
|
-
if ("$text" in filters) {
|
|
21
|
-
delete filters.$text;
|
|
22
|
-
}
|
|
23
|
-
const pipeline = [];
|
|
24
|
-
const refMap = await getReferences(context.description.properties, {
|
|
25
|
-
memoize: context.description.$id
|
|
26
|
-
});
|
|
27
|
-
if ($text) {
|
|
28
|
-
pipeline.push({
|
|
29
|
-
$match: {
|
|
30
|
-
$text
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
const preferredSort = sort ? sort : context.description.timestamps !== false ? {
|
|
35
|
-
_id: -1
|
|
36
|
-
} : null;
|
|
37
|
-
if (preferredSort) {
|
|
38
|
-
pipeline.push({
|
|
39
|
-
$sort: preferredSort
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
const { error: filtersError, result: traversedFilters } = await traverseDocument(filters, context.description, {
|
|
43
|
-
autoCast: true,
|
|
44
|
-
allowOperators: true,
|
|
45
|
-
noRegExpEscaping: options.noRegExpEscaping,
|
|
46
|
-
context
|
|
47
|
-
});
|
|
48
|
-
if (filtersError) {
|
|
49
|
-
switch (filtersError) {
|
|
50
|
-
case ACError.InsecureOperator:
|
|
51
|
-
return context.error(HTTPStatus.Forbidden, {
|
|
52
|
-
code: filtersError
|
|
53
|
-
});
|
|
54
|
-
default:
|
|
55
|
-
throw new Error();
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
if (Object.keys(filters).length > 0) {
|
|
59
|
-
pipeline.push({
|
|
60
|
-
$match: traversedFilters
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
if (offset > 0) {
|
|
64
|
-
pipeline.push({
|
|
65
|
-
$skip: offset
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
if (limit) {
|
|
69
|
-
pipeline.push({
|
|
70
|
-
$limit: limit
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
if (project) {
|
|
74
|
-
const projection = normalizeProjection(project, context.description);
|
|
75
|
-
if (projection) {
|
|
76
|
-
pipeline.push({
|
|
77
|
-
$project: projection
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
pipeline.push(...buildLookupPipeline(refMap, {
|
|
82
|
-
memoize: context.description.$id,
|
|
83
|
-
project: payload.populate ? payload.populate : project
|
|
84
|
-
}));
|
|
85
|
-
if (Object.keys(refMap).length > 0 && preferredSort) {
|
|
86
|
-
pipeline.push({
|
|
87
|
-
$sort: preferredSort
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
const result = await context.collection.model.aggregate(pipeline).toArray();
|
|
91
|
-
const documents = [];
|
|
92
|
-
for (const doc of result) {
|
|
93
|
-
documents.push(throwIfError(await traverseDocument(doc, context.description, {
|
|
94
|
-
context,
|
|
95
|
-
getters: true,
|
|
96
|
-
fromProperties: true,
|
|
97
|
-
recurseReferences: true,
|
|
98
|
-
recurseDeep: true
|
|
99
|
-
})));
|
|
100
|
-
}
|
|
101
|
-
return Result.result(documents);
|
|
102
|
-
};
|
|
103
|
-
export const getAll = async (payload, context, options = {}) => {
|
|
104
|
-
if (!payload) {
|
|
105
|
-
return internalGetAll({}, context, options);
|
|
106
|
-
}
|
|
107
|
-
if (options.bypassSecurity) {
|
|
108
|
-
return internalGetAll(payload, context, options);
|
|
109
|
-
}
|
|
110
|
-
const security = useSecurity(context);
|
|
111
|
-
const { error, result: securedPayload } = await security.secureReadPayload(payload);
|
|
2
|
+
import { Result } from "@aeriajs/types";
|
|
3
|
+
import { makePagination } from "../collection/makePagination.mjs";
|
|
4
|
+
import { unpaginatedGetAll } from "./unpaginatedGetAll.mjs";
|
|
5
|
+
export const getAll = async (payload, context) => {
|
|
6
|
+
const { error, result } = await unpaginatedGetAll(payload, context);
|
|
112
7
|
if (error) {
|
|
113
|
-
return
|
|
114
|
-
code: error
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
if (!options.noDefaultLimit) {
|
|
118
|
-
securedPayload.limit ||= context.config.defaultPaginationLimit;
|
|
8
|
+
return Result.error(error);
|
|
119
9
|
}
|
|
120
|
-
return
|
|
121
|
-
|
|
10
|
+
return Result.result({
|
|
11
|
+
data: result,
|
|
12
|
+
pagination: await makePagination(payload, result, context)
|
|
122
13
|
});
|
|
123
14
|
};
|
|
@@ -2,8 +2,8 @@ export * from './count.js';
|
|
|
2
2
|
export * from './get.js';
|
|
3
3
|
export * from './getAll.js';
|
|
4
4
|
export * from './insert.js';
|
|
5
|
-
export * from './paginatedGetAll.js';
|
|
6
5
|
export * from './remove.js';
|
|
7
6
|
export * from './removeAll.js';
|
|
8
7
|
export * from './removeFile.js';
|
|
8
|
+
export * from './unpaginatedGetAll.js';
|
|
9
9
|
export { upload, } from './upload.js';
|
package/dist/functions/index.js
CHANGED
|
@@ -19,9 +19,9 @@ __exportStar(require("./count.js"), exports);
|
|
|
19
19
|
__exportStar(require("./get.js"), exports);
|
|
20
20
|
__exportStar(require("./getAll.js"), exports);
|
|
21
21
|
__exportStar(require("./insert.js"), exports);
|
|
22
|
-
__exportStar(require("./paginatedGetAll.js"), exports);
|
|
23
22
|
__exportStar(require("./remove.js"), exports);
|
|
24
23
|
__exportStar(require("./removeAll.js"), exports);
|
|
25
24
|
__exportStar(require("./removeFile.js"), exports);
|
|
25
|
+
__exportStar(require("./unpaginatedGetAll.js"), exports);
|
|
26
26
|
var upload_js_1 = require("./upload.js");
|
|
27
27
|
Object.defineProperty(exports, "upload", { enumerable: true, get: function () { return upload_js_1.upload; } });
|
package/dist/functions/index.mjs
CHANGED
|
@@ -3,10 +3,10 @@ export * from "./count.mjs";
|
|
|
3
3
|
export * from "./get.mjs";
|
|
4
4
|
export * from "./getAll.mjs";
|
|
5
5
|
export * from "./insert.mjs";
|
|
6
|
-
export * from "./paginatedGetAll.mjs";
|
|
7
6
|
export * from "./remove.mjs";
|
|
8
7
|
export * from "./removeAll.mjs";
|
|
9
8
|
export * from "./removeFile.mjs";
|
|
9
|
+
export * from "./unpaginatedGetAll.mjs";
|
|
10
10
|
export {
|
|
11
11
|
upload
|
|
12
12
|
} from "./upload.mjs";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Context, SchemaWithId, GetAllPayload, UnpaginatedGetAllReturnType } from '@aeriajs/types';
|
|
2
|
+
export type GetAllOptions = {
|
|
3
|
+
bypassSecurity?: boolean;
|
|
4
|
+
noDefaultLimit?: boolean;
|
|
5
|
+
noRegExpEscaping?: boolean;
|
|
6
|
+
};
|
|
7
|
+
export declare const unpaginatedGetAll: <TContext extends Context>(payload: GetAllPayload<SchemaWithId<TContext["description"]>> | undefined, context: TContext, options?: GetAllOptions) => Promise<UnpaginatedGetAllReturnType<SchemaWithId<TContext["description"]>>>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.unpaginatedGetAll = void 0;
|
|
4
|
+
const security_1 = require("@aeriajs/security");
|
|
5
|
+
const types_1 = require("@aeriajs/types");
|
|
6
|
+
const common_1 = require("@aeriajs/common");
|
|
7
|
+
const index_js_1 = require("../collection/index.js");
|
|
8
|
+
const internalGetAll = async (payload, context, options) => {
|
|
9
|
+
const { limit, sort, project, offset = 0, } = payload;
|
|
10
|
+
const filters = payload.filters
|
|
11
|
+
? Object.assign({}, payload.filters)
|
|
12
|
+
: {};
|
|
13
|
+
const $text = payload.filters && '$text' in payload.filters
|
|
14
|
+
? payload.filters.$text
|
|
15
|
+
: undefined;
|
|
16
|
+
if ('$text' in filters) {
|
|
17
|
+
delete filters.$text;
|
|
18
|
+
}
|
|
19
|
+
const pipeline = [];
|
|
20
|
+
const refMap = await (0, index_js_1.getReferences)(context.description.properties, {
|
|
21
|
+
memoize: context.description.$id,
|
|
22
|
+
});
|
|
23
|
+
if ($text) {
|
|
24
|
+
pipeline.push({
|
|
25
|
+
$match: {
|
|
26
|
+
$text,
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
const preferredSort = sort
|
|
31
|
+
? sort
|
|
32
|
+
: context.description.timestamps !== false
|
|
33
|
+
? {
|
|
34
|
+
_id: -1,
|
|
35
|
+
}
|
|
36
|
+
: null;
|
|
37
|
+
if (preferredSort) {
|
|
38
|
+
pipeline.push({
|
|
39
|
+
$sort: preferredSort,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
const { error: filtersError, result: traversedFilters } = await (0, index_js_1.traverseDocument)(filters, context.description, {
|
|
43
|
+
autoCast: true,
|
|
44
|
+
allowOperators: true,
|
|
45
|
+
noRegExpEscaping: options.noRegExpEscaping,
|
|
46
|
+
context,
|
|
47
|
+
});
|
|
48
|
+
if (filtersError) {
|
|
49
|
+
switch (filtersError) {
|
|
50
|
+
case types_1.ACError.InsecureOperator: return context.error(types_1.HTTPStatus.Forbidden, {
|
|
51
|
+
code: filtersError,
|
|
52
|
+
});
|
|
53
|
+
default: throw new Error;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (Object.keys(filters).length > 0) {
|
|
57
|
+
pipeline.push({
|
|
58
|
+
$match: traversedFilters,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
if (offset > 0) {
|
|
62
|
+
pipeline.push({
|
|
63
|
+
$skip: offset,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (limit) {
|
|
67
|
+
pipeline.push({
|
|
68
|
+
$limit: limit,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
if (project) {
|
|
72
|
+
const projection = (0, index_js_1.normalizeProjection)(project, context.description);
|
|
73
|
+
if (projection) {
|
|
74
|
+
pipeline.push({
|
|
75
|
+
$project: projection,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
pipeline.push(...(0, index_js_1.buildLookupPipeline)(refMap, {
|
|
80
|
+
memoize: context.description.$id,
|
|
81
|
+
project: payload.populate
|
|
82
|
+
? payload.populate
|
|
83
|
+
: project,
|
|
84
|
+
}));
|
|
85
|
+
if (Object.keys(refMap).length > 0 && preferredSort) {
|
|
86
|
+
pipeline.push({
|
|
87
|
+
$sort: preferredSort,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
const result = await context.collection.model.aggregate(pipeline).toArray();
|
|
91
|
+
const documents = [];
|
|
92
|
+
for (const doc of result) {
|
|
93
|
+
documents.push((0, common_1.throwIfError)(await (0, index_js_1.traverseDocument)(doc, context.description, {
|
|
94
|
+
context,
|
|
95
|
+
getters: true,
|
|
96
|
+
fromProperties: true,
|
|
97
|
+
recurseReferences: true,
|
|
98
|
+
recurseDeep: true,
|
|
99
|
+
})));
|
|
100
|
+
}
|
|
101
|
+
return types_1.Result.result(documents);
|
|
102
|
+
};
|
|
103
|
+
const unpaginatedGetAll = async (payload, context, options = {}) => {
|
|
104
|
+
if (!payload) {
|
|
105
|
+
return internalGetAll({}, context, options);
|
|
106
|
+
}
|
|
107
|
+
if (options.bypassSecurity) {
|
|
108
|
+
return internalGetAll(payload, context, options);
|
|
109
|
+
}
|
|
110
|
+
const security = (0, security_1.useSecurity)(context);
|
|
111
|
+
const { error, result: securedPayload } = await security.secureReadPayload(payload);
|
|
112
|
+
if (error) {
|
|
113
|
+
return context.error(types_1.HTTPStatus.Forbidden, {
|
|
114
|
+
code: error,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
if (!options.noDefaultLimit) {
|
|
118
|
+
securedPayload.limit ||= context.config.defaultPaginationLimit;
|
|
119
|
+
}
|
|
120
|
+
return (0, security_1.applyReadMiddlewares)(securedPayload, context, (payload, context) => {
|
|
121
|
+
return internalGetAll(payload, context, options);
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
exports.unpaginatedGetAll = unpaginatedGetAll;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { useSecurity, applyReadMiddlewares } from "@aeriajs/security";
|
|
3
|
+
import { Result, HTTPStatus, ACError } from "@aeriajs/types";
|
|
4
|
+
import { throwIfError } from "@aeriajs/common";
|
|
5
|
+
import {
|
|
6
|
+
traverseDocument,
|
|
7
|
+
normalizeProjection,
|
|
8
|
+
getReferences,
|
|
9
|
+
buildLookupPipeline
|
|
10
|
+
} from "../collection/index.mjs";
|
|
11
|
+
const internalGetAll = async (payload, context, options) => {
|
|
12
|
+
const {
|
|
13
|
+
limit,
|
|
14
|
+
sort,
|
|
15
|
+
project,
|
|
16
|
+
offset = 0
|
|
17
|
+
} = payload;
|
|
18
|
+
const filters = payload.filters ? Object.assign({}, payload.filters) : {};
|
|
19
|
+
const $text = payload.filters && "$text" in payload.filters ? payload.filters.$text : void 0;
|
|
20
|
+
if ("$text" in filters) {
|
|
21
|
+
delete filters.$text;
|
|
22
|
+
}
|
|
23
|
+
const pipeline = [];
|
|
24
|
+
const refMap = await getReferences(context.description.properties, {
|
|
25
|
+
memoize: context.description.$id
|
|
26
|
+
});
|
|
27
|
+
if ($text) {
|
|
28
|
+
pipeline.push({
|
|
29
|
+
$match: {
|
|
30
|
+
$text
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
const preferredSort = sort ? sort : context.description.timestamps !== false ? {
|
|
35
|
+
_id: -1
|
|
36
|
+
} : null;
|
|
37
|
+
if (preferredSort) {
|
|
38
|
+
pipeline.push({
|
|
39
|
+
$sort: preferredSort
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
const { error: filtersError, result: traversedFilters } = await traverseDocument(filters, context.description, {
|
|
43
|
+
autoCast: true,
|
|
44
|
+
allowOperators: true,
|
|
45
|
+
noRegExpEscaping: options.noRegExpEscaping,
|
|
46
|
+
context
|
|
47
|
+
});
|
|
48
|
+
if (filtersError) {
|
|
49
|
+
switch (filtersError) {
|
|
50
|
+
case ACError.InsecureOperator:
|
|
51
|
+
return context.error(HTTPStatus.Forbidden, {
|
|
52
|
+
code: filtersError
|
|
53
|
+
});
|
|
54
|
+
default:
|
|
55
|
+
throw new Error();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (Object.keys(filters).length > 0) {
|
|
59
|
+
pipeline.push({
|
|
60
|
+
$match: traversedFilters
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
if (offset > 0) {
|
|
64
|
+
pipeline.push({
|
|
65
|
+
$skip: offset
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (limit) {
|
|
69
|
+
pipeline.push({
|
|
70
|
+
$limit: limit
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
if (project) {
|
|
74
|
+
const projection = normalizeProjection(project, context.description);
|
|
75
|
+
if (projection) {
|
|
76
|
+
pipeline.push({
|
|
77
|
+
$project: projection
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
pipeline.push(...buildLookupPipeline(refMap, {
|
|
82
|
+
memoize: context.description.$id,
|
|
83
|
+
project: payload.populate ? payload.populate : project
|
|
84
|
+
}));
|
|
85
|
+
if (Object.keys(refMap).length > 0 && preferredSort) {
|
|
86
|
+
pipeline.push({
|
|
87
|
+
$sort: preferredSort
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
const result = await context.collection.model.aggregate(pipeline).toArray();
|
|
91
|
+
const documents = [];
|
|
92
|
+
for (const doc of result) {
|
|
93
|
+
documents.push(throwIfError(await traverseDocument(doc, context.description, {
|
|
94
|
+
context,
|
|
95
|
+
getters: true,
|
|
96
|
+
fromProperties: true,
|
|
97
|
+
recurseReferences: true,
|
|
98
|
+
recurseDeep: true
|
|
99
|
+
})));
|
|
100
|
+
}
|
|
101
|
+
return Result.result(documents);
|
|
102
|
+
};
|
|
103
|
+
export const unpaginatedGetAll = async (payload, context, options = {}) => {
|
|
104
|
+
if (!payload) {
|
|
105
|
+
return internalGetAll({}, context, options);
|
|
106
|
+
}
|
|
107
|
+
if (options.bypassSecurity) {
|
|
108
|
+
return internalGetAll(payload, context, options);
|
|
109
|
+
}
|
|
110
|
+
const security = useSecurity(context);
|
|
111
|
+
const { error, result: securedPayload } = await security.secureReadPayload(payload);
|
|
112
|
+
if (error) {
|
|
113
|
+
return context.error(HTTPStatus.Forbidden, {
|
|
114
|
+
code: error
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
if (!options.noDefaultLimit) {
|
|
118
|
+
securedPayload.limit ||= context.config.defaultPaginationLimit;
|
|
119
|
+
}
|
|
120
|
+
return applyReadMiddlewares(securedPayload, context, (payload2, context2) => {
|
|
121
|
+
return internalGetAll(payload2, context2, options);
|
|
122
|
+
});
|
|
123
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.250",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"aeriaMain": "tests/fixtures/aeriaMain.js",
|
|
@@ -42,13 +42,13 @@
|
|
|
42
42
|
"mongodb-memory-server": "^9.2.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@aeriajs/builtins": "^0.0.
|
|
46
|
-
"@aeriajs/common": "^0.0.
|
|
47
|
-
"@aeriajs/entrypoint": "^0.0.
|
|
48
|
-
"@aeriajs/http": "^0.0.
|
|
49
|
-
"@aeriajs/security": "^0.0.
|
|
50
|
-
"@aeriajs/types": "^0.0.
|
|
51
|
-
"@aeriajs/validation": "^0.0.
|
|
45
|
+
"@aeriajs/builtins": "^0.0.250",
|
|
46
|
+
"@aeriajs/common": "^0.0.139",
|
|
47
|
+
"@aeriajs/entrypoint": "^0.0.143",
|
|
48
|
+
"@aeriajs/http": "^0.0.168",
|
|
49
|
+
"@aeriajs/security": "^0.0.250",
|
|
50
|
+
"@aeriajs/types": "^0.0.121",
|
|
51
|
+
"@aeriajs/validation": "^0.0.155"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"mongodb": "^6.5.0",
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { Description, GetAllPayload, SchemaWithId, Context, CountReturnType } from '@aeriajs/types';
|
|
2
|
-
export declare const paginatedGetAll: <TDescription extends Description, TFunctions>(payload: GetAllPayload<SchemaWithId<TDescription>>, context: Context<TDescription, TFunctions & {
|
|
3
|
-
count?: (...args: unknown[]) => Promise<CountReturnType>;
|
|
4
|
-
}>) => Promise<{
|
|
5
|
-
readonly _tag: "Error";
|
|
6
|
-
readonly error: Pick<{} & Omit<Readonly<import("@aeriajs/types").FilterReadonlyProperties<{
|
|
7
|
-
readonly httpStatus: {
|
|
8
|
-
readonly enum: [403];
|
|
9
|
-
};
|
|
10
|
-
readonly code: {
|
|
11
|
-
readonly enum: ["OWNERSHIP_ERROR", "INSECURE_OPERATOR", "INVALID_LIMIT"];
|
|
12
|
-
};
|
|
13
|
-
readonly message: {
|
|
14
|
-
readonly type: "string";
|
|
15
|
-
};
|
|
16
|
-
readonly details: {
|
|
17
|
-
readonly type: "object";
|
|
18
|
-
readonly additionalProperties: true;
|
|
19
|
-
};
|
|
20
|
-
}>> & {
|
|
21
|
-
code: "INSECURE_OPERATOR" | "OWNERSHIP_ERROR" | "INVALID_LIMIT";
|
|
22
|
-
httpStatus: 403;
|
|
23
|
-
message: string;
|
|
24
|
-
details: any;
|
|
25
|
-
}, never>, "code" | "httpStatus"> & Partial<{} & Omit<Readonly<import("@aeriajs/types").FilterReadonlyProperties<{
|
|
26
|
-
readonly httpStatus: {
|
|
27
|
-
readonly enum: [403];
|
|
28
|
-
};
|
|
29
|
-
readonly code: {
|
|
30
|
-
readonly enum: ["OWNERSHIP_ERROR", "INSECURE_OPERATOR", "INVALID_LIMIT"];
|
|
31
|
-
};
|
|
32
|
-
readonly message: {
|
|
33
|
-
readonly type: "string";
|
|
34
|
-
};
|
|
35
|
-
readonly details: {
|
|
36
|
-
readonly type: "object";
|
|
37
|
-
readonly additionalProperties: true;
|
|
38
|
-
};
|
|
39
|
-
}>> & {
|
|
40
|
-
code: "INSECURE_OPERATOR" | "OWNERSHIP_ERROR" | "INVALID_LIMIT";
|
|
41
|
-
httpStatus: 403;
|
|
42
|
-
message: string;
|
|
43
|
-
details: any;
|
|
44
|
-
}, never>>;
|
|
45
|
-
readonly result: undefined;
|
|
46
|
-
} | {
|
|
47
|
-
readonly _tag: "Result";
|
|
48
|
-
readonly error: undefined;
|
|
49
|
-
readonly result: {
|
|
50
|
-
readonly data: SchemaWithId<TDescription>[];
|
|
51
|
-
readonly pagination: import("@aeriajs/types").Pagination;
|
|
52
|
-
};
|
|
53
|
-
}>;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.paginatedGetAll = void 0;
|
|
4
|
-
const types_1 = require("@aeriajs/types");
|
|
5
|
-
const makePagination_js_1 = require("../collection/makePagination.js");
|
|
6
|
-
const getAll_js_1 = require("./getAll.js");
|
|
7
|
-
const paginatedGetAll = async (payload, context) => {
|
|
8
|
-
const { error, result } = await (0, getAll_js_1.getAll)(payload, context);
|
|
9
|
-
if (error) {
|
|
10
|
-
return types_1.Result.error(error);
|
|
11
|
-
}
|
|
12
|
-
return types_1.Result.result({
|
|
13
|
-
data: result,
|
|
14
|
-
pagination: await (0, makePagination_js_1.makePagination)(payload, result, context),
|
|
15
|
-
});
|
|
16
|
-
};
|
|
17
|
-
exports.paginatedGetAll = paginatedGetAll;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
import { Result } from "@aeriajs/types";
|
|
3
|
-
import { makePagination } from "../collection/makePagination.mjs";
|
|
4
|
-
import { getAll } from "./getAll.mjs";
|
|
5
|
-
export const paginatedGetAll = async (payload, context) => {
|
|
6
|
-
const { error, result } = await getAll(payload, context);
|
|
7
|
-
if (error) {
|
|
8
|
-
return Result.error(error);
|
|
9
|
-
}
|
|
10
|
-
return Result.result({
|
|
11
|
-
data: result,
|
|
12
|
-
pagination: await makePagination(payload, result, context)
|
|
13
|
-
});
|
|
14
|
-
};
|