@aeriajs/core 0.0.248 → 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.
@@ -1,5 +1,4 @@
1
- import type { WithId } from 'mongodb';
2
- import type { Context, Description, Pagination, GetAllPayload, CountReturnType } from '@aeriajs/types';
3
- export declare const makePagination: <T>(payload: GetAllPayload<WithId<T>>, documents: unknown[], context: Context<Description, {
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, undefined | {
4
3
  count?: (...args: unknown[]) => Promise<CountReturnType>;
5
4
  }>) => Promise<Pagination>;
package/dist/context.js CHANGED
@@ -65,6 +65,10 @@ const indepthCollection = (collectionName, collections, parentContext) => {
65
65
  });
66
66
  return {
67
67
  ...collection,
68
+ context: () => (0, exports.createContext)({
69
+ parentContext,
70
+ collectionName,
71
+ }),
68
72
  functions: proxiedFunctions,
69
73
  originalFunctions: collection.functions,
70
74
  model: (0, database_js_1.getDatabaseCollection)(collectionName),
package/dist/context.mjs CHANGED
@@ -28,6 +28,10 @@ const indepthCollection = (collectionName, collections, parentContext) => {
28
28
  });
29
29
  return {
30
30
  ...collection,
31
+ context: () => createContext({
32
+ parentContext,
33
+ collectionName
34
+ }),
31
35
  functions: proxiedFunctions,
32
36
  originalFunctions: collection.functions,
33
37
  model: getDatabaseCollection(collectionName)
@@ -1,7 +1,4 @@
1
- import type { Context, SchemaWithId, GetAllPayload, GetAllReturnType } from '@aeriajs/types';
2
- export type GetAllOptions = {
3
- bypassSecurity?: boolean;
4
- noDefaultLimit?: boolean;
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>>>;
@@ -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 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 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 context.error(types_1.HTTPStatus.Forbidden, {
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 (0, security_1.applyReadMiddlewares)(securedPayload, context, (payload, context) => {
121
- return internalGetAll(payload, context, options);
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 { 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 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 context.error(HTTPStatus.Forbidden, {
114
- code: error
115
- });
116
- }
117
- if (!options.noDefaultLimit) {
118
- securedPayload.limit ||= context.config.defaultPaginationLimit;
8
+ return Result.error(error);
119
9
  }
120
- return applyReadMiddlewares(securedPayload, context, (payload2, context2) => {
121
- return internalGetAll(payload2, context2, options);
10
+ return Result.result({
11
+ data: result,
12
+ pagination: await makePagination(payload, result, context)
122
13
  });
123
14
  };
@@ -5,4 +5,5 @@ export * from './insert.js';
5
5
  export * from './remove.js';
6
6
  export * from './removeAll.js';
7
7
  export * from './removeFile.js';
8
+ export * from './unpaginatedGetAll.js';
8
9
  export { upload, } from './upload.js';
@@ -22,5 +22,6 @@ __exportStar(require("./insert.js"), exports);
22
22
  __exportStar(require("./remove.js"), exports);
23
23
  __exportStar(require("./removeAll.js"), exports);
24
24
  __exportStar(require("./removeFile.js"), exports);
25
+ __exportStar(require("./unpaginatedGetAll.js"), exports);
25
26
  var upload_js_1 = require("./upload.js");
26
27
  Object.defineProperty(exports, "upload", { enumerable: true, get: function () { return upload_js_1.upload; } });
@@ -6,6 +6,7 @@ export * from "./insert.mjs";
6
6
  export * from "./remove.mjs";
7
7
  export * from "./removeAll.mjs";
8
8
  export * from "./removeFile.mjs";
9
+ export * from "./unpaginatedGetAll.mjs";
9
10
  export {
10
11
  upload
11
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.248",
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.248",
46
- "@aeriajs/common": "^0.0.137",
47
- "@aeriajs/entrypoint": "^0.0.141",
48
- "@aeriajs/http": "^0.0.166",
49
- "@aeriajs/security": "^0.0.248",
50
- "@aeriajs/types": "^0.0.119",
51
- "@aeriajs/validation": "^0.0.153"
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",