diff --git a/firestore-next/package.json b/firestore-next/package.json index faff0996..f87855b5 100644 --- a/firestore-next/package.json +++ b/firestore-next/package.json @@ -6,7 +6,7 @@ }, "license": "Apache-2.0", "dependencies": { - "firebase": "^12.4.0", + "firebase": "eap-firestore-pipelines", "geofire-common": "^6.0.0" }, "devDependencies": { diff --git a/firestore-next/test.firestore.js b/firestore-next/test.firestore.js index 0300fb6f..8612ba91 100644 --- a/firestore-next/test.firestore.js +++ b/firestore-next/test.firestore.js @@ -1323,3 +1323,1316 @@ describe("firestore", () => { }); }); }); + +describe("firestore-pipelines", () => { + const { + Firestore, + Timestamp, + collection, + doc, + getFirestore, + orderBy, + query, + setDoc, + startAt, + } = require("firebase/firestore") + const { + Pipeline, + field, + constant, + countAll, + AggregateFunction, + and, + or, + xor, + conditional, + execute + } = require("firebase/firestore/pipelines"); + + let app; + /** @type {Firestore} */ let db; + + before(() => { + const { initializeApp } = require("firebase/app"); + const config = { + apiKey: "AIzaSyCM61mMr_iZnP1DzjT1PMB5vDGxfyWNM64", + authDomain: "firestore-snippets.firebaseapp.com", + projectId: "firestore-snippets" + }; + app = initializeApp(config); + db = getFirestore(app, "enterprise"); + }); + + async function stagesExpressionsExample() { + // [START stages_expressions_example] + const trailing30Days = constant(Timestamp.now().toMillis()) + .unixMillisToTimestamp() + .timestampSubtract("day", 30); + const snapshot = await execute(db.pipeline() + .collection("productViews") + .where(field("viewedAt").greaterThan(trailing30Days)) + .aggregate(field("productId").countDistinct().as("uniqueProductViews")) + ); + // [END stages_expressions_example] + console.log(snapshot); + } + + async function basicRead() { + // [START basic_read] + const readDataPipeline = db.pipeline() + .collection("users"); + + // Execute the pipeline and handle the result + try { + const querySnapshot = await execute(readDataPipeline); + querySnapshot.results.forEach((result) => { + console.log(`${result.id} => ${result.data()}`); + }); + } catch (error) { + console.error("Error getting documents: ", error); + } + // [END basic_read] + } + + function pipelineConcepts() { + // [START pipeline_concepts] + const pipeline = db.pipeline() + // Step 1: Start a query with collection scope + .collection("cities") + // Step 2: Filter the collection + .where(field("population").greaterThan(100000)) + // Step 3: Sort the remaining documents + .sort(field("name").ascending()) + // Step 4: Return the top 10. Note applying the limit earlier in the + // pipeline would have unintentional results. + .limit(10); + // [END pipeline_concepts] + console.log(pipeline); + } + + function pipelineInitialization() { + // [START pipeline_initialization] + const { getFirestore } = require("firebase/firestore"); + const { execute } = require("firebase/firestore/pipelines"); + const database = getFirestore(app, "enterprise"); + const pipeline = database.pipeline(); + // [END pipeline_initialization] + console.log(pipeline); + } + + function fieldVsConstants() { + // [START field_or_constant] + const pipeline = db.pipeline() + .collection("cities") + .where(field("name").equal(constant("Toronto"))); + // [END field_or_constant] + console.log(pipeline); + } + + async function inputStages() { + // [START input_stages] + let results; + + // Return all restaurants in San Francisco + results = await execute(db.pipeline().collection("cities/sf/restaurants")); + + // Return all restaurants + results = await execute(db.pipeline().collectionGroup("restaurants")); + + // Return all documents across all collections in the database (the entire database) + results = await execute(db.pipeline().database()); + + // Batch read of 3 documents + results = await execute(db.pipeline().documents([ + doc(db, "cities", "SF"), + doc(db, "cities", "DC"), + doc(db, "cities", "NY") + ])); + // [END input_stages] + console.log(results); + } + + async function wherePipeline() { + // [START pipeline_where] + let results; + + results = await execute(db.pipeline().collection("books") + .where(field("rating").equal(5)) + .where(field("published").lessThan(1900)) + ); + + results = await execute(db.pipeline().collection("books") + .where(and(field("rating").equal(5), field("published").lessThan(1900))) + ); + // [END pipeline_where] + console.log(results); + } + + async function aggregateGroups() { + // [START aggregate_groups] + const results = await execute(db.pipeline() + .collection("books") + .aggregate( + field("rating").average().as("avg_rating") + ) + .distinct(field("genre")) + ); + // [END aggregate_groups] + console.log(results); + } + + async function aggregateDistinct() { + // [START aggregate_distinct] + const results = await execute(db.pipeline() + .collection("books") + .distinct( + field("author").toUpper().as("author"), + field("genre") + ) + ); + // [END aggregate_distinct] + console.log(results); + } + + async function sort() { + // [START sort] + const results = await execute(db.pipeline() + .collection("books") + .sort( + field("release_date").descending(), field("author").ascending() + ) + ); + // [END sort] + console.log(results); + } + + function sortComparison() { + // [START sort_comparison] + const q = query(collection(db, "cities"), + orderBy("state"), + orderBy("population", "desc")); + + const pipeline = db.pipeline() + .collection("books") + .sort( + field("release_date").descending(), field("author").ascending() + ); + // [END sort_comparison] + console.log(q); + console.log(pipeline); + } + + async function functions() { + // [START functions_example] + let results; + + // Type 1: Scalar (for use in non-aggregation stages) + // Example: Return the min store price for each book. + results = await execute(db.pipeline().collection("books") + .select(field("current").logicalMinimum(field("updated")).as("price_min")) + ); + + // Type 2: Aggregation (for use in aggregate stages) + // Example: Return the min price of all books. + results = await execute(db.pipeline().collection("books") + .aggregate(field("price").minimum().as("min_price")) + ); + // [END functions_example] + console.log(results); + } + + async function creatingIndexes() { + // [START query_example] + const results = await execute(db.pipeline() + .collection("books") + .where(field("published").lessThan(1900)) + .where(field("genre").equal("Science Fiction")) + .where(field("rating").greaterThan(4.3)) + .sort(field("published").descending()) + ); + // [END query_example] + console.log(results); + } + + async function sparseIndexes() { + // [START sparse_index_example] + const results = await execute(db.pipeline() + .collection("books") + .where(field("category").like("%fantasy%")) + ); + // [END sparse_index_example] + console.log(results); + } + + async function sparseIndexes2() { + // [START sparse_index_example_2] + const results = await execute(db.pipeline() + .collection("books") + .sort(field("release_date").ascending()) + ); + // [END sparse_index_example_2] + console.log(results); + } + + async function coveredQuery() { + // [START covered_query] + const results = await execute(db.pipeline() + .collection("books") + .where(field("category").like("%fantasy%")) + .where(field("title").exists()) + .where(field("author").exists()) + .select(field("title"), field("author")) + ); + // [END covered_query] + console.log(results); + } + + async function pagination() { + // [START pagination_not_supported_preview] + // Existing pagination via `startAt()` + const q = + query(collection(db, "cities"), orderBy("population"), startAt(1000000)); + + // Private preview workaround using pipelines + const pageSize = 2; + const pipeline = db.pipeline() + .collection("cities") + .select("name", "population", "__name__") + .sort(field("population").descending(), field("__name__").ascending()); + + // Page 1 results + let snapshot = await execute(pipeline.limit(pageSize)); + + // End of page marker + const lastDoc = snapshot.results[snapshot.results.length - 1]; + + // Page 2 results + snapshot = await execute( + pipeline + .where( + or( + and( + field("population").equal(lastDoc.get("population")), + field("__name__").greaterThan(lastDoc.ref) + ), + field("population").lessThan(lastDoc.get("population")) + ) + ) + .limit(pageSize) + ); + // [END pagination_not_supported_preview] + console.log(q); + console.log(pipeline); + } + + async function collectionStage() { + // [START collection_example] + const results = await execute(db.pipeline() + .collection("users/bob/games") + .sort(field("name").ascending()) + ); + // [END collection_example] + console.log(results); + } + + async function collectionGroupStage() { + // [START collection_group_example] + const results = await execute(db.pipeline() + .collectionGroup("games") + .sort(field("name").ascending()) + ); + // [END collection_group_example] + console.log(results); + } + + async function databaseStage() { + // [START database_example] + // Count all documents in the database + const results = await execute(db.pipeline() + .database() + .aggregate(countAll().as("total")) + ); + // [END database_example] + console.log(results); + } + + async function documentsStage() { + // [START documents_example] + const results = await execute(db.pipeline() + .documents([ + doc(db, "cities", "SF"), + doc(db, "cities", "DC"), + doc(db, "cities", "NY") + ]) + ); + // [END documents_example] + console.log(results); + } + + async function replaceWithStage() { + // [START initial_data] + await setDoc(doc(collection(db, "cities"), "SF"), { + "name": "San Francisco", + "population": 800000, + "location": { + "country": "USA", + "state": "California" + } + }); + await setDoc(doc(collection(db, "cities"), "TO"), { + "name": "Toronto", + "population": 3000000, + "province": "ON", + "location": { + "country": "Canada", + "province": "Ontario" + } + }); + await setDoc(doc(collection(db, "cities"), "NY"), { + "name": "New York", + "location": { + "country": "USA", + "state": "New York" + } + }); + await setDoc(doc(collection(db, "cities"), "AT"), { + "name": "Atlantis", + }); + // [END initial_data] + + // [START full_replace] + const names = await execute(db.pipeline() + .collection("cities") + .replaceWith(field("location")) + ); + // [END full_replace] + + // [START map_merge_overwrite] + // unsupported in client SDKs for now + // [END map_merge_overwrite] + console.log(names); + } + + async function sampleStage() { + // [START sample_example] + let results; + + // Get a sample of 100 documents in a database + results = await execute(db.pipeline() + .database() + .sample(100) + ); + + // Randomly shuffle a list of 3 documents + results = await execute(db.pipeline() + .documents([ + doc(db, "cities", "SF"), + doc(db, "cities", "NY"), + doc(db, "cities", "DC"), + ]) + .sample(3) + ); + // [END sample_example] + console.log(results); + } + + async function samplePercent() { + // [START sample_percent] + // Get a sample of on average 50% of the documents in the database + const results = await execute(db.pipeline() + .database() + .sample({ percentage: 0.5 }) + ); + // [END sample_percent] + console.log(results); + } + + async function unionStage() { + // [START union_stage] + const results = await execute(db.pipeline() + .collection("cities/SF/restaurants") + .where(field("type").equal("Chinese")) + .union(db.pipeline() + .collection("cities/NY/restaurants") + .where(field("type").equal("Italian"))) + .where(field("rating").greaterThanOrEqual(4.5)) + .sort(field("__name__").descending()) + ); + // [END union_stage] + console.log(results); + } + + async function unnestStage() { + // [START unnest_stage] + const results = await execute(db.pipeline() + .database() + .unnest(field("arrayField").as("unnestedArrayField"), "index") + ); + // [END unnest_stage] + console.log(results); + } + + async function unnestStageEmptyOrNonArray() { + // [START unnest_edge_cases] + // Input + // { identifier : 1, neighbors: [ "Alice", "Cathy" ] } + // { identifier : 2, neighbors: [] } + // { identifier : 3, neighbors: "Bob" } + + const results = await execute(db.pipeline() + .database() + .unnest(field("neighbors").as("unnestedNeighbors"), "index" ) + ); + + // Output + // { identifier: 1, neighbors: [ "Alice", "Cathy" ], unnestedNeighbors: "Alice", index: 0 } + // { identifier: 1, neighbors: [ "Alice", "Cathy" ], unnestedNeighbors: "Cathy", index: 1 } + // { identifier: 3, neighbors: "Bob", index: null} + // [END unnest_edge_cases] + console.log(results); + } + + async function countFunction() { + // [START count_function] + // Total number of books in the collection + const countOfAll = await execute(db.pipeline() + .collection("books") + .aggregate(countAll().as("count")) + ); + + // Number of books with nonnull `ratings` field + const countField = await execute(db.pipeline() + .collection("books") + .aggregate(field("ratings").count().as("count")) + ); + // [END count_function] + console.log(countOfAll); + console.log(countField); + } + + async function countIfFunction() { + // [START count_if] + const result = await execute(db.pipeline() + .collection("books") + .aggregate( + field("rating").greaterThan(4).countIf().as("filteredCount") + ) + ); + // [END count_if] + console.log(result); + } + + async function countDistinctFunction() { + // [START count_distinct] + const result = await execute(db.pipeline() + .collection("books") + .aggregate(field("author").countDistinct().as("unique_authors")) + ); + // [END count_distinct] + console.log(result); + } + + async function sumFunction() { + // [START sum_function] + const result = await execute(db.pipeline() + .collection("cities") + .aggregate(field("population").sum().as("totalPopulation")) + ); + // [END sum_function] + console.log(result); + } + + async function avgFunction() { + // [START avg_function] + const result = await execute(db.pipeline() + .collection("cities") + .aggregate(field("population").average().as("averagePopulation")) + ); + // [END avg_function] + console.log(result); + } + + async function minFunction() { + // [START min_function] + const result = await execute(db.pipeline() + .collection("books") + .aggregate(field("price").minimum().as("minimumPrice")) + ); + // [END min_function] + console.log(result); + } + + async function maxFunction() { + // [START max_function] + const result = await execute(db.pipeline() + .collection("books") + .aggregate(field("price").maximum().as("maximumPrice")) + ); + // [END max_function] + console.log(result); + } + + async function addFunction() { + // [START add_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("soldBooks").add(field("unsoldBooks")).as("totalBooks")) + ); + // [END add_function] + console.log(result); + } + + async function subtractFunction() { + // [START subtract_function] + const storeCredit = 7; + const result = await execute(db.pipeline() + .collection("books") + .select(field("price").subtract(constant(storeCredit)).as("totalCost")) + ); + // [END subtract_function] + console.log(result); + } + + async function multiplyFunction() { + // [START multiply_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("price").multiply(field("soldBooks")).as("revenue")) + ); + // [END multiply_function] + console.log(result); + } + + async function divideFunction() { + // [START divide_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("ratings").divide(field("soldBooks")).as("reviewRate")) + ); + // [END divide_function] + console.log(result); + } + + async function modFunction() { + // [START mod_function] + const displayCapacity = 1000; + const result = await execute(db.pipeline() + .collection("books") + .select(field("unsoldBooks").mod(constant(displayCapacity)).as("warehousedBooks")) + ); + // [END mod_function] + console.log(result); + } + + async function ceilFunction() { + // [START ceil_function] + const booksPerShelf = 100; + const result = await execute(db.pipeline() + .collection("books") + .select( + field("unsoldBooks").divide(constant(booksPerShelf)).ceil().as("requiredShelves") + ) + ); + // [END ceil_function] + console.log(result); + } + + async function floorFunction() { + // [START floor_function] + const result = await execute(db.pipeline() + .collection("books") + .addFields( + field("wordCount").divide(field("pages")).floor().as("wordsPerPage") + ) + ); + // [END floor_function] + console.log(result); + } + + async function roundFunction() { + // [START round_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("soldBooks").multiply(field("price")).round().as("partialRevenue")) + .aggregate(field("partialRevenue").sum().as("totalRevenue")) + ); + // [END round_function] + console.log(result); + } + + async function powFunction() { + // [START pow_function] + const googleplex = { latitude: 37.4221, longitude: 122.0853 }; + const result = await execute(db.pipeline() + .collection("cities") + .addFields( + field("lat").subtract(constant(googleplex.latitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("latitudeDifference"), + field("lng").subtract(constant(googleplex.longitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("longitudeDifference") + ) + .select( + field("latitudeDifference").add(field("longitudeDifference")).sqrt() + // Inaccurate for large distances or close to poles + .as("approximateDistanceToGoogle") + ) + ); + // [END pow_function] + console.log(result); + } + + async function sqrtFunction() { + // [START sqrt_function] + const googleplex = { latitude: 37.4221, longitude: 122.0853 }; + const result = await execute(db.pipeline() + .collection("cities") + .addFields( + field("lat").subtract(constant(googleplex.latitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("latitudeDifference"), + field("lng").subtract(constant(googleplex.longitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("longitudeDifference") + ) + .select( + field("latitudeDifference").add(field("longitudeDifference")).sqrt() + // Inaccurate for large distances or close to poles + .as("approximateDistanceToGoogle") + ) + ); + // [END sqrt_function] + console.log(result); + } + + async function expFunction() { + // [START exp_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").exp().as("expRating")) + ); + // [END exp_function] + console.log(result); + } + + async function lnFunction() { + // [START ln_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").ln().as("lnRating")) + ); + // [END ln_function] + console.log(result); + } + + async function logFunction() { + // [START log_function] + // Not supported on JS + // [END log_function] + } + + async function arrayConcat() { + // [START array_concat] + const result = await execute(db.pipeline() + .collection("books") + .select(field("genre").arrayConcat([field("subGenre")]).as("allGenres")) + ); + // [END array_concat] + console.log(result); + } + + async function arrayContains() { + // [START array_contains] + const result = await execute(db.pipeline() + .collection("books") + .select(field("genre").arrayContains(constant("mystery")).as("isMystery")) + ); + // [END array_contains] + console.log(result); + } + + async function arrayContainsAll() { + // [START array_contains_all] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("genre") + .arrayContainsAll([constant("fantasy"), constant("adventure")]) + .as("isFantasyAdventure") + ) + ); + // [END array_contains_all] + console.log(result); + } + + async function arrayContainsAny() { + // [START array_contains_any] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("genre") + .arrayContainsAny([constant("fantasy"), constant("nonfiction")]) + .as("isMysteryOrFantasy") + ) + ); + // [END array_contains_any] + console.log(result); + } + + async function arrayLength() { + // [START array_length] + const result = await execute(db.pipeline() + .collection("books") + .select(field("genre").arrayLength().as("genreCount")) + ); + // [END array_length] + console.log(result); + } + + async function arrayReverse() { + // [START array_reverse] + const result = await execute(db.pipeline() + .collection("books") + .select(field("genre").arrayReverse().as("reversedGenres")) + ); + // [END array_reverse] + console.log(result); + } + + async function equalFunction() { + // [START equal_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").equal(5).as("hasPerfectRating")) + ); + // [END equal_function] + console.log(result); + } + + async function greaterThanFunction() { + // [START greater_than] + const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").greaterThan(4).as("hasHighRating")) + ); + // [END greater_than] + console.log(result); + } + + async function greaterThanOrEqualToFunction() { + // [START greater_or_equal] + const result = await execute(db.pipeline() + .collection("books") + .select(field("published").greaterThanOrEqual(1900).as("publishedIn20thCentury")) + ); + // [END greater_or_equal] + console.log(result); + } + + async function lessThanFunction() { + // [START less_than] + const result = await execute(db.pipeline() + .collection("books") + .select(field("published").lessThan(1923).as("isPublicDomainProbably")) + ); + // [END less_than] + console.log(result); + } + + async function lessThanOrEqualToFunction() { + // [START less_or_equal] + const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").lessThanOrEqual(2).as("hasBadRating")) + ); + // [END less_or_equal] + console.log(result); + } + + async function notEqualFunction() { + // [START not_equal] + const result = await execute(db.pipeline() + .collection("books") + .select(field("title").notEqual("1984").as("not1984")) + ); + // [END not_equal] + console.log(result); + } + + async function existsFunction() { + // [START exists_function] + const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").exists().as("hasRating")) + ); + // [END exists_function] + console.log(result); + } + + async function andFunction() { + // [START and_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + and(field("rating").greaterThan(4), field("price").lessThan(10)) + .as("under10Recommendation") + ) + ); + // [END and_function] + console.log(result); + } + + async function orFunction() { + // [START or_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + or(field("genre").equal("Fantasy"), field("tags").arrayContains("adventure")) + .as("matchesSearchFilters") + ) + ); + // [END or_function] + console.log(result); + } + + async function xorFunction() { + // [START xor_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + xor(field("tags").arrayContains("magic"), field("tags").arrayContains("nonfiction")) + .as("matchesSearchFilters") + ) + ); + // [END xor_function] + console.log(result); + } + + async function notFunction() { + // [START not_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("tags").arrayContains("nonfiction").not() + .as("isFiction") + ) + ); + // [END not_function] + console.log(result); + } + + async function condFunction() { + // [START cond_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("tags").arrayConcat([ + field("pages").greaterThan(100) + .conditional(constant("longRead"), constant("shortRead")) + ]).as("extendedTags") + ) + ); + // [END cond_function] + console.log(result); + } + + async function equalAnyFunction() { + // [START eq_any] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("genre").equalAny(["Science Fiction", "Psychological Thriller"]) + .as("matchesGenreFilters") + ) + ); + // [END eq_any] + console.log(result); + } + + async function notEqualAnyFunction() { + // [START not_eq_any] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("author").notEqualAny(["George Orwell", "F. Scott Fitzgerald"]) + .as("byExcludedAuthors") + ) + ); + // [END not_eq_any] + console.log(result); + } + + async function maxLogicalFunction() { + // [START max_logical_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("rating").logicalMaximum(1).as("flooredRating") + ) + ); + // [END max_logical_function] + console.log(result); + } + + async function minLogicalFunction() { + // [START min_logical_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("rating").logicalMinimum(5).as("cappedRating") + ) + ); + // [END min_logical_function] + console.log(result); + } + + async function mapGetFunction() { + // [START map_get] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("awards").mapGet("pulitzer").as("hasPulitzerAward") + ) + ); + // [END map_get] + console.log(result); + } + + async function byteLengthFunction() { + // [START byte_length] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("title").byteLength().as("titleByteLength") + ) + ); + // [END byte_length] + console.log(result); + } + + async function charLengthFunction() { + // [START char_length] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("title").charLength().as("titleCharLength") + ) + ); + // [END char_length] + console.log(result); + } + + async function startsWithFunction() { + // [START starts_with] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("title").startsWith("The") + .as("needsSpecialAlphabeticalSort") + ) + ); + // [END starts_with] + console.log(result); + } + + async function endsWithFunction() { + // [START ends_with] + const result = await execute(db.pipeline() + .collection("inventory/devices/laptops") + .select( + field("name").endsWith("16 inch") + .as("16InLaptops") + ) + ); + // [END ends_with] + console.log(result); + } + + async function likeFunction() { + // [START like] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("genre").like("%Fiction") + .as("anyFiction") + ) + ); + // [END like] + console.log(result); + } + + async function regexContainsFunction() { + // [START regex_contains] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("title").regexContains("Firestore (Enterprise|Standard)") + .as("isFirestoreRelated") + ) + ); + // [END regex_contains] + console.log(result); + } + + async function regexMatchFunction() { + // [START regex_match] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("title").regexMatch("Firestore (Enterprise|Standard)") + .as("isFirestoreExactly") + ) + ); + // [END regex_match] + console.log(result); + } + + async function strConcatFunction() { + // [START str_concat] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("title").stringConcat(" by ", field("author")) + .as("fullyQualifiedTitle") + ) + ); + // [END str_concat] + console.log(result); + } + + async function strContainsFunction() { + // [START string_contains] + const result = await execute(db.pipeline() + .collection("articles") + .select( + field("body").stringContains("Firestore") + .as("isFirestoreRelated") + ) + ); + // [END string_contains] + console.log(result); + } + + async function toUpperFunction() { + // [START to_upper] + const result = await execute(db.pipeline() + .collection("authors") + .select( + field("name").toUpper() + .as("uppercaseName") + ) + ); + // [END to_upper] + console.log(result); + } + + async function toLowerFunction() { + // [START to_lower] + const result = await execute(db.pipeline() + .collection("authors") + .select( + field("genre").toLower().equal("fantasy") + .as("isFantasy") + ) + ); + // [END to_lower] + } + + async function substrFunction() { + // [START substr_function] + const result = await execute(db.pipeline() + .collection("books") + .where(field("title").startsWith("The ")) + .select( + field("title").substring(4) + .as("titleWithoutLeadingThe") + ) + ); + // [END substr_function] + console.log(result); + } + + async function strReverseFunction() { + // [START str_reverse] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("name").reverse().as("reversedName") + ) + ); + // [END str_reverse] + console.log(result); + } + + async function strTrimFunction() { + // [START trim_function] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("name").trim().as("whitespaceTrimmedName") + ) + ); + // [END trim_function] + console.log(result); + } + + async function strReplaceFunction() { + // not yet supported until GA + } + + async function strSplitFunction() { + // not yet supported until GA + } + + async function unixMicrosToTimestampFunction() { + // [START unix_micros_timestamp] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString") + ) + ); + // [END unix_micros_timestamp] + console.log(result); + } + + async function unixMillisToTimestampFunction() { + // [START unix_millis_timestamp] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("createdAtMillis").unixMillisToTimestamp().as("createdAtString") + ) + ); + // [END unix_millis_timestamp] + console.log(result); + } + + async function unixSecondsToTimestampFunction() { + // [START unix_seconds_timestamp] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString") + ) + ); + // [END unix_seconds_timestamp] + console.log(result); + } + + async function timestampAddFunction() { + // [START timestamp_add] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("createdAt").timestampAdd("day", 3653).as("expiresAt") + ) + ); + // [END timestamp_add] + console.log(result); + } + + async function timestampSubFunction() { + // [START timestamp_sub] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("expiresAt").timestampSubtract("day", 14).as("sendWarningTimestamp") + ) + ); + // [END timestamp_sub] + console.log(result); + } + + async function timestampToUnixMicrosFunction() { + // [START timestamp_unix_micros] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixMicros().as("unixMicros") + ) + ); + // [END timestamp_unix_micros] + console.log(result); + } + + async function timestampToUnixMillisFunction() { + // [START timestamp_unix_millis] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixMillis().as("unixMillis") + ) + ); + // [END timestamp_unix_millis] + console.log(result); + } + + async function timestampToUnixSecondsFunction() { + // [START timestamp_unix_seconds] + const result = await execute(db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixSeconds().as("unixSeconds") + ) + ); + // [END timestamp_unix_seconds] + console.log(result); + } + + async function cosineDistanceFunction() { + // [START cosine_distance] + const sampleVector = [0.0, 1, 2, 3, 4, 5]; + const result = await execute(db.pipeline() + .collection("books") + .select( + field("embedding").cosineDistance(sampleVector).as("cosineDistance") + ) + ); + // [END cosine_distance] + console.log(result); + } + + async function dotProductFunction() { + // [START dot_product] + const sampleVector = [0.0, 1, 2, 3, 4, 5]; + const result = await execute(db.pipeline() + .collection("books") + .select( + field("embedding").dotProduct(sampleVector).as("dotProduct") + ) + ); + // [END dot_product] + console.log(result); + } + + async function euclideanDistanceFunction() { + // [START euclidean_distance] + const sampleVector = [0.0, 1, 2, 3, 4, 5]; + const result = await execute(db.pipeline() + .collection("books") + .select( + field("embedding").euclideanDistance(sampleVector).as("euclideanDistance") + ) + ); + // [END euclidean_distance] + console.log(result); + } + + async function vectorLengthFunction() { + // [START vector_length] + const result = await execute(db.pipeline() + .collection("books") + .select( + field("embedding").vectorLength().as("vectorLength") + ) + ); + // [END vector_length] + console.log(result); + } +}); diff --git a/firestore-temp/package.json b/firestore-temp/package.json new file mode 100644 index 00000000..33968c49 --- /dev/null +++ b/firestore-temp/package.json @@ -0,0 +1,17 @@ +{ + "name": "firestore-temp", + "version": "1.0.0", + "scripts": { + "compile": "cp ../tsconfig.json.template ./tsconfig.json && tsc" + }, + "license": "Apache-2.0", + "devDependencies": { + "@types/chai": "^5.2.3", + "@types/mocha": "^10.0.10", + "chai": "^6.2.0", + "mocha": "^11.7.4" + }, + "dependencies": { + "@google-cloud/firestore": "^8.0.0-pipelines.1" + } +} diff --git a/firestore-temp/test.firestore.js b/firestore-temp/test.firestore.js new file mode 100644 index 00000000..19b7bffb --- /dev/null +++ b/firestore-temp/test.firestore.js @@ -0,0 +1,1311 @@ +// [SNIPPET_REGISTRY disabled] +// [SNIPPETS_SEPARATION enabled] + +const { expect } = require('chai'); + +describe("firestore-pipelines", () => { + const { + Firestore, + Timestamp + } = require("@google-cloud/firestore") + const { + Pipeline, + arrayReverse, + field, + constant, + countAll, + AggregateFunction, + and, + like, + or, + xor, + conditional + } = require("@google-cloud/firestore/pipelines"); + + let app; + /** @type {Firestore} */ let db; + + before(() => { + db = new Firestore({ + projectId: 'your-project-id', + databaseId: 'your-new-enterprise-database' + }); + }); + + async function stagesExpressionsExample() { + // [START stages_expressions_example] + const trailing30Days = constant(Timestamp.now().toMillis()) + .unixMillisToTimestamp() + .timestampSubtract("day", 30); + const snapshot = await db.pipeline() + .collection("productViews") + .where(field("viewedAt").greaterThan(trailing30Days)) + .aggregate(field("productId").countDistinct().as("uniqueProductViews")) + .execute(); + // [END stages_expressions_example] + console.log(snapshot); + } + + async function basicRead() { + // [START basic_read] + const readDataPipeline = db.pipeline() + .collection("users"); + + // Execute the pipeline and handle the result + try { + const querySnapshot = await readDataPipeline.execute(); + querySnapshot.results.forEach((result) => { + console.log(`${result.id} => ${result.data()}`); + }); + } catch (error) { + console.error("Error getting documents: ", error); + } + // [END basic_read] + } + + function pipelineConcepts() { + // [START pipeline_concepts] + const pipeline = db.pipeline() + // Step 1: Start a query with collection scope + .collection("cities") + // Step 2: Filter the collection + .where(field("population").greaterThan(100000)) + // Step 3: Sort the remaining documents + .sort(field("name").ascending()) + // Step 4: Return the top 10. Note applying the limit earlier in the + // pipeline would have unintentional results. + .limit(10); + // [END pipeline_concepts] + console.log(pipeline); + } + + function pipelineInitialization() { + // [START pipeline_initialization] + const { Firestore } = require("@google-cloud/firestore"); + const database = new Firestore({ + projectId: 'your-project-id', + databaseId: 'your-new-enterprise-database' + }); + const pipeline = database.pipeline(); + // [END pipeline_initialization] + console.log(pipeline); + } + + function fieldVsConstants() { + // [START field_or_constant] + const pipeline = db.pipeline() + .collection("cities") + .where(field("name").equal(constant("Toronto"))); + // [END field_or_constant] + console.log(pipeline); + } + + async function inputStages() { + // [START input_stages] + let results; + + // Return all restaurants in San Francisco + results = await db.pipeline().collection("cities/sf/restaurants").execute(); + + // Return all restaurants + results = await db.pipeline().collectionGroup("restaurants").execute(); + + // Return all documents across all collections in the database (the entire database) + results = await db.pipeline().database().execute(); + + // Batch read of 3 documents + results = await db.pipeline().documents([ + db.collection("cities").doc("SF"), + db.collection("cities").doc("DC"), + db.collection("cities").doc("NY"), + ]).execute(); + // [END input_stages] + console.log(results); + } + + async function wherePipeline() { + // [START pipeline_where] + let results; + + results = await db.pipeline().collection("books") + .where(field("rating").equal(5)) + .where(field("published").lessThan(1900)) + .execute(); + + results = await db.pipeline().collection("books") + .where(and(field("rating").equal(5), field("published").lessThan(1900))) + .execute(); + // [END pipeline_where] + console.log(results); + } + + async function aggregateGroups() { + // [START aggregate_groups] + const results = await db.pipeline() + .collection("books") + .aggregate( + field("rating").average().as("avg_rating") + ) + .distinct(field("genre")) + .execute(); + // [END aggregate_groups] + console.log(results); + } + + async function aggregateDistinct() { + // [START aggregate_distinct] + const results = await db.pipeline() + .collection("books") + .distinct( + field("author").toUpper().as("author"), + field("genre") + ) + .execute(); + // [END aggregate_distinct] + console.log(results); + } + + async function sort() { + // [START sort] + const results = await db.pipeline() + .collection("books") + .sort( + field("release_date").descending(), field("author").ascending() + ) + .execute(); + // [END sort] + console.log(results); + } + + function sortComparison() { + // [START sort_comparison] + const q = db.collection("cities") + .orderBy("state") + .orderBy("population", "desc"); + + const pipeline = db.pipeline() + .collection("books") + .sort( + field("release_date").descending(), field("author").ascending() + ); + // [END sort_comparison] + console.log(q); + console.log(pipeline); + } + + async function functions() { + // [START functions_example] + let results; + + // Type 1: Scalar (for use in non-aggregation stages) + // Example: Return the min store price for each book. + results = await db.pipeline().collection("books") + .select(field("current").logicalMinimum(field("updated")).as("price_min")) + .execute(); + + // Type 2: Aggregation (for use in aggregate stages) + // Example: Return the min price of all books. + results = await db.pipeline().collection("books") + .aggregate(field("price").minimum().as("min_price")) + .execute(); + // [END functions_example] + console.log(results); + } + + async function creatingIndexes() { + // [START query_example] + const results = await db.pipeline() + .collection("books") + .where(field("published").lessThan(1900)) + .where(field("genre").equal("Science Fiction")) + .where(field("rating").greaterThan(4.3)) + .sort(field("published").descending()) + .execute(); + // [END query_example] + console.log(results); + } + + async function sparseIndexes() { + // [START sparse_index_example] + const results = await db.pipeline() + .collection("books") + .where(like(field("category"), "%fantasy%")) + .execute(); + // [END sparse_index_example] + console.log(results); + } + + async function sparseIndexes2() { + // [START sparse_index_example_2] + const results = await db.pipeline() + .collection("books") + .sort(field("release_date").ascending()) + .execute(); + // [END sparse_index_example_2] + console.log(results); + } + + async function coveredQuery() { + // [START covered_query] + const results = await db.pipeline() + .collection("books") + .where(like(field("category"), "%fantasy%")) + .where(field("title").exists()) + .where(field("author").exists()) + .select(field("title"), field("author")) + .execute(); + // [END covered_query] + console.log(results); + } + + async function pagination() { + // [START pagination_not_supported_preview] + // Existing pagination via `startAt()` + const q = + db.collection("cities").orderBy("population").startAt(1000000); + + // Private preview workaround using pipelines + const pageSize = 2; + const pipeline = db.pipeline() + .collection("cities") + .select("name", "population", "__name__") + .sort(field("population").descending(), field("__name__").ascending()); + + // Page 1 results + let snapshot = await pipeline.limit(pageSize).execute(); + + // End of page marker + const lastDoc = snapshot.results[snapshot.results.length - 1]; + + // Page 2 results + snapshot = await pipeline + .where( + or( + and( + field("population").equal(lastDoc.get("population")), + field("__name__").greaterThan(lastDoc.ref) + ), + field("population").lessThan(lastDoc.get("population")) + ) + ) + .limit(pageSize) + .execute(); + // [END pagination_not_supported_preview] + console.log(q); + console.log(pipeline); + } + + async function collectionStage() { + // [START collection_example] + const results = await db.pipeline() + .collection("users/bob/games") + .sort(field("name").ascending()) + .execute(); + // [END collection_example] + console.log(results); + } + + async function collectionGroupStage() { + // [START collection_group_example] + const results = await db.pipeline() + .collectionGroup("games") + .sort(field("name").ascending()) + .execute(); + // [END collection_group_example] + console.log(results); + } + + async function databaseStage() { + // [START database_example] + // Count all documents in the database + const results = await db.pipeline() + .database() + .aggregate(countAll().as("total")) + .execute(); + // [END database_example] + console.log(results); + } + + async function documentsStage() { + // [START documents_example] + const results = await db.pipeline() + .documents([ + db.collection("cities").doc("SF"), + db.collection("cities").doc("DC"), + db.collection("cities").doc("NY") + ]) + .execute(); + // [END documents_example] + console.log(results); + } + + async function replaceWithStage() { + // [START initial_data] + await db.collection("cities").doc("SF").set({ + "name": "San Francisco", + "population": 800000, + "location": { + "country": "USA", + "state": "California" + } + }); + await db.collection("cities").doc("TO").set({ + "name": "Toronto", + "population": 3000000, + "province": "ON", + "location": { + "country": "Canada", + "province": "Ontario" + } + }); + await db.collection("cities").doc("NY").set({ + "name": "New York", + "location": { + "country": "USA", + "state": "New York" + } + }); + await db.collection("cities").doc("AT").set({ + "name": "Atlantis", + }); + // [END initial_data] + + // [START full_replace] + const names = await db.pipeline() + .collection("cities") + .replaceWith(field("location")) + .execute(); + // [END full_replace] + + // [START map_merge_overwrite] + // unsupported in client SDKs for now + // [END map_merge_overwrite] + console.log(names); + } + + async function sampleStage() { + // [START sample_example] + let results; + + // Get a sample of 100 documents in a database + results = await db.pipeline() + .database() + .sample(100) + .execute(); + + // Randomly shuffle a list of 3 documents + results = await db.pipeline() + .documents([ + db.collection("cities").doc("SF"), + db.collection("cities").doc("DC"), + db.collection("cities").doc("NY") + ]) + .sample(3) + .execute(); + // [END sample_example] + console.log(results); + } + + async function samplePercent() { + // [START sample_percent] + // Get a sample of on average 50% of the documents in the database + const results = await db.pipeline() + .database() + .sample({ percentage: 0.5 }) + .execute(); + // [END sample_percent] + console.log(results); + } + + async function unionStage() { + // [START union_stage] + const results = await db.pipeline() + .collection("cities/SF/restaurants") + .where(field("type").equal("Chinese")) + .union(db.pipeline() + .collection("cities/NY/restaurants") + .where(field("type").equal("Italian"))) + .where(field("rating").greaterThanOrEqual(4.5)) + .sort(field("__name__").descending()) + .execute(); + // [END union_stage] + console.log(results); + } + + async function unnestStage() { + // [START unnest_stage] + const results = await db.pipeline() + .database() + .unnest(field("arrayField").as("unnestedArrayField"), "index") + .execute(); + // [END unnest_stage] + console.log(results); + } + + async function unnestStageEmptyOrNonArray() { + // [START unnest_edge_cases] + // Input + // { identifier : 1, neighbors: [ "Alice", "Cathy" ] } + // { identifier : 2, neighbors: [] } + // { identifier : 3, neighbors: "Bob" } + + const results = await db.pipeline() + .database() + .unnest(field("neighbors").as("unnestedNeighbors"), "index" ) + .execute(); + + // Output + // { identifier: 1, neighbors: [ "Alice", "Cathy" ], unnestedNeighbors: "Alice", index: 0 } + // { identifier: 1, neighbors: [ "Alice", "Cathy" ], unnestedNeighbors: "Cathy", index: 1 } + // { identifier: 3, neighbors: "Bob", index: null} + // [END unnest_edge_cases] + console.log(results); + } + + async function countFunction() { + // [START count_function] + // Total number of books in the collection + const countOfAll = await db.pipeline() + .collection("books") + .aggregate(countAll().as("count")) + .execute(); + + // Number of books with nonnull `ratings` field + const countField = await db.pipeline() + .collection("books") + .aggregate(field("ratings").count().as("count")) + .execute(); + // [END count_function] + console.log(countOfAll); + console.log(countField); + } + + async function countIfFunction() { + // [START count_if] + const result = await db.pipeline() + .collection("books") + .aggregate( + field("rating").greaterThan(4).countIf().as("filteredCount") + ) + .execute(); + // [END count_if] + console.log(result); + } + + async function countDistinctFunction() { + // [START count_distinct] + const result = await db.pipeline() + .collection("books") + .aggregate(field("author").countDistinct().as("unique_authors")) + .execute(); + // [END count_distinct] + console.log(result); + } + + async function sumFunction() { + // [START sum_function] + const result = await db.pipeline() + .collection("cities") + .aggregate(field("population").sum().as("totalPopulation")) + .execute(); + // [END sum_function] + console.log(result); + } + + async function avgFunction() { + // [START avg_function] + const result = await db.pipeline() + .collection("cities") + .aggregate(field("population").average().as("averagePopulation")) + .execute(); + // [END avg_function] + console.log(result); + } + + async function minFunction() { + // [START min_function] + const result = await db.pipeline() + .collection("books") + .aggregate(field("price").minimum().as("minimumPrice")) + .execute(); + // [END min_function] + console.log(result); + } + + async function maxFunction() { + // [START max_function] + const result = await db.pipeline() + .collection("books") + .aggregate(field("price").maximum().as("maximumPrice")) + .execute(); + // [END max_function] + console.log(result); + } + + async function addFunction() { + // [START add_function] + const result = await db.pipeline() + .collection("books") + .select(field("soldBooks").add(field("unsoldBooks")).as("totalBooks")) + .execute(); + // [END add_function] + console.log(result); + } + + async function subtractFunction() { + // [START subtract_function] + const storeCredit = 7; + const result = await db.pipeline() + .collection("books") + .select(field("price").subtract(constant(storeCredit)).as("totalCost")) + .execute(); + // [END subtract_function] + console.log(result); + } + + async function multiplyFunction() { + // [START multiply_function] + const result = await db.pipeline() + .collection("books") + .select(field("price").multiply(field("soldBooks")).as("revenue")) + .execute(); + // [END multiply_function] + console.log(result); + } + + async function divideFunction() { + // [START divide_function] + const result = await db.pipeline() + .collection("books") + .select(field("ratings").divide(field("soldBooks")).as("reviewRate")) + .execute(); + // [END divide_function] + console.log(result); + } + + async function modFunction() { + // [START mod_function] + const displayCapacity = 1000; + const result = await db.pipeline() + .collection("books") + .select(field("unsoldBooks").mod(constant(displayCapacity)).as("warehousedBooks")) + .execute(); + // [END mod_function] + console.log(result); + } + + async function ceilFunction() { + // [START ceil_function] + const booksPerShelf = 100; + const result = await db.pipeline() + .collection("books") + .select( + field("unsoldBooks").divide(constant(booksPerShelf)).ceil().as("requiredShelves") + ) + .execute(); + // [END ceil_function] + console.log(result); + } + + async function floorFunction() { + // [START floor_function] + const result = await db.pipeline() + .collection("books") + .addFields( + field("wordCount").divide(field("pages")).floor().as("wordsPerPage") + ) + .execute(); + // [END floor_function] + console.log(result); + } + + async function roundFunction() { + // [START round_function] + const result = await db.pipeline() + .collection("books") + .select(field("soldBooks").multiply(field("price")).round().as("partialRevenue")) + .aggregate(field("partialRevenue").sum().as("totalRevenue")) + .execute(); + // [END round_function] + console.log(result); + } + + async function powFunction() { + // [START pow_function] + const googleplex = { latitude: 37.4221, longitude: 122.0853 }; + const result = await db.pipeline() + .collection("cities") + .addFields( + field("lat").subtract(constant(googleplex.latitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("latitudeDifference"), + field("lng").subtract(constant(googleplex.longitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("longitudeDifference") + ) + .select( + field("latitudeDifference").add(field("longitudeDifference")).sqrt() + // Inaccurate for large distances or close to poles + .as("approximateDistanceToGoogle") + ) + .execute(); + // [END pow_function] + console.log(result); + } + + async function sqrtFunction() { + // [START sqrt_function] + const googleplex = { latitude: 37.4221, longitude: 122.0853 }; + const result = await db.pipeline() + .collection("cities") + .addFields( + field("lat").subtract(constant(googleplex.latitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("latitudeDifference"), + field("lng").subtract(constant(googleplex.longitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("longitudeDifference") + ) + .select( + field("latitudeDifference").add(field("longitudeDifference")).sqrt() + // Inaccurate for large distances or close to poles + .as("approximateDistanceToGoogle") + ) + .execute(); + // [END sqrt_function] + console.log(result); + } + + async function expFunction() { + // [START exp_function] + const result = await db.pipeline() + .collection("books") + .select(field("rating").exp().as("expRating")) + .execute(); + // [END exp_function] + console.log(result); + } + + async function lnFunction() { + // [START ln_function] + const result = await db.pipeline() + .collection("books") + .select(field("rating").ln().as("lnRating")) + .execute(); + // [END ln_function] + console.log(result); + } + + async function logFunction() { + // [START log_function] + // Not supported on JS + // [END log_function] + } + + async function arrayConcat() { + // [START array_concat] + const result = await db.pipeline() + .collection("books") + .select(field("genre").arrayConcat([field("subGenre")]).as("allGenres")) + .execute(); + // [END array_concat] + console.log(result); + } + + async function arrayContains() { + // [START array_contains] + const result = await db.pipeline() + .collection("books") + .select(field("genre").arrayContains(constant("mystery")).as("isMystery")) + .execute(); + // [END array_contains] + console.log(result); + } + + async function arrayContainsAll() { + // [START array_contains_all] + const result = await db.pipeline() + .collection("books") + .select( + field("genre") + .arrayContainsAll([constant("fantasy"), constant("adventure")]) + .as("isFantasyAdventure") + ) + .execute(); + // [END array_contains_all] + console.log(result); + } + + async function arrayContainsAny() { + // [START array_contains_any] + const result = await db.pipeline() + .collection("books") + .select( + field("genre") + .arrayContainsAny([constant("fantasy"), constant("nonfiction")]) + .as("isMysteryOrFantasy") + ) + .execute(); + // [END array_contains_any] + console.log(result); + } + + async function arrayLength() { + // [START array_length] + const result = await db.pipeline() + .collection("books") + .select(field("genre").arrayLength().as("genreCount")) + .execute(); + // [END array_length] + console.log(result); + } + + async function arrayReverseSnippet() { + // [START array_reverse] + const result = await db.pipeline() + .collection("books") + .select(arrayReverse(field("genre")).as("reversedGenres")) + .execute(); + // [END array_reverse] + console.log(result); + } + + async function equalFunction() { + // [START equal_function] + const result = await db.pipeline() + .collection("books") + .select(field("rating").equal(5).as("hasPerfectRating")) + .execute(); + // [END equal_function] + console.log(result); + } + + async function greaterThanFunction() { + // [START greater_than] + const result = await db.pipeline() + .collection("books") + .select(field("rating").greaterThan(4).as("hasHighRating")) + .execute(); + // [END greater_than] + console.log(result); + } + + async function greaterThanOrEqualToFunction() { + // [START greater_or_equal] + const result = await db.pipeline() + .collection("books") + .select(field("published").greaterThanOrEqual(1900).as("publishedIn20thCentury")) + .execute(); + // [END greater_or_equal] + console.log(result); + } + + async function lessThanFunction() { + // [START less_than] + const result = await db.pipeline() + .collection("books") + .select(field("published").lessThan(1923).as("isPublicDomainProbably")) + .execute(); + // [END less_than] + console.log(result); + } + + async function lessThanOrEqualToFunction() { + // [START less_or_equal] + const result = await db.pipeline() + .collection("books") + .select(field("rating").lessThanOrEqual(2).as("hasBadRating")) + .execute(); + // [END less_or_equal] + console.log(result); + } + + async function notEqualFunction() { + // [START not_equal] + const result = await db.pipeline() + .collection("books") + .select(field("title").notEqual("1984").as("not1984")) + .execute(); + // [END not_equal] + console.log(result); + } + + async function existsFunction() { + // [START exists_function] + const result = await db.pipeline() + .collection("books") + .select(field("rating").exists().as("hasRating")) + .execute(); + // [END exists_function] + console.log(result); + } + + async function andFunction() { + // [START and_function] + const result = await db.pipeline() + .collection("books") + .select( + and(field("rating").greaterThan(4), field("price").lessThan(10)) + .as("under10Recommendation") + ) + .execute(); + // [END and_function] + console.log(result); + } + + async function orFunction() { + // [START or_function] + const result = await db.pipeline() + .collection("books") + .select( + or(field("genre").equal("Fantasy"), field("tags").arrayContains("adventure")) + .as("matchesSearchFilters") + ) + .execute(); + // [END or_function] + console.log(result); + } + + async function xorFunction() { + // [START xor_function] + const result = await db.pipeline() + .collection("books") + .select( + xor(field("tags").arrayContains("magic"), field("tags").arrayContains("nonfiction")) + .as("matchesSearchFilters") + ) + .execute(); + // [END xor_function] + console.log(result); + } + + async function notFunction() { + // [START not_function] + const result = await db.pipeline() + .collection("books") + .select( + field("tags").arrayContains("nonfiction").not() + .as("isFiction") + ) + .execute(); + // [END not_function] + console.log(result); + } + + async function condFunction() { + // [START cond_function] + const result = await db.pipeline() + .collection("books") + .select( + field("tags").arrayConcat([ + conditional( + field("pages").greaterThan(100), + constant("longRead"), + constant("shortRead") + ) + ]).as("extendedTags") + ) + .execute(); + // [END cond_function] + console.log(result); + } + + async function equalAnyFunction() { + // [START eq_any] + const result = await db.pipeline() + .collection("books") + .select( + field("genre").equalAny(["Science Fiction", "Psychological Thriller"]) + .as("matchesGenreFilters") + ) + .execute(); + // [END eq_any] + console.log(result); + } + + async function notEqualAnyFunction() { + // [START not_eq_any] + const result = await db.pipeline() + .collection("books") + .select( + field("author").notEqualAny(["George Orwell", "F. Scott Fitzgerald"]) + .as("byExcludedAuthors") + ) + .execute(); + // [END not_eq_any] + console.log(result); + } + + async function maxLogicalFunction() { + // [START max_logical_function] + const result = await db.pipeline() + .collection("books") + .select( + field("rating").logicalMaximum(1).as("flooredRating") + ) + .execute(); + // [END max_logical_function] + console.log(result); + } + + async function minLogicalFunction() { + // [START min_logical_function] + const result = await db.pipeline() + .collection("books") + .select( + field("rating").logicalMinimum(5).as("cappedRating") + ) + .execute(); + // [END min_logical_function] + console.log(result); + } + + async function mapGetFunction() { + // [START map_get] + const result = await db.pipeline() + .collection("books") + .select( + field("awards").mapGet("pulitzer").as("hasPulitzerAward") + ) + .execute(); + // [END map_get] + console.log(result); + } + + async function byteLengthFunction() { + // [START byte_length] + const result = await db.pipeline() + .collection("books") + .select( + field("title").byteLength().as("titleByteLength") + ) + .execute(); + // [END byte_length] + console.log(result); + } + + async function charLengthFunction() { + // [START char_length] + const result = await db.pipeline() + .collection("books") + .select( + field("title").charLength().as("titleCharLength") + ) + .execute(); + // [END char_length] + console.log(result); + } + + async function startsWithFunction() { + // [START starts_with] + const result = await db.pipeline() + .collection("books") + .select( + field("title").startsWith("The") + .as("needsSpecialAlphabeticalSort") + ) + .execute(); + // [END starts_with] + console.log(result); + } + + async function endsWithFunction() { + // [START ends_with] + const result = await db.pipeline() + .collection("inventory/devices/laptops") + .select( + field("name").endsWith("16 inch") + .as("16InLaptops") + ) + .execute(); + // [END ends_with] + console.log(result); + } + + async function likeFunction() { + // [START like] + const result = await db.pipeline() + .collection("books") + .select( + field("genre").like("%Fiction") + .as("anyFiction") + ) + .execute(); + // [END like] + console.log(result); + } + + async function regexContainsFunction() { + // [START regex_contains] + const result = await db.pipeline() + .collection("documents") + .select( + field("title").regexContains("Firestore (Enterprise|Standard)") + .as("isFirestoreRelated") + ) + .execute(); + // [END regex_contains] + console.log(result); + } + + async function regexMatchFunction() { + // [START regex_match] + const result = await db.pipeline() + .collection("documents") + .select( + field("title").regexMatch("Firestore (Enterprise|Standard)") + .as("isFirestoreExactly") + ) + .execute(); + // [END regex_match] + console.log(result); + } + + async function strConcatFunction() { + // [START str_concat] + const result = await db.pipeline() + .collection("books") + .select( + field("title").stringConcat(" by ", field("author")) + .as("fullyQualifiedTitle") + ) + .execute(); + // [END str_concat] + console.log(result); + } + + async function strContainsFunction() { + // [START string_contains] + const result = await db.pipeline() + .collection("articles") + .select( + field("body").stringContains("Firestore") + .as("isFirestoreRelated") + ) + .execute(); + // [END string_contains] + console.log(result); + } + + async function toUpperFunction() { + // [START to_upper] + const result = await db.pipeline() + .collection("authors") + .select( + field("name").toUpper() + .as("uppercaseName") + ) + .execute(); + // [END to_upper] + console.log(result); + } + + async function toLowerFunction() { + // [START to_lower] + const result = await db.pipeline() + .collection("authors") + .select( + field("genre").toLower().equal("fantasy") + .as("isFantasy") + ) + .execute(); + // [END to_lower] + } + + async function substrFunction() { + // [START substr_function] + const result = await db.pipeline() + .collection("books") + .where(field("title").startsWith("The ")) + .select( + field("title").substring(4) + .as("titleWithoutLeadingThe") + ) + .execute(); + // [END substr_function] + console.log(result); + } + + async function strReverseFunction() { + // [START str_reverse] + const result = await db.pipeline() + .collection("books") + .select( + field("name").reverse().as("reversedName") + ) + .execute(); + // [END str_reverse] + console.log(result); + } + + async function strTrimFunction() { + // [START trim_function] + const result = await db.pipeline() + .collection("books") + .select( + field("name").trim().as("whitespaceTrimmedName") + ) + .execute(); + // [END trim_function] + console.log(result); + } + + async function strReplaceFunction() { + // not yet supported until GA + } + + async function strSplitFunction() { + // not yet supported until GA + } + + async function unixMicrosToTimestampFunction() { + // [START unix_micros_timestamp] + const result = await db.pipeline() + .collection("documents") + .select( + field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString") + ) + .execute(); + // [END unix_micros_timestamp] + console.log(result); + } + + async function unixMillisToTimestampFunction() { + // [START unix_millis_timestamp] + const result = await db.pipeline() + .collection("documents") + .select( + field("createdAtMillis").unixMillisToTimestamp().as("createdAtString") + ) + .execute(); + // [END unix_millis_timestamp] + console.log(result); + } + + async function unixSecondsToTimestampFunction() { + // [START unix_seconds_timestamp] + const result = await db.pipeline() + .collection("documents") + .select( + field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString") + ) + .execute(); + // [END unix_seconds_timestamp] + console.log(result); + } + + async function timestampAddFunction() { + // [START timestamp_add] + const result = await db.pipeline() + .collection("documents") + .select( + field("createdAt").timestampAdd("day", 3653).as("expiresAt") + ) + .execute(); + // [END timestamp_add] + console.log(result); + } + + async function timestampSubFunction() { + // [START timestamp_sub] + const result = await db.pipeline() + .collection("documents") + .select( + field("expiresAt").timestampSubtract("day", 14).as("sendWarningTimestamp") + ) + .execute(); + // [END timestamp_sub] + console.log(result); + } + + async function timestampToUnixMicrosFunction() { + // [START timestamp_unix_micros] + const result = await db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixMicros().as("unixMicros") + ) + .execute(); + // [END timestamp_unix_micros] + console.log(result); + } + + async function timestampToUnixMillisFunction() { + // [START timestamp_unix_millis] + const result = await db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixMillis().as("unixMillis") + ) + .execute(); + // [END timestamp_unix_millis] + console.log(result); + } + + async function timestampToUnixSecondsFunction() { + // [START timestamp_unix_seconds] + const result = await db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixSeconds().as("unixSeconds") + ) + .execute(); + // [END timestamp_unix_seconds] + console.log(result); + } + + async function cosineDistanceFunction() { + // [START cosine_distance] + const sampleVector = [0.0, 1, 2, 3, 4, 5]; + const result = await db.pipeline() + .collection("books") + .select( + field("embedding").cosineDistance(sampleVector).as("cosineDistance") + ) + .execute(); + // [END cosine_distance] + console.log(result); + } + + async function dotProductFunction() { + // [START dot_product] + const sampleVector = [0.0, 1, 2, 3, 4, 5]; + const result = await db.pipeline() + .collection("books") + .select( + field("embedding").dotProduct(sampleVector).as("dotProduct") + ) + .execute(); + // [END dot_product] + console.log(result); + } + + async function euclideanDistanceFunction() { + // [START euclidean_distance] + const sampleVector = [0.0, 1, 2, 3, 4, 5]; + const result = await db.pipeline() + .collection("books") + .select( + field("embedding").euclideanDistance(sampleVector).as("euclideanDistance") + ) + .execute(); + // [END euclidean_distance] + console.log(result); + } + + async function vectorLengthFunction() { + // [START vector_length] + const result = await db.pipeline() + .collection("books") + .select( + field("embedding").vectorLength().as("vectorLength") + ) + .execute(); + // [END vector_length] + console.log(result); + } +}); diff --git a/snippets/firestore-next/test-firestore/add_function.js b/snippets/firestore-next/test-firestore/add_function.js new file mode 100644 index 00000000..3483c5f3 --- /dev/null +++ b/snippets/firestore-next/test-firestore/add_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START add_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("soldBooks").add(field("unsoldBooks")).as("totalBooks")) +); +// [END add_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/aggregate_distinct.js b/snippets/firestore-next/test-firestore/aggregate_distinct.js new file mode 100644 index 00000000..27f893a0 --- /dev/null +++ b/snippets/firestore-next/test-firestore/aggregate_distinct.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START aggregate_distinct_modular] +const results = await execute(db.pipeline() + .collection("books") + .distinct( + field("author").toUpper().as("author"), + field("genre") + ) +); +// [END aggregate_distinct_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/aggregate_groups.js b/snippets/firestore-next/test-firestore/aggregate_groups.js new file mode 100644 index 00000000..6cc5f8bb --- /dev/null +++ b/snippets/firestore-next/test-firestore/aggregate_groups.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START aggregate_groups_modular] +const results = await execute(db.pipeline() + .collection("books") + .aggregate( + field("rating").average().as("avg_rating") + ) + .distinct(field("genre")) +); +// [END aggregate_groups_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/and_function.js b/snippets/firestore-next/test-firestore/and_function.js new file mode 100644 index 00000000..f84865be --- /dev/null +++ b/snippets/firestore-next/test-firestore/and_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START and_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + and(field("rating").greaterThan(4), field("price").lessThan(10)) + .as("under10Recommendation") + ) +); +// [END and_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/array_concat.js b/snippets/firestore-next/test-firestore/array_concat.js new file mode 100644 index 00000000..05941622 --- /dev/null +++ b/snippets/firestore-next/test-firestore/array_concat.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_concat_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("genre").arrayConcat([field("subGenre")]).as("allGenres")) +); +// [END array_concat_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/array_contains.js b/snippets/firestore-next/test-firestore/array_contains.js new file mode 100644 index 00000000..5289aabc --- /dev/null +++ b/snippets/firestore-next/test-firestore/array_contains.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_contains_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("genre").arrayContains(constant("mystery")).as("isMystery")) +); +// [END array_contains_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/array_contains_all.js b/snippets/firestore-next/test-firestore/array_contains_all.js new file mode 100644 index 00000000..63e425a7 --- /dev/null +++ b/snippets/firestore-next/test-firestore/array_contains_all.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_contains_all_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("genre") + .arrayContainsAll([constant("fantasy"), constant("adventure")]) + .as("isFantasyAdventure") + ) +); +// [END array_contains_all_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/array_contains_any.js b/snippets/firestore-next/test-firestore/array_contains_any.js new file mode 100644 index 00000000..88d61b08 --- /dev/null +++ b/snippets/firestore-next/test-firestore/array_contains_any.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_contains_any_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("genre") + .arrayContainsAny([constant("fantasy"), constant("nonfiction")]) + .as("isMysteryOrFantasy") + ) +); +// [END array_contains_any_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/array_length.js b/snippets/firestore-next/test-firestore/array_length.js new file mode 100644 index 00000000..89d32f6e --- /dev/null +++ b/snippets/firestore-next/test-firestore/array_length.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_length_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("genre").arrayLength().as("genreCount")) +); +// [END array_length_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/array_reverse.js b/snippets/firestore-next/test-firestore/array_reverse.js new file mode 100644 index 00000000..6f4be979 --- /dev/null +++ b/snippets/firestore-next/test-firestore/array_reverse.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_reverse_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("genre").arrayReverse().as("reversedGenres")) +); +// [END array_reverse_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/avg_function.js b/snippets/firestore-next/test-firestore/avg_function.js new file mode 100644 index 00000000..bc9c9116 --- /dev/null +++ b/snippets/firestore-next/test-firestore/avg_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START avg_function_modular] +const result = await execute(db.pipeline() + .collection("cities") + .aggregate(field("population").average().as("averagePopulation")) +); +// [END avg_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/basic_read.js b/snippets/firestore-next/test-firestore/basic_read.js new file mode 100644 index 00000000..2789901c --- /dev/null +++ b/snippets/firestore-next/test-firestore/basic_read.js @@ -0,0 +1,20 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START basic_read_modular] +const readDataPipeline = db.pipeline() + .collection("users"); + +// Execute the pipeline and handle the result +try { + const querySnapshot = await execute(readDataPipeline); + querySnapshot.results.forEach((result) => { + console.log(`${result.id} => ${result.data()}`); + }); +} catch (error) { + console.error("Error getting documents: ", error); +} +// [END basic_read_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/byte_length.js b/snippets/firestore-next/test-firestore/byte_length.js new file mode 100644 index 00000000..12356ff6 --- /dev/null +++ b/snippets/firestore-next/test-firestore/byte_length.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START byte_length_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("title").byteLength().as("titleByteLength") + ) +); +// [END byte_length_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/ceil_function.js b/snippets/firestore-next/test-firestore/ceil_function.js new file mode 100644 index 00000000..569a2a25 --- /dev/null +++ b/snippets/firestore-next/test-firestore/ceil_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START ceil_function_modular] +const booksPerShelf = 100; +const result = await execute(db.pipeline() + .collection("books") + .select( + field("unsoldBooks").divide(constant(booksPerShelf)).ceil().as("requiredShelves") + ) +); +// [END ceil_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/char_length.js b/snippets/firestore-next/test-firestore/char_length.js new file mode 100644 index 00000000..2cf65650 --- /dev/null +++ b/snippets/firestore-next/test-firestore/char_length.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START char_length_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("title").charLength().as("titleCharLength") + ) +); +// [END char_length_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/collection_example.js b/snippets/firestore-next/test-firestore/collection_example.js new file mode 100644 index 00000000..f969634e --- /dev/null +++ b/snippets/firestore-next/test-firestore/collection_example.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START collection_example_modular] +const results = await execute(db.pipeline() + .collection("users/bob/games") + .sort(field("name").ascending()) + ); +// [END collection_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/collection_group_example.js b/snippets/firestore-next/test-firestore/collection_group_example.js new file mode 100644 index 00000000..d567ba48 --- /dev/null +++ b/snippets/firestore-next/test-firestore/collection_group_example.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START collection_group_example_modular] +const results = await execute(db.pipeline() + .collectionGroup("games") + .sort(field("name").ascending()) + ); +// [END collection_group_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/cond_function.js b/snippets/firestore-next/test-firestore/cond_function.js new file mode 100644 index 00000000..245b94fd --- /dev/null +++ b/snippets/firestore-next/test-firestore/cond_function.js @@ -0,0 +1,17 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START cond_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("tags").arrayConcat([ + field("pages").greaterThan(100) + .conditional(constant("longRead"), constant("shortRead")) + ]).as("extendedTags") + ) +); +// [END cond_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/cosine_distance.js b/snippets/firestore-next/test-firestore/cosine_distance.js new file mode 100644 index 00000000..e69fb641 --- /dev/null +++ b/snippets/firestore-next/test-firestore/cosine_distance.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START cosine_distance_modular] +const sampleVector = [0.0, 1, 2, 3, 4, 5]; +const result = await execute(db.pipeline() + .collection("books") + .select( + field("embedding").cosineDistance(sampleVector).as("cosineDistance") + ) +); +// [END cosine_distance_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/count_distinct.js b/snippets/firestore-next/test-firestore/count_distinct.js new file mode 100644 index 00000000..8feeb38b --- /dev/null +++ b/snippets/firestore-next/test-firestore/count_distinct.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START count_distinct_modular] +const result = await execute(db.pipeline() + .collection("books") + .aggregate(field("author").countDistinct().as("unique_authors")) +); +// [END count_distinct_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/count_function.js b/snippets/firestore-next/test-firestore/count_function.js new file mode 100644 index 00000000..3a9b8d09 --- /dev/null +++ b/snippets/firestore-next/test-firestore/count_function.js @@ -0,0 +1,19 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START count_function_modular] +// Total number of books in the collection +const countOfAll = await execute(db.pipeline() + .collection("books") + .aggregate(countAll().as("count")) +); + +// Number of books with nonnull `ratings` field +const countField = await execute(db.pipeline() + .collection("books") + .aggregate(field("ratings").count().as("count")) +); +// [END count_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/count_if.js b/snippets/firestore-next/test-firestore/count_if.js new file mode 100644 index 00000000..6303b8ec --- /dev/null +++ b/snippets/firestore-next/test-firestore/count_if.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START count_if_modular] +const result = await execute(db.pipeline() + .collection("books") + .aggregate( + field("rating").greaterThan(4).countIf().as("filteredCount") + ) +); +// [END count_if_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/covered_query.js b/snippets/firestore-next/test-firestore/covered_query.js new file mode 100644 index 00000000..9f92d282 --- /dev/null +++ b/snippets/firestore-next/test-firestore/covered_query.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START covered_query_modular] +const results = await execute(db.pipeline() + .collection("books") + .where(field("category").like("%fantasy%")) + .where(field("title").exists()) + .where(field("author").exists()) + .select(field("title"), field("author")) +); +// [END covered_query_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/database_example.js b/snippets/firestore-next/test-firestore/database_example.js new file mode 100644 index 00000000..14426381 --- /dev/null +++ b/snippets/firestore-next/test-firestore/database_example.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START database_example_modular] +// Count all documents in the database +const results = await execute(db.pipeline() + .database() + .aggregate(countAll().as("total")) + ); +// [END database_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/divide_function.js b/snippets/firestore-next/test-firestore/divide_function.js new file mode 100644 index 00000000..a4668a58 --- /dev/null +++ b/snippets/firestore-next/test-firestore/divide_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START divide_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("ratings").divide(field("soldBooks")).as("reviewRate")) +); +// [END divide_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/documents_example.js b/snippets/firestore-next/test-firestore/documents_example.js new file mode 100644 index 00000000..83a272a2 --- /dev/null +++ b/snippets/firestore-next/test-firestore/documents_example.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START documents_example_modular] +const results = await execute(db.pipeline() + .documents([ + doc(db, "cities", "SF"), + doc(db, "cities", "DC"), + doc(db, "cities", "NY") + ]) +); +// [END documents_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/dot_product.js b/snippets/firestore-next/test-firestore/dot_product.js new file mode 100644 index 00000000..bf694e79 --- /dev/null +++ b/snippets/firestore-next/test-firestore/dot_product.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START dot_product_modular] +const sampleVector = [0.0, 1, 2, 3, 4, 5]; +const result = await execute(db.pipeline() + .collection("books") + .select( + field("embedding").dotProduct(sampleVector).as("dotProduct") + ) +); +// [END dot_product_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/ends_with.js b/snippets/firestore-next/test-firestore/ends_with.js new file mode 100644 index 00000000..98b95e7f --- /dev/null +++ b/snippets/firestore-next/test-firestore/ends_with.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START ends_with_modular] +const result = await execute(db.pipeline() + .collection("inventory/devices/laptops") + .select( + field("name").endsWith("16 inch") + .as("16InLaptops") + ) +); +// [END ends_with_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/eq_any.js b/snippets/firestore-next/test-firestore/eq_any.js new file mode 100644 index 00000000..d22004fb --- /dev/null +++ b/snippets/firestore-next/test-firestore/eq_any.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START eq_any_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("genre").equalAny(["Science Fiction", "Psychological Thriller"]) + .as("matchesGenreFilters") + ) +); +// [END eq_any_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/equal_function.js b/snippets/firestore-next/test-firestore/equal_function.js new file mode 100644 index 00000000..94881396 --- /dev/null +++ b/snippets/firestore-next/test-firestore/equal_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START equal_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").equal(5).as("hasPerfectRating")) +); +// [END equal_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/euclidean_distance.js b/snippets/firestore-next/test-firestore/euclidean_distance.js new file mode 100644 index 00000000..a1c51f1c --- /dev/null +++ b/snippets/firestore-next/test-firestore/euclidean_distance.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START euclidean_distance_modular] +const sampleVector = [0.0, 1, 2, 3, 4, 5]; +const result = await execute(db.pipeline() + .collection("books") + .select( + field("embedding").euclideanDistance(sampleVector).as("euclideanDistance") + ) +); +// [END euclidean_distance_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/exists_function.js b/snippets/firestore-next/test-firestore/exists_function.js new file mode 100644 index 00000000..b903fc68 --- /dev/null +++ b/snippets/firestore-next/test-firestore/exists_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START exists_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").exists().as("hasRating")) +); +// [END exists_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/exp_function.js b/snippets/firestore-next/test-firestore/exp_function.js new file mode 100644 index 00000000..670e5440 --- /dev/null +++ b/snippets/firestore-next/test-firestore/exp_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START exp_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").exp().as("expRating")) +); +// [END exp_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/field_or_constant.js b/snippets/firestore-next/test-firestore/field_or_constant.js new file mode 100644 index 00000000..e77c253a --- /dev/null +++ b/snippets/firestore-next/test-firestore/field_or_constant.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START field_or_constant_modular] +const pipeline = db.pipeline() + .collection("cities") + .where(field("name").equal(constant("Toronto"))); +// [END field_or_constant_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/floor_function.js b/snippets/firestore-next/test-firestore/floor_function.js new file mode 100644 index 00000000..f5602ea3 --- /dev/null +++ b/snippets/firestore-next/test-firestore/floor_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START floor_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .addFields( + field("wordCount").divide(field("pages")).floor().as("wordsPerPage") + ) +); +// [END floor_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/full_replace.js b/snippets/firestore-next/test-firestore/full_replace.js new file mode 100644 index 00000000..3befe953 --- /dev/null +++ b/snippets/firestore-next/test-firestore/full_replace.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START full_replace_modular] +const names = await execute(db.pipeline() + .collection("cities") + .replaceWith(field("location")) +); +// [END full_replace_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/functions_example.js b/snippets/firestore-next/test-firestore/functions_example.js new file mode 100644 index 00000000..b1a80aeb --- /dev/null +++ b/snippets/firestore-next/test-firestore/functions_example.js @@ -0,0 +1,21 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START functions_example_modular] +let results; + +// Type 1: Scalar (for use in non-aggregation stages) +// Example: Return the min store price for each book. +results = await execute(db.pipeline().collection("books") + .select(field("current").logicalMinimum(field("updated")).as("price_min")) +); + +// Type 2: Aggregation (for use in aggregate stages) +// Example: Return the min price of all books. +results = await execute(db.pipeline().collection("books") + .aggregate(field("price").minimum().as("min_price")) +); +// [END functions_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/greater_or_equal.js b/snippets/firestore-next/test-firestore/greater_or_equal.js new file mode 100644 index 00000000..b4d7534a --- /dev/null +++ b/snippets/firestore-next/test-firestore/greater_or_equal.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START greater_or_equal_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("published").greaterThanOrEqual(1900).as("publishedIn20thCentury")) +); +// [END greater_or_equal_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/greater_than.js b/snippets/firestore-next/test-firestore/greater_than.js new file mode 100644 index 00000000..41d1aa0c --- /dev/null +++ b/snippets/firestore-next/test-firestore/greater_than.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START greater_than_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").greaterThan(4).as("hasHighRating")) +); +// [END greater_than_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/initial_data.js b/snippets/firestore-next/test-firestore/initial_data.js new file mode 100644 index 00000000..f47803c5 --- /dev/null +++ b/snippets/firestore-next/test-firestore/initial_data.js @@ -0,0 +1,35 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START initial_data_modular] +await setDoc(doc(collection(db, "cities"), "SF"), { + "name": "San Francisco", + "population": 800000, + "location": { + "country": "USA", + "state": "California" + } +}); +await setDoc(doc(collection(db, "cities"), "TO"), { + "name": "Toronto", + "population": 3000000, + "province": "ON", + "location": { + "country": "Canada", + "province": "Ontario" + } +}); +await setDoc(doc(collection(db, "cities"), "NY"), { + "name": "New York", + "location": { + "country": "USA", + "state": "New York" + } +}); +await setDoc(doc(collection(db, "cities"), "AT"), { + "name": "Atlantis", +}); +// [END initial_data_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/input_stages.js b/snippets/firestore-next/test-firestore/input_stages.js new file mode 100644 index 00000000..4b596653 --- /dev/null +++ b/snippets/firestore-next/test-firestore/input_stages.js @@ -0,0 +1,25 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START input_stages_modular] +let results; + +// Return all restaurants in San Francisco +results = await execute(db.pipeline().collection("cities/sf/restaurants")); + +// Return all restaurants +results = await execute(db.pipeline().collectionGroup("restaurants")); + +// Return all documents across all collections in the database (the entire database) +results = await execute(db.pipeline().database()); + +// Batch read of 3 documents +results = await execute(db.pipeline().documents([ + doc(db, "cities", "SF"), + doc(db, "cities", "DC"), + doc(db, "cities", "NY") +])); +// [END input_stages_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/less_or_equal.js b/snippets/firestore-next/test-firestore/less_or_equal.js new file mode 100644 index 00000000..8bb30179 --- /dev/null +++ b/snippets/firestore-next/test-firestore/less_or_equal.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START less_or_equal_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").lessThanOrEqual(2).as("hasBadRating")) +); +// [END less_or_equal_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/less_than.js b/snippets/firestore-next/test-firestore/less_than.js new file mode 100644 index 00000000..9d104ba6 --- /dev/null +++ b/snippets/firestore-next/test-firestore/less_than.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START less_than_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("published").lessThan(1923).as("isPublicDomainProbably")) +); +// [END less_than_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/like.js b/snippets/firestore-next/test-firestore/like.js new file mode 100644 index 00000000..d7bf9516 --- /dev/null +++ b/snippets/firestore-next/test-firestore/like.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START like_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("genre").like("%Fiction") + .as("anyFiction") + ) +); +// [END like_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/ln_function.js b/snippets/firestore-next/test-firestore/ln_function.js new file mode 100644 index 00000000..049e0bdd --- /dev/null +++ b/snippets/firestore-next/test-firestore/ln_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START ln_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("rating").ln().as("lnRating")) +); +// [END ln_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/log_function.js b/snippets/firestore-next/test-firestore/log_function.js new file mode 100644 index 00000000..0ef8badd --- /dev/null +++ b/snippets/firestore-next/test-firestore/log_function.js @@ -0,0 +1,9 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START log_function_modular] +// Not supported on JS +// [END log_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/map_get.js b/snippets/firestore-next/test-firestore/map_get.js new file mode 100644 index 00000000..098c1d5b --- /dev/null +++ b/snippets/firestore-next/test-firestore/map_get.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START map_get_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("awards").mapGet("pulitzer").as("hasPulitzerAward") + ) +); +// [END map_get_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/map_merge_overwrite.js b/snippets/firestore-next/test-firestore/map_merge_overwrite.js new file mode 100644 index 00000000..ee6fb613 --- /dev/null +++ b/snippets/firestore-next/test-firestore/map_merge_overwrite.js @@ -0,0 +1,9 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START map_merge_overwrite_modular] +// unsupported in client SDKs for now +// [END map_merge_overwrite_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/max_function.js b/snippets/firestore-next/test-firestore/max_function.js new file mode 100644 index 00000000..9ab828e6 --- /dev/null +++ b/snippets/firestore-next/test-firestore/max_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START max_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .aggregate(field("price").maximum().as("maximumPrice")) +); +// [END max_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/max_logical_function.js b/snippets/firestore-next/test-firestore/max_logical_function.js new file mode 100644 index 00000000..e884226f --- /dev/null +++ b/snippets/firestore-next/test-firestore/max_logical_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START max_logical_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("rating").logicalMaximum(1).as("flooredRating") + ) +); +// [END max_logical_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/min_function.js b/snippets/firestore-next/test-firestore/min_function.js new file mode 100644 index 00000000..9f22c16c --- /dev/null +++ b/snippets/firestore-next/test-firestore/min_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START min_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .aggregate(field("price").minimum().as("minimumPrice")) +); +// [END min_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/min_logical_function.js b/snippets/firestore-next/test-firestore/min_logical_function.js new file mode 100644 index 00000000..91ae75a1 --- /dev/null +++ b/snippets/firestore-next/test-firestore/min_logical_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START min_logical_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("rating").logicalMinimum(5).as("cappedRating") + ) +); +// [END min_logical_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/mod_function.js b/snippets/firestore-next/test-firestore/mod_function.js new file mode 100644 index 00000000..16bb7f8b --- /dev/null +++ b/snippets/firestore-next/test-firestore/mod_function.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START mod_function_modular] +const displayCapacity = 1000; +const result = await execute(db.pipeline() + .collection("books") + .select(field("unsoldBooks").mod(constant(displayCapacity)).as("warehousedBooks")) +); +// [END mod_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/multiply_function.js b/snippets/firestore-next/test-firestore/multiply_function.js new file mode 100644 index 00000000..d89430ff --- /dev/null +++ b/snippets/firestore-next/test-firestore/multiply_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START multiply_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("price").multiply(field("soldBooks")).as("revenue")) +); +// [END multiply_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/not_eq_any.js b/snippets/firestore-next/test-firestore/not_eq_any.js new file mode 100644 index 00000000..5b0c4149 --- /dev/null +++ b/snippets/firestore-next/test-firestore/not_eq_any.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START not_eq_any_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("author").notEqualAny(["George Orwell", "F. Scott Fitzgerald"]) + .as("byExcludedAuthors") + ) +); +// [END not_eq_any_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/not_equal.js b/snippets/firestore-next/test-firestore/not_equal.js new file mode 100644 index 00000000..c8acc8d3 --- /dev/null +++ b/snippets/firestore-next/test-firestore/not_equal.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START not_equal_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("title").notEqual("1984").as("not1984")) +); +// [END not_equal_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/not_function.js b/snippets/firestore-next/test-firestore/not_function.js new file mode 100644 index 00000000..f6905f23 --- /dev/null +++ b/snippets/firestore-next/test-firestore/not_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START not_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("tags").arrayContains("nonfiction").not() + .as("isFiction") + ) +); +// [END not_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/or_function.js b/snippets/firestore-next/test-firestore/or_function.js new file mode 100644 index 00000000..035512d7 --- /dev/null +++ b/snippets/firestore-next/test-firestore/or_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START or_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + or(field("genre").equal("Fantasy"), field("tags").arrayContains("adventure")) + .as("matchesSearchFilters") + ) +); +// [END or_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/pagination_not_supported_preview.js b/snippets/firestore-next/test-firestore/pagination_not_supported_preview.js new file mode 100644 index 00000000..02687926 --- /dev/null +++ b/snippets/firestore-next/test-firestore/pagination_not_supported_preview.js @@ -0,0 +1,39 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pagination_not_supported_preview_modular] +// Existing pagination via `startAt()` +const q = + query(collection(db, "cities"), orderBy("population"), startAt(1000000)); + +// Private preview workaround using pipelines +const pageSize = 2; +const pipeline = db.pipeline() + .collection("cities") + .select("name", "population", "__name__") + .sort(field("population").descending(), field("__name__").ascending()); + +// Page 1 results +let snapshot = await execute(pipeline.limit(pageSize)); + +// End of page marker +const lastDoc = snapshot.results[snapshot.results.length - 1]; + +// Page 2 results +snapshot = await execute( + pipeline + .where( + or( + and( + field("population").equal(lastDoc.get("population")), + field("__name__").greaterThan(lastDoc.ref) + ), + field("population").lessThan(lastDoc.get("population")) + ) + ) + .limit(pageSize) +); +// [END pagination_not_supported_preview_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/pipeline_concepts.js b/snippets/firestore-next/test-firestore/pipeline_concepts.js new file mode 100644 index 00000000..e93b2d9f --- /dev/null +++ b/snippets/firestore-next/test-firestore/pipeline_concepts.js @@ -0,0 +1,18 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pipeline_concepts_modular] +const pipeline = db.pipeline() + // Step 1: Start a query with collection scope + .collection("cities") + // Step 2: Filter the collection + .where(field("population").greaterThan(100000)) + // Step 3: Sort the remaining documents + .sort(field("name").ascending()) + // Step 4: Return the top 10. Note applying the limit earlier in the + // pipeline would have unintentional results. + .limit(10); +// [END pipeline_concepts_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/pipeline_initialization.js b/snippets/firestore-next/test-firestore/pipeline_initialization.js new file mode 100644 index 00000000..d9b20bdf --- /dev/null +++ b/snippets/firestore-next/test-firestore/pipeline_initialization.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pipeline_initialization_modular] +import { getFirestore } from "firebase/firestore"; +import { execute } from "firebase/firestore/pipelines"; +const database = getFirestore(app, "enterprise"); +const pipeline = database.pipeline(); +// [END pipeline_initialization_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/pipeline_where.js b/snippets/firestore-next/test-firestore/pipeline_where.js new file mode 100644 index 00000000..7e7bd55e --- /dev/null +++ b/snippets/firestore-next/test-firestore/pipeline_where.js @@ -0,0 +1,18 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pipeline_where_modular] +let results; + +results = await execute(db.pipeline().collection("books") + .where(field("rating").equal(5)) + .where(field("published").lessThan(1900)) +); + +results = await execute(db.pipeline().collection("books") + .where(and(field("rating").equal(5), field("published").lessThan(1900))) +); +// [END pipeline_where_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/pow_function.js b/snippets/firestore-next/test-firestore/pow_function.js new file mode 100644 index 00000000..1e45eea2 --- /dev/null +++ b/snippets/firestore-next/test-firestore/pow_function.js @@ -0,0 +1,27 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pow_function_modular] +const googleplex = { latitude: 37.4221, longitude: 122.0853 }; +const result = await execute(db.pipeline() + .collection("cities") + .addFields( + field("lat").subtract(constant(googleplex.latitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("latitudeDifference"), + field("lng").subtract(constant(googleplex.longitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("longitudeDifference") + ) + .select( + field("latitudeDifference").add(field("longitudeDifference")).sqrt() + // Inaccurate for large distances or close to poles + .as("approximateDistanceToGoogle") + ) +); +// [END pow_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/query_example.js b/snippets/firestore-next/test-firestore/query_example.js new file mode 100644 index 00000000..0a47a09e --- /dev/null +++ b/snippets/firestore-next/test-firestore/query_example.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START query_example_modular] +const results = await execute(db.pipeline() + .collection("books") + .where(field("published").lessThan(1900)) + .where(field("genre").equal("Science Fiction")) + .where(field("rating").greaterThan(4.3)) + .sort(field("published").descending()) +); +// [END query_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/regex_contains.js b/snippets/firestore-next/test-firestore/regex_contains.js new file mode 100644 index 00000000..22edc7e9 --- /dev/null +++ b/snippets/firestore-next/test-firestore/regex_contains.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START regex_contains_modular] +const result = await execute(db.pipeline() + .collection("documents") + .select( + field("title").regexContains("Firestore (Enterprise|Standard)") + .as("isFirestoreRelated") + ) +); +// [END regex_contains_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/regex_match.js b/snippets/firestore-next/test-firestore/regex_match.js new file mode 100644 index 00000000..84d1d77b --- /dev/null +++ b/snippets/firestore-next/test-firestore/regex_match.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START regex_match_modular] +const result = await execute(db.pipeline() + .collection("documents") + .select( + field("title").regexMatch("Firestore (Enterprise|Standard)") + .as("isFirestoreExactly") + ) +); +// [END regex_match_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/round_function.js b/snippets/firestore-next/test-firestore/round_function.js new file mode 100644 index 00000000..94d85b63 --- /dev/null +++ b/snippets/firestore-next/test-firestore/round_function.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START round_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select(field("soldBooks").multiply(field("price")).round().as("partialRevenue")) + .aggregate(field("partialRevenue").sum().as("totalRevenue")) + ); +// [END round_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sample_example.js b/snippets/firestore-next/test-firestore/sample_example.js new file mode 100644 index 00000000..47873e55 --- /dev/null +++ b/snippets/firestore-next/test-firestore/sample_example.js @@ -0,0 +1,25 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sample_example_modular] +let results; + +// Get a sample of 100 documents in a database +results = await execute(db.pipeline() + .database() + .sample(100) +); + +// Randomly shuffle a list of 3 documents +results = await execute(db.pipeline() + .documents([ + doc(db, "cities", "SF"), + doc(db, "cities", "NY"), + doc(db, "cities", "DC"), + ]) + .sample(3) +); +// [END sample_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sample_percent.js b/snippets/firestore-next/test-firestore/sample_percent.js new file mode 100644 index 00000000..1c105a5f --- /dev/null +++ b/snippets/firestore-next/test-firestore/sample_percent.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sample_percent_modular] +// Get a sample of on average 50% of the documents in the database +const results = await execute(db.pipeline() + .database() + .sample({ percentage: 0.5 }) +); +// [END sample_percent_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sort.js b/snippets/firestore-next/test-firestore/sort.js new file mode 100644 index 00000000..87326619 --- /dev/null +++ b/snippets/firestore-next/test-firestore/sort.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sort_modular] +const results = await execute(db.pipeline() + .collection("books") + .sort( + field("release_date").descending(), field("author").ascending() + ) +); +// [END sort_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sort_comparison.js b/snippets/firestore-next/test-firestore/sort_comparison.js new file mode 100644 index 00000000..9d0ff01e --- /dev/null +++ b/snippets/firestore-next/test-firestore/sort_comparison.js @@ -0,0 +1,17 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sort_comparison_modular] +const q = query(collection(db, "cities"), + orderBy("state"), + orderBy("population", "desc")); + +const pipeline = db.pipeline() + .collection("books") + .sort( + field("release_date").descending(), field("author").ascending() + ); +// [END sort_comparison_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sparse_index_example.js b/snippets/firestore-next/test-firestore/sparse_index_example.js new file mode 100644 index 00000000..420cf8e1 --- /dev/null +++ b/snippets/firestore-next/test-firestore/sparse_index_example.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sparse_index_example_modular] +const results = await execute(db.pipeline() + .collection("books") + .where(field("category").like("%fantasy%")) +); +// [END sparse_index_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sqrt_function.js b/snippets/firestore-next/test-firestore/sqrt_function.js new file mode 100644 index 00000000..15af33e5 --- /dev/null +++ b/snippets/firestore-next/test-firestore/sqrt_function.js @@ -0,0 +1,27 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sqrt_function_modular] +const googleplex = { latitude: 37.4221, longitude: 122.0853 }; +const result = await execute(db.pipeline() + .collection("cities") + .addFields( + field("lat").subtract(constant(googleplex.latitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("latitudeDifference"), + field("lng").subtract(constant(googleplex.longitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("longitudeDifference") + ) + .select( + field("latitudeDifference").add(field("longitudeDifference")).sqrt() + // Inaccurate for large distances or close to poles + .as("approximateDistanceToGoogle") + ) +); +// [END sqrt_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/stages_expressions_example.js b/snippets/firestore-next/test-firestore/stages_expressions_example.js new file mode 100644 index 00000000..ad5a259c --- /dev/null +++ b/snippets/firestore-next/test-firestore/stages_expressions_example.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START stages_expressions_example_modular] +const trailing30Days = constant(Timestamp.now().toMillis()) + .unixMillisToTimestamp() + .timestampSubtract("day", 30); +const snapshot = await execute(db.pipeline() + .collection("productViews") + .where(field("viewedAt").greaterThan(trailing30Days)) + .aggregate(field("productId").countDistinct().as("uniqueProductViews")) +); +// [END stages_expressions_example_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/starts_with.js b/snippets/firestore-next/test-firestore/starts_with.js new file mode 100644 index 00000000..04236279 --- /dev/null +++ b/snippets/firestore-next/test-firestore/starts_with.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START starts_with_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("title").startsWith("The") + .as("needsSpecialAlphabeticalSort") + ) +); +// [END starts_with_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/str_concat.js b/snippets/firestore-next/test-firestore/str_concat.js new file mode 100644 index 00000000..f1d219da --- /dev/null +++ b/snippets/firestore-next/test-firestore/str_concat.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START str_concat_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("title").stringConcat(" by ", field("author")) + .as("fullyQualifiedTitle") + ) +); +// [END str_concat_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/str_reverse.js b/snippets/firestore-next/test-firestore/str_reverse.js new file mode 100644 index 00000000..752a8f86 --- /dev/null +++ b/snippets/firestore-next/test-firestore/str_reverse.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START str_reverse_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("name").reverse().as("reversedName") + ) +); +// [END str_reverse_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/string_contains.js b/snippets/firestore-next/test-firestore/string_contains.js new file mode 100644 index 00000000..d8140aaa --- /dev/null +++ b/snippets/firestore-next/test-firestore/string_contains.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START string_contains_modular] +const result = await execute(db.pipeline() + .collection("articles") + .select( + field("body").stringContains("Firestore") + .as("isFirestoreRelated") + ) +); +// [END string_contains_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/substr_function.js b/snippets/firestore-next/test-firestore/substr_function.js new file mode 100644 index 00000000..0b1733e2 --- /dev/null +++ b/snippets/firestore-next/test-firestore/substr_function.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START substr_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .where(field("title").startsWith("The ")) + .select( + field("title").substring(4) + .as("titleWithoutLeadingThe") + ) +); +// [END substr_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/subtract_function.js b/snippets/firestore-next/test-firestore/subtract_function.js new file mode 100644 index 00000000..53a73a66 --- /dev/null +++ b/snippets/firestore-next/test-firestore/subtract_function.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START subtract_function_modular] +const storeCredit = 7; +const result = await execute(db.pipeline() + .collection("books") + .select(field("price").subtract(constant(storeCredit)).as("totalCost")) +); +// [END subtract_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/sum_function.js b/snippets/firestore-next/test-firestore/sum_function.js new file mode 100644 index 00000000..a77a5003 --- /dev/null +++ b/snippets/firestore-next/test-firestore/sum_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sum_function_modular] +const result = await execute(db.pipeline() + .collection("cities") + .aggregate(field("population").sum().as("totalPopulation")) +); +// [END sum_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/timestamp_add.js b/snippets/firestore-next/test-firestore/timestamp_add.js new file mode 100644 index 00000000..4dbef032 --- /dev/null +++ b/snippets/firestore-next/test-firestore/timestamp_add.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START timestamp_add_modular] +const result = await execute(db.pipeline() + .collection("documents") + .select( + field("createdAt").timestampAdd("day", 3653).as("expiresAt") + ) +); +// [END timestamp_add_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/timestamp_sub.js b/snippets/firestore-next/test-firestore/timestamp_sub.js new file mode 100644 index 00000000..459e0263 --- /dev/null +++ b/snippets/firestore-next/test-firestore/timestamp_sub.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START timestamp_sub_modular] +const result = await execute(db.pipeline() + .collection("documents") + .select( + field("expiresAt").timestampSubtract("day", 14).as("sendWarningTimestamp") + ) +); +// [END timestamp_sub_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/timestamp_unix_micros.js b/snippets/firestore-next/test-firestore/timestamp_unix_micros.js new file mode 100644 index 00000000..48862d27 --- /dev/null +++ b/snippets/firestore-next/test-firestore/timestamp_unix_micros.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START timestamp_unix_micros_modular] +const result = await execute(db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixMicros().as("unixMicros") + ) +); +// [END timestamp_unix_micros_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/timestamp_unix_millis.js b/snippets/firestore-next/test-firestore/timestamp_unix_millis.js new file mode 100644 index 00000000..ca8dbcf2 --- /dev/null +++ b/snippets/firestore-next/test-firestore/timestamp_unix_millis.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START timestamp_unix_millis_modular] +const result = await execute(db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixMillis().as("unixMillis") + ) +); +// [END timestamp_unix_millis_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/timestamp_unix_seconds.js b/snippets/firestore-next/test-firestore/timestamp_unix_seconds.js new file mode 100644 index 00000000..dfcc7423 --- /dev/null +++ b/snippets/firestore-next/test-firestore/timestamp_unix_seconds.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START timestamp_unix_seconds_modular] +const result = await execute(db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixSeconds().as("unixSeconds") + ) +); +// [END timestamp_unix_seconds_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/to_lower.js b/snippets/firestore-next/test-firestore/to_lower.js new file mode 100644 index 00000000..64f001e1 --- /dev/null +++ b/snippets/firestore-next/test-firestore/to_lower.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START to_lower_modular] +const result = await execute(db.pipeline() + .collection("authors") + .select( + field("genre").toLower().equal("fantasy") + .as("isFantasy") + ) +); +// [END to_lower_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/to_upper.js b/snippets/firestore-next/test-firestore/to_upper.js new file mode 100644 index 00000000..468f00d1 --- /dev/null +++ b/snippets/firestore-next/test-firestore/to_upper.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START to_upper_modular] +const result = await execute(db.pipeline() + .collection("authors") + .select( + field("name").toUpper() + .as("uppercaseName") + ) +); +// [END to_upper_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/trim_function.js b/snippets/firestore-next/test-firestore/trim_function.js new file mode 100644 index 00000000..64153138 --- /dev/null +++ b/snippets/firestore-next/test-firestore/trim_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START trim_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("name").trim().as("whitespaceTrimmedName") + ) +); +// [END trim_function_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/union_stage.js b/snippets/firestore-next/test-firestore/union_stage.js new file mode 100644 index 00000000..db549a70 --- /dev/null +++ b/snippets/firestore-next/test-firestore/union_stage.js @@ -0,0 +1,17 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START union_stage_modular] +const results = await execute(db.pipeline() + .collection("cities/SF/restaurants") + .where(field("type").equal("Chinese")) + .union(db.pipeline() + .collection("cities/NY/restaurants") + .where(field("type").equal("Italian"))) + .where(field("rating").greaterThanOrEqual(4.5)) + .sort(field("__name__").descending()) +); +// [END union_stage_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unix_micros_timestamp.js b/snippets/firestore-next/test-firestore/unix_micros_timestamp.js new file mode 100644 index 00000000..4fd69202 --- /dev/null +++ b/snippets/firestore-next/test-firestore/unix_micros_timestamp.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unix_micros_timestamp_modular] +const result = await execute(db.pipeline() + .collection("documents") + .select( + field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString") + ) +); +// [END unix_micros_timestamp_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unix_millis_timestamp.js b/snippets/firestore-next/test-firestore/unix_millis_timestamp.js new file mode 100644 index 00000000..87f547fa --- /dev/null +++ b/snippets/firestore-next/test-firestore/unix_millis_timestamp.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unix_millis_timestamp_modular] +const result = await execute(db.pipeline() + .collection("documents") + .select( + field("createdAtMillis").unixMillisToTimestamp().as("createdAtString") + ) +); +// [END unix_millis_timestamp_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unix_seconds_timestamp.js b/snippets/firestore-next/test-firestore/unix_seconds_timestamp.js new file mode 100644 index 00000000..84b25c30 --- /dev/null +++ b/snippets/firestore-next/test-firestore/unix_seconds_timestamp.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unix_seconds_timestamp_modular] +const result = await execute(db.pipeline() + .collection("documents") + .select( + field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString") + ) +); +// [END unix_seconds_timestamp_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unnest_edge_cases.js b/snippets/firestore-next/test-firestore/unnest_edge_cases.js new file mode 100644 index 00000000..72d6bbce --- /dev/null +++ b/snippets/firestore-next/test-firestore/unnest_edge_cases.js @@ -0,0 +1,21 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_edge_cases_modular] +// Input +// { identifier : 1, neighbors: [ "Alice", "Cathy" ] } +// { identifier : 2, neighbors: [] } +// { identifier : 3, neighbors: "Bob" } +const results = await execute(db.pipeline() + .database() + .unnest(field("neighbors").as("unnestedNeighbors"), "index" ) +); + +// Output +// { identifier: 1, neighbors: [ "Alice", "Cathy" ], unnestedNeighbors: "Alice", index: 0 } +// { identifier: 1, neighbors: [ "Alice", "Cathy" ], unnestedNeighbors: "Cathy", index: 1 } +// { identifier: 3, neighbors: "Bob", index: null} +// [END unnest_edge_cases_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/unnest_stage.js b/snippets/firestore-next/test-firestore/unnest_stage.js new file mode 100644 index 00000000..ded031b6 --- /dev/null +++ b/snippets/firestore-next/test-firestore/unnest_stage.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_stage_modular] +const results = await execute(db.pipeline() + .database() + .unnest(field("arrayField").as("unnestedArrayField"), "index") +); +// [END unnest_stage_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/vector_length.js b/snippets/firestore-next/test-firestore/vector_length.js new file mode 100644 index 00000000..5eb0bf0e --- /dev/null +++ b/snippets/firestore-next/test-firestore/vector_length.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START vector_length_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + field("embedding").vectorLength().as("vectorLength") + ) +); +// [END vector_length_modular] \ No newline at end of file diff --git a/snippets/firestore-next/test-firestore/xor_function.js b/snippets/firestore-next/test-firestore/xor_function.js new file mode 100644 index 00000000..f4523929 --- /dev/null +++ b/snippets/firestore-next/test-firestore/xor_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-next/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START xor_function_modular] +const result = await execute(db.pipeline() + .collection("books") + .select( + xor(field("tags").arrayContains("magic"), field("tags").arrayContains("nonfiction")) + .as("matchesSearchFilters") + ) +); +// [END xor_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/add_function.js b/snippets/firestore-temp/test-firestore/add_function.js new file mode 100644 index 00000000..ff354a12 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/add_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START add_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("soldBooks").add(field("unsoldBooks")).as("totalBooks")) + .execute(); +// [END add_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/aggregate_distinct.js b/snippets/firestore-temp/test-firestore/aggregate_distinct.js new file mode 100644 index 00000000..ee5464d6 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/aggregate_distinct.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START aggregate_distinct_modular] +const results = await db.pipeline() + .collection("books") + .distinct( + field("author").toUpper().as("author"), + field("genre") + ) + .execute(); +// [END aggregate_distinct_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/aggregate_groups.js b/snippets/firestore-temp/test-firestore/aggregate_groups.js new file mode 100644 index 00000000..b45c26e6 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/aggregate_groups.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START aggregate_groups_modular] +const results = await db.pipeline() + .collection("books") + .aggregate( + field("rating").average().as("avg_rating") + ) + .distinct(field("genre")) + .execute(); +// [END aggregate_groups_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/and_function.js b/snippets/firestore-temp/test-firestore/and_function.js new file mode 100644 index 00000000..817c21ea --- /dev/null +++ b/snippets/firestore-temp/test-firestore/and_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START and_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + and(field("rating").greaterThan(4), field("price").lessThan(10)) + .as("under10Recommendation") + ) + .execute(); +// [END and_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/array_concat.js b/snippets/firestore-temp/test-firestore/array_concat.js new file mode 100644 index 00000000..ac9d4482 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/array_concat.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_concat_modular] +const result = await db.pipeline() + .collection("books") + .select(field("genre").arrayConcat([field("subGenre")]).as("allGenres")) + .execute(); +// [END array_concat_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/array_contains.js b/snippets/firestore-temp/test-firestore/array_contains.js new file mode 100644 index 00000000..6b63e01a --- /dev/null +++ b/snippets/firestore-temp/test-firestore/array_contains.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_contains_modular] +const result = await db.pipeline() + .collection("books") + .select(field("genre").arrayContains(constant("mystery")).as("isMystery")) + .execute(); +// [END array_contains_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/array_contains_all.js b/snippets/firestore-temp/test-firestore/array_contains_all.js new file mode 100644 index 00000000..d79e2250 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/array_contains_all.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_contains_all_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("genre") + .arrayContainsAll([constant("fantasy"), constant("adventure")]) + .as("isFantasyAdventure") + ) + .execute(); +// [END array_contains_all_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/array_contains_any.js b/snippets/firestore-temp/test-firestore/array_contains_any.js new file mode 100644 index 00000000..5ccbc3d5 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/array_contains_any.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_contains_any_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("genre") + .arrayContainsAny([constant("fantasy"), constant("nonfiction")]) + .as("isMysteryOrFantasy") + ) + .execute(); +// [END array_contains_any_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/array_length.js b/snippets/firestore-temp/test-firestore/array_length.js new file mode 100644 index 00000000..2627557d --- /dev/null +++ b/snippets/firestore-temp/test-firestore/array_length.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_length_modular] +const result = await db.pipeline() + .collection("books") + .select(field("genre").arrayLength().as("genreCount")) + .execute(); +// [END array_length_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/array_reverse.js b/snippets/firestore-temp/test-firestore/array_reverse.js new file mode 100644 index 00000000..21b8ee7c --- /dev/null +++ b/snippets/firestore-temp/test-firestore/array_reverse.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START array_reverse_modular] +const result = await db.pipeline() + .collection("books") + .select(arrayReverse(field("genre")).as("reversedGenres")) + .execute(); +// [END array_reverse_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/avg_function.js b/snippets/firestore-temp/test-firestore/avg_function.js new file mode 100644 index 00000000..dc7b1a8e --- /dev/null +++ b/snippets/firestore-temp/test-firestore/avg_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START avg_function_modular] +const result = await db.pipeline() + .collection("cities") + .aggregate(field("population").average().as("averagePopulation")) + .execute(); +// [END avg_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/basic_read.js b/snippets/firestore-temp/test-firestore/basic_read.js new file mode 100644 index 00000000..9589129b --- /dev/null +++ b/snippets/firestore-temp/test-firestore/basic_read.js @@ -0,0 +1,20 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START basic_read_modular] +const readDataPipeline = db.pipeline() + .collection("users"); + +// Execute the pipeline and handle the result +try { + const querySnapshot = await readDataPipeline.execute(); + querySnapshot.results.forEach((result) => { + console.log(`${result.id} => ${result.data()}`); + }); +} catch (error) { + console.error("Error getting documents: ", error); +} +// [END basic_read_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/byte_length.js b/snippets/firestore-temp/test-firestore/byte_length.js new file mode 100644 index 00000000..1de8e3dc --- /dev/null +++ b/snippets/firestore-temp/test-firestore/byte_length.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START byte_length_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("title").byteLength().as("titleByteLength") + ) + .execute(); +// [END byte_length_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/ceil_function.js b/snippets/firestore-temp/test-firestore/ceil_function.js new file mode 100644 index 00000000..5adb33d7 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/ceil_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START ceil_function_modular] +const booksPerShelf = 100; +const result = await db.pipeline() + .collection("books") + .select( + field("unsoldBooks").divide(constant(booksPerShelf)).ceil().as("requiredShelves") + ) + .execute(); +// [END ceil_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/char_length.js b/snippets/firestore-temp/test-firestore/char_length.js new file mode 100644 index 00000000..4483d7bc --- /dev/null +++ b/snippets/firestore-temp/test-firestore/char_length.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START char_length_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("title").charLength().as("titleCharLength") + ) + .execute(); +// [END char_length_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/collection_example.js b/snippets/firestore-temp/test-firestore/collection_example.js new file mode 100644 index 00000000..05d3be56 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/collection_example.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START collection_example_modular] +const results = await db.pipeline() + .collection("users/bob/games") + .sort(field("name").ascending()) + .execute(); +// [END collection_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/collection_group_example.js b/snippets/firestore-temp/test-firestore/collection_group_example.js new file mode 100644 index 00000000..25c6d8da --- /dev/null +++ b/snippets/firestore-temp/test-firestore/collection_group_example.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START collection_group_example_modular] +const results = await db.pipeline() + .collectionGroup("games") + .sort(field("name").ascending()) + .execute(); +// [END collection_group_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/cond_function.js b/snippets/firestore-temp/test-firestore/cond_function.js new file mode 100644 index 00000000..e630d758 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/cond_function.js @@ -0,0 +1,20 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START cond_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("tags").arrayConcat([ + conditional( + field("pages").greaterThan(100), + constant("longRead"), + constant("shortRead") + ) + ]).as("extendedTags") + ) + .execute(); +// [END cond_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/cosine_distance.js b/snippets/firestore-temp/test-firestore/cosine_distance.js new file mode 100644 index 00000000..c5a4293a --- /dev/null +++ b/snippets/firestore-temp/test-firestore/cosine_distance.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START cosine_distance_modular] +const sampleVector = [0.0, 1, 2, 3, 4, 5]; +const result = await db.pipeline() + .collection("books") + .select( + field("embedding").cosineDistance(sampleVector).as("cosineDistance") + ) + .execute(); +// [END cosine_distance_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/count_distinct.js b/snippets/firestore-temp/test-firestore/count_distinct.js new file mode 100644 index 00000000..bb68e3f6 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/count_distinct.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START count_distinct_modular] +const result = await db.pipeline() + .collection("books") + .aggregate(field("author").countDistinct().as("unique_authors")) + .execute(); +// [END count_distinct_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/count_function.js b/snippets/firestore-temp/test-firestore/count_function.js new file mode 100644 index 00000000..aceba2d6 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/count_function.js @@ -0,0 +1,19 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START count_function_modular] +// Total number of books in the collection +const countOfAll = await db.pipeline() + .collection("books") + .aggregate(countAll().as("count")) + .execute(); + +// Number of books with nonnull `ratings` field +const countField = await db.pipeline() + .collection("books") + .aggregate(field("ratings").count().as("count")) + .execute(); +// [END count_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/count_if.js b/snippets/firestore-temp/test-firestore/count_if.js new file mode 100644 index 00000000..198574bc --- /dev/null +++ b/snippets/firestore-temp/test-firestore/count_if.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START count_if_modular] +const result = await db.pipeline() + .collection("books") + .aggregate( + field("rating").greaterThan(4).countIf().as("filteredCount") + ) + .execute(); +// [END count_if_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/covered_query.js b/snippets/firestore-temp/test-firestore/covered_query.js new file mode 100644 index 00000000..371a3c81 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/covered_query.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START covered_query_modular] +const results = await db.pipeline() + .collection("books") + .where(like(field("category"), "%fantasy%")) + .where(field("title").exists()) + .where(field("author").exists()) + .select(field("title"), field("author")) + .execute(); +// [END covered_query_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/database_example.js b/snippets/firestore-temp/test-firestore/database_example.js new file mode 100644 index 00000000..ab9c3f54 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/database_example.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START database_example_modular] +// Count all documents in the database +const results = await db.pipeline() + .database() + .aggregate(countAll().as("total")) + .execute(); +// [END database_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/divide_function.js b/snippets/firestore-temp/test-firestore/divide_function.js new file mode 100644 index 00000000..97507210 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/divide_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START divide_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("ratings").divide(field("soldBooks")).as("reviewRate")) + .execute(); +// [END divide_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/documents_example.js b/snippets/firestore-temp/test-firestore/documents_example.js new file mode 100644 index 00000000..a515dd36 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/documents_example.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START documents_example_modular] +const results = await db.pipeline() + .documents([ + db.collection("cities").doc("SF"), + db.collection("cities").doc("DC"), + db.collection("cities").doc("NY") + ]) + .execute(); +// [END documents_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/dot_product.js b/snippets/firestore-temp/test-firestore/dot_product.js new file mode 100644 index 00000000..f102e93d --- /dev/null +++ b/snippets/firestore-temp/test-firestore/dot_product.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START dot_product_modular] +const sampleVector = [0.0, 1, 2, 3, 4, 5]; +const result = await db.pipeline() + .collection("books") + .select( + field("embedding").dotProduct(sampleVector).as("dotProduct") + ) + .execute(); +// [END dot_product_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/ends_with.js b/snippets/firestore-temp/test-firestore/ends_with.js new file mode 100644 index 00000000..60b648e1 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/ends_with.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START ends_with_modular] +const result = await db.pipeline() + .collection("inventory/devices/laptops") + .select( + field("name").endsWith("16 inch") + .as("16InLaptops") + ) + .execute(); +// [END ends_with_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/eq_any.js b/snippets/firestore-temp/test-firestore/eq_any.js new file mode 100644 index 00000000..e73dde30 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/eq_any.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START eq_any_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("genre").equalAny(["Science Fiction", "Psychological Thriller"]) + .as("matchesGenreFilters") + ) + .execute(); +// [END eq_any_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/equal_function.js b/snippets/firestore-temp/test-firestore/equal_function.js new file mode 100644 index 00000000..36ae12ad --- /dev/null +++ b/snippets/firestore-temp/test-firestore/equal_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START equal_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("rating").equal(5).as("hasPerfectRating")) + .execute(); +// [END equal_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/euclidean_distance.js b/snippets/firestore-temp/test-firestore/euclidean_distance.js new file mode 100644 index 00000000..f8bf9e87 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/euclidean_distance.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START euclidean_distance_modular] +const sampleVector = [0.0, 1, 2, 3, 4, 5]; +const result = await db.pipeline() + .collection("books") + .select( + field("embedding").euclideanDistance(sampleVector).as("euclideanDistance") + ) + .execute(); +// [END euclidean_distance_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/exists_function.js b/snippets/firestore-temp/test-firestore/exists_function.js new file mode 100644 index 00000000..563c7b9e --- /dev/null +++ b/snippets/firestore-temp/test-firestore/exists_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START exists_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("rating").exists().as("hasRating")) + .execute(); +// [END exists_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/exp_function.js b/snippets/firestore-temp/test-firestore/exp_function.js new file mode 100644 index 00000000..a74efb19 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/exp_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START exp_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("rating").exp().as("expRating")) + .execute(); +// [END exp_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/field_or_constant.js b/snippets/firestore-temp/test-firestore/field_or_constant.js new file mode 100644 index 00000000..de7059c9 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/field_or_constant.js @@ -0,0 +1,11 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START field_or_constant_modular] +const pipeline = db.pipeline() + .collection("cities") + .where(field("name").equal(constant("Toronto"))); +// [END field_or_constant_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/floor_function.js b/snippets/firestore-temp/test-firestore/floor_function.js new file mode 100644 index 00000000..bc23f0fb --- /dev/null +++ b/snippets/firestore-temp/test-firestore/floor_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START floor_function_modular] +const result = await db.pipeline() + .collection("books") + .addFields( + field("wordCount").divide(field("pages")).floor().as("wordsPerPage") + ) + .execute(); +// [END floor_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/full_replace.js b/snippets/firestore-temp/test-firestore/full_replace.js new file mode 100644 index 00000000..9c2e3517 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/full_replace.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START full_replace_modular] +const names = await db.pipeline() + .collection("cities") + .replaceWith(field("location")) + .execute(); +// [END full_replace_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/functions_example.js b/snippets/firestore-temp/test-firestore/functions_example.js new file mode 100644 index 00000000..0c4d080b --- /dev/null +++ b/snippets/firestore-temp/test-firestore/functions_example.js @@ -0,0 +1,21 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START functions_example_modular] +let results; + +// Type 1: Scalar (for use in non-aggregation stages) +// Example: Return the min store price for each book. +results = await db.pipeline().collection("books") + .select(field("current").logicalMinimum(field("updated")).as("price_min")) + .execute(); + +// Type 2: Aggregation (for use in aggregate stages) +// Example: Return the min price of all books. +results = await db.pipeline().collection("books") + .aggregate(field("price").minimum().as("min_price")) + .execute(); +// [END functions_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/greater_or_equal.js b/snippets/firestore-temp/test-firestore/greater_or_equal.js new file mode 100644 index 00000000..77c01510 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/greater_or_equal.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START greater_or_equal_modular] +const result = await db.pipeline() + .collection("books") + .select(field("published").greaterThanOrEqual(1900).as("publishedIn20thCentury")) + .execute(); +// [END greater_or_equal_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/greater_than.js b/snippets/firestore-temp/test-firestore/greater_than.js new file mode 100644 index 00000000..f727a8df --- /dev/null +++ b/snippets/firestore-temp/test-firestore/greater_than.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START greater_than_modular] +const result = await db.pipeline() + .collection("books") + .select(field("rating").greaterThan(4).as("hasHighRating")) + .execute(); +// [END greater_than_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/initial_data.js b/snippets/firestore-temp/test-firestore/initial_data.js new file mode 100644 index 00000000..0d8ea4b4 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/initial_data.js @@ -0,0 +1,35 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START initial_data_modular] +await db.collection("cities").doc("SF").set({ + "name": "San Francisco", + "population": 800000, + "location": { + "country": "USA", + "state": "California" + } +}); +await db.collection("cities").doc("TO").set({ + "name": "Toronto", + "population": 3000000, + "province": "ON", + "location": { + "country": "Canada", + "province": "Ontario" + } +}); +await db.collection("cities").doc("NY").set({ + "name": "New York", + "location": { + "country": "USA", + "state": "New York" + } +}); +await db.collection("cities").doc("AT").set({ + "name": "Atlantis", +}); +// [END initial_data_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/input_stages.js b/snippets/firestore-temp/test-firestore/input_stages.js new file mode 100644 index 00000000..432c0758 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/input_stages.js @@ -0,0 +1,25 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START input_stages_modular] +let results; + +// Return all restaurants in San Francisco +results = await db.pipeline().collection("cities/sf/restaurants").execute(); + +// Return all restaurants +results = await db.pipeline().collectionGroup("restaurants").execute(); + +// Return all documents across all collections in the database (the entire database) +results = await db.pipeline().database().execute(); + +// Batch read of 3 documents +results = await db.pipeline().documents([ + db.collection("cities").doc("SF"), + db.collection("cities").doc("DC"), + db.collection("cities").doc("NY"), +]).execute(); +// [END input_stages_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/less_or_equal.js b/snippets/firestore-temp/test-firestore/less_or_equal.js new file mode 100644 index 00000000..1e6d427a --- /dev/null +++ b/snippets/firestore-temp/test-firestore/less_or_equal.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START less_or_equal_modular] +const result = await db.pipeline() + .collection("books") + .select(field("rating").lessThanOrEqual(2).as("hasBadRating")) + .execute(); +// [END less_or_equal_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/less_than.js b/snippets/firestore-temp/test-firestore/less_than.js new file mode 100644 index 00000000..f831bb91 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/less_than.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START less_than_modular] +const result = await db.pipeline() + .collection("books") + .select(field("published").lessThan(1923).as("isPublicDomainProbably")) + .execute(); +// [END less_than_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/like.js b/snippets/firestore-temp/test-firestore/like.js new file mode 100644 index 00000000..df0c5ffa --- /dev/null +++ b/snippets/firestore-temp/test-firestore/like.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START like_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("genre").like("%Fiction") + .as("anyFiction") + ) + .execute(); +// [END like_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/ln_function.js b/snippets/firestore-temp/test-firestore/ln_function.js new file mode 100644 index 00000000..31b3cd67 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/ln_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START ln_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("rating").ln().as("lnRating")) + .execute(); +// [END ln_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/log_function.js b/snippets/firestore-temp/test-firestore/log_function.js new file mode 100644 index 00000000..20add84a --- /dev/null +++ b/snippets/firestore-temp/test-firestore/log_function.js @@ -0,0 +1,9 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START log_function_modular] +// Not supported on JS +// [END log_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/map_get.js b/snippets/firestore-temp/test-firestore/map_get.js new file mode 100644 index 00000000..f08df243 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/map_get.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START map_get_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("awards").mapGet("pulitzer").as("hasPulitzerAward") + ) + .execute(); +// [END map_get_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/map_merge_overwrite.js b/snippets/firestore-temp/test-firestore/map_merge_overwrite.js new file mode 100644 index 00000000..92c1f05b --- /dev/null +++ b/snippets/firestore-temp/test-firestore/map_merge_overwrite.js @@ -0,0 +1,9 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START map_merge_overwrite_modular] +// unsupported in client SDKs for now +// [END map_merge_overwrite_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/max_function.js b/snippets/firestore-temp/test-firestore/max_function.js new file mode 100644 index 00000000..c553370a --- /dev/null +++ b/snippets/firestore-temp/test-firestore/max_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START max_function_modular] +const result = await db.pipeline() + .collection("books") + .aggregate(field("price").maximum().as("maximumPrice")) + .execute(); +// [END max_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/max_logical_function.js b/snippets/firestore-temp/test-firestore/max_logical_function.js new file mode 100644 index 00000000..fbe6ce26 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/max_logical_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START max_logical_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("rating").logicalMaximum(1).as("flooredRating") + ) + .execute(); +// [END max_logical_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/min_function.js b/snippets/firestore-temp/test-firestore/min_function.js new file mode 100644 index 00000000..92978486 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/min_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START min_function_modular] +const result = await db.pipeline() + .collection("books") + .aggregate(field("price").minimum().as("minimumPrice")) + .execute(); +// [END min_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/min_logical_function.js b/snippets/firestore-temp/test-firestore/min_logical_function.js new file mode 100644 index 00000000..d2f8f0df --- /dev/null +++ b/snippets/firestore-temp/test-firestore/min_logical_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START min_logical_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("rating").logicalMinimum(5).as("cappedRating") + ) + .execute(); +// [END min_logical_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/mod_function.js b/snippets/firestore-temp/test-firestore/mod_function.js new file mode 100644 index 00000000..c7859f76 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/mod_function.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START mod_function_modular] +const displayCapacity = 1000; +const result = await db.pipeline() + .collection("books") + .select(field("unsoldBooks").mod(constant(displayCapacity)).as("warehousedBooks")) + .execute(); +// [END mod_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/multiply_function.js b/snippets/firestore-temp/test-firestore/multiply_function.js new file mode 100644 index 00000000..bbe1ca6f --- /dev/null +++ b/snippets/firestore-temp/test-firestore/multiply_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START multiply_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("price").multiply(field("soldBooks")).as("revenue")) + .execute(); +// [END multiply_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/not_eq_any.js b/snippets/firestore-temp/test-firestore/not_eq_any.js new file mode 100644 index 00000000..68fca3ee --- /dev/null +++ b/snippets/firestore-temp/test-firestore/not_eq_any.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START not_eq_any_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("author").notEqualAny(["George Orwell", "F. Scott Fitzgerald"]) + .as("byExcludedAuthors") + ) + .execute(); +// [END not_eq_any_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/not_equal.js b/snippets/firestore-temp/test-firestore/not_equal.js new file mode 100644 index 00000000..837f08a5 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/not_equal.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START not_equal_modular] +const result = await db.pipeline() + .collection("books") + .select(field("title").notEqual("1984").as("not1984")) + .execute(); +// [END not_equal_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/not_function.js b/snippets/firestore-temp/test-firestore/not_function.js new file mode 100644 index 00000000..1ecb8178 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/not_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START not_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("tags").arrayContains("nonfiction").not() + .as("isFiction") + ) + .execute(); +// [END not_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/or_function.js b/snippets/firestore-temp/test-firestore/or_function.js new file mode 100644 index 00000000..19ee8be3 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/or_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START or_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + or(field("genre").equal("Fantasy"), field("tags").arrayContains("adventure")) + .as("matchesSearchFilters") + ) + .execute(); +// [END or_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/pagination_not_supported_preview.js b/snippets/firestore-temp/test-firestore/pagination_not_supported_preview.js new file mode 100644 index 00000000..93a797f5 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/pagination_not_supported_preview.js @@ -0,0 +1,38 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pagination_not_supported_preview_modular] +// Existing pagination via `startAt()` +const q = + db.collection("cities").orderBy("population").startAt(1000000); + +// Private preview workaround using pipelines +const pageSize = 2; +const pipeline = db.pipeline() + .collection("cities") + .select("name", "population", "__name__") + .sort(field("population").descending(), field("__name__").ascending()); + +// Page 1 results +let snapshot = await pipeline.limit(pageSize).execute(); + +// End of page marker +const lastDoc = snapshot.results[snapshot.results.length - 1]; + +// Page 2 results +snapshot = await pipeline + .where( + or( + and( + field("population").equal(lastDoc.get("population")), + field("__name__").greaterThan(lastDoc.ref) + ), + field("population").lessThan(lastDoc.get("population")) + ) + ) + .limit(pageSize) + .execute(); +// [END pagination_not_supported_preview_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/pipeline_concepts.js b/snippets/firestore-temp/test-firestore/pipeline_concepts.js new file mode 100644 index 00000000..60e862b8 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/pipeline_concepts.js @@ -0,0 +1,18 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pipeline_concepts_modular] +const pipeline = db.pipeline() + // Step 1: Start a query with collection scope + .collection("cities") + // Step 2: Filter the collection + .where(field("population").greaterThan(100000)) + // Step 3: Sort the remaining documents + .sort(field("name").ascending()) + // Step 4: Return the top 10. Note applying the limit earlier in the + // pipeline would have unintentional results. + .limit(10); +// [END pipeline_concepts_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/pipeline_initialization.js b/snippets/firestore-temp/test-firestore/pipeline_initialization.js new file mode 100644 index 00000000..806986f6 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/pipeline_initialization.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pipeline_initialization_modular] +import { Firestore } from "@google-cloud/firestore"; +const database = new Firestore({ + projectId: 'your-project-id', + databaseId: 'your-new-enterprise-database' +}); +const pipeline = database.pipeline(); +// [END pipeline_initialization_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/pipeline_where.js b/snippets/firestore-temp/test-firestore/pipeline_where.js new file mode 100644 index 00000000..ab9ba88b --- /dev/null +++ b/snippets/firestore-temp/test-firestore/pipeline_where.js @@ -0,0 +1,18 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pipeline_where_modular] +let results; + +results = await db.pipeline().collection("books") + .where(field("rating").equal(5)) + .where(field("published").lessThan(1900)) + .execute(); + +results = await db.pipeline().collection("books") + .where(and(field("rating").equal(5), field("published").lessThan(1900))) + .execute(); +// [END pipeline_where_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/pow_function.js b/snippets/firestore-temp/test-firestore/pow_function.js new file mode 100644 index 00000000..e13d2117 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/pow_function.js @@ -0,0 +1,27 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START pow_function_modular] +const googleplex = { latitude: 37.4221, longitude: 122.0853 }; +const result = await db.pipeline() + .collection("cities") + .addFields( + field("lat").subtract(constant(googleplex.latitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("latitudeDifference"), + field("lng").subtract(constant(googleplex.longitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("longitudeDifference") + ) + .select( + field("latitudeDifference").add(field("longitudeDifference")).sqrt() + // Inaccurate for large distances or close to poles + .as("approximateDistanceToGoogle") + ) + .execute(); +// [END pow_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/query_example.js b/snippets/firestore-temp/test-firestore/query_example.js new file mode 100644 index 00000000..eb3ead85 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/query_example.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START query_example_modular] +const results = await db.pipeline() + .collection("books") + .where(field("published").lessThan(1900)) + .where(field("genre").equal("Science Fiction")) + .where(field("rating").greaterThan(4.3)) + .sort(field("published").descending()) + .execute(); +// [END query_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/regex_contains.js b/snippets/firestore-temp/test-firestore/regex_contains.js new file mode 100644 index 00000000..33b617f5 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/regex_contains.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START regex_contains_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("title").regexContains("Firestore (Enterprise|Standard)") + .as("isFirestoreRelated") + ) + .execute(); +// [END regex_contains_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/regex_match.js b/snippets/firestore-temp/test-firestore/regex_match.js new file mode 100644 index 00000000..5dd8f123 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/regex_match.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START regex_match_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("title").regexMatch("Firestore (Enterprise|Standard)") + .as("isFirestoreExactly") + ) + .execute(); +// [END regex_match_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/round_function.js b/snippets/firestore-temp/test-firestore/round_function.js new file mode 100644 index 00000000..37017562 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/round_function.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START round_function_modular] +const result = await db.pipeline() + .collection("books") + .select(field("soldBooks").multiply(field("price")).round().as("partialRevenue")) + .aggregate(field("partialRevenue").sum().as("totalRevenue")) + .execute(); +// [END round_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sample_example.js b/snippets/firestore-temp/test-firestore/sample_example.js new file mode 100644 index 00000000..e5cb7443 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sample_example.js @@ -0,0 +1,25 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sample_example_modular] +let results; + +// Get a sample of 100 documents in a database +results = await db.pipeline() + .database() + .sample(100) + .execute(); + +// Randomly shuffle a list of 3 documents +results = await db.pipeline() + .documents([ + db.collection("cities").doc("SF"), + db.collection("cities").doc("DC"), + db.collection("cities").doc("NY") + ]) + .sample(3) + .execute(); +// [END sample_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sample_percent.js b/snippets/firestore-temp/test-firestore/sample_percent.js new file mode 100644 index 00000000..bb6876e9 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sample_percent.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sample_percent_modular] +// Get a sample of on average 50% of the documents in the database +const results = await db.pipeline() + .database() + .sample({ percentage: 0.5 }) + .execute(); +// [END sample_percent_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sort.js b/snippets/firestore-temp/test-firestore/sort.js new file mode 100644 index 00000000..62dc86c4 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sort.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sort_modular] +const results = await db.pipeline() + .collection("books") + .sort( + field("release_date").descending(), field("author").ascending() + ) + .execute(); +// [END sort_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sort_comparison.js b/snippets/firestore-temp/test-firestore/sort_comparison.js new file mode 100644 index 00000000..d4c861a5 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sort_comparison.js @@ -0,0 +1,17 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sort_comparison_modular] +const q = db.collection("cities") + .orderBy("state") + .orderBy("population", "desc"); + +const pipeline = db.pipeline() + .collection("books") + .sort( + field("release_date").descending(), field("author").ascending() + ); +// [END sort_comparison_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sparse_index_example.js b/snippets/firestore-temp/test-firestore/sparse_index_example.js new file mode 100644 index 00000000..628ed8a6 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sparse_index_example.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sparse_index_example_modular] +const results = await db.pipeline() + .collection("books") + .where(like(field("category"), "%fantasy%")) + .execute(); +// [END sparse_index_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sqrt_function.js b/snippets/firestore-temp/test-firestore/sqrt_function.js new file mode 100644 index 00000000..fc24ead6 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sqrt_function.js @@ -0,0 +1,27 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sqrt_function_modular] +const googleplex = { latitude: 37.4221, longitude: 122.0853 }; +const result = await db.pipeline() + .collection("cities") + .addFields( + field("lat").subtract(constant(googleplex.latitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("latitudeDifference"), + field("lng").subtract(constant(googleplex.longitude)) + .multiply(111 /* km per degree */) + .pow(2) + .as("longitudeDifference") + ) + .select( + field("latitudeDifference").add(field("longitudeDifference")).sqrt() + // Inaccurate for large distances or close to poles + .as("approximateDistanceToGoogle") + ) + .execute(); +// [END sqrt_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/stages_expressions_example.js b/snippets/firestore-temp/test-firestore/stages_expressions_example.js new file mode 100644 index 00000000..43e923c0 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/stages_expressions_example.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START stages_expressions_example_modular] +const trailing30Days = constant(Timestamp.now().toMillis()) + .unixMillisToTimestamp() + .timestampSubtract("day", 30); +const snapshot = await db.pipeline() + .collection("productViews") + .where(field("viewedAt").greaterThan(trailing30Days)) + .aggregate(field("productId").countDistinct().as("uniqueProductViews")) + .execute(); +// [END stages_expressions_example_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/starts_with.js b/snippets/firestore-temp/test-firestore/starts_with.js new file mode 100644 index 00000000..8cbfb6de --- /dev/null +++ b/snippets/firestore-temp/test-firestore/starts_with.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START starts_with_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("title").startsWith("The") + .as("needsSpecialAlphabeticalSort") + ) + .execute(); +// [END starts_with_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/str_concat.js b/snippets/firestore-temp/test-firestore/str_concat.js new file mode 100644 index 00000000..6386fef2 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/str_concat.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START str_concat_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("title").stringConcat(" by ", field("author")) + .as("fullyQualifiedTitle") + ) + .execute(); +// [END str_concat_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/str_reverse.js b/snippets/firestore-temp/test-firestore/str_reverse.js new file mode 100644 index 00000000..60b50ffc --- /dev/null +++ b/snippets/firestore-temp/test-firestore/str_reverse.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START str_reverse_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("name").reverse().as("reversedName") + ) + .execute(); +// [END str_reverse_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/string_contains.js b/snippets/firestore-temp/test-firestore/string_contains.js new file mode 100644 index 00000000..a785fc74 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/string_contains.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START string_contains_modular] +const result = await db.pipeline() + .collection("articles") + .select( + field("body").stringContains("Firestore") + .as("isFirestoreRelated") + ) + .execute(); +// [END string_contains_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/substr_function.js b/snippets/firestore-temp/test-firestore/substr_function.js new file mode 100644 index 00000000..051f2dbb --- /dev/null +++ b/snippets/firestore-temp/test-firestore/substr_function.js @@ -0,0 +1,16 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START substr_function_modular] +const result = await db.pipeline() + .collection("books") + .where(field("title").startsWith("The ")) + .select( + field("title").substring(4) + .as("titleWithoutLeadingThe") + ) + .execute(); +// [END substr_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/subtract_function.js b/snippets/firestore-temp/test-firestore/subtract_function.js new file mode 100644 index 00000000..e350cdd0 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/subtract_function.js @@ -0,0 +1,13 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START subtract_function_modular] +const storeCredit = 7; +const result = await db.pipeline() + .collection("books") + .select(field("price").subtract(constant(storeCredit)).as("totalCost")) + .execute(); +// [END subtract_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/sum_function.js b/snippets/firestore-temp/test-firestore/sum_function.js new file mode 100644 index 00000000..26c97bf0 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/sum_function.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START sum_function_modular] +const result = await db.pipeline() + .collection("cities") + .aggregate(field("population").sum().as("totalPopulation")) + .execute(); +// [END sum_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/timestamp_add.js b/snippets/firestore-temp/test-firestore/timestamp_add.js new file mode 100644 index 00000000..b0686a16 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/timestamp_add.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START timestamp_add_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("createdAt").timestampAdd("day", 3653).as("expiresAt") + ) + .execute(); +// [END timestamp_add_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/timestamp_sub.js b/snippets/firestore-temp/test-firestore/timestamp_sub.js new file mode 100644 index 00000000..c62f6fb8 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/timestamp_sub.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START timestamp_sub_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("expiresAt").timestampSubtract("day", 14).as("sendWarningTimestamp") + ) + .execute(); +// [END timestamp_sub_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/timestamp_unix_micros.js b/snippets/firestore-temp/test-firestore/timestamp_unix_micros.js new file mode 100644 index 00000000..e060f6ca --- /dev/null +++ b/snippets/firestore-temp/test-firestore/timestamp_unix_micros.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START timestamp_unix_micros_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixMicros().as("unixMicros") + ) + .execute(); +// [END timestamp_unix_micros_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/timestamp_unix_millis.js b/snippets/firestore-temp/test-firestore/timestamp_unix_millis.js new file mode 100644 index 00000000..690e5d8c --- /dev/null +++ b/snippets/firestore-temp/test-firestore/timestamp_unix_millis.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START timestamp_unix_millis_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixMillis().as("unixMillis") + ) + .execute(); +// [END timestamp_unix_millis_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/timestamp_unix_seconds.js b/snippets/firestore-temp/test-firestore/timestamp_unix_seconds.js new file mode 100644 index 00000000..a272cae5 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/timestamp_unix_seconds.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START timestamp_unix_seconds_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("dateString").timestampToUnixSeconds().as("unixSeconds") + ) + .execute(); +// [END timestamp_unix_seconds_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/to_lower.js b/snippets/firestore-temp/test-firestore/to_lower.js new file mode 100644 index 00000000..c40d0780 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/to_lower.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START to_lower_modular] +const result = await db.pipeline() + .collection("authors") + .select( + field("genre").toLower().equal("fantasy") + .as("isFantasy") + ) + .execute(); +// [END to_lower_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/to_upper.js b/snippets/firestore-temp/test-firestore/to_upper.js new file mode 100644 index 00000000..ec9b64d2 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/to_upper.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START to_upper_modular] +const result = await db.pipeline() + .collection("authors") + .select( + field("name").toUpper() + .as("uppercaseName") + ) + .execute(); +// [END to_upper_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/trim_function.js b/snippets/firestore-temp/test-firestore/trim_function.js new file mode 100644 index 00000000..c0bf58da --- /dev/null +++ b/snippets/firestore-temp/test-firestore/trim_function.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START trim_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("name").trim().as("whitespaceTrimmedName") + ) + .execute(); +// [END trim_function_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/union_stage.js b/snippets/firestore-temp/test-firestore/union_stage.js new file mode 100644 index 00000000..67637868 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/union_stage.js @@ -0,0 +1,17 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START union_stage_modular] +const results = await db.pipeline() + .collection("cities/SF/restaurants") + .where(field("type").equal("Chinese")) + .union(db.pipeline() + .collection("cities/NY/restaurants") + .where(field("type").equal("Italian"))) + .where(field("rating").greaterThanOrEqual(4.5)) + .sort(field("__name__").descending()) + .execute(); +// [END union_stage_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unix_micros_timestamp.js b/snippets/firestore-temp/test-firestore/unix_micros_timestamp.js new file mode 100644 index 00000000..fefc3c1f --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unix_micros_timestamp.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unix_micros_timestamp_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString") + ) + .execute(); +// [END unix_micros_timestamp_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unix_millis_timestamp.js b/snippets/firestore-temp/test-firestore/unix_millis_timestamp.js new file mode 100644 index 00000000..ed5f260f --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unix_millis_timestamp.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unix_millis_timestamp_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("createdAtMillis").unixMillisToTimestamp().as("createdAtString") + ) + .execute(); +// [END unix_millis_timestamp_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unix_seconds_timestamp.js b/snippets/firestore-temp/test-firestore/unix_seconds_timestamp.js new file mode 100644 index 00000000..a5b8a674 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unix_seconds_timestamp.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unix_seconds_timestamp_modular] +const result = await db.pipeline() + .collection("documents") + .select( + field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString") + ) + .execute(); +// [END unix_seconds_timestamp_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unnest_edge_cases.js b/snippets/firestore-temp/test-firestore/unnest_edge_cases.js new file mode 100644 index 00000000..383acb3e --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unnest_edge_cases.js @@ -0,0 +1,21 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_edge_cases_modular] +// Input +// { identifier : 1, neighbors: [ "Alice", "Cathy" ] } +// { identifier : 2, neighbors: [] } +// { identifier : 3, neighbors: "Bob" } +const results = await db.pipeline() + .database() + .unnest(field("neighbors").as("unnestedNeighbors"), "index" ) + .execute(); + +// Output +// { identifier: 1, neighbors: [ "Alice", "Cathy" ], unnestedNeighbors: "Alice", index: 0 } +// { identifier: 1, neighbors: [ "Alice", "Cathy" ], unnestedNeighbors: "Cathy", index: 1 } +// { identifier: 3, neighbors: "Bob", index: null} +// [END unnest_edge_cases_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/unnest_stage.js b/snippets/firestore-temp/test-firestore/unnest_stage.js new file mode 100644 index 00000000..58390262 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/unnest_stage.js @@ -0,0 +1,12 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START unnest_stage_modular] +const results = await db.pipeline() + .database() + .unnest(field("arrayField").as("unnestedArrayField"), "index") + .execute(); +// [END unnest_stage_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/vector_length.js b/snippets/firestore-temp/test-firestore/vector_length.js new file mode 100644 index 00000000..6cf36022 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/vector_length.js @@ -0,0 +1,14 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START vector_length_modular] +const result = await db.pipeline() + .collection("books") + .select( + field("embedding").vectorLength().as("vectorLength") + ) + .execute(); +// [END vector_length_modular] \ No newline at end of file diff --git a/snippets/firestore-temp/test-firestore/xor_function.js b/snippets/firestore-temp/test-firestore/xor_function.js new file mode 100644 index 00000000..0c5906f3 --- /dev/null +++ b/snippets/firestore-temp/test-firestore/xor_function.js @@ -0,0 +1,15 @@ +// This snippet file was generated by processing the source file: +// ./firestore-temp/test.firestore.js +// +// To update the snippets in this file, edit the source and then run +// 'npm run snippets'. + +// [START xor_function_modular] +const result = await db.pipeline() + .collection("books") + .select( + xor(field("tags").arrayContains("magic"), field("tags").arrayContains("nonfiction")) + .as("matchesSearchFilters") + ) + .execute(); +// [END xor_function_modular] \ No newline at end of file