@aeriajs/core 0.0.178 → 0.0.180

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.
@@ -5,6 +5,6 @@ export * from './isValidCollection.js';
5
5
  export * from './makePagination.js';
6
6
  export * from './normalizeProjection.js';
7
7
  export * from './preload.js';
8
- export * from './prepareInsert.js';
8
+ export * from './prepareCreate.js';
9
9
  export * from './reference.js';
10
10
  export * from './traverseDocument.js';
@@ -21,6 +21,6 @@ __exportStar(require("./isValidCollection.js"), exports);
21
21
  __exportStar(require("./makePagination.js"), exports);
22
22
  __exportStar(require("./normalizeProjection.js"), exports);
23
23
  __exportStar(require("./preload.js"), exports);
24
- __exportStar(require("./prepareInsert.js"), exports);
24
+ __exportStar(require("./prepareCreate.js"), exports);
25
25
  __exportStar(require("./reference.js"), exports);
26
26
  __exportStar(require("./traverseDocument.js"), exports);
@@ -6,6 +6,6 @@ export * from "./isValidCollection.mjs";
6
6
  export * from "./makePagination.mjs";
7
7
  export * from "./normalizeProjection.mjs";
8
8
  export * from "./preload.mjs";
9
- export * from "./prepareInsert.mjs";
9
+ export * from "./prepareCreate.mjs";
10
10
  export * from "./reference.mjs";
11
11
  export * from "./traverseDocument.mjs";
@@ -1,3 +1,2 @@
1
1
  import type { Description } from '@aeriajs/types';
2
2
  export declare const prepareCreate: (doc: Record<string, unknown>, description: Description) => Record<string, unknown>;
3
- export declare const prepareUpdate: (doc: Record<string, unknown>) => Record<"$set" | "$unset", Record<string, unknown>>;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.prepareCreate = void 0;
4
+ const prepareCreate = (doc, description) => {
5
+ const result = {};
6
+ if (description.defaults) {
7
+ Object.assign(result, description.defaults);
8
+ }
9
+ Object.assign(result, doc);
10
+ return result;
11
+ };
12
+ exports.prepareCreate = prepareCreate;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ export const prepareCreate = (doc, description) => {
3
+ const result = {};
4
+ if (description.defaults) {
5
+ Object.assign(result, description.defaults);
6
+ }
7
+ Object.assign(result, doc);
8
+ return result;
9
+ };
@@ -7,7 +7,7 @@ export type TraverseOptionsBase = {
7
7
  validateRequired?: Description['required'];
8
8
  fromProperties?: boolean;
9
9
  allowOperators?: boolean;
10
- skipUndefined?: boolean;
10
+ undefinedToNull?: boolean;
11
11
  preserveHidden?: boolean;
12
12
  recurseDeep?: boolean;
13
13
  recurseReferences?: boolean;
@@ -275,24 +275,38 @@ const recurse = async (target, ctx) => {
275
275
  for (const propName in entrypoint) {
276
276
  const value = target[propName];
277
277
  const property = getProperty(propName, ctx.property);
278
- if (ctx.options.skipUndefined) {
279
- if (value === undefined && !('getters' in ctx.options && ctx.options.getters && property && 'getter' in property)) {
280
- continue;
278
+ if (propName === '_id') {
279
+ if (value) {
280
+ if (ctx.options.autoCast) {
281
+ entries.push([
282
+ propName,
283
+ autoCast(value, {
284
+ ...ctx,
285
+ target,
286
+ propName,
287
+ property: {
288
+ $ref: '',
289
+ },
290
+ }),
291
+ ]);
292
+ }
293
+ else {
294
+ entries.push([
295
+ propName,
296
+ value,
297
+ ]);
298
+ }
281
299
  }
300
+ continue;
282
301
  }
283
- if (ctx.options.autoCast && propName === '_id') {
284
- entries.push([
285
- propName,
286
- autoCast(value, {
287
- ...ctx,
288
- target,
302
+ if (ctx.options.undefinedToNull) {
303
+ if (value === undefined) {
304
+ entries.push([
289
305
  propName,
290
- property: {
291
- $ref: '',
292
- },
293
- }),
294
- ]);
295
- continue;
306
+ null,
307
+ ]);
308
+ continue;
309
+ }
296
310
  }
297
311
  if (!property) {
298
312
  if (value && (value.constructor === Object || value.constructor === Array)) {
@@ -238,24 +238,37 @@ const recurse = async (target, ctx) => {
238
238
  for (const propName in entrypoint) {
239
239
  const value = target[propName];
240
240
  const property = getProperty(propName, ctx.property);
241
- if (ctx.options.skipUndefined) {
242
- if (value === void 0 && !("getters" in ctx.options && ctx.options.getters && property && "getter" in property)) {
243
- continue;
241
+ if (propName === "_id") {
242
+ if (value) {
243
+ if (ctx.options.autoCast) {
244
+ entries.push([
245
+ propName,
246
+ autoCast(value, {
247
+ ...ctx,
248
+ target,
249
+ propName,
250
+ property: {
251
+ $ref: ""
252
+ }
253
+ })
254
+ ]);
255
+ } else {
256
+ entries.push([
257
+ propName,
258
+ value
259
+ ]);
260
+ }
244
261
  }
262
+ continue;
245
263
  }
246
- if (ctx.options.autoCast && propName === "_id") {
247
- entries.push([
248
- propName,
249
- autoCast(value, {
250
- ...ctx,
251
- target,
264
+ if (ctx.options.undefinedToNull) {
265
+ if (value === void 0) {
266
+ entries.push([
252
267
  propName,
253
- property: {
254
- $ref: ""
255
- }
256
- })
257
- ]);
258
- continue;
268
+ null
269
+ ]);
270
+ continue;
271
+ }
259
272
  }
260
273
  if (!property) {
261
274
  if (value && (value.constructor === Object || value.constructor === Array)) {
@@ -29,15 +29,17 @@ const insertErrorSchema = () => (0, common_1.endpointErrorSchema)({
29
29
  });
30
30
  exports.insertErrorSchema = insertErrorSchema;
31
31
  const internalInsert = async (payload, context) => {
32
+ const isUpdate = !!('_id' in payload.what && payload.what._id);
32
33
  const { error, result: what } = await (0, index_js_1.traverseDocument)(payload.what, context.description, {
33
34
  recurseDeep: true,
34
35
  autoCast: true,
35
36
  validate: true,
36
- validateRequired: '_id' in payload.what && payload.what._id
37
+ validateRequired: isUpdate
37
38
  ? []
38
39
  : context.description.required,
39
40
  moveFiles: true,
40
- skipUndefined: true,
41
+ fromProperties: !isUpdate,
42
+ undefinedToNull: true,
41
43
  preserveHidden: true,
42
44
  context,
43
45
  });
@@ -66,11 +68,14 @@ const internalInsert = async (payload, context) => {
66
68
  newId = (await context.collection.model.insertOne(content)).insertedId;
67
69
  }
68
70
  else {
69
- const content = (0, index_js_1.prepareUpdate)(what);
70
- content.$set.updated_at = new Date();
71
71
  await context.collection.model.updateOne({
72
72
  _id: newId,
73
- }, content);
73
+ }, {
74
+ $set: {
75
+ ...what,
76
+ updated_at: new Date(),
77
+ },
78
+ });
74
79
  }
75
80
  const inheritedContext = {
76
81
  ...context,
@@ -3,7 +3,7 @@ import { ObjectId } from "mongodb";
3
3
  import { Result, HTTPStatus, ACError, ValidationErrorCode, TraverseError } from "@aeriajs/types";
4
4
  import { useSecurity, applyWriteMiddlewares } from "@aeriajs/security";
5
5
  import { endpointErrorSchema } from "@aeriajs/common";
6
- import { traverseDocument, prepareCreate, prepareUpdate } from "../collection/index.mjs";
6
+ import { traverseDocument, prepareCreate } from "../collection/index.mjs";
7
7
  import { get } from "./get.mjs";
8
8
  export const insertErrorSchema = () => endpointErrorSchema({
9
9
  httpStatus: [
@@ -26,13 +26,15 @@ export const insertErrorSchema = () => endpointErrorSchema({
26
26
  ]
27
27
  });
28
28
  const internalInsert = async (payload, context) => {
29
+ const isUpdate = !!("_id" in payload.what && payload.what._id);
29
30
  const { error, result: what } = await traverseDocument(payload.what, context.description, {
30
31
  recurseDeep: true,
31
32
  autoCast: true,
32
33
  validate: true,
33
- validateRequired: "_id" in payload.what && payload.what._id ? [] : context.description.required,
34
+ validateRequired: isUpdate ? [] : context.description.required,
34
35
  moveFiles: true,
35
- skipUndefined: true,
36
+ fromProperties: !isUpdate,
37
+ undefinedToNull: true,
36
38
  preserveHidden: true,
37
39
  context
38
40
  });
@@ -58,11 +60,14 @@ const internalInsert = async (payload, context) => {
58
60
  });
59
61
  newId = (await context.collection.model.insertOne(content)).insertedId;
60
62
  } else {
61
- const content = prepareUpdate(what);
62
- content.$set.updated_at = /* @__PURE__ */ new Date();
63
63
  await context.collection.model.updateOne({
64
64
  _id: newId
65
- }, content);
65
+ }, {
66
+ $set: {
67
+ ...what,
68
+ updated_at: /* @__PURE__ */ new Date()
69
+ }
70
+ });
66
71
  }
67
72
  const inheritedContext = {
68
73
  ...context,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/core",
3
- "version": "0.0.178",
3
+ "version": "0.0.180",
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.178",
46
- "@aeriajs/common": "^0.0.108",
47
- "@aeriajs/entrypoint": "^0.0.111",
48
- "@aeriajs/http": "^0.0.122",
49
- "@aeriajs/security": "^0.0.178",
50
- "@aeriajs/types": "^0.0.91",
51
- "@aeriajs/validation": "^0.0.111"
45
+ "@aeriajs/builtins": "^0.0.180",
46
+ "@aeriajs/common": "^0.0.110",
47
+ "@aeriajs/entrypoint": "^0.0.113",
48
+ "@aeriajs/http": "^0.0.124",
49
+ "@aeriajs/security": "^0.0.180",
50
+ "@aeriajs/types": "^0.0.93",
51
+ "@aeriajs/validation": "^0.0.113"
52
52
  },
53
53
  "dependencies": {
54
54
  "mongodb": "^6.5.0",
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.prepareUpdate = exports.prepareCreate = void 0;
4
- const prepareCreate = (doc, description) => {
5
- const result = Object.assign({}, description.defaults || {});
6
- for (const propName in doc) {
7
- const value = doc[propName];
8
- if (value === null || value === undefined) {
9
- continue;
10
- }
11
- result[propName] = value;
12
- }
13
- return result;
14
- };
15
- exports.prepareCreate = prepareCreate;
16
- const prepareUpdate = (doc) => {
17
- const result = {
18
- $set: {},
19
- $unset: {},
20
- };
21
- for (const propName in doc) {
22
- const value = doc[propName];
23
- if (value === null || value === undefined) {
24
- result.$unset[propName] = value;
25
- continue;
26
- }
27
- result.$set[propName] = value;
28
- }
29
- return result;
30
- };
31
- exports.prepareUpdate = prepareUpdate;
@@ -1,27 +0,0 @@
1
- "use strict";
2
- export const prepareCreate = (doc, description) => {
3
- const result = Object.assign({}, description.defaults || {});
4
- for (const propName in doc) {
5
- const value = doc[propName];
6
- if (value === null || value === void 0) {
7
- continue;
8
- }
9
- result[propName] = value;
10
- }
11
- return result;
12
- };
13
- export const prepareUpdate = (doc) => {
14
- const result = {
15
- $set: {},
16
- $unset: {}
17
- };
18
- for (const propName in doc) {
19
- const value = doc[propName];
20
- if (value === null || value === void 0) {
21
- result.$unset[propName] = value;
22
- continue;
23
- }
24
- result.$set[propName] = value;
25
- }
26
- return result;
27
- };