Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
@typegoose/typegoose
Advanced tools
(These badges are from typegoose:master)
Define Mongoose models using TypeScript classes.
import { prop, getModelForClass } from '@typegoose/typegoose';
import * as mongoose from 'mongoose';
class User {
@prop()
public name?: string;
}
const UserModel = getModelForClass(User); // UserModel is a regular Mongoose Model with correct types
(async () => {
await mongoose.connect('mongodb://localhost:27017/', { useNewUrlParser: true, useUnifiedTopology: true, dbName: "test" });
const { _id: id } = await UserModel.create({ name: 'JohnDoe' } as User); // an "as" assertion, to have types for all properties
const user = await UserModel.findById(id).exec();
console.log(user); // prints { _id: 59218f686409d670a97e53e0, name: 'JohnDoe', __v: 0 }
})();
A common problem when using Mongoose with TypeScript is that you have to define both the Mongoose model and the TypeScript interface. If the model changes, you also have to keep the TypeScript interface file in sync or the TypeScript interface would not represent the real data structure of the model.
Typegoose aims to solve this problem by defining only a TypeScript interface (class) which need to be enhanced with special Typegoose decorators.
Under the hood it uses the Reflect & reflect-metadata API to retrieve the types of the properties, so redundancy can be significantly reduced.
Instead of:
interface Car {
model?: string;
}
interface Job {
title?: string;
position?: string;
}
interface User {
name?: string;
age: number;
job?: Job;
car: Car | string;
}
mongoose.model('User', {
name: String,
age: { type: Number, required: true },
job: {
title: String;
position: String;
},
car: { type: Schema.Types.ObjectId, ref: 'Car' }
});
mongoose.model('Car', {
model: string,
});
You can just:
class Job {
@prop()
public title?: string;
@prop()
public position?: string;
}
class Car {
@prop()
public model?: string;
}
class User {
@prop()
public name?: string;
@prop({ required: true })
public age!: number;
@prop()
public job?: Job;
@prop({ ref: Car })
public car?: Ref<Car>;
}
emitDecoratorMetadata
and experimentalDecorators
must be enabled in tsconfig.json
npm i -s @typegoose/typegoose
You also need to install mongoose
, since version 5 it is listed as a peer-dependency
npm i -s mongoose
npm run test
Run our tests after running npm i -D
Major.Minor.Fix
(or how npm expresses it Major.Minor.Patch
)
(This Project should comply with Semver)
To ask questions or just talk with us join our Discord Server
Here is the Documentation
Here are Guides
+1
or something like that, use the Reactionsnpm run doc
generates all documentation for all files that can be used as modules (is used for github-pages)npm run doc:all
generates documentation even for internal modules6.1.0
@staticMethod
& @instanceMethod
, because they were completely obsolete@prop({ validate })
now accepts { validator, message }
as an arraydeleteModel
& deleteModelWithClass
setGlobalOptions
runSyncIndexes
allowMixed
text
to PropOptionsitemsRef
, itemsRefPath
& itemsRefType
DocumentType
will now overwrite the type of _id
if the class is extending Base
(in TypeScript there is currently no other way)tslib
as dependency to minimize generated codesetModelForClass
setGlobalOptions({ globalOptions: { useNewEnum: true } })
(to not break existing databases made with the old handling)getModelWithString
initAsObject
and initAsArray
into initProperty
isNullOrUndefined
checksMixed
is doneIModelOptions
are merged (thanks to lodash cloneDeepWith
& mergeWith
)Buffer
& Decimal
now workprop.ts
FAQs
Define Mongoose models using TypeScript classes
The npm package @typegoose/typegoose receives a total of 44,587 weekly downloads. As such, @typegoose/typegoose popularity was classified as popular.
We found that @typegoose/typegoose demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.