Skip to content

Commit

Permalink
feat(mongoose): add support for nested properties with name "type"
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrià Sala committed Feb 14, 2024
1 parent 9cfa496 commit e6ca7a1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
6 changes: 3 additions & 3 deletions packages/mongoose/src/generateModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ export const generateSchema = <T>(

const prop = {
...omit(options ?? {}, ['type']),
type
$type: type
};

return {
...acc,
[reflection.name]: isArray ? [prop.type] : prop
[reflection.name]: isArray ? { ...prop, $type: [prop.$type] } : prop
};
}, {});

Expand All @@ -128,7 +128,7 @@ export const generateSchema = <T>(
const schemaOptions = schemaDecoration?.options;
const schema: Schema<U> =
(forceCreateSchema || schemaDecoration) &&
new Schema(definition, { ...(mergedOptions ?? schemaOptions) });
new Schema(definition, { ...(mergedOptions ?? schemaOptions), typeKey: '$type' });

// register methods
const methods = classReflection.methods.reduce((acc, methodReflection) => {
Expand Down
69 changes: 36 additions & 33 deletions packages/mongoose/test/unit/generateModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ describe('generateModel', () => {

expect(schema.obj).to.be.deep.equal({
firstname: {
type: String
$type: String
},
age: {
type: Number
$type: Number
},
isActive: {
type: Boolean
$type: Boolean
}
});
});
Expand All @@ -59,9 +59,9 @@ describe('generateModel', () => {

expect(schema.obj).to.be.deep.equal({
birth: {
type: {
$type: {
place: {
type: String
$type: String
}
}
}
Expand All @@ -84,14 +84,12 @@ describe('generateModel', () => {

const schema = mgoose.generateSchema(Customer, {});
expect(schema.obj).to.be.deep.equal({
birth: [
{
place: {
type: String
}
}
],
tags: [String]
birth: {
$type: [{ place: { $type: String } }]
},
tags: {
$type: [String]
}
});

const CustomerModel = model('Customer', schema);
Expand Down Expand Up @@ -126,44 +124,49 @@ describe('generateModel', () => {
expect(Object.keys(baseSchema.obj)).be.deep.equal(['createdAt', 'updatedAt']);
});

/*
it('supports nested properties, with name "type"', () => {
class Phone {
class Phones {
@mgoose.prop()
type: string;

@mgoose.prop()
number: string;
}
class Profile {
@mgoose.prop()
name: string;

@mgoose.prop({ type: [Phones], required: true })
phones: Phones[];
}

class Customer {
@mgoose.prop({ type: [Phone], required: true })
phones: Phone[];
@mgoose.prop({ type: [Profile], required: true })
profiles: Profile[];
}

const schema = mgoose.generateSchema(Customer, {});

expect(schema.obj).to.be.deep.equal({
profiles: [
{
required: true,
type: {
name: { type: String },
phones: [
{
required: true,
type: {
type: { type: String },
number: { type: String }
profiles: {
required: true,
$type: [
{
name: { $type: String },
phones: {
required: true,
$type: [
{
type: { $type: String },
number: { $type: String }
}
}
]
]
}
}
}
]
]
}
});
});
*/
});

describe('#mgoose.generateSchema', () => {
Expand Down

0 comments on commit e6ca7a1

Please sign in to comment.