Firestore 中嵌套对象数组的数据模型
Data model for nested array of objects in Firestore
我需要经验丰富的 NoSQL 工程师关于我应该如何构建数据的建议。
- 我想为 Google Cloud Firestore 将我的 SQL 数据结构建模为 NoSQL。
- 我之前没有使用 NoSQL 数据库的经验,但我精通传统的 SQL。
- 我使用 Node.js 来编写查询。
到目前为止,我将三个表转换为 JSON 个包含示例数据的文档:
{
"session": {
"userId": 99992222,
"token": "jwttoken1191891j1kj1khjjk1hjk1kj1",
"created": "timestamp"
}
}
{
"user": {
"id": 99992222,
"username": "userName",
"avatarUrl": "https://url-xxxx.com",
"lastLogin": "2019-11-23 13:59:48.884549",
"created": "2019-11-23 13:59:48.884549",
"modified": "2019-11-23 13:59:48.884549",
"visits": 1,
"profile": true,
"basketDetail": { // I get this data from a third party API
"response": {
"product_count": 2,
"products": [
{
"product_id": 111,
"usageInMinutes_recent": 0,
"usageInMinutes": 0,
"usageInMinutes_windows": 0,
"usageInMinutes_mac": 0,
"usageInMinutes_linux": 0
},
{
"product_id": 222, // no recent usage here
"usageInMinutes": 0,
"usageInMinutes_windows": 0,
"usageInMinutes_mac": 0,
"usageInMinutes_linux": 0
}
]
}
}
}
}
{
"visitor": {
"id": 999922221,
"created": "2019-11-23 13:59:48.884549"
}
}
我的问题:
- session.userId、user.id、visitor.id都可以表示同一个用户。 SQL 中相当于外键的 Firestore 是什么?我如何 connect/join 查询这三个集合?
如何处理嵌套对象basketDetail
?它在哪里好还是我应该定义自己的集合?
我预计查询
- 偶尔补充一下最近的用法。
- 经常检查用户是否拥有特定的 product_id
- 经常用新数据替换整个
baskedDetail
对象。
- 偶尔更新一个特定的product_id。
如果我将集合 user
与查询中的 basketDetail
分开,我将如何连接它?
谢谢指教!
- session.userId, user.id, visitor.id can all signify the same user. What is the Firestore equivalent to foreign keys in SQL? How would I connect/join these three collections in a query?
遗憾的是,Firestore 中没有 JOIN
子句。 Firestore 中的查询很浅,只能从查询 运行 所针对的集合中获取元素。除非您使用 collection group query,否则无法在单个查询中从两个集合中获取文档,但事实并非如此,因为您项目中的集合具有不同的名称。
如果您有三个集合,则需要三个单独的查询。您无法一次完成。
- What do I do about the nested object basketDetail? Is it fine where it is or should I define its own collection?
对于可以放入文档的数据量有一些限制。根据有关 usage and limits 的官方文档:
Maximum size for a document: 1 MiB (1,048,576 bytes)
如您所见,单个文档中的数据总量不得超过 1 MiB。因此,如果您认为嵌套对象 basketDetail
可以保持在此限制内,那么您可以使用该架构,否则,将其添加到子集合中。除此之外,所有这些操作在 Firestore 中都是允许的。如果您在实施它们时遇到困难,post 另一个问题,我们可以看看。
How would I connect collections user with basketDetail in a query if I separated it?
您不能 connect/join 两个合集。如果您在子集合中分隔 basketDetail
,则需要两次查询。
// To parse this data:
//
// const Convert = require("./file");
//
// const survey = Convert.toSurvey(json);
//
// These functions will throw an error if the JSON doesn't
// match the expected interface, even if the JSON is valid.
// Converts JSON strings to/from your types
// and asserts the results of JSON.parse at runtime
function toSurvey(json) {
return cast(JSON.parse(json), a(r("Survey")));
}
function surveyToJson(value) {
return JSON.stringify(uncast(value, a(r("Survey"))), null, 2);
}
function invalidValue(typ, val, key = '') {
if (key) {
throw Error(`Invalid value for key "${key}". Expected type ${JSON.stringify(typ)} but got ${JSON.stringify(val)}`);
}
throw Error(`Invalid value ${JSON.stringify(val)} for type ${JSON.stringify(typ)}`, );
}
function jsonToJSProps(typ) {
if (typ.jsonToJS === undefined) {
const map = {};
typ.props.forEach((p) => map[p.json] = { key: p.js, typ: p.typ });
typ.jsonToJS = map;
}
return typ.jsonToJS;
}
function jsToJSONProps(typ) {
if (typ.jsToJSON === undefined) {
const map = {};
typ.props.forEach((p) => map[p.js] = { key: p.json, typ: p.typ });
typ.jsToJSON = map;
}
return typ.jsToJSON;
}
function transform(val, typ, getProps, key = '') {
function transformPrimitive(typ, val) {
if (typeof typ === typeof val) return val;
return invalidValue(typ, val, key);
}
function transformUnion(typs, val) {
// val must validate against one typ in typs
const l = typs.length;
for (let i = 0; i < l; i++) {
const typ = typs[i];
try {
return transform(val, typ, getProps);
} catch (_) {}
}
return invalidValue(typs, val);
}
function transformEnum(cases, val) {
if (cases.indexOf(val) !== -1) return val;
return invalidValue(cases, val);
}
function transformArray(typ, val) {
// val must be an array with no invalid elements
if (!Array.isArray(val)) return invalidValue("array", val);
return val.map(el => transform(el, typ, getProps));
}
function transformDate(val) {
if (val === null) {
return null;
}
const d = new Date(val);
if (isNaN(d.valueOf())) {
return invalidValue("Date", val);
}
return d;
}
function transformObject(props, additional, val) {
if (val === null || typeof val !== "object" || Array.isArray(val)) {
return invalidValue("object", val);
}
const result = {};
Object.getOwnPropertyNames(props).forEach(key => {
const prop = props[key];
const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined;
result[prop.key] = transform(v, prop.typ, getProps, prop.key);
});
Object.getOwnPropertyNames(val).forEach(key => {
if (!Object.prototype.hasOwnProperty.call(props, key)) {
result[key] = transform(val[key], additional, getProps, key);
}
});
return result;
}
if (typ === "any") return val;
if (typ === null) {
if (val === null) return val;
return invalidValue(typ, val);
}
if (typ === false) return invalidValue(typ, val);
while (typeof typ === "object" && typ.ref !== undefined) {
typ = typeMap[typ.ref];
}
if (Array.isArray(typ)) return transformEnum(typ, val);
if (typeof typ === "object") {
return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val)
: typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val)
: typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val)
: invalidValue(typ, val);
}
// Numbers can be parsed by Date but shouldn't be.
if (typ === Date && typeof val !== "number") return transformDate(val);
return transformPrimitive(typ, val);
}
function cast(val, typ) {
return transform(val, typ, jsonToJSProps);
}
function uncast(val, typ) {
return transform(val, typ, jsToJSONProps);
}
function a(typ) {
return { arrayItems: typ };
}
function u(...typs) {
return { unionMembers: typs };
}
function o(props, additional) {
return { props, additional };
}
function m(additional) {
return { props: [], additional };
}
function r(name) {
return { ref: name };
}
const typeMap = {
"Survey": o([
{ json: "id", js: "id", typ: 0 },
{ json: "username", js: "username", typ: "" },
{ json: "avatarUrl", js: "avatarUrl", typ: "" },
{ json: "lastLogin", js: "lastLogin", typ: Date },
{ json: "created", js: "created", typ: Date },
{ json: "modified", js: "modified", typ: Date },
{ json: "visits", js: "visits", typ: 0 },
{ json: "profile", js: "profile", typ: true },
{ json: "basketDetail", js: "basketDetail", typ: a(r("BasketDetail")) },
], false),
"BasketDetail": o([
{ json: "response", js: "response", typ: a(r("Response")) },
], false),
"Response": o([
{ json: "product_count", js: "product_count", typ: 0 },
{ json: "products", js: "products", typ: a(r("Product")) },
], false),
"Product": o([
{ json: "product_id", js: "product_id", typ: 0 },
{ json: "usageInMinutes_recent", js: "usageInMinutes_recent", typ: u(undefined, 0) },
{ json: "usageInMinutes", js: "usageInMinutes", typ: 0 },
{ json: "usageInMinutes_windows", js: "usageInMinutes_windows", typ: 0 },
{ json: "usageInMinutes_mac", js: "usageInMinutes_mac", typ: 0 },
{ json: "usageInMinutes_linux", js: "usageInMinutes_linux", typ: 0 },
], false),
};
module.exports = {
"surveyToJson": surveyToJson,
"toSurvey": toSurvey,
};
试试这个这个模型可以帮助
我需要经验丰富的 NoSQL 工程师关于我应该如何构建数据的建议。
- 我想为 Google Cloud Firestore 将我的 SQL 数据结构建模为 NoSQL。
- 我之前没有使用 NoSQL 数据库的经验,但我精通传统的 SQL。
- 我使用 Node.js 来编写查询。
到目前为止,我将三个表转换为 JSON 个包含示例数据的文档:
{
"session": {
"userId": 99992222,
"token": "jwttoken1191891j1kj1khjjk1hjk1kj1",
"created": "timestamp"
}
}
{
"user": {
"id": 99992222,
"username": "userName",
"avatarUrl": "https://url-xxxx.com",
"lastLogin": "2019-11-23 13:59:48.884549",
"created": "2019-11-23 13:59:48.884549",
"modified": "2019-11-23 13:59:48.884549",
"visits": 1,
"profile": true,
"basketDetail": { // I get this data from a third party API
"response": {
"product_count": 2,
"products": [
{
"product_id": 111,
"usageInMinutes_recent": 0,
"usageInMinutes": 0,
"usageInMinutes_windows": 0,
"usageInMinutes_mac": 0,
"usageInMinutes_linux": 0
},
{
"product_id": 222, // no recent usage here
"usageInMinutes": 0,
"usageInMinutes_windows": 0,
"usageInMinutes_mac": 0,
"usageInMinutes_linux": 0
}
]
}
}
}
}
{
"visitor": {
"id": 999922221,
"created": "2019-11-23 13:59:48.884549"
}
}
我的问题:
- session.userId、user.id、visitor.id都可以表示同一个用户。 SQL 中相当于外键的 Firestore 是什么?我如何 connect/join 查询这三个集合?
如何处理嵌套对象
basketDetail
?它在哪里好还是我应该定义自己的集合? 我预计查询- 偶尔补充一下最近的用法。
- 经常检查用户是否拥有特定的 product_id
- 经常用新数据替换整个
baskedDetail
对象。 - 偶尔更新一个特定的product_id。
如果我将集合
user
与查询中的basketDetail
分开,我将如何连接它?
谢谢指教!
- session.userId, user.id, visitor.id can all signify the same user. What is the Firestore equivalent to foreign keys in SQL? How would I connect/join these three collections in a query?
遗憾的是,Firestore 中没有 JOIN
子句。 Firestore 中的查询很浅,只能从查询 运行 所针对的集合中获取元素。除非您使用 collection group query,否则无法在单个查询中从两个集合中获取文档,但事实并非如此,因为您项目中的集合具有不同的名称。
如果您有三个集合,则需要三个单独的查询。您无法一次完成。
- What do I do about the nested object basketDetail? Is it fine where it is or should I define its own collection?
对于可以放入文档的数据量有一些限制。根据有关 usage and limits 的官方文档:
Maximum size for a document: 1 MiB (1,048,576 bytes)
如您所见,单个文档中的数据总量不得超过 1 MiB。因此,如果您认为嵌套对象 basketDetail
可以保持在此限制内,那么您可以使用该架构,否则,将其添加到子集合中。除此之外,所有这些操作在 Firestore 中都是允许的。如果您在实施它们时遇到困难,post 另一个问题,我们可以看看。
How would I connect collections user with basketDetail in a query if I separated it?
您不能 connect/join 两个合集。如果您在子集合中分隔 basketDetail
,则需要两次查询。
// To parse this data:
//
// const Convert = require("./file");
//
// const survey = Convert.toSurvey(json);
//
// These functions will throw an error if the JSON doesn't
// match the expected interface, even if the JSON is valid.
// Converts JSON strings to/from your types
// and asserts the results of JSON.parse at runtime
function toSurvey(json) {
return cast(JSON.parse(json), a(r("Survey")));
}
function surveyToJson(value) {
return JSON.stringify(uncast(value, a(r("Survey"))), null, 2);
}
function invalidValue(typ, val, key = '') {
if (key) {
throw Error(`Invalid value for key "${key}". Expected type ${JSON.stringify(typ)} but got ${JSON.stringify(val)}`);
}
throw Error(`Invalid value ${JSON.stringify(val)} for type ${JSON.stringify(typ)}`, );
}
function jsonToJSProps(typ) {
if (typ.jsonToJS === undefined) {
const map = {};
typ.props.forEach((p) => map[p.json] = { key: p.js, typ: p.typ });
typ.jsonToJS = map;
}
return typ.jsonToJS;
}
function jsToJSONProps(typ) {
if (typ.jsToJSON === undefined) {
const map = {};
typ.props.forEach((p) => map[p.js] = { key: p.json, typ: p.typ });
typ.jsToJSON = map;
}
return typ.jsToJSON;
}
function transform(val, typ, getProps, key = '') {
function transformPrimitive(typ, val) {
if (typeof typ === typeof val) return val;
return invalidValue(typ, val, key);
}
function transformUnion(typs, val) {
// val must validate against one typ in typs
const l = typs.length;
for (let i = 0; i < l; i++) {
const typ = typs[i];
try {
return transform(val, typ, getProps);
} catch (_) {}
}
return invalidValue(typs, val);
}
function transformEnum(cases, val) {
if (cases.indexOf(val) !== -1) return val;
return invalidValue(cases, val);
}
function transformArray(typ, val) {
// val must be an array with no invalid elements
if (!Array.isArray(val)) return invalidValue("array", val);
return val.map(el => transform(el, typ, getProps));
}
function transformDate(val) {
if (val === null) {
return null;
}
const d = new Date(val);
if (isNaN(d.valueOf())) {
return invalidValue("Date", val);
}
return d;
}
function transformObject(props, additional, val) {
if (val === null || typeof val !== "object" || Array.isArray(val)) {
return invalidValue("object", val);
}
const result = {};
Object.getOwnPropertyNames(props).forEach(key => {
const prop = props[key];
const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined;
result[prop.key] = transform(v, prop.typ, getProps, prop.key);
});
Object.getOwnPropertyNames(val).forEach(key => {
if (!Object.prototype.hasOwnProperty.call(props, key)) {
result[key] = transform(val[key], additional, getProps, key);
}
});
return result;
}
if (typ === "any") return val;
if (typ === null) {
if (val === null) return val;
return invalidValue(typ, val);
}
if (typ === false) return invalidValue(typ, val);
while (typeof typ === "object" && typ.ref !== undefined) {
typ = typeMap[typ.ref];
}
if (Array.isArray(typ)) return transformEnum(typ, val);
if (typeof typ === "object") {
return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val)
: typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val)
: typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val)
: invalidValue(typ, val);
}
// Numbers can be parsed by Date but shouldn't be.
if (typ === Date && typeof val !== "number") return transformDate(val);
return transformPrimitive(typ, val);
}
function cast(val, typ) {
return transform(val, typ, jsonToJSProps);
}
function uncast(val, typ) {
return transform(val, typ, jsToJSONProps);
}
function a(typ) {
return { arrayItems: typ };
}
function u(...typs) {
return { unionMembers: typs };
}
function o(props, additional) {
return { props, additional };
}
function m(additional) {
return { props: [], additional };
}
function r(name) {
return { ref: name };
}
const typeMap = {
"Survey": o([
{ json: "id", js: "id", typ: 0 },
{ json: "username", js: "username", typ: "" },
{ json: "avatarUrl", js: "avatarUrl", typ: "" },
{ json: "lastLogin", js: "lastLogin", typ: Date },
{ json: "created", js: "created", typ: Date },
{ json: "modified", js: "modified", typ: Date },
{ json: "visits", js: "visits", typ: 0 },
{ json: "profile", js: "profile", typ: true },
{ json: "basketDetail", js: "basketDetail", typ: a(r("BasketDetail")) },
], false),
"BasketDetail": o([
{ json: "response", js: "response", typ: a(r("Response")) },
], false),
"Response": o([
{ json: "product_count", js: "product_count", typ: 0 },
{ json: "products", js: "products", typ: a(r("Product")) },
], false),
"Product": o([
{ json: "product_id", js: "product_id", typ: 0 },
{ json: "usageInMinutes_recent", js: "usageInMinutes_recent", typ: u(undefined, 0) },
{ json: "usageInMinutes", js: "usageInMinutes", typ: 0 },
{ json: "usageInMinutes_windows", js: "usageInMinutes_windows", typ: 0 },
{ json: "usageInMinutes_mac", js: "usageInMinutes_mac", typ: 0 },
{ json: "usageInMinutes_linux", js: "usageInMinutes_linux", typ: 0 },
], false),
};
module.exports = {
"surveyToJson": surveyToJson,
"toSurvey": toSurvey,
};
试试这个这个模型可以帮助