document 파일은 module의 database 동작을 정의합니다. constant 파일이 data shape을 설명한다면, document 파일은 저장된 document를 어떻게 query, mutate, load, index, operate할지 설명합니다.
일반적인 document 파일에는 검색 조건, document 단위 동작, service가 사용하는 database model helper가 들어갑니다.
표준 Document 구조
collection에 저장되는 일반 model은 이 구조를 사용합니다. 비즈니스 document는 보통 query rule, 단일 document 동작, model-level helper를 함께 정의합니다.
Filter
재사용 list, lookup, sort 조건입니다.
Document
load된 document 하나의 동작입니다.
Model
service가 사용하는 model-level helper입니다.
ticket.document.ts
Query, Sort, 자동 생성 함수
자주 쓰는 list와 lookup 조건을 한 번 정의한 뒤 service나 signal에서 자동 생성된 함수를 사용합니다. 예를 들어 inProject라는 query는 listInProject, countInProject, existsInProject 같은 함수로 이어집니다.
전체 document 조회와 latest/oldest 정렬은 framework가 이미 제공하므로, 비즈니스에 필요한 검색과 정렬 규칙만 추가합니다.
story.document.ts
arg()
query에 반드시 필요한 입력입니다. 필수 arg는 optional arg보다 앞에 둡니다.
opt()
선택 입력입니다. 값이 있을 때만 조건부로 query에 반영합니다.
q helper
all, any, not, oneOf, notOneOf, between, gte, lte, contains, exists, empty 같은 helper를 사용합니다.
작은 state transition과 document-level check는 load된 document class에 둡니다. method는 보통 this를 변경하고, 여러 field를 함께 바꿀 때 this.set(...)을 사용하며, chaining을 위해 this를 반환합니다.
각 method가 같은 document를 반환하므로, service 코드에서 여러 document method를 체이닝한 뒤 마지막에 한 번만 save할 수 있습니다.
ticket.document.ts
ticket.service.ts
Model-level helper
collection-level operation은 model class에 둡니다. service 파일은 직접 database update, batch operation, counter, document generation이 필요할 때 이런 helper를 호출합니다.
story.document.ts
Generated document 확장
generated 또는 app-template domain은 기존 filter, document, model 동작을 제공할 수 있습니다. class 선언에는 generated ref를 유지하고, 현재 app에 필요한 method만 추가합니다.
user.document.ts
Loader와 custom lookup
자주 쓰이고 일관되게 유지되어야 하는 lookup shape은 loader로 선언합니다. seller별 product처럼 단일 field lookup에는 single-field loader를, shop과 order number 조합처럼 안정적인 조합에는 multi-field loader를 사용합니다.
product.document.ts
order.document.ts
Schema hook과 index
storage schema에 index 또는 가벼운 save hook이 필요할 때 _onSchema를 사용합니다. 무거운 비즈니스 workflow는 service나 model method에 두고, schema hook은 persistence concern에 집중시킵니다.
story.document.ts
user.document.ts
실전 규칙
Filter, Document, Model class 이름은 constant model 이름과 맞춥니다.
반복되는 list와 lookup 조건은 service method마다 쓰지 말고 filter section에 둡니다.
단일 document의 state transition은 document class에, collection-level operation은 model class에 둡니다.