0.1.19 • Published 2 years ago

mongoose-schema-ts-infer v0.1.19

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

What does it do and why?

mongoose has typescript support since v5.11.0.

But until mongoose@>=6.3.1, 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...).

  1. 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 typetypescript type
Stringstring
Numbernumber
DateDate
BufferBuffer
"buffer"Buffer
"Buffer"Buffer
mongoose.Schema.Types.BufferBuffer
Booleanboolean
mongoose.Schema.Types.Mixedany
Objectany
mongoose.Schema.Typ@es.ObjectIdmongoose.Types.ObjectId
ArrayArray
mongoose.Schema.Types.Decimal128mongoose.Types.Decimal128
MapMap
SchemaInferred 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 nametypescript inferExample
requiredMark 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" folder:

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
0.1.19

2 years ago

0.1.18

2 years ago

0.1.17

2 years ago

0.1.16

2 years ago

0.1.15

2 years ago

0.1.13

2 years ago

0.1.12

2 years ago

0.1.11

2 years ago

0.1.10

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago