Firebase Data Connect에서는 워크플로와 환경에 따라 다양한 방법으로 대량 데이터 로드 및 업데이트를 실행할 수 있습니다.
로컬 프로토타입 제작에서 대체 스키마를 시도할 때 VS Code 확장 프로그램, Data Connect 에뮬레이터, 로컬 데이터베이스 인스턴스를 사용하여 로컬 개발 환경에서 데이터 시드 변형을 만들고 호출할 수 있습니다.
안정적인 스키마를 사용하는 프로덕션 개발에서 대규모 CI/CD 흐름을 실행하고 프로덕션 데이터를 관리할 때는 다음 두 가지 옵션이 있습니다.
권장되는 접근 방식은 권한이 있는 환경에서 실행되는 라이브러리 집합인 Firebase Admin SDK를 사용하는 것입니다.
데이터베이스 스키마가 아닌 데이터를 수정하는 경우 Cloud SQL 인스턴스에서 SQL 도구를 사용하여 대량 로드 및 업데이트를 실행할 수도 있습니다. SQL 도구를 사용하여 데이터베이스 스키마를 직접 수정하면 Data Connect 스키마와 커넥터가 손상될 수 있습니다.
로컬 프로토타입 제작: 로컬 인스턴스에서 시드 데이터
시작 가이드에서는 임시 삽입 변형을 사용하여 단일 테이블에 단일 레코드를 추가하는 앱을 설정합니다.
사용 가능하려면 영화 리뷰 앱에 실제 데이터로 여러 테이블에서 조인 및 기타 작업을 사용하는 프로토타입 쿼리 및 변형을 위한 영화, 리뷰, 사용자 데이터가 필요합니다. 스키마를 확장하고 데이터베이스를 시드할 수 있습니다.
프로토타입 환경에는 데이터 시딩을 실행하는 코드가 필요합니다. 이 가이드에서는 다음을 보여주는 몇 가지 샘플을 제공합니다.
- 개별 테이블에서
_insertMany
및_upsertMany
사용 - 관련 테이블에서
_insertMany
사용
영화 리뷰 앱 스키마 업데이트
_insertMany
및 _upsertMany
변형을 사용하여 데이터베이스 테이블을 한 번에 하나씩 업데이트하거나 조인 관계로 연결된 여러 테이블을 업데이트할 수 있습니다. 이러한 사용 사례와 예를 설명하는 데 도움이 되는 확장된 영화 리뷰 앱 스키마는 아래에 나와 있습니다. 시작 Movie
유형을 넘어 Actor
및 MovieActor
유형을 포함하도록 schema.gql
가 확장되어 더 복잡한 쿼리를 프로토타입으로 만들 수 있습니다.
# Actors
# Suppose an actor can participate in multiple movies and movies can have multiple actors
# Movie - Actors (or vice versa) is a many to many relationship
type Actor @table {
id: UUID!
imageUrl: String!
name: String! @col(name: "name", dataType: "varchar(30)")
}
# Join table for many-to-many relationship for movies and actors
# The 'key' param signifies the primary key(s) of this table
# In this case, the keys are [movieId, actorId], the generated fields of the reference types [movie, actor]
type MovieActor @table(key: ["movie", "actor"]) {
# @ref creates a field in the current table (MovieActor) that holds the primary key of the referenced type
# In this case, @ref(fields: "movieId", references: "id") is implied
movie: Movie!
# movieId: UUID! <- this is created by the implied @ref
actor: Actor!
# actorId: UUID! <- this is created by the implied @ref
role: String! # "main" or "supporting"
}
변형을 작성하여 제로 스테이트 데이터 시드
프로토타입 제작 중에 쿼리와 변형을 다양한 개별 값에 대해 테스트해야 하는 경우 여러 레코드로 데이터를 채울 수 있습니다. 예를 들어 비교 및 필터링을 테스트하기 위해 장르와 등급이 다른 영화 레코드를 여러 개 추가할 수 있습니다.
Movie
및 Actor
테이블에 시드 데이터
프로토타입 제작 단계에 따라 시작 가이드에 소개된 것과 동일한 기법을 사용하여 레코드를 하나 또는 두 개 삽입할 수 있습니다. 즉, VS Code 확장 프로그램에서 CodeLenses를 사용하여 _insert
변형을 만들고, 데이터를 하드 코딩하고, VS Code에서 이러한 변형을 실행할 수 있습니다.
결국 _insertMany
작업을 사용하여 테이블에 많은 레코드를 추가하는 것이 더 합리적입니다. 영화 리뷰 앱 예시에서는 Movie
및 Actor
에 초기 데이터 세트를 삽입합니다.
VS Code Firebase 확장 프로그램을 사용하여 다음 변형을 실행하려면 적절한 파일 편집기 뷰에서 프로덕션 서비스 또는 로컬 데이터베이스로 프로토타입을 제작하는지에 따라 실행 (프로덕션) 또는 실행 (로컬) CodeLens 버튼을 클릭합니다.
# insertMany for Movie
# 2 records shown
mutation {
movie_insertMany(data: [
{
id: "550e8400-e29b-41d4-a716-446655440000",
title: "Inception",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Finception.jpg?alt=media&token=07b09781-b302-4623-a5c3-1956d0143168",
genre: "sci-fi",
},
{
id: "550e8400-e29b-41d4-a716-446655440001",
title: "The Matrix",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Fthe_matrix.jpg?alt=media&token=4975645d-fef8-409e-84a5-bcc1046e2059",
genre: "action",
}
])
}
# insertMany for Actor
# 2 records shown
mutation {
actor_insertMany(data: [
{
id: "123e4567-e89b-12d3-a456-426614174000",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/actors%2Fdicaprio.jpeg?alt=media&token=452e030a-efa5-4ef4-bb81-502b23241316",
name: "Leonardo DiCaprio"
},
{
id: "123e4567-e89b-12d3-a456-426614174001",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/actors%2Fkeanu.jpg?alt=media&token=6056520c-ef3e-4823-aad0-108aab163115",
name: "Keanu Reeves"
}
])
}
MovieActor
조인 테이블에 데이터 시드
조인 및 기타 복잡한 작업을 사용하여 쿼리와 변형을 테스트하려면 MovieActor
테이블에 여러 레코드를 추가하면 됩니다.
여기서 이러한 종류의 관계에서 여러 테이블을 업데이트할 때 @transaction
지시어를 추가하여 업데이트가 올바르게 완료되도록 할 수 있습니다.
mutation @transaction {
movie_insertMany(data: [
{
id: "550e8400-e29b-41d4-a716-446655440000",
title: "Inception",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Finception.jpg?alt=media&token=07b09781-b302-4623-a5c3-1956d0143168",
genre: "sci-fi",
},
{
id: "550e8400-e29b-41d4-a716-446655440001",
title: "The Matrix",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Fthe_matrix.jpg?alt=media&token=4975645d-fef8-409e-84a5-bcc1046e2059",
genre: "action",
}
])
actor_insertMany(data: [
{
id: "123e4567-e89b-12d3-a456-426614174000",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/actors%2Fdicaprio.jpeg?alt=media&token=452e030a-efa5-4ef4-bb81-502b23241316",
name: "Leonardo DiCaprio"
},
{
id: "123e4567-e89b-12d3-a456-426614174001",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/actors%2Fkeanu.jpg?alt=media&token=6056520c-ef3e-4823-aad0-108aab163115",
name: "Keanu Reeves"
}
])
}
시드 데이터를 재설정하는 변형 작성
프로토타입을 제작하고 CI/CD를 실행하는 동안 새 데이터 세트에서 새로운 테스트 시리즈를 실행하기 위해 데이터를 0 상태로 재설정하는 것이 유용할 수 있습니다.
이렇게 하려면 프로토타입 코드에서 테이블에 레코드를 추가하지 않는 경우 Data Connect에서 제공하는 _upsertMany
변이를 사용하세요.
다음 예에서는 초기 값으로 movie_upsertMany
를 호출하여 영화 레코드를 원래 상태로 업데이트합니다.
mutation {
# Execute an upsertMany operation to update the Movie table
movie_upsertMany(data: [
{
id: "550e8400-e29b-41d4-a716-446655440000",
title: "Inception",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Finception.jpg?alt=media&token=07b09781-b302-4623-a5c3-1956d0143168",
genre: "sci-fi",
},
{
id: "550e8400-e29b-41d4-a716-446655440001",
title: "The Matrix",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Fthe_matrix.jpg?alt=media&token=4975645d-fef8-409e-84a5-bcc1046e2059",
genre: "action",
}
…
}
프로덕션 개발: Admin SDK를 사용하여
Firebase Admin SDK은 권한이 있는 환경에서 작업하려는 경우에 사용할 수 있습니다. 프로덕션 데이터에 대한 대량 데이터 작업의 중요한 특성을 고려할 때 수천 개의 레코드를 로드하려는 경우 이는 중요한 사용 사례입니다.
Firebase Admin SDK 설치
주로 로컬에서 작업하는 경우에도 로컬 환경을 비롯한 권한이 있는 환경에서 Firebase Data Connect를 사용할 수 있도록 Admin SDK를 설정하는 것이 좋습니다. Node.js용 Admin SDK을 설정해야 합니다.
다른 Data Connect 사용 사례에서 Admin SDK 사용에 대해 자세히 알아보세요.
프로덕션 데이터의 일괄 로드 및 업데이트 실행
대량 데이터 관리 API는 로컬에서 여기저기에 행을 추가하기 위해 앞에서 설명한 executeGraphQL
API로 mutation {...}
문자열을 빌드하도록 요청하는 대신 사용자를 대신하여 GraphQL 변이를 빌드합니다.
관리 API의 주요 이점은 CI/CD 흐름을 위해 데이터 배열을 별도로 관리하고 재사용하거나 프로덕션 데이터를 위해 대규모 일괄 데이터 파일을 설정할 수 있다는 점입니다.
다음 스니펫은 대량 데이터 스크립트를 설정하는 방법을 보여줍니다.
import { initializeApp } from 'firebase-admin/app';
import { getDataConnect } from 'firebase-admin/data-connect';
const app = initializeApp();
const dc = getDataConnect({ location: "us-west2", serviceId: "my-service" });
const data = [
{
id: "550e8400-e29b-41d4-a716-446655440000",
title: "Inception",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Finception.jpg?alt=media&token=07b09781-b302-4623-a5c3-1956d0143168",
genre: "sci-fi",
},
{
id: "550e8400-e29b-41d4-a716-446655440001",
title: "The Matrix",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Fthe_matrix.jpg?alt=media&token=4975645d-fef8-409e-84a5-bcc1046e2059",
genre: "action",
}
];
// Methods of the bulk operations API
const resp = await dc.insert("movie" /*table name*/, data[0]);
// Or
const resp = await dc.insertMany("movie" /*table name*/, data);
// Or
const resp = await dc.upsert("movie" /*table name*/, data[0]);
// Or
const resp = await dc.upsertMany("movie" /*table name*/, data);
프로덕션 개발: SQL을 사용하여 대량 데이터 업데이트
프로덕션에서 안정적인 스키마를 사용하고 스키마를 수정하지 않는 경우 Cloud SQL 인스턴스에서 데이터 로드 및 업데이트를 관리할 수 있습니다.
데이터 가져오기를 위한 PostgreSQL용 Cloud SQL 가이드를 참고하세요.
다음 단계
- Admin SDK을 Data Connect 프로젝트에 통합하는 방법을 알아봅니다.