# mongoose-schema-ts-infer

> Infer document type from mongoose schema

Latest version **0.1.19** (published 2022-08-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongoose-schema-ts-infer
pnpm add mongoose-schema-ts-infer
yarn add mongoose-schema-ts-infer
bun add mongoose-schema-ts-infer
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.19 |
| Published | 2022-08-29 |
| First published | 2022-08-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 29.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Nicolas De Boose |
| Maintainers | nikoms |
| Keywords | typescript, mongoose, schema, type, inference |

## Links

- npm: https://www.npmjs.com/package/mongoose-schema-ts-infer
- Repository: https://github.com/salsafire/mongoose-schema-ts-infer
- Homepage: https://github.com/salsafire/mongoose-schema-ts-infer#readme
- Issues: https://github.com/salsafire/mongoose-schema-ts-infer/issues
- npm.io page: https://npm.io/package/mongoose-schema-ts-infer

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.1.19 (latest) — 2022-08-29
- 0.1.18 — 2022-08-20
- 0.1.17 — 2022-08-20
- 0.1.16 — 2022-08-20
- 0.1.15 — 2022-08-19
- 0.1.13 — 2022-08-19
- 0.1.12 — 2022-08-19
- 0.1.11 — 2022-08-19
- 0.1.10 — 2022-08-18
- 0.1.9 — 2022-08-18
- 0.1.8 — 2022-08-18
- 0.1.6 — 2022-08-18
- 0.1.5 — 2022-08-18
- 0.1.4 — 2022-08-17
- 0.1.3 — 2022-08-17
- … 3 more at https://npm.io/package/mongoose-schema-ts-infer/versions

## README

# What does it do and why?

`mongoose` has [typescript support](https://mongoosejs.com/docs/typescript.html) since `v5.11.0`.

[But until `mongoose@>=6.3.1`](https://mongoosejs.com/docs/typescript/schemas.html), everytime we create a schema, we must create an interface representing a document in MongoDB.

**This library has been made for these people 😁**: Create your schema and infer the document type from it!

# Install

```
npm i mongoose-schema-ts-infer
```

# Usage

1. Create a mongoose schema:

```
const userSchema = {
  name: {
    type: String, 
    required: true as const
  },
  email: String,
  favoriteColor: {
    type: String, 
    required: false as const, 
    enum: ['white', 'black'] as const
  },
}
```

> **Warning**
> `as const` is **mandatory** for `required` option, `enum` option and `type` (if it's a string like `"buffer"`, `"Buffer"`, etc...).

2. Infer a document type from it:

```
import {InferFromSchema} from "mongoose-schema-ts-infer";

type IUser = InferFromSchema<typeof userSchema>;

/**
 * `IUser` type is equivalent to:
 * { 
 *     _id ?: ObjectId
 *     name: string, 
 *     email?: string
 *     favoriteColor?: 'white'|'black
 * }
**/


const document: IUser = {
  name: 'Nicolas',
  favoriteColor: 'white'
}
```

# Supported types

We don't support (yet) all the types. They will come! Feel free to create MR to add if you are in the hurry 😁.

| Schema type                        | typescript type                                      |
|------------------------------------|------------------------------------------------------|
| `String`                           | `string`                                             |
| `Number`                           | `number`                                             |
| `Date`                             | `Date`                                               |
| `Buffer`                           | `Buffer`                                             |
| `"buffer"`                         | `Buffer`                                             |
| `"Buffer"`                         | `Buffer`                                             |
| `mongoose.Schema.Types.Buffer`     | `Buffer`                                             |
| `Boolean`                          | `boolean`                                            |
| `mongoose.Schema.Types.Mixed`      | `any`                                                |
| `Object`                           | `any`                                                |
| `mongoose.Schema.Typ@es.ObjectId`  | `mongoose.Types.ObjectId`                            |
| `Array`                            | `Array`                                              |
| `mongoose.Schema.Types.Decimal128` | `mongoose.Types.Decimal128`                          |
| `Map`                              | `Map`                                                |
| `Schema`                           | Inferred from the generic given during instantiation |
| Nested                             | ✅                                                    |

Both shorthand notation (`{name: String}`) and "classic" notation (`{name: {type: String}}`) are supported.
For the latter, some options are also taken into account:

| Option name                     | typescript infer                                                 | Example                                                                                |
|---------------------------------|------------------------------------------------------------------|----------------------------------------------------------------------------------------|
| `required`                      | Mark the field as optional or not (default:`false`)              | `{name: {type: String, required: false as const} }` gives `{name?:string}`             |
| `enum` (only for `String` type) | Restrict the value to one of the specified item in the list      | `{accept: {type: String, enum: ['yes','no'] as const} }` gives `{accept:'yes' / 'no'}` |
| `of` (only for `Map` type)      | Restrict values of the `Map` to a specific type (default: `any`) | `{name: {type: Map, of: Number} }` gives `{name?: Map<string,number>}`                 |

# Examples

All examples are available in the "[examples](./examples)" folder:

- [The basics](./examples/basic.ts)
- [enum option](./examples/enum.ts)
- [array](./examples/array.ts)
- [nested schema](./examples/nested.ts)
- [Map](./examples/map.ts)
- [Sub document/schema](./examples/sub-schema.ts)

# Test, transpile and publish on npm

Before publishing, we need to transpile the code into `lib` and make sure everything works:

```
npm test && npm run build
```

For publishing on npm (make sure to update the version in `package.json` first):

```
npm publish
```

---
_Source: https://npm.io/package/mongoose-schema-ts-infer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
