From 2cc22e7adaa8a79a04945eaa2bf34bbf1327a5e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A1=E8=BE=BA=E5=AD=9D=E4=B9=8B?= Date: Thu, 15 Jul 2021 16:54:29 +0900 Subject: [PATCH] =?UTF-8?q?data=20bundle=E3=81=8B=E3=82=89=E4=BD=99?= =?UTF-8?q?=E5=88=86=E3=81=AA=E7=89=A9=E3=82=92=E5=8F=96=E3=82=8A=E9=99=A4?= =?UTF-8?q?=E3=81=84=E3=81=A6=E6=95=B4=E5=BD=A2=E3=81=97=E3=81=A6=E3=83=95?= =?UTF-8?q?=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=AB=E5=87=BA=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../book-bundle-query-pretty-stringify.ts | 30 ++++ .../book-bundle-query-pretty-stringify.ts.txt | 169 ++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 sample/admin/bundle/book-bundle-query-pretty-stringify.ts create mode 100644 sample/admin/bundle/book-bundle-query-pretty-stringify.ts.txt diff --git a/sample/admin/bundle/book-bundle-query-pretty-stringify.ts b/sample/admin/bundle/book-bundle-query-pretty-stringify.ts new file mode 100644 index 0000000..3bc89c6 --- /dev/null +++ b/sample/admin/bundle/book-bundle-query-pretty-stringify.ts @@ -0,0 +1,30 @@ +import { getDb } from '../getInitializeAppOptions'; +import { open } from 'fs/promises'; + +(async function () { + const firestore = getDb(); + const bundleId = "latest-books"; + const bundle = firestore.bundle(bundleId); + + var querySnapshot = await firestore.collection('books').get(); + + // Build the bundle + // Note how querySnapshot is named "latest-books-query" + var bundleBuffer = bundle + .add('latest-books-query', querySnapshot) // Add a named query. + .build(); + + const first = bundleBuffer.toString().replace(/\}\d{3}\{/ig, '},{'); + const second = first.replace(/\d{3}\{/i, '[{'); + const third = second + ']'; + + const obj = JSON.parse(third); + + let filehandle; + try { + filehandle = await open(`${__filename}.txt`, 'w'); + filehandle.write(JSON.stringify(obj, null, 2)); + } finally { + await filehandle?.close(); + } +})(); diff --git a/sample/admin/bundle/book-bundle-query-pretty-stringify.ts.txt b/sample/admin/bundle/book-bundle-query-pretty-stringify.ts.txt new file mode 100644 index 0000000..af95c3d --- /dev/null +++ b/sample/admin/bundle/book-bundle-query-pretty-stringify.ts.txt @@ -0,0 +1,169 @@ +[ + { + "metadata": { + "id": "latest-books", + "createTime": { + "seconds": "1626334378", + "nanos": 45935000 + }, + "version": 1, + "totalDocuments": 4, + "totalBytes": "2358" + } + }, + { + "namedQuery": { + "name": "latest-books-query", + "bundledQuery": { + "parent": "projects/practice-da34f/databases/(default)/documents", + "structuredQuery": { + "from": [ + { + "collectionId": "books" + } + ] + } + }, + "readTime": { + "seconds": "1626334378", + "nanos": 45935000 + } + } + }, + { + "documentMetadata": { + "name": "projects/practice-da34f/databases/(default)/documents/books/2sSJ75K4haptiuQXugyg", + "readTime": { + "seconds": "1626334378", + "nanos": 45935000 + }, + "exists": true, + "queries": [ + "latest-books-query" + ] + } + }, + { + "document": { + "name": "projects/practice-da34f/databases/(default)/documents/books/2sSJ75K4haptiuQXugyg", + "fields": { + "id": { + "stringValue": "2sSJ75K4haptiuQXugyg" + }, + "title": { + "stringValue": "Readable Code" + } + }, + "createTime": { + "seconds": "1618836159", + "nanos": 982976000 + }, + "updateTime": { + "seconds": "1618836159", + "nanos": 982976000 + } + } + }, + { + "documentMetadata": { + "name": "projects/practice-da34f/databases/(default)/documents/books/7m0k0BNvnqZvRMxOkNCF", + "readTime": { + "seconds": "1626334378", + "nanos": 45935000 + }, + "exists": true, + "queries": [ + "latest-books-query" + ] + } + }, + { + "document": { + "name": "projects/practice-da34f/databases/(default)/documents/books/7m0k0BNvnqZvRMxOkNCF", + "fields": { + "id": { + "stringValue": "7m0k0BNvnqZvRMxOkNCF" + }, + "title": { + "stringValue": "Readable Code" + } + }, + "createTime": { + "seconds": "1618836067", + "nanos": 24546000 + }, + "updateTime": { + "seconds": "1618836067", + "nanos": 24546000 + } + } + }, + { + "documentMetadata": { + "name": "projects/practice-da34f/databases/(default)/documents/books/iFwHN1y9b4BZ28lJZYUP", + "readTime": { + "seconds": "1626334378", + "nanos": 45935000 + }, + "exists": true, + "queries": [ + "latest-books-query" + ] + } + }, + { + "document": { + "name": "projects/practice-da34f/databases/(default)/documents/books/iFwHN1y9b4BZ28lJZYUP", + "fields": { + "title": { + "stringValue": "Clean Architecture" + }, + "id": { + "stringValue": "iFwHN1y9b4BZ28lJZYUP" + } + }, + "createTime": { + "seconds": "1618836067", + "nanos": 24546000 + }, + "updateTime": { + "seconds": "1618836067", + "nanos": 24546000 + } + } + }, + { + "documentMetadata": { + "name": "projects/practice-da34f/databases/(default)/documents/books/yIxOD4HnbWggMPiWS2O1", + "readTime": { + "seconds": "1626334378", + "nanos": 45935000 + }, + "exists": true, + "queries": [ + "latest-books-query" + ] + } + }, + { + "document": { + "name": "projects/practice-da34f/databases/(default)/documents/books/yIxOD4HnbWggMPiWS2O1", + "fields": { + "title": { + "stringValue": "Clean Architecture" + }, + "id": { + "stringValue": "yIxOD4HnbWggMPiWS2O1" + } + }, + "createTime": { + "seconds": "1618836159", + "nanos": 982976000 + }, + "updateTime": { + "seconds": "1618836159", + "nanos": 982976000 + } + } + } +] \ No newline at end of file