# Firestore で大量 batch をいい感じにする ※ 未検証 ```typescript import * as admin from "firebase-admin"; // 利用例 // batchScope(setBatch => { // items.forEach(item => { // .setBatch(ref, item.data()}); // } // }); export const batchScope = (proc: (setBatch: SetBatch) => Promise<void>) => { let batch = admin.firestore().batch(); let index = 1; const setBatch = <T>( documentRef: FirebaseFirestore.DocumentReference<T>, data: FirebaseFirestore.WithFieldValue<T> ) => { const ret = batch.set(documentRef, data); index++; if (index % 500 == 0) { batch.commit(); batch = admin.firestore().batch(); } return ret; }; proc(setBatch).then(() => batch.commit()); }; export type SetBatch = <T>( documentRef: FirebaseFirestore.DocumentReference<T>, data: FirebaseFirestore.WithFieldValue<T> ) => FirebaseFirestore.WriteBatch; ```