简体   繁体   English

您如何为 mongodb 数据库播种,以便 Keystone 5 CMS 识别多对多关系?

[英]How do you seed a mongodb database such that the Keystone 5 CMS recognizes the many-to-many relationships?

Let's say I have two objects: Product and Seller假设我有两个对象:Product 和 Seller

Products can have multiple Sellers.产品可以有多个卖家。 A single Seller can sell multiple Products.一个卖家可以销售多个产品。

The goal is to write a seeding script that successfully seeds my MongoDB database such that Keystone.js's CMS recognizes the many-to-many relationship.目标是编写一个播种脚本,成功播种我的 MongoDB 数据库,以便 Keystone.js 的 CMS 识别多对多关系。

Schemas模式

Product.ts

import { text, relationship } from "@keystone-next/fields";
import { list } from "@keystone-next/keystone/schema";

export const Product = list({
  fields: {
    name: text({ isRequired: true }),
    sellers: relationship({
      ref: "Seller.products",
      many: true,
    }),
  },
});

Seller.ts

import { text, relationship } from "@keystone-next/fields";
import { list } from "@keystone-next/keystone/schema";

export const Product = list({
  fields: {
    name: text({ isRequired: true }),
    products: relationship({
      ref: "Product.sellers",
      many: true,
    }),
  },
});

KeystoneJS config KeystoneJS 配置

My keystone.ts config, shortened for brevity, looks like this:为了简洁起见,我的keystone.ts配置如下所示:

import { insertSeedData } from "./seed-data"
...
db: {
  adapter: "mongoose",
  url: databaseURL,
  async onConnect(keystone) {
    console.log("Connected to the database!");
    if (process.argv.includes("--seed-data")) {
      await insertSeedData(keystone);
    }
  },
},
lists: createSchema({
  Product,
  Seller,
}),
...

Seeding Scripts (these are the files I expect to change)播种脚本(这些是我希望更改的文件)

I have a script that populates the database ( seed-data/index.ts ):我有一个填充数据库的脚本( seed-data/index.ts ):

import { products } from "./data";
import { sellers } from "./data";

export async function insertSeedData(ks: any) {

  // setup code
  const keystone = ks.keystone || ks;
  const adapter = keystone.adapters?.MongooseAdapter || keystone.adapter;
  const { mongoose } = adapter;
  mongoose.set("debug", true);

  // adding products to DB
  for (const product of products) {
    await mongoose.model("Product").create(product);
  }

  // adding sellers to DB
  for (const seller of sellers) {
    await mongoose.model("Seller").create(seller);
  }
}

And finally, data.ts looks something like this:最后, data.ts看起来像这样:

export const products = [
  {
    name: "apple",
    sellers: ["Joe", "Anne", "Duke", "Alicia"],
  },
  {
    name: "orange",
    sellers: ["Duke", "Alicia"],
  },
  ...
];
export const sellers = [
  {
    name: "Joe",
    products: ["apple", "banana"],
  },
  {
    name: "Duke",
    products: ["apple", "orange", "banana"],
  },
  ...
];

The above setup does not work for a variety of reasons.由于各种原因,上述设置不起作用。 The most obvious is that the sellers and products attributes of the Product and Seller objects (respectively) should reference objects ( ObjectId ) and not names (eg "apple", "Joe").最明显的是ProductSeller对象(分别)的sellersproducts属性应该引用对象( ObjectId )而不是名称(例如“apple”、“Joe”)。

I'll post a few attempts below that I thought would work, but did not:我将在下面发布一些我认为可行的尝试,但没有:

Attempt 1尝试 1

I figured I'd just give them temporary ids (the id attribute in data.ts below) and then, once MongoDB assigns an ObjectId , I'll use those.我想我只是给他们临时 ID(下面data.ts中的id属性),然后,一旦 MongoDB 分配一个ObjectId ,我就会使用它们。

seed-data/index.ts

...
  const productIdsMapping = [];
...
  // adding products to DB
  for (const product of products) {
    const productToPutInMongoDB = { name: product.name };
    const { _id } = await mongoose.model("Product").create(productToPutInMongoDB);
    productIdsMapping.push(_id);
  }

  // adding sellers to DB (using product IDs created by MongoDB)
  for (const seller of sellers) {
    const productMongoDBIds = [];
    for (const productSeedId of seller.products) {
      productMongoDBIds.push(productIdsMapping[productSeedId]);
    const sellerToPutInMongoDB = { name: seller.name, products: productMongoDBIds };
    await mongoose.model("Seller").create(sellerToPutInMongoDB);
  }
...

data.ts

export const products = [
  {
    id: 0,
    name: "apple",
    sellers: [0, 1, 2, 3],
  },
  {
    id: 1,
    name: "orange",
    sellers: [2, 3],
  },
  ...
];
export const sellers = [
  {
    id: 0
    name: "Joe",
    products: [0, 2],
  },
  ...
  {
    id: 2
    name: "Duke",
    products: [0, 1, 2],
  },
  ...
];

Output (attempt 1): Output(尝试 1):

It just doesn't seem to care about or acknowledge the products attribute.它似乎并不关心或承认products属性。

Mongoose: sellers.insertOne({ _id: ObjectId("$ID"), name: 'Joe', __v: 0}, { session: null })
{
  results: {
    _id: $ID,
    name: 'Joe',
    __v: 0
  }
}

Attempt 2尝试 2

I figured maybe I just didn't format it correctly, for some reason, so maybe if I queried the products and shoved them directly into the seller object, that would work.我想也许我只是因为某种原因没有正确格式化它,所以如果我查询产品并将它们直接推到卖家 object 中,那可能会起作用。

seed-data/index.ts

...
  const productIdsMapping = [];
...
  // adding products to DB
  for (const product of products) {
    const productToPutInMongoDB = { name: product.name };
    const { _id } = await mongoose.model("Product").create(productToPutInMongoDB);
    productIdsMapping.push(_id);
  }

  // adding sellers to DB (using product IDs created by MongoDB)
  for (const seller of sellers) {
    const productMongoDBIds = [];
    for (const productSeedId of seller.products) {
      productMongoDBIds.push(productIdsMapping[productSeedId]);
    }
    const sellerToPutInMongoDB = { name: seller.name };
    const { _id } = await mongoose.model("Seller").create(sellerToPutInMongoDB);
    const resultsToBeConsoleLogged = await mongoose.model("Seller").findByIdAndUpdate(
      _id,
      {
        $push: {
          products: productMongoDBIds,
        },
      },
      { new: true, useFindAndModify: false, upsert: true }
    );
  }
...

data.ts

Same data.ts file as attempt 1.与尝试 1 相同的 data.ts 文件。

Output (attempt 2): Output(尝试 2):

Same thing.一样。 No luck on the products attribute appearing.出现的products属性没有运气。

Mongoose: sellers.insertOne({ _id: ObjectId("$ID"), name: 'Joe', __v: 0}, { session: null })
{
  results: {
    _id: $ID,
    name: 'Joe',
    __v: 0
  }
}

So, now I'm stuck.所以,现在我被困住了。 I figured attempt 1 would Just Work™ like this answer:我认为尝试 1 会像这个答案一样 Just Work™:

https://stackoverflow.com/a/52965025 https://stackoverflow.com/a/52965025

Any thoughts?有什么想法吗?

I figured out a solution.我想出了一个解决办法。 Here's the background:这是背景:

When I define the schema, Keystone creates corresponding MongoDB collections.当我定义架构时,Keystone 会创建相应的 MongoDB collections。 If there is a many-to-many relationship between object A and object B, Keystone will create 3 collections: A, B, and A_relationshipToB_B_relationshipToA.如果 object A 和 object B 之间存在多对多关系,Keystone 将创建 3 个 collections:A、B 和 A_relationshipToB_B_relationshipTo

That 3rd collection is the interface between the two.第三个集合是两者之间的接口。 It's just a collection with pairs of ids from A and B.它只是一个包含来自 A 和 B 的 id 对的集合。

Hence, in order to seed my database with a many-to-many relationship that shows up in the Keystone CMS, I have to seed not only A and B, but also the 3rd collection: A_relationshipToB_B_relationshipToA.因此,为了使用 Keystone CMS 中显示的多对多关系为我的数据库播种,我不仅需要播种 A 和 B,还需要播种第三个集合:A_relationshipToB_B_relationshipToA。

Hence, seed-data/index.ts will have some code that inserts into that table:因此, seed-data/index.ts将有一些代码插入到该表中:

...
for (const seller of sellers) {
    const sellerToAdd = { name: seller.name };
    const { _id } = await mongoose.model("Seller").create(sellerToAdd);

    // Product_sellers_Seller_products Insertion
    for (const productId of seller.products) {
      await mongoose
        .model("Product_sellers_Seller_products")
        .create({
          Product_left_id: productIds[productId], // (data.ts id) --> (Mongo ID)
          Seller_right_id: _id,
        });
    }
  }
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM