mongoose#SchemaTypes TypeScript Examples
The following examples show how to use
mongoose#SchemaTypes.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: file.entity.ts From nestjs-file-streaming with MIT License | 6 votes |
FileModel = new Schema({
_id: SchemaTypes.ObjectId,
length: SchemaTypes.Number,
chunkSize: SchemaTypes.Number,
uploadDate: SchemaTypes.Date,
filename: SchemaTypes.String,
md5: SchemaTypes.String,
contentType: SchemaTypes.String,
})
Example #2
Source File: comment.model.ts From nestjs-rest-sample with GNU General Public License v3.0 | 6 votes |
CommentSchema = new Schema<Comment>(
{
content: SchemaTypes.String,
post: { type: SchemaTypes.ObjectId, ref: 'Post', required: false },
createdBy: { type: SchemaTypes.ObjectId, ref: 'User', required: false },
updatedBy: { type: SchemaTypes.ObjectId, ref: 'User', required: false },
},
{ timestamps: true },
)
Example #3
Source File: post.model.ts From nestjs-rest-sample with GNU General Public License v3.0 | 6 votes |
PostSchema = new Schema<Post>(
{
title: SchemaTypes.String,
content: SchemaTypes.String,
createdBy: { type: SchemaTypes.ObjectId, ref: 'User', required: false },
updatedBy: { type: SchemaTypes.ObjectId, ref: 'User', required: false },
},
{ timestamps: true },
)
Example #4
Source File: user.model.ts From nestjs-rest-sample with GNU General Public License v3.0 | 6 votes |
UserSchema = new Schema<User>(
{
username: SchemaTypes.String,
password: SchemaTypes.String,
email: SchemaTypes.String,
firstName: { type: SchemaTypes.String, required: false },
lastName: { type: SchemaTypes.String, required: false },
roles: [
{ type: SchemaTypes.String, enum: ['ADMIN', 'USER'], required: false },
],
// use timestamps option to generate it automaticially.
// createdAt: { type: SchemaTypes.Date, required: false },
// updatedAt: { type: SchemaTypes.Date, required: false },
},
{
timestamps: true,
toJSON: {
virtuals: true,
},
},
)
Example #5
Source File: generateModel.test.ts From davinci with MIT License | 4 votes |
describe('typed mongoose', () => {
afterEach(() => {
sinon.restore();
});
describe('schema generation', () => {
it('supports primitive types', () => {
class Customer {
@prop()
firstname: string;
@prop()
age: number;
@prop()
isActive: boolean;
}
const schema = generateSchema(Customer, {}, false);
should(schema).be.deepEqual({
firstname: {
type: String
},
age: {
type: Number
},
isActive: {
type: Boolean
}
});
});
it('supports nested classes', () => {
class CustomerBirth {
@prop()
place: string;
}
class Customer {
@prop()
birth: CustomerBirth;
}
const schema = generateSchema(Customer, {}, false);
should(schema).be.deepEqual({
birth: {
type: {
place: {
type: String
}
}
}
});
});
it('supports arrays', () => {
class CustomerBirth {
@prop()
place: string;
}
class Customer {
@prop({ type: [CustomerBirth] })
birth: CustomerBirth[];
@prop({ type: [String] })
tags: string[];
}
const schema = generateSchema(Customer, {}, false);
should(schema).be.deepEqual({
birth: [
{
type: {
place: {
type: String
}
}
}
],
tags: [{ type: String }]
});
});
it('supports class inheritance', () => {
class BaseSchema {
@prop()
createdAt: string;
@prop()
updatedAt: number;
}
class MyClass1 extends BaseSchema {
@prop()
otherProp1: string;
}
class MyClass2 extends BaseSchema {
@prop()
otherProp2: string;
}
const schema1 = generateSchema(MyClass1, {}, false);
const schema2 = generateSchema(MyClass2, {}, false);
const baseSchema = generateSchema(BaseSchema, {}, false);
should(Object.keys(schema1)).be.deepEqual(['createdAt', 'updatedAt', 'otherProp1']);
should(Object.keys(schema2)).be.deepEqual(['createdAt', 'updatedAt', 'otherProp2']);
should(Object.keys(baseSchema)).be.deepEqual(['createdAt', 'updatedAt']);
});
});
describe('#generateSchema', () => {
it('attach statics and model methods', () => {
class Customer {
@method()
static myStaticMethod() {}
@method()
myMethod() {}
}
const schema = generateSchema(Customer);
should(schema.statics.myStaticMethod).be.equal(Customer.myStaticMethod);
should(schema.methods.myMethod).be.equal(Customer.prototype.myMethod);
});
it('should add the indexes', () => {
@index({ firstname: 1, lastname: 1 })
@index({ lastname: 1, unique: true })
class Customer {
@prop({ index: true })
firstname: string;
@prop()
lastname: string;
}
const schema = generateSchema(Customer);
should(schema.indexes()).be.deepEqual([
[
{
firstname: 1
},
{
background: true
}
],
[
{
lastname: 1,
unique: true
},
{
background: true
}
],
[
{
firstname: 1,
lastname: 1
},
{
background: true
}
]
]);
});
it('should add validators', () => {
const validateFn = sinon.stub().returns(true);
class Customer {
@prop({ validate: validateFn })
firstname: string;
}
const schema = generateSchema(Customer);
// @ts-ignore
should(schema.path('firstname').validators).match([{ validator: validateFn }]);
});
it('should support passing raw mongoose types', () => {
class Customer {
@prop({ required: true, index: true, rawType: SchemaTypes.Decimal128 })
firstname: string;
}
const schema = generateSchema(Customer);
// @ts-ignore
should(schema.path('firstname')).be.instanceOf(SchemaTypes.Decimal128);
should(schema.path('firstname')).match({
options: { required: true, index: true }
});
});
it('should support attaching mongoose functionalities to sub-schemas', () => {
class Item {
@virtual()
categories() {}
}
class Order {
@prop({ index: true })
firstname: string;
@prop({ type: [Item] })
items: Item[];
}
const schema = generateSchema(Order);
should(schema.path('items').schema.virtualpath('categories')).be.ok();
});
it('avoid passing the schema options down to subschemas', () => {
class Item {
@virtual()
categories() {}
}
class Order {
@prop({ index: true })
firstname: string;
@prop({ type: [Item] })
items: Item[];
}
const schema = generateSchema(Order, { timestamps: true });
should(schema.options.timestamps).be.True();
should(schema.path('items').schema.options.timestamps).not.be.True();
});
it('should allow passing the schema options using the decorator', () => {
@mgoose.schema({ timestamps: false, id: false, _id: false })
class Item {
@virtual()
categories() {}
}
@mgoose.schema({ timestamps: true, id: true, _id: true })
class Order {
@prop({ index: true })
firstname: string;
@prop({ type: [Item] })
items: Item[];
}
const schema = generateSchema(Order);
should(schema.options).match({ timestamps: true, id: true, _id: true });
should(schema.path('items').schema.options).match({ timestamps: false, id: false, _id: false });
});
});
describe('#generateModel', () => {
it('should generate a mongoose model', () => {
class Customer {
firstname: string;
}
const CustomerModel = generateModel(Customer);
should(CustomerModel.prototype).be.instanceOf(Model);
});
});
});