Typegoose: Support for multidimensional array types

Created on 3 Oct 2019  路  8Comments  路  Source: typegoose/typegoose

Hi, thanks for maintaining this amazing project.

We ran into a problem during porting our Mongoose schemas to Typegoose definitions. We use multidimensional arrays in our schemas, but I haven't found any information about this use-case in the documentation. Is it supported by the lib?

E.g. I have a class definition like this:

class Foo {
  @arrayProp({
    items: [Number]
  })
  public bar: number[][];
}

const FooModel = getModelForClass(Foo);

When I run the code it gives me an error:
Error: "Foo.bar"'s Type is invalid! Type is: "function Number() { [native code] }"

As far as I know this is a valid Mongoose schema:

new mongoose.Schema({
  bar: [[Number]]
})

Could you provide some information about this problem?
Thanks

feature parity

Most helpful comment

Added this to the 7.0 Roadmap, with the following proposal:

add an option dimensions (or dim) which would be of type Number, this would add support for typed-multi-dimension arrays

class SomeClass {
  @arrayProp({ items: String, dimensions: 5 })
  public lengthyArray: string[][][][][]; // i hope the type is right
}

implementation proposal:
loop over the number of "dimensions" and at the end, add the type that is listed

All 8 comments

If it's just saving a multidimensional array, and not an array of multidimensional arrays than you can simply use:

@prop()
bar: number[][][]

multidimensional arrays are not official supported (means we dont have tests for it)

and i dont really know how to support them explicitly, currently (by ryanns example) it can just be the type, which would translate to an (any) Array
-> any pr's for this would be welcome, but i dont currently how to implement it without fully reworking prop again


@RyannGalea just to make sure, in your example you used @prop to make an array, does it not work with @arrayProp?

Currently, I am using this for my GeoJson polygon type without any issues. The @field is to do with 'type-graphql' which is also workaround by using a custom scalar type.

I have not tried to use it with @arrayProp as I just simply haven't had any issues.

@ObjectType()
export class PolygonGeometryGeoJson {

  @Field(type => PointTypeEnum)
  @prop({ required: true, enum: Object.values(PointTypeEnum) })
  type: PointTypeEnum

  @Field(type => TripleNumberArrayScalar)
  @prop({ required: true })
  coordinates: number[][][]

}

@RyannGalea i just mean does coordinates: number[][][] not "compile" to an Array(any) and allow everything?

It perhaps does I am not certain, in my case because I'm using Graphql I am strictly typed to the Graphql schema anyway so any data I send is type-checked before it hits my mongoose schema.

Added this to the 7.0 Roadmap, with the following proposal:

add an option dimensions (or dim) which would be of type Number, this would add support for typed-multi-dimension arrays

class SomeClass {
  @arrayProp({ items: String, dimensions: 5 })
  public lengthyArray: string[][][][][]; // i hope the type is right
}

implementation proposal:
loop over the number of "dimensions" and at the end, add the type that is listed

added in 8c980eaf69f4fedcf4d13a2ffc41df40c0931565, shipped with 7.0+ (or 7.0.0-1+)
-> testing would be appreciated

closing because feature is implemented since 8c980eaf69f4fedcf4d13a2ffc41df40c0931565

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AbderrazzakB picture AbderrazzakB  路  3Comments

dandv picture dandv  路  10Comments

gserracalahorra picture gserracalahorra  路  6Comments

kerolloz picture kerolloz  路  6Comments

wxs77577 picture wxs77577  路  5Comments