@abhisekp/mongoose-to-json 中文文档教程
@abhisekp/mongoose-to-json
从@meanie/mongoose-to-json
Mongoose 规范化 JSON 输出的插件
Installation
您可以使用 yarn
或 npm
安装此包。
#yarn
yarn add @abhisekp/mongoose-to-json
#npm
npm install @abhisekp/mongoose-to-json --save
Usage
设置为所有 Mongoose 模式的全局插件:
const mongoose = require('mongoose');
const toJson = require('@abhisekp/mongoose-to-json');
mongoose.plugin(toJson);
重要:确保在定义模型之前加载您的插件,否则插件将不会在您的模型上注册!
或对于特定(子)模式:
const mongoose = require('mongoose');
const toJson = require('@abhisekp/mongoose-to-json');
const {Schema} = mongoose;
const MySchema = new Schema({});
MySchema.plugin(toJson);
此插件将为客户端应用程序规范化 JSON 输出:
{
"_id": "400e8324a71d4410b9dc3980b5f8cdea",
"__v": 2,
"name": "Item A"
}
至清洁器:
{
"id": "400e8324a71d4410b9dc3980b5f8cdea",
"name": "Item A"
}
您还可以从 JSON 中删除私有路径:
const mongoose = require('mongoose');
const toJson = require('@abhisekp/mongoose-to-json');
const {Schema} = mongoose;
const schema = new Schema({
email: {type: String},
password: {type: String, private: true},
});
schema.plugin(toJson);
const User = mongoose.model('users', schema);
const user = new User({email: 'test@test.com', password: 'test'});
console.log(user.toJSON());
这将输出:
{
"id": "400e8324a71d4410b9dc3980b5f8cdea",
"email": "test@test.com"
}
Issues & feature requests
请报告任何错误、问题、建议和功能请求在 @abhisekp/mongoose-to-json 问题跟踪器 中。
Contributing
欢迎请求请求! 如果您想为 Meanie 做出贡献,请查看 Meanie 贡献指南。
Sponsor
此套餐由 Hello Club 赞助,一站式俱乐部和会员管理解决方案,包括预订系统、自动会员续订、在线支付以及集成访问和灯光控制。 如果您碰巧属于任何类型的俱乐部,或者您是否认识帮助经营俱乐部的人,请查看我们!
License
(麻省理工学院许可证)
版权所有 2016-2020,Adam Reis
@abhisekp/mongoose-to-json
Forked from @meanie/mongoose-to-json
A plugin for Mongoose to normalize JSON output
Installation
You can install this package using yarn
or npm
.
#yarn
yarn add @abhisekp/mongoose-to-json
#npm
npm install @abhisekp/mongoose-to-json --save
Usage
Setup as a global plugin for all Mongoose schema's:
const mongoose = require('mongoose');
const toJson = require('@abhisekp/mongoose-to-json');
mongoose.plugin(toJson);
Important: Make sure to load your plugins prior to defining models, otherwise the plugin won't get registered on your models!
Or for a specific (sub) schema:
const mongoose = require('mongoose');
const toJson = require('@abhisekp/mongoose-to-json');
const {Schema} = mongoose;
const MySchema = new Schema({});
MySchema.plugin(toJson);
This plugin will normalize JSON output for client side applications from:
{
"_id": "400e8324a71d4410b9dc3980b5f8cdea",
"__v": 2,
"name": "Item A"
}
To a cleaner:
{
"id": "400e8324a71d4410b9dc3980b5f8cdea",
"name": "Item A"
}
You can also remove private paths from the JSON:
const mongoose = require('mongoose');
const toJson = require('@abhisekp/mongoose-to-json');
const {Schema} = mongoose;
const schema = new Schema({
email: {type: String},
password: {type: String, private: true},
});
schema.plugin(toJson);
const User = mongoose.model('users', schema);
const user = new User({email: 'test@test.com', password: 'test'});
console.log(user.toJSON());
This will output:
{
"id": "400e8324a71d4410b9dc3980b5f8cdea",
"email": "test@test.com"
}
Issues & feature requests
Please report any bugs, issues, suggestions and feature requests in the @abhisekp/mongoose-to-json issue tracker.
Contributing
Pull requests are welcome! If you would like to contribute to Meanie, please check out the Meanie contributing guidelines.
Sponsor
This package has been kindly sponsored by Hello Club, an all in one club and membership management solution complete with booking system, automated membership renewals, online payments and integrated access and light control. Check us out if you happen to belong to any kind of club or if you know someone who helps run a club!
License
(MIT License)
Copyright 2016-2020, Adam Reis