Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange Timestamps Type Inference #14013

Closed
1 task done
kiki-kanri opened this issue Oct 28, 2023 · 2 comments
Closed
1 task done

Strange Timestamps Type Inference #14013

kiki-kanri opened this issue Oct 28, 2023 · 2 comments
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@kiki-kanri
Copy link

kiki-kanri commented Oct 28, 2023

Prerequisites

  • I have written a descriptive issue title

Mongoose version

7.6.3

Node.js version

20.9

MongoDB version

7.0.2

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

ubuntu22.04

Issue

It may be related to this PR.

I wrote a model build function to make it easier for me to standardize the addition of plugins when building a model.

Model builder function:

import { model, PaginateModel, Schema } from 'mongoose';
import mongoosePaginate from 'mongoose-paginate-v2';
import normalize from 'normalize-mongoose';

export const buildModel = <T extends Schema>(collection: string, name: string, schema: T) => {
	schema.plugin(mongoosePaginate);
	schema.plugin(normalize);
	const tModel = model(name, schema, collection);
	return tModel as typeof tModel & PaginateModel<T, T['query'], T['methods']>;
}

Schema:

const adminSchema = new Schema({
	// Fields
}, {
	timestamps: true
});

Under normal conditions there is an automatic type determination of timestamps.

const AdminModel = buildModel('10_admins', 'Admin', adminSchema);
const admin = await AdminModel.findOne();
admin?.createdAt // It works

image

But as soon as I set statics or methods, timestamps related types are disabled.

const adminSchema = new Schema({
	// Fields
}, {
        statics: {
		// Some statics
	},
	timestamps: true
});

const AdminModel = buildModel('10_admins', 'Admin', adminSchema);
const admin = await AdminModel.findOne();
admin?.createdAt // no createdAt attributes

image

But when I change the timestamps in DefaultSchemaOptions interface directly, it works again.

Is this by design or is there no definitive solution yet?

@kiki-kanri kiki-kanri added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Oct 28, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.6.5, 7.6.6 Oct 31, 2023
Copy link

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label Nov 15, 2023
@vkarpov15 vkarpov15 removed the Stale label Nov 19, 2023
@vkarpov15
Copy link
Collaborator

Unfortunately this is expected behavior currently, see automatic type inference docs

image

We have an issue #12807 to track this issue, as a workaround you should define createdAt and updatedAt yourself in your schemas if you use statics, methods, or virtuals.

@vkarpov15 vkarpov15 removed this from the 7.6.6 milestone Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants