๐ Auto-generate TypeScript interfaces from Mongoose schemas!
- Remotely
npx tongoose ./path/to/mongoose/model(s/)
- Locally (recommended)
npm install --global tongoose
# or
yarn global add tongoose
and run
tongoose ./path/to/mongoose/model(s/)
That's it! Now just integrate the generated interfaces with your Mongoose Schemas!
Don't know how? Head over to the From 0 to hero section - you'll set up a sample TypeScript + Babel project & will learn how to integrate TypeScript with mongoose Schemas!
Note - the code is available at /tongoose/TypeScript-Babel-Starter
Warning - this is currently out-of-date. We'll update this tutorial shortly.
git clone https://github.com/Microsoft/TypeScript-Babel-Starter.git
cd TypeScript-Babel-Starter
- Install dependencies
npm i mongoose
npm i --save-dev @types/node @types/mongoose @types/mongodb
- buckle up
rm src/index.ts
mkdir -pv src/models/
touch src/models/User.ts
- open
src/models/User.ts
file in your favorite text editor
// src/models/User.ts
import mongoose from "mongoose";
const UserSchema = new mongoose.Schema({
username: String,
email: { type: String, required: true },
password: { type: String, required: true },
});
const User = mongoose.model("User", UserSchema, "users");
export = User;
- Great. Now install
tongoose
& take a look at the--help
message:
npm i -g tongoose
tongoose
Usage: tongoose ./path/to/models/ [--opt [arg]]
Options:
-s, --src, --source [required] [selected by default] relative path to
mongoose models' directory
-o, --output [auto=source/index.d.ts] relative path for index.d.ts
type definition output
-n, --noFormat [auto=false] do not format the type definition files
-d, --debug [auto=false] enables debugging - generates .tongoose/
directory with separate type definition, raw & clean
json files for each schema
-v, --version Show version number
-h, --help Show help
Examples:
tongoose ./src/models
tongoose ./src/models --noFormat
tongoose --help
- Run
tongoose
with the path to oursrc/models/
directory
tongoose src/models/
โก๏ธ Tongoose finished
๐ Created `.tongoose` directory for manual debugging (you can safely delete it)
๐ Main `index.d.ts` file placed in ๐ `src/models/index.d.ts`
- open the generated
src/models/index.d.ts
file & take a look!
import mongoose from "mongoose"
import { ObjectId } from "bson"; // `npm i --save-dev @types/mongodb`
// Notice the difference between `IUserModel` & `IUser`!
export interface IUserModel extends IUser, mongoose.Document {}
export interface IUser {
username?: string;
email: string;
password: string;
}
- head back to
src/models/User.ts
- make the following changes:
// src/models/User.ts
import mongoose from "mongoose";
+import { IUserModel } from './index.d';
const UserSchema = new mongoose.Schema({
username: String,
email: { type: String, required: true },
password: { type: String, required: true },
});
-const User = mongoose.model("User", UserSchema, "users");
+const User = mongoose.model<IUserModel>("User", UserSchema, "users");
export = User;
- Create a
src/test.ts
file
touch src/test.ts
- open it & add the following code:
// src/test.ts
import User = require("./models/User");
const user = new User({});
- Start typing
user<dot>
and see what comes up!
Plot twist:
-
You can see the
email
field right at the bottom together withmongoose.Document
's fields. Everything (hopefully) worked great! -
Congratulations!
Back to Table of Contents โ๏ธ
Contributions are welcome! Please fork the repo, modify it & submit a pull request!
The Roadmap is a good place to start ๐
- Kipras Melnikovas - author - /sarpik ๐