'Creating a schema with minified version of a type

How can I create a Prisma schema for mongoDb that contains sort of a minified version of another object?

To explain better the data I am working with could look something like this:

const basicUser = {
  id: "62091d9ae17cad32fbe0eda5",
  displayName: "John Smith",
};
const post = {
  title: "My Title",
  content: "My content",
  author: basicUser,
};

const user = {
    id: "62091d9ae17cad32fbe0eda5",
    firstName: "John",
    lastName: "Smith",
    displayName: "John Smith",
    emailAddress:"[email protected]"
}

I am not interested in creating relations as I just want to include this basic data and then to be able to look up the full user if I need to.



Solution 1:[1]

Prisma has released support for composite types (Known as Embedded Documents in MongoDB) for embedding documents inside documents.

From your use case, it seems that composite types should be helpful to you.

You can define basicUser as a type and reference it in your post model.

type basicUser {
  id          String
  DisplayName String
}
model Post {
  title   String
  content String
  author  basicUser
}

Reference for using composite types.

Please note that this feature is in preview and you need to be on version 3.10.0 or later.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nurul Sundarani