Created
December 6, 2016 13:30
-
-
Save daviatorstorm/9179642194ae04c38c932998fab1664d to your computer and use it in GitHub Desktop.
Mongoose ts simple saving
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Schema, connect, Document, model } from "mongoose"; | |
let connectionString = "mongodb://your/connection/string" | |
connect(connectionString); | |
interface ITodo { | |
text?: string; | |
} | |
interface ITodoDoc extends Document, ITodo { } | |
let todoSchema = new Schema({ | |
text: { | |
type: String | |
} | |
}); | |
let Todo = model<ITodoDoc>("Todo", todoSchema); | |
// If "new<TI>(doc?: TI): T;" exists in @types/mongoose/index.d.ts | |
new Todo<ITodo>({ | |
text: "Do something crazy" | |
}).save().then(data => { | |
console.log(`saved data ${JSON.stringify(data, undefined, 2)}`); | |
}, e => { | |
for (let key in e.errors) { | |
console.log(e.errors[key].message); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment