Skip to content

Commit

Permalink
Merge branch 'master' into 6.5
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelrahmanHafez committed Jul 15, 2022
2 parents d97938b + 115f922 commit 486934f
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 75 deletions.
24 changes: 11 additions & 13 deletions .github/workflows/benchmark.yml
Expand Up @@ -2,20 +2,18 @@ name: Benchmark
on:
pull_request:
paths:
- '.github/workflows/benchmark.yml'
- 'package.json'
- 'types/**'
- 'benchmarks/typescript/**'
- 'benchmarks/typescript.js'
- ".github/workflows/benchmark.yml"
- "package.json"
- "types/**"
- "benchmarks/typescript/**"
push:
branches:
- master
paths:
- '.github/workflows/benchmark.yml'
- 'package.json'
- 'types/**'
- 'benchmarks/typescript/**'
- 'benchmarks/typescript.js'
- ".github/workflows/benchmark.yml"
- "package.json"
- "types/**"
- "benchmarks/typescript/**"
permissions:
contents: read

Expand All @@ -27,12 +25,12 @@ jobs:
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2

- name: Setup node
uses: actions/setup-node@eeb10cff27034e7acf239c5d29f62154018672fd # v3.1.1
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # v3.4.1
with:
node-version: 16

- run: npm install

- run: node ./benchmarks/typescript
- run: npx ts-benchmark -p ./benchmarks/typescript/simple -f 17/100000 18 29 32 -b master -g --colors
env:
DB_URL: ${{ secrets.DB_URL }}
DB_URL: ${{ secrets.DB_URL }}
2 changes: 1 addition & 1 deletion .github/workflows/documentation.yml
Expand Up @@ -31,7 +31,7 @@ jobs:
- uses: actions/checkout@a12a3943b4bdde767164f792f33f40b04645d846 # v3

- name: Setup node
uses: actions/setup-node@eeb10cff27034e7acf239c5d29f62154018672fd # v3.1.0
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # v3.4.1
with:
node-version: 16

Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Expand Up @@ -27,7 +27,7 @@ jobs:
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2

- name: Setup node
uses: actions/setup-node@eeb10cff27034e7acf239c5d29f62154018672fd # v3.1.1
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # v3.4.1
with:
node-version: 14

Expand Down Expand Up @@ -58,7 +58,7 @@ jobs:
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2

- name: Setup node
uses: actions/setup-node@eeb10cff27034e7acf239c5d29f62154018672fd # v3.1.1
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # v3.4.1
with:
node-version: ${{ matrix.node }}

Expand All @@ -84,7 +84,7 @@ jobs:
steps:
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2
- name: Setup node
uses: actions/setup-node@eeb10cff27034e7acf239c5d29f62154018672fd # v3.1.1
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # v3.4.1
with:
node-version: 16
- run: npm install
Expand All @@ -99,7 +99,7 @@ jobs:
steps:
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2
- name: Setup node
uses: actions/setup-node@eeb10cff27034e7acf239c5d29f62154018672fd # v3.1.1
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # v3.4.1
with:
node-version: 16
- run: npm install
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tidelift-alignment.yml
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Checkout
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2
- name: Setup node
uses: actions/setup-node@eeb10cff27034e7acf239c5d29f62154018672fd # v3.1.1
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # v3.4.1
with:
node-version: 16
- name: Alignment
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tsd.yml
Expand Up @@ -25,7 +25,7 @@ jobs:
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2

- name: Setup node
uses: actions/setup-node@eeb10cff27034e7acf239c5d29f62154018672fd # v3.1.1
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # v3.4.1
with:
node-version: 14

Expand All @@ -43,7 +43,7 @@ jobs:
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2

- name: Setup node
uses: actions/setup-node@eeb10cff27034e7acf239c5d29f62154018672fd # v3.1.1
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # v3.4.1
with:
node-version: 12

Expand Down
36 changes: 0 additions & 36 deletions docs/subdocs.md
Expand Up @@ -30,7 +30,6 @@ a group of fields (e.g. dateRange.fromDate <= dateRange.toDate).
<li><a href="#removing-subdocs">Removing Subdocs</a></li>
<li><a href="#subdoc-parents">Parents of Subdocs</a></li>
<li><a href="#altsyntaxarrays">Alternate declaration syntax for arrays</a></li>
<li><a href="#altsyntaxsingle">Alternate declaration syntax for single subdocuments</a></li>
</ul>

### What is a Subdocument?
Expand Down Expand Up @@ -319,41 +318,6 @@ const parentSchema = new Schema({
});
```

<h4 id="altsyntaxsingle"><a href="#altsyntaxsingle">Alternate declaration syntax for single nested subdocuments</a></h4>

Unlike document arrays, Mongoose 5 does not convert an objects in schemas
into nested schemas. In the below example, `nested` is a _nested path_
rather than a subdocument.

```javascript
const schema = new Schema({
nested: {
prop: String
}
});
```

This leads to some surprising behavior when you attempt to define a
nested path with validators or getters/setters.

```javascript
const schema = new Schema({
nested: {
// Do not do this! This makes `nested` a mixed path in Mongoose 5
type: { prop: String },
required: true
}
});

const schema = new Schema({
nested: {
// This works correctly
type: new Schema({ prop: String }),
required: true
}
});
```

### Next Up

Now that we've covered Subdocuments, let's take a look at
Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -60,7 +60,7 @@
"serve-handler": "6.1.3",
"sinon": "14.0.0",
"stream-browserify": "3.0.0",
"ts-benchmark": "^1.0.2",
"ts-benchmark": "^1.1.5",
"tsd": "0.20.0",
"typescript": "4.7.4",
"uuid": "8.3.2",
Expand Down Expand Up @@ -98,8 +98,8 @@
"test-tsd": "node ./test/types/check-types-filename && tsd",
"tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
"test-coverage": "nyc --reporter=html --reporter=text npm test",
"ts-benchmark": "node ./benchmarks/typescript.js",
"ts-benchmark-watch": "ts-benchmark -p ./benchmarks/typescript/simple -w ./types -i -s -f 17 18 29 32 -b master"
"ts-benchmark": "ts-benchmark -p ./benchmarks/typescript/simple -f 17/100000 18 29 32",
"ts-benchmark-watch": "ts-benchmark -p ./benchmarks/typescript/simple -w ./types -i -s -f 17/100000 18 29 32 -b master"
},
"main": "./index.js",
"types": "./types/index.d.ts",
Expand Down
23 changes: 23 additions & 0 deletions test/types/PipelineStage.test.ts
Expand Up @@ -392,3 +392,26 @@ const stages3: PipelineStage[] = [
}
}
];

const stages4: PipelineStage[] = [
{
$addFields: {
usersCount: {
$let: {
vars: {
users: { $push: '$user' }
},
in: {
$reduce: {
input: '$users',
initialValue: 0,
in: {
$cond: { if: { $isArray: '$$this' }, then: { $size: '$$this' }, else: '$$this' }
}
}
}
}
}
}
}
];
10 changes: 10 additions & 0 deletions test/types/expressions.test.ts
Expand Up @@ -193,3 +193,13 @@ const nullExpr: Expression = {
const nullNETupleExpr: Expression = {
$ne: ['$name', null]
};

const switchExpr: Expression.Switch = {
$switch: {
branches: [
{ case: { $eq: ['$name', 'Detlef'] }, then: 'Detlef' },
{ case: { $eq: ['$name', 'John'] }, then: 'John' }
],
default: 'Hello'
}
};
76 changes: 74 additions & 2 deletions test/types/schema.test.ts
Expand Up @@ -429,7 +429,7 @@ export function autoTypedSchema() {
mixed3: Schema.Types.Mixed,
objectId1: Schema.Types.ObjectId,
objectId2: 'ObjectId',
objectId3: 'objectId',
objectId3: 'ObjectID',
customSchema: Int8,
map1: { type: Map, of: String },
map2: { type: Map, of: Number },
Expand Down Expand Up @@ -540,7 +540,7 @@ export type AutoTypedSchemaType = {
favoritDrink?: 'Tea' | 'Coffee',
favoritColorMode: 'dark' | 'light'
friendID?: Types.ObjectId;
nestedArray: Array<{
nestedArray: Types.DocumentArray<{
date: Date;
messages?: number;
}>
Expand Down Expand Up @@ -630,3 +630,75 @@ function gh11987() {
expectError(userSchema.path<'foo'>('name'));
expectType<SchemaTypeOptions<string>>(userSchema.path<'name'>('name').OptionsConstructor);
}

function gh12030() {
const Schema1 = new Schema({
users: [
{
username: { type: String }
}
]
});

expectType<{
users: {
username?: string
}[];
}>({} as InferSchemaType<typeof Schema1>);

const Schema2 = new Schema({
createdAt: { type: Date, default: Date.now }
});

expectType<{ createdAt: Date }>({} as InferSchemaType<typeof Schema2>);

const Schema3 = new Schema({
users: [
new Schema({
username: { type: String },
credit: { type: Number, default: 0 }
})
]
});

expectType<{
users: Types.DocumentArray<{
credit: number;
username?: string;
}>;
}>({} as InferSchemaType<typeof Schema3>);


const Schema4 = new Schema({
data: { type: { role: String }, default: {} }
});

expectType<{ data: { role?: string } }>({} as InferSchemaType<typeof Schema4>);

const Schema5 = new Schema({
data: { type: { role: Object }, default: {} }
});

expectType<{ data: { role?: any } }>({} as InferSchemaType<typeof Schema5>);

const Schema6 = new Schema({
track: {
backupCount: {
type: Number,
default: 0
},
count: {
type: Number,
default: 0
}
}
});

expectType<{
track?: {
backupCount: number;
count: number;
};
}>({} as InferSchemaType<typeof Schema6>);

}
7 changes: 4 additions & 3 deletions types/expressions.d.ts
Expand Up @@ -1067,13 +1067,13 @@ declare module 'mongoose' {
* - $case
* - $then
*/
$branches: { $case: Expression, then: Expression }[];
branches: { case: Expression, then: Expression }[];
/**
* The path to take if no branch case expression evaluates to true.
*
* Although optional, if default is unspecified and no branch case evaluates to true, $switch returns an error.
*/
$default: Expression;
default: Expression;
};
}

Expand Down Expand Up @@ -2448,7 +2448,8 @@ declare module 'mongoose' {
BinaryExpression |
FunctionExpression |
ObjectIdExpression |
ConditionalExpressionOperator;
ConditionalExpressionOperator |
Expression.Let;

export type ObjectIdExpression =
TypeExpressionOperatorReturningObjectId;
Expand Down

0 comments on commit 486934f

Please sign in to comment.