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.

schema type auto infer bugs

See original GitHub issue

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.4.2

Node.js version

18

MongoDB server version

5.0

Description

I’ve found a few more bugs in auto infer type

  1. Adding an object directly to the array doesn’t infer type correctly
const userSchema = new Schema({
	users: [
		{
			username: { type: String }
		}
	]
});
  1. passing function in default makes the key optional
const userSchema = new Schema({
	createdAt: { type: Date, default: Date.now }
});
  1. Array type becomes Array instead of Types.DocumentArray which makes pushing fields difficult
const userSchema = new Schema({
	users: [
		new Schema({
			username: { type: String },
			credit: { type: Number, default: 0 }
		})
	]
});

const userModel = model('user', userSchema);

const data = await userModel.findOne();
data!.users.push({ username: 'parbez' });
// ts warns to add createdAt which can be solved using Types.DocumentArray
  1. Adding empty object as default doesn’t makes the key required
const userSchema = new Schema({
	data: { type: { role: Object }, default: {} }
});
  1. Assigning an object directly to a key doesn’t infer type correctly
const userSchema = new Schema({
	data: {
		username: String
	}
});

Steps to Reproduce

added code blocks above

Expected Behavior

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
mohammad0-0ahmadcommented, Jul 11, 2022

Is fixed in 6.4.5?

Waiting for maintainer response.

2reactions
shiva-prasad-reddycommented, Jul 2, 2022
const schema = new Schema({
  track: {
    backupCount: {
      type: Number,
      default: 0,
    },
    count: {
      type: Number,
      default: 0,
    },
  },
});
type ISchema = InferSchemaType<typeof schema>;

resulting to a type of

type ISchema = {
    track: {
        backupCount: {
            type: NumberConstructor;
            default: number;
        };
        count: {
            type: NumberConstructor;
            default: number;
        };
    };
}

but it should be

type ISchema = {
    track: {
        backupCount: NumberConstructor;
        count: NumberConstructor;
    };
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Open-sourcing Facebook Infer: Identify bugs before you ship
Today, we're open-sourcing Facebook Infer, a static program analyzer that Facebook uses to identify bugs before mobile code is shipped.
Read more >
In Loving Memory of Strictly-Typed Schemas | SSENSE-TECH
How does one infer a schema? Do you write an algorithm to make an educated guess based on a subset of homogenous data?...
Read more >
Unable to infer schema when loading Parquet file
This error usually occurs when you try to read an empty directory as parquet. Probably your outcome Dataframe is empty.
Read more >
Auto Loader FAQ | Databricks on AWS
The schema is inferred when the DataFrame is first defined in your code. During each micro-batch, schema changes are evaluated on the fly; ......
Read more >
Databricks Runtime 9.1 LTS - Azure - Microsoft Learn
Array and map types are supported in Override schema inference with schema hints for Auto Loader. Examples of schema hints for arrays ...
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