Tongoose
📘 Auto-generate TypeScript interfaces from Mongoose schemas!
Usage
- Remotely
npx tongoose ./path/to/mongoose/model
- Locally (recommended)
npm install --global tongoose# or yarn global add tongoose
and run
tongoose ./path/to/mongoose/model
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!
Table of contents
From 0 to hero (you'll love it)
Note - the code is available at /tongoose/TypeScript-Babel-Starter
Warning - this is currently out-of-date. We'll update this tutorial shortly.
Set up a simple TypeScript + Babel project
git clone https://github.com/Microsoft/TypeScript-Babel-Starter.gitcd TypeScript-Babel-Starter
- Install dependencies
npm i mongoosenpm i --save-dev @types/node @types/mongoose @types/mongodb
- buckle up
rm src/index.tsmkdir -pv src/models/touch src/models/User.ts
- open
src/models/User.ts
file in your favorite text editor
Create a mongoose schema
// src/models/User.ts; ; ; ;
- Great. Now install
tongoose
& take a look at the--help
message:
npm i -g tongoosetongoose
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
Generate the type definition file!
- 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!
; // `npm i --save-dev @types/mongodb` // Notice the difference between `IUserModel` & `IUser`!
Such Success! 😱 Much Greatness! 😍 Now let's try to apply & use the generated type interface
- head back to
src/models/User.ts
- make the following changes:
// src/models/User.tsimport 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;
Awesome! Now let's test it!
- Create a
src/test.ts
file
touch src/test.ts
- open it & add the following code:
// src/test.ts; ;
- 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!
Our Roadmap
Contributing
Contributions are welcome! Please fork the repo, modify it & submit a pull request!
The Roadmap is a good place to start 😃
Developers
- Kipras Melnikovas - author - /sarpik 😎