@aeriajs/core 0.0.277 → 0.0.279

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.
Files changed (73) hide show
  1. package/dist/__scripts__/postinstall.js +2 -37
  2. package/dist/accessControl.js +14 -18
  3. package/dist/assets.js +25 -31
  4. package/dist/collection/cascadingRemove.js +14 -19
  5. package/dist/collection/define.js +5 -10
  6. package/dist/collection/description.js +2 -7
  7. package/dist/collection/index.js +8 -24
  8. package/dist/collection/makePagination.js +5 -9
  9. package/dist/collection/normalizeProjection.js +1 -5
  10. package/dist/collection/preload.js +13 -18
  11. package/dist/collection/reference.js +19 -26
  12. package/dist/collection/traverseDocument.js +90 -127
  13. package/dist/context.js +16 -53
  14. package/dist/database.js +12 -52
  15. package/dist/endpoints.js +11 -48
  16. package/dist/functions/count.js +11 -15
  17. package/dist/functions/get.js +20 -24
  18. package/dist/functions/getAll.js +8 -12
  19. package/dist/functions/index.js +9 -27
  20. package/dist/functions/insert.js +19 -23
  21. package/dist/functions/remove.js +15 -19
  22. package/dist/functions/removeAll.js +11 -15
  23. package/dist/functions/removeFile.js +7 -11
  24. package/dist/functions/unpaginatedGetAll.js +15 -19
  25. package/dist/functions/upload.js +19 -57
  26. package/dist/index.js +11 -51
  27. package/dist/presets/add.js +1 -4
  28. package/dist/presets/crud.js +1 -4
  29. package/dist/presets/duplicate.js +1 -4
  30. package/dist/presets/index.js +17 -20
  31. package/dist/presets/owned.js +1 -4
  32. package/dist/presets/remove.js +1 -4
  33. package/dist/presets/removeAll.js +1 -4
  34. package/dist/presets/timestamped.js +1 -4
  35. package/dist/presets/view.js +1 -4
  36. package/dist/token.js +7 -15
  37. package/package.json +12 -17
  38. package/dist/__scripts__/postinstall.mjs +0 -50
  39. package/dist/accessControl.mjs +0 -31
  40. package/dist/assets.mjs +0 -67
  41. package/dist/collection/cascadingRemove.mjs +0 -71
  42. package/dist/collection/define.mjs +0 -13
  43. package/dist/collection/description.mjs +0 -8
  44. package/dist/collection/index.mjs +0 -9
  45. package/dist/collection/makePagination.mjs +0 -20
  46. package/dist/collection/normalizeProjection.mjs +0 -17
  47. package/dist/collection/preload.mjs +0 -88
  48. package/dist/collection/reference.mjs +0 -374
  49. package/dist/collection/traverseDocument.mjs +0 -454
  50. package/dist/context.mjs +0 -120
  51. package/dist/database.mjs +0 -49
  52. package/dist/endpoints.mjs +0 -52
  53. package/dist/functions/count.mjs +0 -50
  54. package/dist/functions/get.mjs +0 -89
  55. package/dist/functions/getAll.mjs +0 -14
  56. package/dist/functions/index.mjs +0 -12
  57. package/dist/functions/insert.mjs +0 -102
  58. package/dist/functions/remove.mjs +0 -41
  59. package/dist/functions/removeAll.mjs +0 -40
  60. package/dist/functions/removeFile.mjs +0 -29
  61. package/dist/functions/unpaginatedGetAll.mjs +0 -123
  62. package/dist/functions/upload.mjs +0 -91
  63. package/dist/index.mjs +0 -14
  64. package/dist/presets/add.mjs +0 -12
  65. package/dist/presets/crud.mjs +0 -35
  66. package/dist/presets/duplicate.mjs +0 -11
  67. package/dist/presets/index.mjs +0 -19
  68. package/dist/presets/owned.mjs +0 -9
  69. package/dist/presets/remove.mjs +0 -11
  70. package/dist/presets/removeAll.mjs +0 -11
  71. package/dist/presets/timestamped.mjs +0 -19
  72. package/dist/presets/view.mjs +0 -14
  73. package/dist/token.mjs +0 -31
package/dist/database.js CHANGED
@@ -1,65 +1,28 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.getDatabaseCollection = exports.getDatabaseSync = exports.getDatabase = exports.ObjectId = void 0;
37
- const entrypoint_1 = require("@aeriajs/entrypoint");
38
- const node_util_1 = require("node:util");
39
- const mongodb_1 = require("mongodb");
40
- var mongodb_2 = require("mongodb");
41
- Object.defineProperty(exports, "ObjectId", { enumerable: true, get: function () { return mongodb_2.ObjectId; } });
1
+ import { getConfig } from '@aeriajs/entrypoint';
2
+ import { inspect } from 'node:util';
3
+ import { MongoClient } from 'mongodb';
4
+ export { ObjectId, } from 'mongodb';
42
5
  const dbMemo = {};
43
- const getDatabase = async () => {
6
+ export const getDatabase = async () => {
44
7
  if (!dbMemo.db) {
45
- const config = await (0, entrypoint_1.getConfig)();
8
+ const config = await getConfig();
46
9
  const mongodbUri = await (async () => {
47
10
  const envUri = config.database?.mongodbUrl;
48
11
  if (!envUri) {
49
12
  console.warn("mongo URI wasn't supplied, fallbacking to memory storage (this means your data will only be alive during runtime)");
50
- const { MongoMemoryServer } = await Promise.resolve().then(() => __importStar(require('mongodb-memory-server')));
13
+ const { MongoMemoryServer } = await import('mongodb-memory-server');
51
14
  const mongod = await MongoMemoryServer.create();
52
15
  return mongod.getUri();
53
16
  }
54
17
  return envUri;
55
18
  })();
56
19
  const logQueries = config.database?.logQueries || process.env.NODE_ENV === 'debug';
57
- const client = new mongodb_1.MongoClient(mongodbUri, {
20
+ const client = new MongoClient(mongodbUri, {
58
21
  monitorCommands: logQueries,
59
22
  });
60
23
  if (logQueries) {
61
24
  client.on('commandStarted', (event) => {
62
- console.debug((0, node_util_1.inspect)(event, {
25
+ console.debug(inspect(event, {
63
26
  colors: true,
64
27
  compact: false,
65
28
  depth: Infinity,
@@ -71,16 +34,13 @@ const getDatabase = async () => {
71
34
  }
72
35
  return dbMemo;
73
36
  };
74
- exports.getDatabase = getDatabase;
75
- const getDatabaseSync = () => {
37
+ export const getDatabaseSync = () => {
76
38
  if (!dbMemo.db) {
77
39
  throw new Error('getDatabaseSync() called with no active database');
78
40
  }
79
41
  return dbMemo.db;
80
42
  };
81
- exports.getDatabaseSync = getDatabaseSync;
82
- const getDatabaseCollection = (collectionName) => {
83
- const db = (0, exports.getDatabaseSync)();
43
+ export const getDatabaseCollection = (collectionName) => {
44
+ const db = getDatabaseSync();
84
45
  return db.collection(collectionName);
85
46
  };
86
- exports.getDatabaseCollection = getDatabaseCollection;
package/dist/endpoints.js CHANGED
@@ -1,46 +1,10 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.getEndpoints = void 0;
37
- const entrypoint_1 = require("@aeriajs/entrypoint");
38
- const common_1 = require("@aeriajs/common");
39
- const accessControl_js_1 = require("./accessControl.js");
40
- const builtinFunctions = __importStar(require("./functions/index.js"));
41
- const getEndpoints = async () => {
42
- const router = await (0, entrypoint_1.getRouter)();
43
- const collections = await (0, entrypoint_1.getCollections)();
1
+ import { getCollections, getRouter, getAvailableRoles } from '@aeriajs/entrypoint';
2
+ import { deepMerge } from '@aeriajs/common';
3
+ import { isFunctionExposed, FunctionExposedStatus } from './accessControl.js';
4
+ import * as builtinFunctions from './functions/index.js';
5
+ export const getEndpoints = async () => {
6
+ const router = await getRouter();
7
+ const collections = await getCollections();
44
8
  const functions = {};
45
9
  for (const collectionName in collections) {
46
10
  const candidate = collections[collectionName];
@@ -50,8 +14,8 @@ const getEndpoints = async () => {
50
14
  const { description, functions: collectionFunctions, contracts, exposedFunctions = {}, } = collection;
51
15
  if (collectionFunctions) {
52
16
  for (const fnName in collectionFunctions) {
53
- const exposedStatus = await (0, accessControl_js_1.isFunctionExposed)(collection, fnName);
54
- if (exposedStatus !== accessControl_js_1.FunctionExposedStatus.FunctionAccessible) {
17
+ const exposedStatus = await isFunctionExposed(collection, fnName);
18
+ if (exposedStatus !== FunctionExposedStatus.FunctionAccessible) {
55
19
  continue;
56
20
  }
57
21
  const endpoint = `/${description.$id}/${fnName}`;
@@ -59,7 +23,7 @@ const getEndpoints = async () => {
59
23
  const roles = Array.isArray(exposed)
60
24
  ? exposed
61
25
  : exposed
62
- ? await (0, entrypoint_1.getAvailableRoles)()
26
+ ? await getAvailableRoles()
63
27
  : [];
64
28
  const endpointContracts = {
65
29
  POST: null,
@@ -80,7 +44,6 @@ const getEndpoints = async () => {
80
44
  }
81
45
  }
82
46
  }
83
- const result = (0, common_1.deepMerge)(functions, router?.routesMeta || {});
47
+ const result = deepMerge(functions, router?.routesMeta || {});
84
48
  return result;
85
49
  };
86
- exports.getEndpoints = getEndpoints;
@@ -1,10 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.count = 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");
1
+ import { useSecurity, applyReadMiddlewares } from '@aeriajs/security';
2
+ import { Result, HTTPStatus } from '@aeriajs/types';
3
+ import { throwIfError } from '@aeriajs/common';
4
+ import { traverseDocument } from '../collection/index.js';
8
5
  const internalCount = async (payload, context, options) => {
9
6
  const { filters = {} } = payload;
10
7
  const $text = '$text' in filters
@@ -13,7 +10,7 @@ const internalCount = async (payload, context, options) => {
13
10
  if ('$text' in filters) {
14
11
  delete filters.$text;
15
12
  }
16
- const traversedFilters = (0, common_1.throwIfError)(await (0, index_js_1.traverseDocument)(filters, context.description, {
13
+ const traversedFilters = throwIfError(await traverseDocument(filters, context.description, {
17
14
  autoCast: true,
18
15
  allowOperators: true,
19
16
  noRegExpEscaping: options.noRegExpEscaping,
@@ -33,25 +30,24 @@ const internalCount = async (payload, context, options) => {
33
30
  $count: 'total',
34
31
  });
35
32
  const result = await context.collection.model.aggregate(pipeline).next();
36
- return types_1.Result.result(result
33
+ return Result.result(result
37
34
  ? result.total
38
35
  : 0);
39
36
  }
40
- return types_1.Result.result(await context.collection.model.countDocuments(traversedFilters));
37
+ return Result.result(await context.collection.model.countDocuments(traversedFilters));
41
38
  };
42
- const count = async (payload, context, options = {}) => {
39
+ export const count = async (payload, context, options = {}) => {
43
40
  if (options.bypassSecurity) {
44
41
  return internalCount(payload, context, options);
45
42
  }
46
- const security = (0, security_1.useSecurity)(context);
43
+ const security = useSecurity(context);
47
44
  const { error, result: securedPayload } = await security.secureReadPayload(payload);
48
45
  if (error) {
49
- return context.error(types_1.HTTPStatus.Forbidden, {
46
+ return context.error(HTTPStatus.Forbidden, {
50
47
  code: error,
51
48
  });
52
49
  }
53
- return (0, security_1.applyReadMiddlewares)(securedPayload, context, (payload, context) => {
50
+ return applyReadMiddlewares(securedPayload, context, (payload, context) => {
54
51
  return internalCount(payload, context, options);
55
52
  });
56
53
  };
57
- exports.count = count;
@@ -1,22 +1,19 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.get = 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");
1
+ import { useSecurity, applyReadMiddlewares } from '@aeriajs/security';
2
+ import { Result, HTTPStatus, ACError } from '@aeriajs/types';
3
+ import { throwIfError } from '@aeriajs/common';
4
+ import { traverseDocument, normalizeProjection, getReferences, buildLookupPipeline, } from '../collection/index.js';
8
5
  const internalGet = async (payload, context, options) => {
9
6
  const { filters = {}, project, } = payload;
10
7
  if (Object.keys(filters).length === 0) {
11
- return context.error(types_1.HTTPStatus.BadRequest, {
12
- code: types_1.ACError.MalformedInput,
8
+ return context.error(HTTPStatus.BadRequest, {
9
+ code: ACError.MalformedInput,
13
10
  });
14
11
  }
15
12
  const pipeline = [];
16
- const refMap = await (0, index_js_1.getReferences)(context.description.properties, {
13
+ const refMap = await getReferences(context.description.properties, {
17
14
  memoize: context.description.$id,
18
15
  });
19
- const { error: filtersError, result: traversedFilters } = await (0, index_js_1.traverseDocument)(filters, context.description, {
16
+ const { error: filtersError, result: traversedFilters } = await traverseDocument(filters, context.description, {
20
17
  autoCast: true,
21
18
  allowOperators: true,
22
19
  noRegExpEscaping: options.noRegExpEscaping,
@@ -24,7 +21,7 @@ const internalGet = async (payload, context, options) => {
24
21
  });
25
22
  if (filtersError) {
26
23
  switch (filtersError) {
27
- case types_1.ACError.InsecureOperator: return context.error(types_1.HTTPStatus.Forbidden, {
24
+ case ACError.InsecureOperator: return context.error(HTTPStatus.Forbidden, {
28
25
  code: filtersError,
29
26
  });
30
27
  default: throw new Error;
@@ -34,14 +31,14 @@ const internalGet = async (payload, context, options) => {
34
31
  $match: traversedFilters,
35
32
  });
36
33
  if (project) {
37
- const projection = (0, index_js_1.normalizeProjection)(project, context.description);
34
+ const projection = normalizeProjection(project, context.description);
38
35
  if (projection) {
39
36
  pipeline.push({
40
37
  $project: projection,
41
38
  });
42
39
  }
43
40
  }
44
- pipeline.push(...(0, index_js_1.buildLookupPipeline)(refMap, {
41
+ pipeline.push(...buildLookupPipeline(refMap, {
45
42
  memoize: context.description.$id,
46
43
  project: payload.populate
47
44
  ? payload.populate
@@ -49,35 +46,34 @@ const internalGet = async (payload, context, options) => {
49
46
  }));
50
47
  const doc = await context.collection.model.aggregate(pipeline).next();
51
48
  if (!doc) {
52
- return context.error(types_1.HTTPStatus.NotFound, {
53
- code: types_1.ACError.ResourceNotFound,
49
+ return context.error(HTTPStatus.NotFound, {
50
+ code: ACError.ResourceNotFound,
54
51
  });
55
52
  }
56
- const result = (0, common_1.throwIfError)(await (0, index_js_1.traverseDocument)(doc, context.description, {
53
+ const result = throwIfError(await traverseDocument(doc, context.description, {
57
54
  context,
58
55
  getters: true,
59
56
  fromProperties: true,
60
57
  recurseReferences: true,
61
58
  recurseDeep: true,
62
59
  }));
63
- return types_1.Result.result(result);
60
+ return Result.result(result);
64
61
  };
65
- const get = async (payload, context, options = {}) => {
62
+ export const get = async (payload, context, options = {}) => {
66
63
  if (options.bypassSecurity) {
67
64
  return internalGet(payload, context, options);
68
65
  }
69
- const security = (0, security_1.useSecurity)(context);
66
+ const security = useSecurity(context);
70
67
  const { error, result: securedPayload } = await security.secureReadPayload(payload);
71
68
  if (error) {
72
69
  switch (error) {
73
- case types_1.ACError.InvalidLimit: throw new Error;
70
+ case ACError.InvalidLimit: throw new Error;
74
71
  }
75
- return context.error(types_1.HTTPStatus.Forbidden, {
72
+ return context.error(HTTPStatus.Forbidden, {
76
73
  code: error,
77
74
  });
78
75
  }
79
- return (0, security_1.applyReadMiddlewares)(securedPayload, context, (payload, context) => {
76
+ return applyReadMiddlewares(securedPayload, context, (payload, context) => {
80
77
  return internalGet(payload, context, options);
81
78
  });
82
79
  };
83
- exports.get = get;
@@ -1,17 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getAll = void 0;
4
- const types_1 = require("@aeriajs/types");
5
- const makePagination_js_1 = require("../collection/makePagination.js");
6
- const unpaginatedGetAll_js_1 = require("./unpaginatedGetAll.js");
7
- const getAll = async (payload, context, options) => {
8
- const { error, result } = await (0, unpaginatedGetAll_js_1.unpaginatedGetAll)(payload, context, options);
1
+ import { Result } from '@aeriajs/types';
2
+ import { makePagination } from '../collection/makePagination.js';
3
+ import { unpaginatedGetAll } from './unpaginatedGetAll.js';
4
+ export const getAll = async (payload, context, options) => {
5
+ const { error, result } = await unpaginatedGetAll(payload, context, options);
9
6
  if (error) {
10
- return types_1.Result.error(error);
7
+ return Result.error(error);
11
8
  }
12
- return types_1.Result.result({
9
+ return Result.result({
13
10
  data: result,
14
- pagination: await (0, makePagination_js_1.makePagination)(payload, result, context),
11
+ pagination: await makePagination(payload, result, context),
15
12
  });
16
13
  };
17
- exports.getAll = getAll;
@@ -1,27 +1,9 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.upload = void 0;
18
- __exportStar(require("./count.js"), exports);
19
- __exportStar(require("./get.js"), exports);
20
- __exportStar(require("./getAll.js"), exports);
21
- __exportStar(require("./insert.js"), exports);
22
- __exportStar(require("./remove.js"), exports);
23
- __exportStar(require("./removeAll.js"), exports);
24
- __exportStar(require("./removeFile.js"), exports);
25
- __exportStar(require("./unpaginatedGetAll.js"), exports);
26
- var upload_js_1 = require("./upload.js");
27
- Object.defineProperty(exports, "upload", { enumerable: true, get: function () { return upload_js_1.upload; } });
1
+ export * from './count.js';
2
+ export * from './get.js';
3
+ export * from './getAll.js';
4
+ export * from './insert.js';
5
+ export * from './remove.js';
6
+ export * from './removeAll.js';
7
+ export * from './removeFile.js';
8
+ export * from './unpaginatedGetAll.js';
9
+ export { upload, } from './upload.js';
@@ -1,11 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.insert = void 0;
4
- const mongodb_1 = require("mongodb");
5
- const types_1 = require("@aeriajs/types");
6
- const security_1 = require("@aeriajs/security");
7
- const index_js_1 = require("../collection/index.js");
8
- const get_js_1 = require("./get.js");
1
+ import { ObjectId, MongoServerError } from 'mongodb';
2
+ import { Result, HTTPStatus, ACError } from '@aeriajs/types';
3
+ import { useSecurity, applyWriteMiddlewares } from '@aeriajs/security';
4
+ import { traverseDocument } from '../collection/index.js';
5
+ import { get } from './get.js';
9
6
  const prepareCreate = (doc, description) => {
10
7
  const result = {};
11
8
  if (description.defaults) {
@@ -16,7 +13,7 @@ const prepareCreate = (doc, description) => {
16
13
  };
17
14
  const internalInsert = async (payload, context) => {
18
15
  const isUpdate = !!('_id' in payload.what && payload.what._id);
19
- const { error, result: what } = await (0, index_js_1.traverseDocument)(payload.what, context.description, {
16
+ const { error, result: what } = await traverseDocument(payload.what, context.description, {
20
17
  recurseDeep: true,
21
18
  autoCast: true,
22
19
  moveFiles: true,
@@ -32,16 +29,16 @@ const internalInsert = async (payload, context) => {
32
29
  });
33
30
  if (error) {
34
31
  if (typeof error === 'string') {
35
- return context.error(types_1.HTTPStatus.UnprocessableContent, {
32
+ return context.error(HTTPStatus.UnprocessableContent, {
36
33
  code: error,
37
34
  });
38
35
  }
39
- return context.error(types_1.HTTPStatus.UnprocessableContent, {
36
+ return context.error(HTTPStatus.UnprocessableContent, {
40
37
  code: error.code,
41
38
  details: error.details,
42
39
  });
43
40
  }
44
- const docId = '_id' in what && what._id instanceof mongodb_1.ObjectId
41
+ const docId = '_id' in what && what._id instanceof ObjectId
45
42
  ? what._id
46
43
  : null;
47
44
  let newId = docId;
@@ -67,10 +64,10 @@ const internalInsert = async (payload, context) => {
67
64
  }
68
65
  }
69
66
  catch (err) {
70
- if (err instanceof mongodb_1.MongoServerError) {
67
+ if (err instanceof MongoServerError) {
71
68
  switch (err.code) {
72
- case 11000: return context.error(types_1.HTTPStatus.InternalServerError, {
73
- code: types_1.ACError.UniquenessViolated,
69
+ case 11000: return context.error(HTTPStatus.InternalServerError, {
70
+ code: ACError.UniquenessViolated,
74
71
  });
75
72
  default: throw err;
76
73
  }
@@ -81,7 +78,7 @@ const internalInsert = async (payload, context) => {
81
78
  ...context,
82
79
  inherited: true,
83
80
  };
84
- const { error: getError, result: newDocument } = await (0, get_js_1.get)({
81
+ const { error: getError, result: newDocument } = await get({
85
82
  filters: {
86
83
  _id: newId,
87
84
  },
@@ -89,21 +86,20 @@ const internalInsert = async (payload, context) => {
89
86
  bypassSecurity: true,
90
87
  });
91
88
  if (getError) {
92
- return types_1.Result.error(getError);
89
+ return Result.error(getError);
93
90
  }
94
- return types_1.Result.result(newDocument);
91
+ return Result.result(newDocument);
95
92
  };
96
- const insert = async (payload, context, options = {}) => {
93
+ export const insert = async (payload, context, options = {}) => {
97
94
  if (options.bypassSecurity) {
98
95
  return internalInsert(payload, context);
99
96
  }
100
- const security = (0, security_1.useSecurity)(context);
97
+ const security = useSecurity(context);
101
98
  const { error, result: securedPayload } = await security.secureWritePayload(payload);
102
99
  if (error) {
103
- return context.error(types_1.HTTPStatus.Forbidden, {
100
+ return context.error(HTTPStatus.Forbidden, {
104
101
  code: error,
105
102
  });
106
103
  }
107
- return (0, security_1.applyWriteMiddlewares)(securedPayload, context, internalInsert);
104
+ return applyWriteMiddlewares(securedPayload, context, internalInsert);
108
105
  };
109
- exports.insert = insert;
@@ -1,43 +1,39 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.remove = void 0;
4
- const types_1 = require("@aeriajs/types");
5
- const common_1 = require("@aeriajs/common");
6
- const security_1 = require("@aeriajs/security");
7
- const index_js_1 = require("../collection/index.js");
1
+ import { Result, HTTPStatus, ACError } from '@aeriajs/types';
2
+ import { throwIfError } from '@aeriajs/common';
3
+ import { useSecurity } from '@aeriajs/security';
4
+ import { traverseDocument, cascadingRemove } from '../collection/index.js';
8
5
  const internalRemove = async (payload, context) => {
9
6
  if (!payload.filters._id) {
10
- return context.error(types_1.HTTPStatus.NotFound, {
11
- code: types_1.ACError.ResourceNotFound,
7
+ return context.error(HTTPStatus.NotFound, {
8
+ code: ACError.ResourceNotFound,
12
9
  });
13
10
  }
14
- const filters = (0, common_1.throwIfError)(await (0, index_js_1.traverseDocument)(payload.filters, context.description, {
11
+ const filters = throwIfError(await traverseDocument(payload.filters, context.description, {
15
12
  autoCast: true,
16
13
  context,
17
14
  }));
18
15
  const target = await context.collection.model.findOne(filters);
19
16
  if (!target) {
20
- return context.error(types_1.HTTPStatus.NotFound, {
21
- code: types_1.ACError.ResourceNotFound,
17
+ return context.error(HTTPStatus.NotFound, {
18
+ code: ACError.ResourceNotFound,
22
19
  });
23
20
  }
24
- await (0, index_js_1.cascadingRemove)(target, context);
25
- return types_1.Result.result(await context.collection.model.findOneAndDelete(filters));
21
+ await cascadingRemove(target, context);
22
+ return Result.result(await context.collection.model.findOneAndDelete(filters));
26
23
  };
27
- const remove = async (payload, context, options = {}) => {
24
+ export const remove = async (payload, context, options = {}) => {
28
25
  if (options.bypassSecurity) {
29
26
  return internalRemove(payload, context);
30
27
  }
31
- const security = (0, security_1.useSecurity)(context);
28
+ const security = useSecurity(context);
32
29
  const { error, result: securedPayload } = await security.secureReadPayload(payload);
33
30
  if (error) {
34
31
  switch (error) {
35
- case types_1.ACError.InvalidLimit: throw new Error;
32
+ case ACError.InvalidLimit: throw new Error;
36
33
  }
37
- return context.error(types_1.HTTPStatus.Forbidden, {
34
+ return context.error(HTTPStatus.Forbidden, {
38
35
  code: error,
39
36
  });
40
37
  }
41
38
  return internalRemove(securedPayload, context);
42
39
  };
43
- exports.remove = remove;
@@ -1,22 +1,19 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.removeAll = void 0;
4
- const types_1 = require("@aeriajs/types");
5
- const common_1 = require("@aeriajs/common");
6
- const security_1 = require("@aeriajs/security");
7
- const index_js_1 = require("../collection/index.js");
1
+ import { Result, ACError, HTTPStatus } from '@aeriajs/types';
2
+ import { throwIfError } from '@aeriajs/common';
3
+ import { useSecurity } from '@aeriajs/security';
4
+ import { traverseDocument, cascadingRemove } from '../collection/index.js';
8
5
  const internalRemoveAll = async (payload, context) => {
9
- const filters = (0, common_1.throwIfError)(await (0, index_js_1.traverseDocument)(payload.filters, context.description, {
6
+ const filters = throwIfError(await traverseDocument(payload.filters, context.description, {
10
7
  autoCast: true,
11
8
  context,
12
9
  }));
13
10
  const it = context.collection.model.find(filters);
14
11
  for await (const doc of it) {
15
- await (0, index_js_1.cascadingRemove)(doc, context);
12
+ await cascadingRemove(doc, context);
16
13
  }
17
- return types_1.Result.result(await context.collection.model.deleteMany(filters));
14
+ return Result.result(await context.collection.model.deleteMany(filters));
18
15
  };
19
- const removeAll = async (_payload, context, options = {}) => {
16
+ export const removeAll = async (_payload, context, options = {}) => {
20
17
  const payload = {
21
18
  filters: {
22
19
  _id: {
@@ -27,16 +24,15 @@ const removeAll = async (_payload, context, options = {}) => {
27
24
  if (options.bypassSecurity) {
28
25
  return internalRemoveAll(payload, context);
29
26
  }
30
- const security = (0, security_1.useSecurity)(context);
27
+ const security = useSecurity(context);
31
28
  const { error, result: securedPayload } = await security.secureReadPayload(payload);
32
29
  if (error) {
33
30
  switch (error) {
34
- case types_1.ACError.InvalidLimit: throw new Error;
31
+ case ACError.InvalidLimit: throw new Error;
35
32
  }
36
- return context.error(types_1.HTTPStatus.Forbidden, {
33
+ return context.error(HTTPStatus.Forbidden, {
37
34
  code: error,
38
35
  });
39
36
  }
40
37
  return internalRemoveAll(securedPayload, context);
41
38
  };
42
- exports.removeAll = removeAll;
@@ -1,27 +1,23 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.removeFile = void 0;
4
- const types_1 = require("@aeriajs/types");
5
- const security_1 = require("@aeriajs/security");
1
+ import { Result, ACError, HTTPStatus } from '@aeriajs/types';
2
+ import { useSecurity } from '@aeriajs/security';
6
3
  const internalRemoveFile = async (payload, context) => {
7
4
  const { propName, parentId, ...props } = payload;
8
5
  const doc = await context.collections.file.functions.remove(props);
9
- return types_1.Result.result(doc);
6
+ return Result.result(doc);
10
7
  };
11
- const removeFile = async (payload, context, options = {}) => {
8
+ export const removeFile = async (payload, context, options = {}) => {
12
9
  if (options.bypassSecurity) {
13
10
  return internalRemoveFile(payload, context);
14
11
  }
15
- const security = (0, security_1.useSecurity)(context);
12
+ const security = useSecurity(context);
16
13
  const { error, result: securedPayload } = await security.secureReadPayload(payload);
17
14
  if (error) {
18
15
  switch (error) {
19
- case types_1.ACError.InvalidLimit: throw new Error;
16
+ case ACError.InvalidLimit: throw new Error;
20
17
  }
21
- return context.error(types_1.HTTPStatus.Forbidden, {
18
+ return context.error(HTTPStatus.Forbidden, {
22
19
  code: error,
23
20
  });
24
21
  }
25
22
  return internalRemoveFile(securedPayload, context);
26
23
  };
27
- exports.removeFile = removeFile;