Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.