ด้านล่างนี้ประกอบด้วยภาพรวมของข้อกำหนดสำหรับ Bundle Builder API รวมถึงคำจำกัดความของ TypeScript และคำอธิบายโดยละเอียด
ส่วนต่อประสานเอกสาร Bundle
ข้อกำหนดสำหรับเอกสารฉบับเดียวภายในคอลเลกชันที่กำหนดค่าไว้:
type BundleDocument = {
// A list of document IDs to serve in the bundle.
docs?: Array<string>;
// A map containing individual named queries and their definitions.
queries?: Map<string, QueryDefinition[]>;
// A map of parameters and their definitions, which can be provided to a query definition.
params?: Map<string, ParamDefinition>;
// Specifies how long to keep the bundle in the client's cache, in seconds. If not defined, client-side cache is disabled.
clientCache?: string;
// Only used in combination with Firebase Hosting. Specifies how long to keep the bundle in Firebase Hosting CDN cache, in seconds.
serverCache: string;
// Specifies how long (in seconds) to keep the bundle in a Cloud Storage bucket, in seconds. If not defined, Cloud Storage bucket is not accessed.
fileCache?: string;
// If a 'File Cache' is specified, bundles created before this timestamp will not be file cached.
notBefore?: Timestamp;
};
อินเทอร์เฟซ ParamDefinition
ข้อกำหนดของพารามิเตอร์เดียวที่กำหนดใน BundleDocument
type ParamDefinition = {
// Whether this parameter is required. If not provided as a query string, an error will be thrown.
required: boolean;
// The type of value which will be parsed, defaults to 'string'.
type?:
| "string"
| "integer"
| "float"
| "boolean"
| "string-array"
| "integer-array"
| "float-array";
};
ตัวอย่างเช่น กำหนดพารามิเตอร์ต่อไปนี้:
params: {
name: {
required: true,
type: 'string',
}
}
เมื่อทำการร้องขอไปยังจุดสิ้นสุดบันเดิล HTTP พารามิเตอร์สามารถจัดเตรียมผ่านพารามิเตอร์เคียวรี เช่น ?name=david
สามารถใช้พารามิเตอร์ภายในค่า QueryDefinition
(ดูด้านล่าง) ( $name
) เพื่อสร้างบันเดิลแบบไดนามิก
อินเทอร์เฟซ QueryDefinition
คำจำกัดความของแบบสอบถามถูกใช้เพื่อสร้างแบบสอบถามที่มีชื่อบนบันเดิล แต่ละวัตถุภายในแมป queries
จะสร้างแบบสอบถามที่มีชื่อใหม่ โดยใช้คีย์วัตถุเป็นชื่อ การสืบค้นแต่ละครั้งจะต้องระบุคอลเลกชัน และรายการเงื่อนไขการสืบค้นที่จะดำเนินการ (ไม่บังคับ)
type QueryDefinition = {
// The collection to perform the query on.
collection: string;
// An optional list of conditions to perform on the specified collection.
conditions?: QueryCondition[];
};
พารามิเตอร์ conditions
สามารถมีอาร์เรย์ของอินเทอร์เฟซ QueryCondition
แต่ละรายการในอาร์เรย์ต้องมีเงื่อนไขเดียวเท่านั้น
type QueryCondition = {
// Performs a `where` filter on the collection on a given FieldPath, operator and value.
where?: [
string,
(
| "<"
| "<="
| "=="
| ">="
| ">"
| "!="
| "array-contains"
| "in"
| "not-in"
| "array-contains-any"
),
any
];
orderBy?: [string, ("asc" | "desc")?];
limit?: number;
limitToLast?: number;
offset?: number;
startAt?: string;
startAfter?: string;
endAt?: string;
endBefore?: string;
};
ตัวอย่างเช่น หากต้องการสร้างแบบสอบถามชื่อ "ผลิตภัณฑ์" ในคอลเลกชัน products
ที่มีเงื่อนไขและขีดจำกัด ผลลัพธ์ของโครงสร้างข้อมูลควรตรงกับสิ่งต่อไปนี้:
queries: {
products: {
collection: 'products',
conditions: [
{ where: ['type', '==', 'featured'] },
{ limit: 10 },
],
}
}
เมื่อระบุค่าอาร์เรย์ให้กับตัวกรอง in
, not-in
หรือ array-contains-any
คุณต้องระบุค่าที่คั่นด้วยเครื่องหมายจุลภาค เนื่องจาก Firestore ไม่รองรับค่าอาร์เรย์ที่ซ้อนกัน ตัวอย่างเช่น:
{ where: ['category', 'in', 'womens,shorts'] }, // ['womens', 'shorts']
ค่าตัวเลขใดๆ จะถูกแยกวิเคราะห์เป็นตัวเลข อย่างไรก็ตาม หากจำเป็นต้องระบุค่าตัวเลขสตริง ค่านั้นควรอยู่ในวงเล็บ:
{ where: ['price', 'in', '1,2.5'] }, // [1, 2.5]
{ where: ['price', 'in', '"1","2.5"'] }, // ['1', '2.5']
สามารถใช้เงื่อนไขควบคู่ไปกับพารามิเตอร์ได้ ตัวอย่างเช่น หากมีการกำหนด type
พารามิเตอร์ (ดูด้านบน) ก็สามารถระบุค่าเงื่อนไขเพื่อจัดเตรียมบันเดิลข้อมูลแบบไดนามิกผ่านไวยากรณ์ $
:
// ?type=featured
conditions: [
{ where: ['type', '==', '$type'] },