tongoose

0.6.1 • Public • Published

Tongoose

📘 Auto-generate TypeScript interfaces from Mongoose schemas!

MIT

Usage

  • 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!


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.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

Create a mongoose schema

// 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

Generate the type definition file!

  • Run tongoose with the path to our src/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;
}

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.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;

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
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 with mongoose.Document's fields. Everything (hopefully) worked great!

  • Congratulations!


Back to Table of Contents ☝️


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 😎

License

MIT

Package Sidebar

Install

npm i tongoose

Weekly Downloads

3

Version

0.6.1

License

MIT

Unpacked Size

38.9 kB

Total Files

20

Last publish

Collaborators

  • kipras