GithubHelp home page GithubHelp logo

kiprasmel / tongoose Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 165 KB

[Discontinued] :blue_book: Auto-generate TypeScript interfaces from Mongoose schemas!

License: MIT License

JavaScript 100.00%
mongoose typescript type definition interface generator types interfaces mongodb tongoose

tongoose's Introduction

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 โ˜๏ธ


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

tongoose's People

Contributors

dependabot[bot] avatar kiprasmel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tongoose's Issues

Create live demo

Provide a live demo website that would showcase what tongoose can do.

  • A simple (& editable?) mongoose schema on the left
  • The (soon to be) generated typescript output on the right
  • A button to convert
  • A button to reset

We can host it on github pages or I could host it on my own server & domain @ kipras.org

Issue with Schema.Types.ObjectId

Hello Sparpik,

I tried tongoose, it works pretty well but on schemas with relationships, mongoose does not work...

export const UserSchema = new Schema({ address: { ref: "Address", type: Schema.Types.ObjectId }, ...

Output:
undefined:4 type: "Schema".Types."ObjectId"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.