Skip to content

Instantly share code, notes, and snippets.

@daviatorstorm
Created December 6, 2016 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daviatorstorm/9179642194ae04c38c932998fab1664d to your computer and use it in GitHub Desktop.
Save daviatorstorm/9179642194ae04c38c932998fab1664d to your computer and use it in GitHub Desktop.
Mongoose ts simple saving
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