본문으로 건너뛰기

함수: localOnlyCollectionOptions()

호출 시그니처

function localOnlyCollectionOptions<T, TKey>(config): CollectionConfig<InferSchemaOutput<T>, TKey, T, LocalOnlyCollectionUtils> & object & object;

정의 위치: packages/db/src/local-only.ts:151

표준 Collection과 함께 사용할 로컬 전용 컬렉션 옵션을 생성합니다

외부 소스와 동기화하지 않고 루프백 동기화 구성을 사용하는 인메모리 컬렉션입니다. 이 구성은 모든 낙관적 변경 사항을 컬렉션에 즉시 "동기화"하여 영구적으로 만듭니다. 영속성이나 외부 동기화가 필요하지 않은 로컬 전용 데이터에 적합합니다.

수동 트랜잭션에서 사용:

수동 트랜잭션에서는 utils.acceptMutations()을 트랜잭션의 mutationFn에서 호출해야 tx.mutate() 중에 적용한 변경 사항이 영속화됩니다. 로컬 전용 컬렉션은 수동 트랜잭션의 표준 뮤테이션 핸들러 흐름에 참여하지 않으므로 이 호출이 필요합니다.

타입 매개변수

T

T extends StandardSchemaV1&lt;unknown, unknown>

스키마가 제공된 경우 스키마 타입이고, 그렇지 않으면 컬렉션에 있는 항목의 타입입니다

TKey

TKey extends string | number = string | number

getKey가 반환하는 키의 유형입니다

매개변수

config

LocalOnlyCollectionConfig&lt;InferSchemaOutput&lt;T>, T, TKey> & object

로컬 전용 컬렉션의 구성 옵션입니다

반환값

CollectionConfig&lt;InferSchemaOutput&lt;T>, TKey, T, LocalOnlyCollectionUtils> & object & object

acceptMutations를 포함한 유틸리티가 있는 컬렉션 옵션입니다

예시

// Basic local-only collection
const collection = createCollection(
localOnlyCollectionOptions({
getKey: (item) => item.id,
})
)
// Local-only collection with initial data
const collection = createCollection(
localOnlyCollectionOptions({
getKey: (item) => item.id,
initialData: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
],
})
)
// Local-only collection with mutation handlers
const collection = createCollection(
localOnlyCollectionOptions({
getKey: (item) => item.id,
onInsert: async ({ transaction }) => {
console.log('Item inserted:', transaction.mutations[0].modified)
// Custom logic after insert
},
})
)
// Using with manual transactions
const localData = createCollection(
localOnlyCollectionOptions({
getKey: (item) => item.id,
})
)

const tx = createTransaction({
mutationFn: async ({ transaction }) => {
// Use local data in API call
const localMutations = transaction.mutations.filter(m => m.collection === localData)
await api.save({ metadata: localMutations[0]?.modified })

// Persist local-only mutations after API success
localData.utils.acceptMutations(transaction)
}
})

tx.mutate(() => {
localData.insert({ id: 1, data: 'metadata' })
apiCollection.insert({ id: 2, data: 'main data' })
})

await tx.commit()

호출 시그니처

function localOnlyCollectionOptions<T, TKey>(config): CollectionConfig<T, TKey, never, LocalOnlyCollectionUtils> & object & object;

정의 위치: packages/db/src/local-only.ts:164

표준 Collection과 함께 사용할 로컬 전용 컬렉션 옵션을 생성합니다

외부 소스와 동기화하지 않고 루프백 동기화 구성을 사용하는 인메모리 컬렉션입니다. 이 구성은 모든 낙관적 변경 사항을 컬렉션에 즉시 "동기화"하여 영구적으로 만듭니다. 영속성이나 외부 동기화가 필요하지 않은 로컬 전용 데이터에 적합합니다.

수동 트랜잭션에서 사용:

수동 트랜잭션에서는 utils.acceptMutations()을 트랜잭션의 mutationFn에서 호출해야 tx.mutate() 중에 적용한 변경 사항이 영속화됩니다. 로컬 전용 컬렉션은 수동 트랜잭션의 표준 뮤테이션 핸들러 흐름에 참여하지 않으므로 이 호출이 필요합니다.

타입 매개변수

T

T extends object

스키마가 제공된 경우 스키마 타입이고, 그렇지 않으면 컬렉션에 있는 항목의 타입입니다

TKey

TKey extends string | number = string | number

getKey가 반환하는 키의 유형입니다

매개변수

config

LocalOnlyCollectionConfig&lt;T, never, TKey> & object

로컬 전용 컬렉션의 구성 옵션입니다

반환값

CollectionConfig&lt;T, TKey, never, LocalOnlyCollectionUtils> & object & object

acceptMutations를 포함한 유틸리티가 있는 컬렉션 옵션입니다

예시

// Basic local-only collection
const collection = createCollection(
localOnlyCollectionOptions({
getKey: (item) => item.id,
})
)
// Local-only collection with initial data
const collection = createCollection(
localOnlyCollectionOptions({
getKey: (item) => item.id,
initialData: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
],
})
)
// Local-only collection with mutation handlers
const collection = createCollection(
localOnlyCollectionOptions({
getKey: (item) => item.id,
onInsert: async ({ transaction }) => {
console.log('Item inserted:', transaction.mutations[0].modified)
// Custom logic after insert
},
})
)
// Using with manual transactions
const localData = createCollection(
localOnlyCollectionOptions({
getKey: (item) => item.id,
})
)

const tx = createTransaction({
mutationFn: async ({ transaction }) => {
// Use local data in API call
const localMutations = transaction.mutations.filter(m => m.collection === localData)
await api.save({ metadata: localMutations[0]?.modified })

// Persist local-only mutations after API success
localData.utils.acceptMutations(transaction)
}
})

tx.mutate(() => {
localData.insert({ id: 1, data: 'metadata' })
apiCollection.insert({ id: 2, data: 'main data' })
})

await tx.commit()