MCPcopy Create free account
hub / github.com/zenstackhq/zenstack / processCreate

Method processCreate

packages/server/src/api/rest/index.ts:1470–1531  ·  view source on GitHub ↗
(
        client: ClientContract<Schema>,
        type: string,
        _query: Record<string, string | string[]> | undefined,
        requestBody: unknown,
    )

Source from the content-addressed store, hash-verified

1468 }
1469
1470 private async processCreate(
1471 client: ClientContract<Schema>,
1472 type: string,
1473 _query: Record<string, string | string[]> | undefined,
1474 requestBody: unknown,
1475 ): Promise<Response> {
1476 const typeInfo = this.getModelInfo(type);
1477 if (!typeInfo) {
1478 return this.makeUnsupportedModelError(type);
1479 }
1480
1481 const { attributes, relationships, error } = this.processRequestBody(requestBody);
1482 if (error) {
1483 return error;
1484 }
1485
1486 const createPayload: any = { data: { ...attributes } };
1487
1488 // turn relationship payload into `connect` objects
1489 if (relationships) {
1490 for (const [key, data] of Object.entries<any>(relationships)) {
1491 if (!data?.data) {
1492 return this.makeError('invalidRelationData');
1493 }
1494
1495 const relationInfo = typeInfo.relationships[key];
1496 if (!relationInfo) {
1497 return this.makeUnsupportedRelationshipError(type, key, 400);
1498 }
1499
1500 if (relationInfo.isCollection) {
1501 createPayload.data[key] = {
1502 connect: enumerate(data.data).map((item: any) =>
1503 this.makeIdConnect(relationInfo.idFields, item.id),
1504 ),
1505 };
1506 } else {
1507 if (typeof data.data !== 'object') {
1508 return this.makeError('invalidRelationData');
1509 }
1510 createPayload.data[key] = {
1511 connect: this.makeIdConnect(relationInfo.idFields, data.data.id),
1512 };
1513 }
1514
1515 // make sure ID fields are included for result serialization
1516 createPayload.include = {
1517 ...createPayload.include,
1518 [key]: { select: { [this.makeDefaultIdKey(relationInfo.idFields)]: true } },
1519 };
1520 }
1521 }
1522
1523 // include IDs of relation fields so that they can be serialized.
1524 this.includeRelationshipIds(type, createPayload, 'include');
1525
1526 const entity = await (client as any)[type].create(createPayload);
1527 return {

Callers 1

handleRequestMethod · 0.95

Calls 10

getModelInfoMethod · 0.95
processRequestBodyMethod · 0.95
makeErrorMethod · 0.95
makeIdConnectMethod · 0.95
makeDefaultIdKeyMethod · 0.95
serializeItemsMethod · 0.95
enumerateFunction · 0.90

Tested by

no test coverage detected