@aeriajs/core 0.0.167 → 0.0.169

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,6 +1,7 @@
1
1
  export * from './cascadingRemove.js';
2
2
  export * from './define.js';
3
3
  export * from './description.js';
4
+ export * from './isValidCollection.js';
4
5
  export * from './makePagination.js';
5
6
  export * from './normalizeProjection.js';
6
7
  export * from './preload.js';
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./cascadingRemove.js"), exports);
18
18
  __exportStar(require("./define.js"), exports);
19
19
  __exportStar(require("./description.js"), exports);
20
+ __exportStar(require("./isValidCollection.js"), exports);
20
21
  __exportStar(require("./makePagination.js"), exports);
21
22
  __exportStar(require("./normalizeProjection.js"), exports);
22
23
  __exportStar(require("./preload.js"), exports);
@@ -2,6 +2,7 @@
2
2
  export * from "./cascadingRemove.mjs";
3
3
  export * from "./define.mjs";
4
4
  export * from "./description.mjs";
5
+ export * from "./isValidCollection.mjs";
5
6
  export * from "./makePagination.mjs";
6
7
  export * from "./normalizeProjection.mjs";
7
8
  export * from "./preload.mjs";
@@ -0,0 +1,3 @@
1
+ import type { Description, Collection } from '@aeriajs/types';
2
+ export declare const isValidDescription: (value: unknown) => value is Description;
3
+ export declare const isValidCollection: (value: unknown) => value is Collection;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isValidCollection = exports.isValidDescription = void 0;
4
+ const isValidDescription = (value) => {
5
+ return !!(value
6
+ && typeof value === 'object'
7
+ && '$id' in value
8
+ && 'properties' in value
9
+ && value.properties
10
+ && typeof value.$id === 'string'
11
+ && typeof value.properties === 'object');
12
+ };
13
+ exports.isValidDescription = isValidDescription;
14
+ const isValidCollection = (value) => {
15
+ return !!(value
16
+ && typeof value === 'object'
17
+ && 'description' in value
18
+ && (0, exports.isValidDescription)(value.description));
19
+ };
20
+ exports.isValidCollection = isValidCollection;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ export const isValidDescription = (value) => {
3
+ return !!(value && typeof value === "object" && "$id" in value && "properties" in value && value.properties && typeof value.$id === "string" && typeof value.properties === "object");
4
+ };
5
+ export const isValidCollection = (value) => {
6
+ return !!(value && typeof value === "object" && "description" in value && isValidDescription(value.description));
7
+ };
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.makePagination = void 0;
4
+ const count_js_1 = require("../functions/count.js");
4
5
  const common_1 = require("@aeriajs/common");
5
6
  const makePagination = async (payload, documents, context) => {
6
7
  const { offset = 0, limit = context.config.defaultPaginationLimit, } = payload;
@@ -8,7 +9,9 @@ const makePagination = async (payload, documents, context) => {
8
9
  ? (0, common_1.throwIfError)(await context.collection.functions.count({
9
10
  filters: payload.filters,
10
11
  }))
11
- : await context.collection.model.countDocuments(payload.filters);
12
+ : (0, common_1.throwIfError)(await (0, count_js_1.count)({
13
+ filters: payload.filters,
14
+ }, context));
12
15
  return {
13
16
  recordsCount: documents.length,
14
17
  recordsTotal,
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
+ import { count } from "../functions/count.mjs";
2
3
  import { throwIfError } from "@aeriajs/common";
3
4
  export const makePagination = async (payload, documents, context) => {
4
5
  const {
@@ -7,7 +8,9 @@ export const makePagination = async (payload, documents, context) => {
7
8
  } = payload;
8
9
  const recordsTotal = typeof context.collection.functions.count === "function" ? throwIfError(await context.collection.functions.count({
9
10
  filters: payload.filters
10
- })) : await context.collection.model.countDocuments(payload.filters);
11
+ })) : throwIfError(await count({
12
+ filters: payload.filters
13
+ }, context));
11
14
  return {
12
15
  recordsCount: documents.length,
13
16
  recordsTotal,
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getLookupPipeline = exports.buildLookupPipeline = exports.recurseSetStage = exports.getReferences = void 0;
4
4
  const common_1 = require("@aeriajs/common");
5
5
  const assets_js_1 = require("../assets.js");
6
- const database_js_1 = require("../database.js");
7
6
  const getTempName = (path) => {
8
7
  return `_${path.join('_')}`;
9
8
  };
@@ -288,7 +287,7 @@ const buildLookupPipeline = (refMap, options = {}) => {
288
287
  : path.concat(refName).join('.');
289
288
  rootPipeline.unshift({
290
289
  $lookup: {
291
- from: (0, database_js_1.prepareCollectionName)(reference.referencedCollection),
290
+ from: reference.referencedCollection,
292
291
  foreignField: '_id',
293
292
  localField,
294
293
  as: tempName,
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  import { throwIfError, getReferenceProperty } from "@aeriajs/common";
3
3
  import { getCollectionAsset } from "../assets.mjs";
4
- import { prepareCollectionName } from "../database.mjs";
5
4
  const getTempName = (path) => {
6
5
  return `_${path.join("_")}`;
7
6
  };
@@ -276,7 +275,7 @@ export const buildLookupPipeline = (refMap, options = {}) => {
276
275
  const localField = reference.isRecursive ? `${getTempName(path)}.${refName}` : path.concat(refName).join(".");
277
276
  rootPipeline.unshift({
278
277
  $lookup: {
279
- from: prepareCollectionName(reference.referencedCollection),
278
+ from: reference.referencedCollection,
280
279
  foreignField: "_id",
281
280
  localField,
282
281
  as: tempName,
@@ -25,7 +25,7 @@ export type TraverseNormalized = {
25
25
  description: Description;
26
26
  pipe: <T = unknown>(value: unknown, phaseContext: PhaseContext) => T | Promise<T>;
27
27
  };
28
- export type ValidTempFile = undefined | ObjectId | {
28
+ export type ValidTempFile = null | undefined | ObjectId | {
29
29
  tempId: string;
30
30
  };
31
31
  type PhaseContext = {
@@ -183,24 +183,27 @@ const validate = (value, ctx) => {
183
183
  };
184
184
  const isValidTempFile = (value) => {
185
185
  if (value && typeof value === 'object') {
186
- return 'tempId' in value && typeof value.tempId === 'string';
186
+ if (value instanceof mongodb_1.ObjectId) {
187
+ return true;
188
+ }
189
+ return 'tempId' in value && (typeof value.tempId === 'string' || value.tempId instanceof mongodb_1.ObjectId);
187
190
  }
188
191
  return !!(value === undefined
189
- || value instanceof mongodb_1.ObjectId);
192
+ || value === null);
190
193
  };
191
194
  const moveFiles = async (value, ctx) => {
192
195
  if (!('$ref' in ctx.property) || ctx.property.$ref !== 'file') {
193
196
  return value;
194
197
  }
198
+ if (!isValidTempFile(value)) {
199
+ return types_1.Result.error(types_1.TraverseError.InvalidTempfile);
200
+ }
195
201
  if (!value) {
196
202
  if (ctx.root._id && !ctx.isArray) {
197
203
  await disposeOldFiles(ctx);
198
204
  }
199
205
  return null;
200
206
  }
201
- if (!isValidTempFile(value)) {
202
- return types_1.Result.error(types_1.TraverseError.InvalidTempfile);
203
- }
204
207
  if (value instanceof mongodb_1.ObjectId) {
205
208
  return value;
206
209
  }
@@ -398,13 +401,14 @@ const recurse = async (target, ctx) => {
398
401
  return types_1.Result.result(Object.fromEntries(entries));
399
402
  };
400
403
  const traverseDocument = async (what, description, _options) => {
401
- const options = Object.assign({}, _options);
402
- const functions = [];
403
404
  if (!what) {
404
405
  return types_1.Result.result(what);
405
406
  }
406
- if (!options.validate && Object.keys(what).length === 0) {
407
- return types_1.Result.result(what);
407
+ const whatCopy = Object.assign({}, what);
408
+ const options = Object.assign({}, _options);
409
+ const functions = [];
410
+ if (!options.validate && Object.keys(whatCopy).length === 0) {
411
+ return types_1.Result.result(whatCopy);
408
412
  }
409
413
  if (options.recurseDeep) {
410
414
  functions.push(recurseDeep);
@@ -420,7 +424,7 @@ const traverseDocument = async (what, description, _options) => {
420
424
  if (options.validateRequired) {
421
425
  descriptionCopy.required = options.validateRequired;
422
426
  }
423
- const wholenessError = (0, validation_1.validateWholeness)(what, descriptionCopy);
427
+ const wholenessError = (0, validation_1.validateWholeness)(whatCopy, descriptionCopy);
424
428
  if (wholenessError) {
425
429
  return types_1.Result.error(wholenessError);
426
430
  }
@@ -457,8 +461,8 @@ const traverseDocument = async (what, description, _options) => {
457
461
  },
458
462
  }),
459
463
  });
460
- const { error, result } = await recurse(what, {
461
- root: what,
464
+ const { error, result } = await recurse(whatCopy, {
465
+ root: whatCopy,
462
466
  property: description,
463
467
  propPath: '',
464
468
  options,
@@ -149,23 +149,26 @@ const validate = (value, ctx) => {
149
149
  };
150
150
  const isValidTempFile = (value) => {
151
151
  if (value && typeof value === "object") {
152
- return "tempId" in value && typeof value.tempId === "string";
152
+ if (value instanceof ObjectId) {
153
+ return true;
154
+ }
155
+ return "tempId" in value && (typeof value.tempId === "string" || value.tempId instanceof ObjectId);
153
156
  }
154
- return !!(value === void 0 || value instanceof ObjectId);
157
+ return !!(value === void 0 || value === null);
155
158
  };
156
159
  const moveFiles = async (value, ctx) => {
157
160
  if (!("$ref" in ctx.property) || ctx.property.$ref !== "file") {
158
161
  return value;
159
162
  }
163
+ if (!isValidTempFile(value)) {
164
+ return Result.error(TraverseError.InvalidTempfile);
165
+ }
160
166
  if (!value) {
161
167
  if (ctx.root._id && !ctx.isArray) {
162
168
  await disposeOldFiles(ctx);
163
169
  }
164
170
  return null;
165
171
  }
166
- if (!isValidTempFile(value)) {
167
- return Result.error(TraverseError.InvalidTempfile);
168
- }
169
172
  if (value instanceof ObjectId) {
170
173
  return value;
171
174
  }
@@ -355,13 +358,14 @@ const recurse = async (target, ctx) => {
355
358
  return Result.result(Object.fromEntries(entries));
356
359
  };
357
360
  export const traverseDocument = async (what, description, _options) => {
358
- const options = Object.assign({}, _options);
359
- const functions = [];
360
361
  if (!what) {
361
362
  return Result.result(what);
362
363
  }
363
- if (!options.validate && Object.keys(what).length === 0) {
364
- return Result.result(what);
364
+ const whatCopy = Object.assign({}, what);
365
+ const options = Object.assign({}, _options);
366
+ const functions = [];
367
+ if (!options.validate && Object.keys(whatCopy).length === 0) {
368
+ return Result.result(whatCopy);
365
369
  }
366
370
  if (options.recurseDeep) {
367
371
  functions.push(recurseDeep);
@@ -377,7 +381,7 @@ export const traverseDocument = async (what, description, _options) => {
377
381
  if (options.validateRequired) {
378
382
  descriptionCopy.required = options.validateRequired;
379
383
  }
380
- const wholenessError = validateWholeness(what, descriptionCopy);
384
+ const wholenessError = validateWholeness(whatCopy, descriptionCopy);
381
385
  if (wholenessError) {
382
386
  return Result.error(wholenessError);
383
387
  }
@@ -414,8 +418,8 @@ export const traverseDocument = async (what, description, _options) => {
414
418
  }
415
419
  })
416
420
  });
417
- const { error, result } = await recurse(what, {
418
- root: what,
421
+ const { error, result } = await recurse(whatCopy, {
422
+ root: whatCopy,
419
423
  property: description,
420
424
  propPath: "",
421
425
  options
package/dist/context.js CHANGED
@@ -39,7 +39,8 @@ const indepthCollection = (collectionName, collections, parentContext) => {
39
39
  if (typeof functionName !== 'string') {
40
40
  throw new Error();
41
41
  }
42
- if (!collection.functions?.[functionName]) {
42
+ const fn = collection.functions?.[functionName];
43
+ if (!fn) {
43
44
  return null;
44
45
  }
45
46
  return async (props, ...args) => {
@@ -48,7 +49,7 @@ const indepthCollection = (collectionName, collections, parentContext) => {
48
49
  collectionName,
49
50
  inherited: true,
50
51
  });
51
- return collection.functions[functionName](props, childContext, ...args);
52
+ return fn(props, childContext, ...args);
52
53
  };
53
54
  },
54
55
  });
package/dist/context.mjs CHANGED
@@ -12,7 +12,8 @@ const indepthCollection = (collectionName, collections, parentContext) => {
12
12
  if (typeof functionName !== "string") {
13
13
  throw new Error();
14
14
  }
15
- if (!collection.functions?.[functionName]) {
15
+ const fn = collection.functions?.[functionName];
16
+ if (!fn) {
16
17
  return null;
17
18
  }
18
19
  return async (props, ...args) => {
@@ -21,7 +22,7 @@ const indepthCollection = (collectionName, collections, parentContext) => {
21
22
  collectionName,
22
23
  inherited: true
23
24
  });
24
- return collection.functions[functionName](props, childContext, ...args);
25
+ return fn(props, childContext, ...args);
25
26
  };
26
27
  }
27
28
  });
@@ -6,5 +6,4 @@ export declare const getDatabase: () => Promise<{
6
6
  db: ReturnType<MongoClient["db"]> | undefined;
7
7
  }>;
8
8
  export declare const getDatabaseSync: () => import("mongodb").Db;
9
- export declare const prepareCollectionName: (collectionName: string) => string;
10
9
  export declare const getDatabaseCollection: <TDocument extends Record<string, unknown>>(collectionName: string) => import("mongodb").Collection<PackReferences<TDocument>>;
package/dist/database.js CHANGED
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.getDatabaseCollection = exports.prepareCollectionName = exports.getDatabaseSync = exports.getDatabase = exports.ObjectId = void 0;
26
+ exports.getDatabaseCollection = exports.getDatabaseSync = exports.getDatabase = exports.ObjectId = void 0;
27
27
  const entrypoint_1 = require("@aeriajs/entrypoint");
28
28
  const mongodb_1 = require("mongodb");
29
29
  var mongodb_2 = require("mongodb");
@@ -69,22 +69,8 @@ const getDatabaseSync = () => {
69
69
  return dbMemo.db;
70
70
  };
71
71
  exports.getDatabaseSync = getDatabaseSync;
72
- const prepareCollectionName = (collectionName) => {
73
- let pluralized;
74
- if (collectionName.endsWith('y')) {
75
- pluralized = collectionName.replace(/y$/, 'ies');
76
- }
77
- else if (collectionName.endsWith('s')) {
78
- pluralized = `${collectionName}es`;
79
- }
80
- else {
81
- pluralized = `${collectionName}s`;
82
- }
83
- return pluralized.toLowerCase();
84
- };
85
- exports.prepareCollectionName = prepareCollectionName;
86
72
  const getDatabaseCollection = (collectionName) => {
87
73
  const db = (0, exports.getDatabaseSync)();
88
- return db.collection((0, exports.prepareCollectionName)(collectionName));
74
+ return db.collection(collectionName);
89
75
  };
90
76
  exports.getDatabaseCollection = getDatabaseCollection;
package/dist/database.mjs CHANGED
@@ -42,18 +42,7 @@ export const getDatabaseSync = () => {
42
42
  }
43
43
  return dbMemo.db;
44
44
  };
45
- export const prepareCollectionName = (collectionName) => {
46
- let pluralized;
47
- if (collectionName.endsWith("y")) {
48
- pluralized = collectionName.replace(/y$/, "ies");
49
- } else if (collectionName.endsWith("s")) {
50
- pluralized = `${collectionName}es`;
51
- } else {
52
- pluralized = `${collectionName}s`;
53
- }
54
- return pluralized.toLowerCase();
55
- };
56
45
  export const getDatabaseCollection = (collectionName) => {
57
46
  const db = getDatabaseSync();
58
- return db.collection(prepareCollectionName(collectionName));
47
+ return db.collection(collectionName);
59
48
  };
@@ -3,4 +3,8 @@ import { Result } from '@aeriajs/types';
3
3
  export type CountOptions = {
4
4
  bypassSecurity?: boolean;
5
5
  };
6
- export declare const count: <TContext extends Context>(payload: CountPayload<SchemaWithId<TContext["description"]>>, context: TContext extends Context ? TContext : never, options?: CountOptions) => Promise<Result.Either<any, any>>;
6
+ export declare const count: <TContext extends Context>(payload: CountPayload<SchemaWithId<TContext["description"]>>, context: TContext extends Context ? TContext : never, options?: CountOptions) => Promise<Result.Error<any> | {
7
+ readonly _tag: "Result";
8
+ readonly error: undefined;
9
+ readonly result: any;
10
+ }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/core",
3
- "version": "0.0.167",
3
+ "version": "0.0.169",
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.167",
46
- "@aeriajs/common": "^0.0.103",
47
- "@aeriajs/entrypoint": "^0.0.105",
48
- "@aeriajs/http": "^0.0.116",
49
- "@aeriajs/security": "^0.0.167",
45
+ "@aeriajs/builtins": "^0.0.169",
46
+ "@aeriajs/common": "^0.0.104",
47
+ "@aeriajs/entrypoint": "^0.0.106",
48
+ "@aeriajs/http": "^0.0.117",
49
+ "@aeriajs/security": "^0.0.169",
50
50
  "@aeriajs/types": "^0.0.87",
51
- "@aeriajs/validation": "^0.0.106"
51
+ "@aeriajs/validation": "^0.0.107"
52
52
  },
53
53
  "dependencies": {
54
54
  "mongodb": "^6.5.0",