Skip to content

Commit

Permalink
docs(typescript): rename examples to follow mongoose/TS conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelrahmanHafez committed Apr 3, 2022
1 parent 11a7c76 commit 8dc7545
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions docs/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,50 @@ To get started with Mongoose in TypeScript, you need to:
import { Schema, model, connect } from 'mongoose';

// 1. Create an interface representing a document in MongoDB.
interface User {
interface IUser {
name: string;
email: string;
avatar?: string;
}

// 2. Create a Schema corresponding to the document interface.
const schema = new Schema<User>({
const userSchema = new Schema<IUser>({
name: { type: String, required: true },
email: { type: String, required: true },
avatar: String
});

// 3. Create a Model.
const UserModel = model<User>('User', schema);
const User = model<IUser>('User', userSchema);

run().catch(err => console.log(err));

async function run(): Promise<void> {
async function run() {
// 4. Connect to MongoDB
await connect('mongodb://localhost:27017/test');

const doc = new UserModel({
const user = new User({
name: 'Bill',
email: 'bill@initech.com',
avatar: 'https://i.imgur.com/dM7Thhn.png'
});
await doc.save();
await user.save();

console.log(doc.email); // 'bill@initech.com'
console.log(user.email); // 'bill@initech.com'
}
```

You as the developer are responsible for ensuring that your document interface lines up with your Mongoose schema.
For example, Mongoose won't report an error if `email` is `required` in your Mongoose schema but optional in your document interface.

The `UserModel()` constructor returns an instance of `HydratedDocument<User>`.
`User` is a _document interface_, it represents the raw object structure that `User` objects look like in MongoDB.
`HydratedDocument<User>` represents a hydrated Mongoose document, with methods, virtuals, and other Mongoose-specific features.
The `User()` constructor returns an instance of `HydratedDocument<IUser>`.
`IUser` is a _document interface_, it represents the raw object structure that `IUser` objects look like in MongoDB.
`HydratedDocument<IUser>` represents a hydrated Mongoose document, with methods, virtuals, and other Mongoose-specific features.

```ts
import { HydratedDocument } from 'mongoose';

const doc: HydratedDocument<User> = new UserModel({
const user: HydratedDocument<IUser> = new User({
name: 'Bill',
email: 'bill@initech.com',
avatar: 'https://i.imgur.com/dM7Thhn.png'
Expand All @@ -75,15 +75,15 @@ To define a property of type `ObjectId`, you should use `Types.ObjectId` in the
import { Schema, Types } from 'mongoose';

// 1. Create an interface representing a document in MongoDB.
interface User {
interface IUser {
name: string;
email: string;
// Use `Types.ObjectId` in document interface...
organization: Types.ObjectId;
}

// 2. Create a Schema corresponding to the document interface.
const schema = new Schema<User>({
const userSchema = new Schema<IUser>({
name: { type: String, required: true },
email: { type: String, required: true },
// And `Schema.Types.ObjectId` in the schema definition.
Expand All @@ -103,7 +103,7 @@ Many Mongoose TypeScript codebases use the below approach.
```typescript
import { Document, Schema, model, connect } from 'mongoose';

interface User extends Document {
interface IUser extends Document {
name: string;
email: string;
avatar?: string;
Expand Down

0 comments on commit 8dc7545

Please sign in to comment.