Skip to content

Commit

Permalink
data bundleから余分な物を取り除いて整形してファイルに出力
Browse files Browse the repository at this point in the history
  • Loading branch information
na8esin committed Jul 15, 2021
1 parent 820f94c commit 2cc22e7
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 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();
}
})();
169 changes: 169 additions & 0 deletions 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
}
}
}
]

0 comments on commit 2cc22e7

Please sign in to comment.