Coder Social home page Coder Social logo

Comments (5)

lwallent avatar lwallent commented on August 20, 2024

Hi @ElMatella,

You are trying to make an instance of the Model which may not be what you had in mind?

May I suggest keeping things separate - doing something like:

export class User extends Typegoose {
    ...
    @prop({ required: true})
    public firstName: string;
    ...
}

export const UserStore = new User().getModelForClass(User);

and then importing the User and/or Store?

import { User, UserStore } from './User';

let user:User = new User(); 

 const store = new UserStore(user); 
 await store.validate();
 user = await store.save();

But be aware, there is a snake in paradise ...
I will create a question myself about properties not having the writable property set in a minute.

Currently if you wish to work directly on the object properties you will have to make the properties writable in a hackish kind of way (not desirable):

export class User extends Typegoose {
    ...
   @prop(<any>{required:true, writable: true})
    public x: string;
    ...
}

export const UserStore = new User().getModelForClass(User);

let user:User = new User(); 
user.x = "test";
let store = new UserStore(user);
store.save();

Not making the property writable will produce a TypeError like:

TypeError: Cannot assign to read only property 'x' of object '#<User>'

Please note I am not part of the typegoose team and the above approach is not recommended in the long run, but I hope to convince the creators of the library to add the writable flag within the typegoose code :-) I have a local typegoose version where I have made this change. If you need it - then we are two :-) Good luck with your project!

Kind Regards
Lasse

from typegoose.

lwallent avatar lwallent commented on August 20, 2024

Hi @ElMatella,
I have created a PR on the writable issue described in my previous post.
KR
Lasse

from typegoose.

ElMatella avatar ElMatella commented on August 20, 2024

Thanks a lot for your response, it totally makes sense that way. It feels more like a repository kind of thing implementation. It would be great to have that documented if your PR is being merged.

from typegoose.

szokodiakos avatar szokodiakos commented on August 20, 2024

Hi, I've reviewed the PR. There needs to be some adjustments to make it fully featured. I've described the details at the PR's page.

Akos

from typegoose.

hasezoey avatar hasezoey commented on August 20, 2024

@Ben305 i think this can be closed

from typegoose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.