question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

We’re currently in the process of migrating from mongoose to papr. Here’s a question that I have: Say I have schema for users collection:

const userSchema = schema(
  {
    numPosts: types.number(),

    emailPreferences: types.object({
      monthly: types.boolean(),
      newsletter: types.boolean(),
      followers: types.boolean(),
    }),

    appreciations: types.array(types.object({
      badge: types.objectId(),
      count: types.number(),
    })),
  },
  {
    defaults: {
      numPosts: 0,
      
      emailPreferences: {
        monthly: true,
        newsletter: true,
        followers: true,
      },

     appreciations: [
        {
          count: 0
        }
      ],
    }
  }
)

As you can see, I have 3 root fields in this schema: numPosts, emailPreferences, and appreciations. I use papr’s default functionality for all 3.

For numPosts, it’s straight forward. As for emailPreferences, what happens when I try to insert an object that only has one field defined ? will papr merge that with the defaults of the 2 remaining fields ?

EDIT: The answer is no. It will not be merged w/ the other defaults. When I tried the following insert:

UserModel.insertOne({
        name: "dsafnaisdhfujabsf1",
        email: "iqwuheiqhsiadiasidu1@gmail.com",
        username: "dsafnaisdhfujabsf1",
        emailPreferences: {
          newsletter: false,
        },
      });

The resulting document only included newsletter: false:

...
emailPreferences: { newsletter: false },

This is not ideal. The expected behaviour here is that it should merge the one field I defined with other undefined fields from the defaults object.

And as for appreciations, how do I represent defaults for an array of objects ? is this the correct way ? What would happen if I try to insert this document without appreciations field ? will it insert just the count field ?

EDIT: the answer is yes. Papr is going to insert an array that contains: appreciations: [{ count: 0 }] by default. The expected behaviour here is that these defaults are only used when I try to insert an array. How do I achieve this ?

Does Papr allow me to use nested schemas ? EDIT: the answer is no. Papr models will work, but they will not make use of the nested schema.

Overall, I think the current API for defaults in papr is really limited and confusing. I believe defaults shouldn’t be in a separate object. They should be included in the type definitions themselves. Which is the case in mongoose and ts-mongoose:

newsletter: Type.boolean({ default: false })

Is such a change possible/planned ? And if not, how can I achieve the same capabilities for defining defaults using Papr ?

Thanks a lot for your amazing package.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9

github_iconTop GitHub Comments

3reactions
avalycommented, Jul 21, 2022

I’m OOO now, but I saw this thread and wanted to comment.

Regarding your last comment about the API resembling the ts-mongoose one, I remember that was an initial implementation of our first version of papr, before we open sourced it. But we had to move away from that API, because we need the defaults type as a generic type as well at the top level of a Model in order to use it in certain methods like insertOne, etc.

If you can make this API change, and keep all the existing functionality in the tests, we’re more than happy to consider such a change.

1reaction
BahaaZidancommented, Jul 21, 2022

@ejmartin504 From an API standpoint, I think the defaults API of ts-mongoose (may she rest in piece) is a good place to be at.

const userSchema = schema(
  {
    appreciations: types.array(types.object({
      badge: types.objectId(),
      count: types.number({ default: 0 }),
    }), { default: [] }),
  }
)

What do you think about this ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

deep-defaults - npm
Recursive version of _.defaults. Latest version: 1.0.5, last published: 4 years ago. Start using deep-defaults in your project by running ...
Read more >
GitHub - d5/deep-defaults: Recursive version of _.
Recursive version of _.defaults. Contribute to d5/deep-defaults development by creating an account on GitHub.
Read more >
_.defaultsDeep – Lodash Docs v4.17.11
This method is like _.defaults except that it recursively assigns default properties. Note: This method mutates object . Since. 3.10.0. Arguments.
Read more >
Defaults | Deep Dives - Cuetorials
Home / Deep Dives / Defaults. Defaults. https://cuelang.org/docs/references/spec/#default-values · Embedding.
Read more >
Defaults-deep Project - CVE.report
A prototype pollution vulnerability was found in defaults-deep <=0.2.4 that would allow a malicious user to inject properties... 9.8 - CRITICAL, 2019-02-01 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found