Coder Social home page Coder Social logo

Objects within Schema about typegoose HOT 10 CLOSED

szokodiakos avatar szokodiakos commented on July 20, 2024 8
Objects within Schema

from typegoose.

Comments (10)

szokodiakos avatar szokodiakos commented on July 20, 2024

Hello, thanks for your interest in Typegoose. Yes I'm afraid currently sub schemas are supported only. This is an already requested feature so it is on the list.

from typegoose.

MichalLytek avatar MichalLytek commented on July 20, 2024

@szokodiakos I'm afraid you won't be able to do that. Typescript reflection is very limited and the only way to have sub documents is by defining sub schemas using classes.

from typegoose.

MickL avatar MickL commented on July 20, 2024

@szokodiakos Any estimation for this? Is this complicated and do you have time for this enhancement?

from typegoose.

szokodiakos avatar szokodiakos commented on July 20, 2024

@MickL Sorry I don't have the time for research and implementation right now. Currently I'm just trying to fix critical issues, and review other folks PRs.

@19majkel94 that is bad news, then maybe there isn't a viable solution currently at all. 😞

from typegoose.

ykaiser avatar ykaiser commented on July 20, 2024

@MickL @szokodiakos there's a workaround that i just found.

You could create a class for your address and use it in person.
All you have to do, is cast it to an object while setting it before saving.

Here's an example:

class Address {
  street: string
  streetnumber: number
}

class Person extends Typegoose {
  @prop()
  id: string;

 @prop()
  address: Address
});

export let PersonModel = new Person().getModelForClass(Address)
// Here the code before saving

const person = new PersonModel()
const address = new Address()
address.street = "some street"
address.streetnumber = 42
person.address = Object.assign({}, address)
person.save()

No need to cast it back to Address when loading, the only trick is the Object.assign({}, address) which creates an object containing the fields in address.

Hope that helps you!

from typegoose.

richarddd avatar richarddd commented on July 20, 2024

This issue should be closed? The work around is to add a new class and use that class as type. Another option is the use object (works with latest typegoose)

from typegoose.

dainbrump avatar dainbrump commented on July 20, 2024

What if I have a Ref in a nested object? For example, I have an Environment model:

class EnvironmentModel extends Typegoose {
  @prop()
  name: string;
  //... other properties below
}

and then I have an Organization model that references that Environment model in a nested object. The desired schema is:

const OrganizationSchema = new mongoose.Schema({
  name: { type: String, required: true, index: { unique: true } },
  suborganizations: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Organization' }],
  settings: {
    environments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Environment' }]
  }
} // exported with mongoose.model('Organization', OrganizationSchema)

I've tried the following:

class OrgSettings {
  environment: Ref<EnvironmentModel>[]
}

class OrganizationModel extends Typegoose {
  @prop({required: true, index: true, unique: true })
  name: string;

  @arrayProp({ itemsRef: OrganizationModel })
  suborganizations: Ref<OrganizationModel>[];

  @prop()
  settings: OrgSettings
}

This doesn't appear to work and I'm curious if I'm misunderstanding something.

from typegoose.

AlecDusheck avatar AlecDusheck commented on July 20, 2024

Anyone come up with a good solution for the problem mentioned above?

from typegoose.

hasezoey avatar hasezoey commented on July 20, 2024

This seems to work in version 6.0.0

from typegoose.

hasezoey avatar hasezoey commented on July 20, 2024

@Ben305 this can be closed because this should work in 5.9.0+

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.