@aeriajs/core 0.0.165 → 0.0.166
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/assets.d.ts +3 -3
- package/dist/collection/cascadingRemove.js +10 -2
- package/dist/collection/cascadingRemove.mjs +10 -2
- package/dist/collection/define.d.ts +4 -3
- package/dist/collection/makePagination.d.ts +4 -3
- package/dist/collection/prepareInsert.d.ts +2 -1
- package/dist/collection/prepareInsert.js +3 -8
- package/dist/collection/prepareInsert.mjs +2 -6
- package/dist/collection/traverseDocument.d.ts +5 -2
- package/dist/collection/traverseDocument.js +16 -5
- package/dist/collection/traverseDocument.mjs +15 -5
- package/dist/context.d.ts +2 -2
- package/dist/functions/count.d.ts +1 -5
- package/dist/functions/insert.js +14 -32
- package/dist/functions/insert.mjs +15 -31
- package/dist/use.d.ts +1 -1
- package/package.json +8 -8
package/dist/assets.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Context, Collection, Token } from '@aeriajs/types';
|
|
2
2
|
import { ACError } from '@aeriajs/types';
|
|
3
|
-
export declare const internalGetCollectionAsset: <TCollectionName extends string, TAssetName extends keyof Collections[TCollectionName] &
|
|
3
|
+
export declare const internalGetCollectionAsset: <TCollectionName extends string, TAssetName extends keyof Collections[TCollectionName] & keyof Collection>(collectionName: TCollectionName, assetName: TAssetName) => Promise<{
|
|
4
4
|
readonly _tag: "Error";
|
|
5
5
|
readonly error: ACError.ResourceNotFound;
|
|
6
6
|
readonly result: undefined;
|
|
@@ -9,7 +9,7 @@ export declare const internalGetCollectionAsset: <TCollectionName extends string
|
|
|
9
9
|
readonly error: undefined;
|
|
10
10
|
readonly result: NonNullable<Collection[TAssetName]>;
|
|
11
11
|
}>;
|
|
12
|
-
export declare const getCollectionAsset: <TCollectionName extends string, TAssetName extends keyof Collections[TCollectionName] &
|
|
12
|
+
export declare const getCollectionAsset: <TCollectionName extends string, TAssetName extends keyof Collections[TCollectionName] & keyof Collection>(collectionName: TCollectionName, assetName: TAssetName) => Promise<{
|
|
13
13
|
readonly _tag: "Error";
|
|
14
14
|
readonly error: ACError.ResourceNotFound;
|
|
15
15
|
readonly result: undefined;
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.cascadingRemove = void 0;
|
|
4
|
+
const mongodb_1 = require("mongodb");
|
|
4
5
|
const context_js_1 = require("../context.js");
|
|
5
6
|
const assets_js_1 = require("../assets.js");
|
|
6
7
|
const database_js_1 = require("../database.js");
|
|
7
8
|
const reference_js_1 = require("./reference.js");
|
|
9
|
+
const isObject = (value) => {
|
|
10
|
+
return typeof value === 'object';
|
|
11
|
+
};
|
|
8
12
|
const preferredRemove = async (targetId, reference, parentContext) => {
|
|
9
13
|
if (!reference.referencedCollection) {
|
|
10
14
|
return;
|
|
@@ -46,7 +50,9 @@ const internalCascadingRemove = async (target, refMap, context) => {
|
|
|
46
50
|
continue;
|
|
47
51
|
}
|
|
48
52
|
if (reference.isInline || reference.referencedCollection === 'file') {
|
|
49
|
-
|
|
53
|
+
if (target[refName] instanceof mongodb_1.ObjectId) {
|
|
54
|
+
await preferredRemove(target[refName], reference, context);
|
|
55
|
+
}
|
|
50
56
|
}
|
|
51
57
|
if (reference.deepReferences) {
|
|
52
58
|
if (Array.isArray(target[refName])) {
|
|
@@ -55,7 +61,9 @@ const internalCascadingRemove = async (target, refMap, context) => {
|
|
|
55
61
|
}
|
|
56
62
|
continue;
|
|
57
63
|
}
|
|
58
|
-
|
|
64
|
+
if (isObject(target[refName])) {
|
|
65
|
+
await internalCascadingRemove(target[refName], reference.deepReferences, context);
|
|
66
|
+
}
|
|
59
67
|
}
|
|
60
68
|
}
|
|
61
69
|
};
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
import { ObjectId } from "mongodb";
|
|
2
3
|
import { createContext } from "../context.mjs";
|
|
3
4
|
import { getFunction } from "../assets.mjs";
|
|
4
5
|
import { getDatabaseCollection } from "../database.mjs";
|
|
5
6
|
import { getReferences } from "./reference.mjs";
|
|
7
|
+
const isObject = (value) => {
|
|
8
|
+
return typeof value === "object";
|
|
9
|
+
};
|
|
6
10
|
const preferredRemove = async (targetId, reference, parentContext) => {
|
|
7
11
|
if (!reference.referencedCollection) {
|
|
8
12
|
return;
|
|
@@ -44,7 +48,9 @@ const internalCascadingRemove = async (target, refMap, context) => {
|
|
|
44
48
|
continue;
|
|
45
49
|
}
|
|
46
50
|
if (reference.isInline || reference.referencedCollection === "file") {
|
|
47
|
-
|
|
51
|
+
if (target[refName] instanceof ObjectId) {
|
|
52
|
+
await preferredRemove(target[refName], reference, context);
|
|
53
|
+
}
|
|
48
54
|
}
|
|
49
55
|
if (reference.deepReferences) {
|
|
50
56
|
if (Array.isArray(target[refName])) {
|
|
@@ -53,7 +59,9 @@ const internalCascadingRemove = async (target, refMap, context) => {
|
|
|
53
59
|
}
|
|
54
60
|
continue;
|
|
55
61
|
}
|
|
56
|
-
|
|
62
|
+
if (isObject(target[refName])) {
|
|
63
|
+
await internalCascadingRemove(target[refName], reference.deepReferences, context);
|
|
64
|
+
}
|
|
57
65
|
}
|
|
58
66
|
}
|
|
59
67
|
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { SchemaWithId, Collection, StrictContext, Contract, ContractToFunction, Description, CollectionSecurityPolicy, AccessCondition, RoleFromAccessCondition } from '@aeriajs/types';
|
|
2
|
-
export type ExtendCollection<TLeftCollection extends Collection, TRightCollection> =
|
|
2
|
+
export type ExtendCollection<TLeftCollection extends Omit<Collection, 'item'>, TRightCollection> = TLeftCollection & TRightCollection & {
|
|
3
3
|
item: SchemaWithId<(TLeftCollection & TRightCollection)['description']>;
|
|
4
4
|
};
|
|
5
|
-
export declare const defineCollection: <TCollection extends Collection<TCollection extends Collection ? TCollection : never> extends infer Coll ? Omit<Coll, "item" | "description" | "functions" | "exposedFunctions" | "security"> : never, const TDescription extends Description<TDescription>, const TContracts extends { [P in keyof TFunctions]?: Contract; },
|
|
5
|
+
export declare const defineCollection: <TCollection extends Collection<TCollection extends Collection ? TCollection : never> extends infer Coll ? Omit<Coll, "item" | "description" | "functions" | "exposedFunctions" | "security" | "middlewares"> : never, const TDescription extends Description<TDescription>, const TContracts extends { [P in keyof TFunctions]?: Contract; }, TExposedFunctions extends Partial<Record<keyof TFunctions, AccessCondition>>, TFunctions extends undefined | (Record<string, (payload: any, context: StrictContext<any>) => any> & { [P in keyof TContracts | keyof TExposedFunctions]: ContractToFunction<P extends keyof TContracts ? NonNullable<TContracts[P]> : any, StrictContext<P extends keyof TExposedFunctions ? RoleFromAccessCondition<TExposedFunctions[P]> : never, TDescription>>; })>(collection: TCollection & {
|
|
6
6
|
description: TDescription;
|
|
7
7
|
functions?: TFunctions;
|
|
8
8
|
contracts?: TContracts;
|
|
@@ -10,10 +10,11 @@ export declare const defineCollection: <TCollection extends Collection<TCollecti
|
|
|
10
10
|
security?: CollectionSecurityPolicy<{
|
|
11
11
|
functions: TFunctions;
|
|
12
12
|
}>;
|
|
13
|
+
middlewares?: Collection["middlewares"];
|
|
13
14
|
}) => TCollection & {
|
|
14
15
|
item: SchemaWithId<TDescription>;
|
|
15
16
|
description: TDescription;
|
|
16
17
|
functions: TFunctions;
|
|
17
18
|
contracts: TContracts;
|
|
18
19
|
};
|
|
19
|
-
export declare const extendCollection: <const TLeftCollection extends Collection, const TRightCollection extends { [P in keyof Collection]?: Partial<Collection[P]>; }>(left: TLeftCollection, right: TRightCollection) => ExtendCollection<TLeftCollection, TRightCollection>;
|
|
20
|
+
export declare const extendCollection: <const TLeftCollection extends Collection, const TRightCollection extends { [P in Exclude<keyof Collection, "item" | "middlewares">]?: Partial<Collection[P]>; }>(left: TLeftCollection, right: TRightCollection) => ExtendCollection<TLeftCollection, TRightCollection>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import type { WithId } from 'mongodb';
|
|
2
|
+
import type { Context, Description, Pagination, GetAllPayload, CountReturnType } from '@aeriajs/types';
|
|
3
|
+
export declare const makePagination: (payload: GetAllPayload<WithId<unknown>>, documents: unknown[], context: Context<Description, {
|
|
4
|
+
count?: (...args: unknown[]) => Promise<CountReturnType>;
|
|
4
5
|
}>) => Promise<Pagination>;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import type { Description } from '@aeriajs/types';
|
|
2
|
-
export declare const
|
|
2
|
+
export declare const prepareCreate: (doc: Record<string, unknown>, description: Description) => Record<string, unknown>;
|
|
3
|
+
export declare const prepareUpdate: (doc: Record<string, unknown>) => Record<string, Record<string, unknown>>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.prepareUpdate = exports.prepareCreate = void 0;
|
|
4
4
|
const prepareCreate = (doc, description) => {
|
|
5
5
|
const result = Object.assign({}, description.defaults || {});
|
|
6
6
|
for (const propName in doc) {
|
|
@@ -12,6 +12,7 @@ const prepareCreate = (doc, description) => {
|
|
|
12
12
|
}
|
|
13
13
|
return result;
|
|
14
14
|
};
|
|
15
|
+
exports.prepareCreate = prepareCreate;
|
|
15
16
|
const prepareUpdate = (doc) => {
|
|
16
17
|
const result = {
|
|
17
18
|
$set: {},
|
|
@@ -27,10 +28,4 @@ const prepareUpdate = (doc) => {
|
|
|
27
28
|
}
|
|
28
29
|
return result;
|
|
29
30
|
};
|
|
30
|
-
|
|
31
|
-
const { _id: docId, ...doc } = Object.assign({}, payload);
|
|
32
|
-
return docId
|
|
33
|
-
? prepareUpdate(doc)
|
|
34
|
-
: prepareCreate(doc, description);
|
|
35
|
-
};
|
|
36
|
-
exports.prepareInsert = prepareInsert;
|
|
31
|
+
exports.prepareUpdate = prepareUpdate;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
const prepareCreate = (doc, description) => {
|
|
2
|
+
export const prepareCreate = (doc, description) => {
|
|
3
3
|
const result = Object.assign({}, description.defaults || {});
|
|
4
4
|
for (const propName in doc) {
|
|
5
5
|
const value = doc[propName];
|
|
@@ -10,7 +10,7 @@ const prepareCreate = (doc, description) => {
|
|
|
10
10
|
}
|
|
11
11
|
return result;
|
|
12
12
|
};
|
|
13
|
-
const prepareUpdate = (doc) => {
|
|
13
|
+
export const prepareUpdate = (doc) => {
|
|
14
14
|
const result = {
|
|
15
15
|
$set: {},
|
|
16
16
|
$unset: {}
|
|
@@ -25,7 +25,3 @@ const prepareUpdate = (doc) => {
|
|
|
25
25
|
}
|
|
26
26
|
return result;
|
|
27
27
|
};
|
|
28
|
-
export const prepareInsert = (payload, description) => {
|
|
29
|
-
const { _id: docId, ...doc } = Object.assign({}, payload);
|
|
30
|
-
return docId ? prepareUpdate(doc) : prepareCreate(doc, description);
|
|
31
|
-
};
|
|
@@ -25,8 +25,11 @@ 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 | {
|
|
29
|
+
tempId: string;
|
|
30
|
+
};
|
|
28
31
|
type PhaseContext = {
|
|
29
|
-
target:
|
|
32
|
+
target: Record<string, unknown>;
|
|
30
33
|
root: {
|
|
31
34
|
_id?: ObjectId | string;
|
|
32
35
|
};
|
|
@@ -36,7 +39,7 @@ type PhaseContext = {
|
|
|
36
39
|
options: TraverseOptions & TraverseNormalized;
|
|
37
40
|
isArray?: boolean;
|
|
38
41
|
};
|
|
39
|
-
export declare const traverseDocument: <
|
|
42
|
+
export declare const traverseDocument: <TWhat>(what: TWhat, description: Description, _options: TraverseOptions) => Promise<{
|
|
40
43
|
readonly _tag: "Result";
|
|
41
44
|
readonly error: undefined;
|
|
42
45
|
readonly result: TWhat;
|
|
@@ -169,7 +169,7 @@ const getters = (value, ctx) => {
|
|
|
169
169
|
};
|
|
170
170
|
const validate = (value, ctx) => {
|
|
171
171
|
if (ctx.options.recurseDeep) {
|
|
172
|
-
if ('
|
|
172
|
+
if ('properties' in ctx.property) {
|
|
173
173
|
return value;
|
|
174
174
|
}
|
|
175
175
|
}
|
|
@@ -181,8 +181,21 @@ const validate = (value, ctx) => {
|
|
|
181
181
|
}
|
|
182
182
|
return value;
|
|
183
183
|
};
|
|
184
|
+
const isValidTempFile = (value) => {
|
|
185
|
+
if (value && typeof value === 'object') {
|
|
186
|
+
return 'tempId' in value && typeof value.tempId === 'string';
|
|
187
|
+
}
|
|
188
|
+
return !!(value === undefined
|
|
189
|
+
|| value instanceof mongodb_1.ObjectId);
|
|
190
|
+
};
|
|
184
191
|
const moveFiles = async (value, ctx) => {
|
|
185
|
-
if (!('$ref' in ctx.property) || ctx.property.$ref !== 'file'
|
|
192
|
+
if (!('$ref' in ctx.property) || ctx.property.$ref !== 'file') {
|
|
193
|
+
return value;
|
|
194
|
+
}
|
|
195
|
+
if (!isValidTempFile(value)) {
|
|
196
|
+
return types_1.Result.error(types_1.TraverseError.InvalidTempfile);
|
|
197
|
+
}
|
|
198
|
+
if (value instanceof mongodb_1.ObjectId) {
|
|
186
199
|
return value;
|
|
187
200
|
}
|
|
188
201
|
if (!ctx.options.context) {
|
|
@@ -421,9 +434,7 @@ const traverseDocument = async (what, description, _options) => {
|
|
|
421
434
|
const mutateTarget = (fn) => {
|
|
422
435
|
return async (value, ctx) => {
|
|
423
436
|
const result = await fn(value, ctx);
|
|
424
|
-
|
|
425
|
-
ctx.target[ctx.propName] = result;
|
|
426
|
-
}
|
|
437
|
+
ctx.target[ctx.propName] = result;
|
|
427
438
|
return result;
|
|
428
439
|
};
|
|
429
440
|
};
|
|
@@ -135,7 +135,7 @@ const getters = (value, ctx) => {
|
|
|
135
135
|
};
|
|
136
136
|
const validate = (value, ctx) => {
|
|
137
137
|
if (ctx.options.recurseDeep) {
|
|
138
|
-
if ("
|
|
138
|
+
if ("properties" in ctx.property) {
|
|
139
139
|
return value;
|
|
140
140
|
}
|
|
141
141
|
}
|
|
@@ -147,8 +147,20 @@ const validate = (value, ctx) => {
|
|
|
147
147
|
}
|
|
148
148
|
return value;
|
|
149
149
|
};
|
|
150
|
+
const isValidTempFile = (value) => {
|
|
151
|
+
if (value && typeof value === "object") {
|
|
152
|
+
return "tempId" in value && typeof value.tempId === "string";
|
|
153
|
+
}
|
|
154
|
+
return !!(value === void 0 || value instanceof ObjectId);
|
|
155
|
+
};
|
|
150
156
|
const moveFiles = async (value, ctx) => {
|
|
151
|
-
if (!("$ref" in ctx.property) || ctx.property.$ref !== "file"
|
|
157
|
+
if (!("$ref" in ctx.property) || ctx.property.$ref !== "file") {
|
|
158
|
+
return value;
|
|
159
|
+
}
|
|
160
|
+
if (!isValidTempFile(value)) {
|
|
161
|
+
return Result.error(TraverseError.InvalidTempfile);
|
|
162
|
+
}
|
|
163
|
+
if (value instanceof ObjectId) {
|
|
152
164
|
return value;
|
|
153
165
|
}
|
|
154
166
|
if (!ctx.options.context) {
|
|
@@ -379,9 +391,7 @@ export const traverseDocument = async (what, description, _options) => {
|
|
|
379
391
|
const mutateTarget = (fn) => {
|
|
380
392
|
return async (value, ctx) => {
|
|
381
393
|
const result2 = await fn(value, ctx);
|
|
382
|
-
|
|
383
|
-
ctx.target[ctx.propName] = result2;
|
|
384
|
-
}
|
|
394
|
+
ctx.target[ctx.propName] = result2;
|
|
385
395
|
return result2;
|
|
386
396
|
};
|
|
387
397
|
};
|
package/dist/context.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { ContextOptions } from '@aeriajs/types';
|
|
2
|
-
export declare const createContext: (options?: ContextOptions) => Promise<import("@aeriajs/types").RouteContext<null> & import("@aeriajs/types").CollectionContext<import("@aeriajs/types").Description, any>>;
|
|
1
|
+
import type { Context, ContextOptions } from '@aeriajs/types';
|
|
2
|
+
export declare const createContext: (options?: ContextOptions) => Promise<import("@aeriajs/types").RouteContext<null> & import("@aeriajs/types").CollectionContext<import("@aeriajs/types").Description, Record<string, (payload: any, context: Context<any>, ...args: any[]) => any> | undefined>>;
|
|
@@ -3,8 +3,4 @@ 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
|
|
7
|
-
readonly _tag: "Result";
|
|
8
|
-
readonly error: undefined;
|
|
9
|
-
readonly result: any;
|
|
10
|
-
} | Result.Error<any>>;
|
|
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>>;
|
package/dist/functions/insert.js
CHANGED
|
@@ -6,6 +6,7 @@ const types_1 = require("@aeriajs/types");
|
|
|
6
6
|
const security_1 = require("@aeriajs/security");
|
|
7
7
|
const common_1 = require("@aeriajs/common");
|
|
8
8
|
const index_js_1 = require("../collection/index.js");
|
|
9
|
+
const get_js_1 = require("./get.js");
|
|
9
10
|
const insertErrorSchema = () => (0, common_1.endpointErrorSchema)({
|
|
10
11
|
httpStatus: [
|
|
11
12
|
types_1.HTTPStatus.Forbidden,
|
|
@@ -52,12 +53,9 @@ const internalInsert = async (payload, context) => {
|
|
|
52
53
|
const docId = '_id' in what && what._id instanceof mongodb_1.ObjectId
|
|
53
54
|
? what._id
|
|
54
55
|
: null;
|
|
55
|
-
const content = (0, index_js_1.prepareInsert)(what, context.description);
|
|
56
|
-
const projection = payload.project
|
|
57
|
-
? (0, index_js_1.normalizeProjection)(payload.project, context.description)
|
|
58
|
-
: {};
|
|
59
56
|
let newId = docId;
|
|
60
57
|
if (!newId) {
|
|
58
|
+
const content = (0, index_js_1.prepareCreate)(what, context.description);
|
|
61
59
|
const now = new Date();
|
|
62
60
|
Object.assign(content, {
|
|
63
61
|
created_at: now,
|
|
@@ -66,40 +64,24 @@ const internalInsert = async (payload, context) => {
|
|
|
66
64
|
newId = (await context.collection.model.insertOne(content)).insertedId;
|
|
67
65
|
}
|
|
68
66
|
else {
|
|
69
|
-
content
|
|
67
|
+
const content = (0, index_js_1.prepareUpdate)(what);
|
|
70
68
|
content.$set.updated_at = new Date();
|
|
71
69
|
await context.collection.model.updateOne({
|
|
72
70
|
_id: newId,
|
|
73
71
|
}, content);
|
|
74
72
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
},
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
const doc = await context.collection.model.findOne({
|
|
87
|
-
_id: newId,
|
|
88
|
-
}, {
|
|
89
|
-
projection,
|
|
90
|
-
});
|
|
91
|
-
if (!doc) {
|
|
92
|
-
return context.error(types_1.HTTPStatus.NotFound, {
|
|
93
|
-
code: types_1.ACError.ResourceNotFound,
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
const result = (0, common_1.throwIfError)(await (0, index_js_1.traverseDocument)(doc, context.description, {
|
|
97
|
-
context,
|
|
98
|
-
getters: true,
|
|
99
|
-
fromProperties: true,
|
|
100
|
-
recurseReferences: true,
|
|
73
|
+
const inheritedContext = {
|
|
74
|
+
...context,
|
|
75
|
+
inherited: true,
|
|
76
|
+
};
|
|
77
|
+
const newDocument = (0, common_1.throwIfError)(await (0, get_js_1.get)({
|
|
78
|
+
filters: {
|
|
79
|
+
_id: newId,
|
|
80
|
+
},
|
|
81
|
+
}, inheritedContext, {
|
|
82
|
+
bypassSecurity: true,
|
|
101
83
|
}));
|
|
102
|
-
return types_1.Result.result(
|
|
84
|
+
return types_1.Result.result(newDocument);
|
|
103
85
|
};
|
|
104
86
|
const insert = async (payload, context, options = {}) => {
|
|
105
87
|
if (options.bypassSecurity) {
|
|
@@ -3,7 +3,8 @@ 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 { throwIfError, endpointErrorSchema } from "@aeriajs/common";
|
|
6
|
-
import { traverseDocument,
|
|
6
|
+
import { traverseDocument, prepareCreate, prepareUpdate } from "../collection/index.mjs";
|
|
7
|
+
import { get } from "./get.mjs";
|
|
7
8
|
export const insertErrorSchema = () => endpointErrorSchema({
|
|
8
9
|
httpStatus: [
|
|
9
10
|
HTTPStatus.Forbidden,
|
|
@@ -45,10 +46,9 @@ const internalInsert = async (payload, context) => {
|
|
|
45
46
|
});
|
|
46
47
|
}
|
|
47
48
|
const docId = "_id" in what && what._id instanceof ObjectId ? what._id : null;
|
|
48
|
-
const content = prepareInsert(what, context.description);
|
|
49
|
-
const projection = payload.project ? normalizeProjection(payload.project, context.description) : {};
|
|
50
49
|
let newId = docId;
|
|
51
50
|
if (!newId) {
|
|
51
|
+
const content = prepareCreate(what, context.description);
|
|
52
52
|
const now = /* @__PURE__ */ new Date();
|
|
53
53
|
Object.assign(content, {
|
|
54
54
|
created_at: now,
|
|
@@ -56,40 +56,24 @@ const internalInsert = async (payload, context) => {
|
|
|
56
56
|
});
|
|
57
57
|
newId = (await context.collection.model.insertOne(content)).insertedId;
|
|
58
58
|
} else {
|
|
59
|
-
content
|
|
59
|
+
const content = prepareUpdate(what);
|
|
60
60
|
content.$set.updated_at = /* @__PURE__ */ new Date();
|
|
61
61
|
await context.collection.model.updateOne({
|
|
62
62
|
_id: newId
|
|
63
63
|
}, content);
|
|
64
64
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
const doc = await context.collection.model.findOne({
|
|
77
|
-
_id: newId
|
|
78
|
-
}, {
|
|
79
|
-
projection
|
|
80
|
-
});
|
|
81
|
-
if (!doc) {
|
|
82
|
-
return context.error(HTTPStatus.NotFound, {
|
|
83
|
-
code: ACError.ResourceNotFound
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
const result = throwIfError(await traverseDocument(doc, context.description, {
|
|
87
|
-
context,
|
|
88
|
-
getters: true,
|
|
89
|
-
fromProperties: true,
|
|
90
|
-
recurseReferences: true
|
|
65
|
+
const inheritedContext = {
|
|
66
|
+
...context,
|
|
67
|
+
inherited: true
|
|
68
|
+
};
|
|
69
|
+
const newDocument = throwIfError(await get({
|
|
70
|
+
filters: {
|
|
71
|
+
_id: newId
|
|
72
|
+
}
|
|
73
|
+
}, inheritedContext, {
|
|
74
|
+
bypassSecurity: true
|
|
91
75
|
}));
|
|
92
|
-
return Result.result(
|
|
76
|
+
return Result.result(newDocument);
|
|
93
77
|
};
|
|
94
78
|
export const insert = async (payload, context, options = {}) => {
|
|
95
79
|
if (options.bypassSecurity) {
|
package/dist/use.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const createAeria: () => Promise<import("@aeriajs/types").RouteContext<null> & import("@aeriajs/types").CollectionContext<import("@aeriajs/types").Description, any>>;
|
|
1
|
+
export declare const createAeria: () => Promise<import("@aeriajs/types").RouteContext<null> & import("@aeriajs/types").CollectionContext<import("@aeriajs/types").Description, Record<string, (payload: any, context: import("@aeriajs/types").Context<any>, ...args: any[]) => any> | undefined>>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.166",
|
|
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.166",
|
|
46
|
+
"@aeriajs/common": "^0.0.102",
|
|
47
|
+
"@aeriajs/entrypoint": "^0.0.104",
|
|
48
|
+
"@aeriajs/http": "^0.0.115",
|
|
49
|
+
"@aeriajs/security": "^0.0.166",
|
|
50
|
+
"@aeriajs/types": "^0.0.87",
|
|
51
|
+
"@aeriajs/validation": "^0.0.105"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"mongodb": "^6.5.0",
|