[CAP for Node] OData V4 쿼리 심화 — $filter, $expand, $orderby 실전 활용

AI News · 조회 1

[CAP for Node] OData V4 쿼리 심화 — $filter, $expand, $orderby 실전 활용

개요

SAP CAP(Cloud Application Programming) Node.js에서 OData V4 쿼리 옵션은 클라이언트가 서버로부터 필요한 데이터만 정확히 가져오는 핵심 메커니즘입니다. $filter로 조건 필터링, $expand로 연관 엔티티 동시 조회, $orderby로 정렬을 수행하며, CAP은 이를 CDS 쿼리(CQN)로 자동 변환합니다. OData V2는 더 이상 권장되지 않으며(deprecated), V4가 CAP의 기본 프로토콜입니다.

이 글에서 다루는 것

핵심 개념

코드 예제

Books와 Authors 엔티티를 정의하고 OData V4 쿼리를 활용하는 예제입니다.

// db/schema.cds
namespace bookshop;

entity Books : cuid, managed {
  title    : String(200);
  price    : Decimal(10,2);
  currency : String(3);
  stock    : Integer;
  author   : Association to Authors;
  genre    : String(50);
}

entity Authors : cuid, managed {
  name    : String(100);
  country : String(2);
  books   : Composition of many Books on books.author = $self;
}
// srv/cat-service.js
const cds = require('@sap/cds');

module.exports = class CatalogService extends cds.ApplicationService {
  async init() {
    const { Books } = this.entities;

    // $filter 커스텀 처리 예제 — 외부 API 소비 시
    this.on('READ', 'Books', async (req) => {
      // req.query 에 CQN 형태로 변환된 쿼리가 들어옴
      // CAP이 자동 처리하지만, 커스텀 로직 추가 가능
      const query = req.query;

      // 예: stock이 0인 도서에 'Out of Stock' 표시
      const results = await cds.run(query);
      return results.map(b => ({
        ...b,
        availability: b.stock > 0 ? 'In Stock' : 'Out of Stock'
      }));
    });

    await super.init();
  }
};

클라이언트에서 호출하는 OData V4 쿼리 예시입니다.

-- 기본 필터링
GET /catalog/Books?$filter=genre eq 'Fiction' and price lt 30

-- 저자와 함께 확장 조회 + 중첩 필터
GET /catalog/Authors?$expand=books($filter=stock gt 0;$orderby=price asc;$top=5)

-- 페이지네이션
GET /catalog/Books?$orderby=createdAt desc&$top=20&$skip=40&$count=true

-- 함수 기반 필터 (부분 문자열 검색)
GET /catalog/Books?$filter=contains(title,'Cloud') or startswith(title,'SAP')

실무 팁

자세한 내용은 본문에서

참고 자료


📌 본 게시물은 AI(Claude)가 공개된 자료를 기반으로 자동 생성한 콘텐츠입니다. 기술 내용의 정확성은 SAP 공식 문서 와 교차 확인하시기 바랍니다.

™ SAP, S/4HANA, ABAP, Fiori, SAP BTP 등은 SAP SE 또는 그 계열사의 등록 상표입니다. 본 사이트는 SAP SE 와 공식적인 관련이 없는 비공식 학습 자료 입니다.

📧 저작권 침해 / 오류 / 콘텐츠 신고: btpstacks.com 의 "문의" 메뉴를 이용해주세요.