|
1 |
| -import { Schema, model, Document, Types } from 'mongoose'; |
| 1 | +import { Schema, model, Types, CallbackError } from 'mongoose'; |
| 2 | +import { expectError, expectType } from 'tsd'; |
2 | 3 |
|
3 | 4 | const schema: Schema = new Schema({ name: { type: 'String' } });
|
4 | 5 |
|
5 |
| -interface ITest extends Document { |
| 6 | +interface ITest { |
6 | 7 | _id?: Types.ObjectId;
|
7 | 8 | name?: string;
|
8 | 9 | }
|
9 | 10 |
|
10 | 11 | const Test = model<ITest>('Test', schema);
|
11 | 12 |
|
12 |
| -Test.create({ _id: new Types.ObjectId('0'.repeat(24)), name: 'test' }).then((doc: ITest) => console.log(doc.name)); |
| 13 | +Test.create({ _id: '000000000000000000000000', name: 'test' }).then(doc => { |
| 14 | + expectType<Types.ObjectId>(doc._id); |
| 15 | + expectType<string>(doc.name); |
| 16 | + expectType<boolean>(doc.isNew); |
| 17 | +}); |
13 | 18 |
|
14 |
| -Test.create([{ name: 'test' }], { validateBeforeSave: false }).then((docs: ITest[]) => console.log(docs[0].name)); |
| 19 | +Test.create({ _id: new Types.ObjectId('000000000000000000000000'), name: 'test' }).then((doc) => { |
| 20 | + expectType<Types.ObjectId>(doc._id); |
| 21 | + expectType<string>(doc.name); |
| 22 | + expectType<boolean>(doc.isNew); |
| 23 | +}); |
15 | 24 |
|
16 |
| -Test.create({ name: 'test' }, { name: 'test2' }).then((docs: ITest[]) => console.log(docs[0].name)); |
| 25 | +Test.create([{ name: 'test' }], { validateBeforeSave: false }).then(docs => { |
| 26 | + expectType<Types.ObjectId>(docs[0]._id); |
| 27 | + expectType<string>(docs[0].name); |
| 28 | + expectType<boolean>(docs[0].isNew); |
| 29 | +}); |
17 | 30 |
|
18 |
| -Test.insertMany({ name: 'test' }).then((docs: ITest[]) => console.log(docs[0].name)); |
| 31 | +Test.create({ name: 'test' }, { name: 'test2' }).then(docs => { |
| 32 | + expectType<Types.ObjectId>(docs[0]._id); |
| 33 | + expectType<string>(docs[0].name); |
| 34 | + expectType<Types.ObjectId>(docs[1]._id); |
| 35 | + expectType<string>(docs[1].name); |
| 36 | +}); |
19 | 37 |
|
20 |
| -Test.create([{ name: 'test' }], { validateBeforeSave: true }).then((docs: ITest[]) => console.log(docs[0].name)); |
| 38 | +Test.create([{ name: 'test' }], { validateBeforeSave: true }).then(docs => { |
| 39 | + expectType<Types.ObjectId>(docs[0]._id); |
| 40 | + expectType<string>(docs[0].name); |
| 41 | +}); |
21 | 42 |
|
22 |
| -(async() => { |
| 43 | + |
| 44 | +Test.insertMany({ name: 'test' }, {}, (err, docs) => { |
| 45 | + expectType<CallbackError>(err); |
| 46 | + expectType<Types.ObjectId>(docs[0]._id); |
| 47 | + expectType<string>(docs[0].name); |
| 48 | + expectType<boolean>(docs[0].isNew); |
| 49 | +}); |
| 50 | + |
| 51 | +Test.insertMany({ name: 'test' }, { lean: true }, (err, docs) => { |
| 52 | + expectType<CallbackError>(err); |
| 53 | + expectType<Types.ObjectId>(docs[0]._id); |
| 54 | + expectType<string>(docs[0].name); |
| 55 | + expectError(docs[0].isNew); |
| 56 | +}); |
| 57 | + |
| 58 | +Test.insertMany({ name: 'test' }, (err, docs) => { |
| 59 | + expectType<CallbackError>(err); |
| 60 | + expectType<Types.ObjectId>(docs[0]._id); |
| 61 | + expectType<string>(docs[0].name); |
| 62 | + expectType<boolean>(docs[0].isNew); |
| 63 | +}); |
| 64 | + |
| 65 | +Test.insertMany({ name: 'test' }, {}, (err, docs) => { |
| 66 | + expectType<CallbackError>(err); |
| 67 | + expectType<Types.ObjectId>(docs[0]._id); |
| 68 | + expectType<string>(docs[0].name); |
| 69 | + expectType<boolean>(docs[0].isNew); |
| 70 | +}); |
| 71 | + |
| 72 | +Test.insertMany([{ name: 'test' }], { rawResult: true }, (err, result) => { |
| 73 | + expectType<CallbackError>(err); |
| 74 | + expectType<boolean>(result.acknowledged); |
| 75 | + expectType<number>(result.insertedCount); |
| 76 | + expectType<{[key: number]: Types.ObjectId;}>(result.insertedIds) |
| 77 | +}); |
| 78 | + |
| 79 | +Test.insertMany([{ name: 'test' }], { rawResult: true }, (err, result) => { |
| 80 | + expectType<CallbackError>(err); |
| 81 | + expectType<boolean>(result.acknowledged); |
| 82 | + expectType<number>(result.insertedCount); |
| 83 | + expectType<{[key: number]: Types.ObjectId;}>(result.insertedIds) |
| 84 | +}); |
| 85 | + |
| 86 | +Test.insertMany([{ name: 'test' }], { lean: true }, (err, docs) => { |
| 87 | + expectType<CallbackError>(err); |
| 88 | + expectType<Types.ObjectId>(docs[0]._id); |
| 89 | + expectType<string>(docs[0].name); |
| 90 | + expectError(docs[0].isNew); |
| 91 | +}); |
| 92 | + |
| 93 | +Test.insertMany([{ name: 'test' }], (err, docs) => { |
| 94 | + expectType<CallbackError>(err); |
| 95 | + expectType<Types.ObjectId>(docs[0]._id); |
| 96 | + expectType<string>(docs[0].name); |
| 97 | + expectType<boolean>(docs[0].isNew); |
| 98 | +}); |
| 99 | + |
| 100 | +Test.insertMany({ _id: '000000000000000000000000', name: 'test' }, (err, docs) => { |
| 101 | + expectType<Types.ObjectId>(docs[0]._id); |
| 102 | + expectType<string>(docs[0].name); |
| 103 | + expectType<boolean>(docs[0].isNew); |
| 104 | +}); |
| 105 | + |
| 106 | +Test.insertMany({ _id: new Types.ObjectId('000000000000000000000000')}, (err, docs) => { |
| 107 | + expectType<Types.ObjectId>(docs[0]._id); |
| 108 | + expectType<string | undefined>(docs[0].name); |
| 109 | + expectType<boolean>(docs[0].isNew); |
| 110 | +}); |
| 111 | + |
| 112 | +Test.insertMany({ name: 'test' }, {}).then(docs => { |
| 113 | + expectType<Types.ObjectId>(docs[0]._id); |
| 114 | + expectType<string>(docs[0].name); |
| 115 | + expectType<boolean>(docs[0].isNew); |
| 116 | +}); |
| 117 | + |
| 118 | +Test.insertMany({ name: 'test' }, { lean: true }).then(docs => { |
| 119 | + expectType<Types.ObjectId>(docs[0]._id); |
| 120 | + expectType<string>(docs[0].name); |
| 121 | + expectError(docs[0].isNew); |
| 122 | +}); |
| 123 | + |
| 124 | +Test.insertMany({ name: 'test' }).then(docs => { |
| 125 | + expectType<Types.ObjectId>(docs[0]._id); |
| 126 | + expectType<string>(docs[0].name); |
| 127 | + expectType<boolean>(docs[0].isNew); |
| 128 | +}); |
| 129 | + |
| 130 | +Test.insertMany({ name: 'test' }, {}).then(docs => { |
| 131 | + expectType<Types.ObjectId>(docs[0]._id); |
| 132 | + expectType<string>(docs[0].name); |
| 133 | + expectType<boolean>(docs[0].isNew); |
| 134 | +}); |
| 135 | + |
| 136 | +Test.insertMany([{ name: 'test' }], { rawResult: true }).then(result => { |
| 137 | + expectType<boolean>(result.acknowledged); |
| 138 | + expectType<number>(result.insertedCount); |
| 139 | + expectType<{[key: number]: Types.ObjectId;}>(result.insertedIds) |
| 140 | +}); |
| 141 | + |
| 142 | +Test.insertMany([{ name: 'test' }], { rawResult: true }).then(result => { |
| 143 | + expectType<boolean>(result.acknowledged); |
| 144 | + expectType<number>(result.insertedCount); |
| 145 | + expectType<{[key: number]: Types.ObjectId;}>(result.insertedIds) |
| 146 | +}); |
| 147 | + |
| 148 | +Test.insertMany([{ name: 'test' }], { lean: true }).then(docs => { |
| 149 | + expectType<Types.ObjectId>(docs[0]._id); |
| 150 | + expectType<string>(docs[0].name); |
| 151 | + expectError(docs[0].isNew); |
| 152 | +}); |
| 153 | + |
| 154 | +Test.insertMany([{ name: 'test' }], { lean: false }).then(docs => { |
| 155 | + expectType<Types.ObjectId>(docs[0]._id); |
| 156 | + expectType<string | undefined>(docs[0].name); |
| 157 | + expectType<boolean>(docs[0].isNew); |
| 158 | +}); |
| 159 | + |
| 160 | +Test.insertMany([{ name: 'test' }], { }).then(docs => { |
| 161 | + expectType<Types.ObjectId>(docs[0]._id); |
| 162 | + expectType<string | undefined>(docs[0].name); |
| 163 | + expectType<boolean>(docs[0].isNew); |
| 164 | +}); |
| 165 | + |
| 166 | +Test.insertMany([{ name: 'test' }]).then(docs => { |
| 167 | + expectType<Types.ObjectId>(docs[0]._id); |
| 168 | + expectType<string>(docs[0].name); |
| 169 | + expectType<boolean>(docs[0].isNew); |
| 170 | +}); |
| 171 | + |
| 172 | +Test.insertMany({ _id: '000000000000000000000000', name: 'test' }).then(docs => { |
| 173 | + expectType<Types.ObjectId>(docs[0]._id); |
| 174 | + expectType<string>(docs[0].name); |
| 175 | + expectType<boolean>(docs[0].isNew); |
| 176 | +}); |
| 177 | + |
| 178 | +Test.insertMany({ _id: new Types.ObjectId('000000000000000000000000'), name: 'test' }).then(docs => { |
| 179 | + expectType<Types.ObjectId>(docs[0]._id); |
| 180 | + expectType<string>(docs[0].name); |
| 181 | + expectType<boolean>(docs[0].isNew); |
| 182 | +}); |
| 183 | + |
| 184 | +(async () => { |
23 | 185 | const [t1] = await Test.create([{ name: 'test' }]);
|
24 | 186 | const [t2, t3, t4] = await Test.create({ name: 'test' }, { name: 'test' }, { name: 'test' });
|
25 | 187 | (await Test.create([{ name: 'test' }]))[0];
|
|
0 commit comments