본문 바로가기

안녕하세요!

프로그래밍 언어/CAP

[ CAP ] Node.js - Facade Pattern - @sap/cds

Facade Pattern


 

Facade 패턴은 low level Interface들을 하나의 high level Interface로 통합시켜 주는 패턴이다.

클라이언트 객체가 여러 low level의 Interface 동작을 통제하고자 한다면,
여러 개의 low level의 Interface 메서드들을 하나 하나 호출해야 하는데,
Facade 패턴을 사용하게 되면 high level의 Interface 메서드 호출만으로한 번에 할 수 있게 된다.

따라서 Facade 패턴에서는 high level의 Interface를 low level의 Interface로부터 통합하여 'Unified Interface'라고 부른다.

 

@sap/cds


 

SAP에서는 '@sap/cds'를 사용하면 여러 가지 메서드들의 재사용을 허용하는데,
이는 위와 같이 Facade 패턴을 활용할 수 있게 해준다.

 

const cds = require('@sap/cds')

let csn = cds.complie('entity Products{}')

 

cds.home


 

 

터미널에 'cds repl'을 시작하면 '@sap/cds'에 있는 각종 메서드들을 사용해 디버깅 작업을 할 수 있다.
'cds.home'을 입력하면, cds Facade 모듈의 @sap/cds 설치 폴더의 경로를 보여준다.

 

cds.root


 

 

'cds.root'는 현재 root 경로로 사용되는 프로젝트의 root를 보여준다.

 

cds.env


 

 

'cds.env'는 로컬 package.json이나 .cdsrc.json과 service 바인딩 및 프로세스 환경을 포함한
다양한 소스에서 현재 프로세스 구성 접근에 관한 정보를 제공한다.

 

cds.requires


 

 

'cds.requires'는 위에서 사용해 보았던 'cds.env'에서 제공된 정보 중
필요한 정보만을 출력시킬 수 있다.

 

cds.services


 

let { CatalogService db } = cds.services
let all_services = [ ... cds.services ]

for (let k in cds.services)
for (let s of cds.services)

 

'cds.services'에 있는 instance들은
'cds.serve()'를 통해 구성되거나 'cds.connect()'에 의해 연결됩니다.

반응형

cds.model / cds.app / cds.db



 

const cds = require('@sap/cds')
cds.server = module.exports = async function (options) {

  const app = cds.app = o.app || require('express')()
  cds.emit ('bootstrap', app)

  // load model from all sources
  const csn = await cds.load('*')
  cds.model = cds.compile.for.nodejs(csn)
  cds.emit ('loaded', cds.model)

  // connect to prominent required services
  if (cds.requires.db)  cds.db = await cds.connect.to ('db')
  if (cds.requires.messaging)    await cds.connect.to ('messaging')

  // serve own services as declared in model
  await cds.serve ('all') .from(csn) .in (app)
  await cds.emit ('served', cds.services)

  // launch HTTP server
  cds .emit ('launching', app)
  const port = o.port ?? process.env.PORT || 4004
  const server = app.server = app.listen(port) .once ('listening', ()=>
    cds.emit('listening', { server, url: `http://localhost:4004` })
  )
}

 

'cds.model'을 사용하여 어떤 모델을 사용할 것인지에 대해 정의할 수 있다.

 

'const cds = require('@sap/cds')'

'@sap/cds' 모듈을 불러와 'cds' 객체를 생성한다.
이 모듈은 CAP 프레임워크의 핵심 모듈로
데이터, 데이터베이스 모델링, 서비스 정의, 데이터베이스 연결 등의 기능을 제공한다.

 

'cds.server = module.exports =async funtion(options)'

서버 초기화 함수를 정의한다.
이 함수는 CAP Application 주요 설정과 초기화를 수행한다.

 

'const app = cds.app = o.app || require('express')()'

Express Application을 생성하거나 기존 Application을 사용한다.
'cds.app'은 CAP Application과 Express Application을 연결한다.

 

'const csn = await cds.load('*')'

모든 데이터 모델을 로드한다.
CAP은 데이터 모델을 사용해 서비스 및 데이터베이스 연결을 설정한다.
객체 'csn'은 'Compact Service Notation'의 약자로, 데이터 모델을 설명하는 형식이다.

 

'cds.model = cds.compile.for.nodejs(csn)'

로드한 데이터 모델 'csn'을 컴파일해 Node.js Application에서 사용할 수 있는 형태로 변환한다.

 

'cds.requires.db'
'cds.requires.messaging'

데이터베이스와 메시징서비스의 연결을 설정한다.

 

'await cds.serve('all').from(csn).in(app)'

정의된 서비스를 Express Application에 연결해 라우팅을 설정한다.

 

'const port = o.port ?? process.env.PORT || 4004'

서버의 포트 번호를 설정한다.
'o.port'에 지정된 값이 없을 경우 기본값으로 '4004'를 사용한다.

 

'const server = app.server = app.listen(port)'

Express Application을 지정된 PORT로 시작해 서버를 실행한다.

 


 

728x90
반응형

'프로그래밍 언어 > CAP' 카테고리의 다른 글

[ CAP ] Class cds.Service - Handler Registration API(1)  (0) 2023.04.17
[ CAP ] Views and Projections  (2) 2023.04.12
[ CAP ] Temporal Database  (0) 2023.04.10
[ CAP ] What is the 'CSV'?  (0) 2023.04.04

loading