-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
discriminator.test.ts
78 lines (59 loc) · 1.8 KB
/
discriminator.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import mongoose, { Document, Model, Schema, SchemaDefinition, SchemaOptions, Types, model } from 'mongoose';
const schema: Schema = new Schema({ name: { type: 'String' } });
interface IBaseTest extends Document {
name?: string;
}
interface IDiscriminatorTest extends IBaseTest {
email?: string;
}
const Base = model<IBaseTest>('Test', schema);
const Disc = Base.discriminator<IDiscriminatorTest>('Test2', new Schema({ email: { type: String } }));
const doc: IDiscriminatorTest = new Disc({ name: 'foo', email: 'hi' });
doc.name = 'bar';
doc.email = 'hello';
const Disc2 = Base.discriminator<IDiscriminatorTest>(
'Disc2',
new Schema({ email: { type: String } }),
{ value: 'test', mergeHooks: false }
);
function test(): void {
enum CardType {
Artifact = 'artifact',
Creature = 'creature',
Enchantment = 'enchantment',
Land = 'land',
}
interface CardDb extends Document {
_id: Types.ObjectId;
type: CardType;
}
interface LandDb extends CardDb {
type: CardType.Land;
}
const cardDbBaseSchemaDefinition: SchemaDefinition = {
type: { type: String, required: true }
};
const cardDbSchemaOptions: SchemaOptions = { discriminatorKey: 'type' };
const cardDbSchema: Schema = new Schema(
cardDbBaseSchemaDefinition,
cardDbSchemaOptions
);
const cardDbModel: Model<CardDb> = mongoose.model<CardDb>(
'Card',
cardDbSchema,
'card'
);
const landDbAdditionalPropertiesSchemaDefinition: SchemaDefinition = {};
const landDbSchema: Schema = new Schema(
landDbAdditionalPropertiesSchemaDefinition
);
const landDbModel: Model<LandDb> = cardDbModel.discriminator<LandDb>(
'Land',
landDbSchema,
CardType.Land
);
const sampleLandDb: LandDb = new landDbModel({
type: CardType.Land
});
const sampleCardDb: CardDb = sampleLandDb;
}