Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
rapidquery
Advanced tools
Readme
一种使用JSON来查询API的接口协议,前端只需要GET/POST发送JSON到一个地址(比如下面这个API地址),就可以与MongoDB数据库进行CRUD。
比如Post/Get 下面这个JSON, 参数名为 "query" :
{
"read users":{
id: 1
}
}
至
http://localhost:8080/rapidql
就可以查询到数据
{
users: [{
id: 1,
name: "tt",
age : 29,
gender: "male"
}]
}
npm install --save rapidquery
或使用淘宝镜像
cnpm install --save rapidquery
const RapidQuery = require("rapidquery");
RapidQuery.connect({
host: "mongodb://localhost:27017/rapid"
});
或使用用户名和密码
RapidQuery.connect({
host: "mongodb://admin:12345678@localhost:27017/rapid?authSource=admin"
});
在Express或Koa下使用
router.post('/rapidquery', async function(ctx, next) {
try {
if (ctx.request.body.query) {
var data = await RapidQuery.query(ctx.request.body.query);
ctx.type = "application/json"
ctx.body = data;
} else {
throw new Error("Query JSON is required.")
}
} catch (err) {
ctx.status = 400;
ctx.body = `${err.name} : ${err.message}`;
console.error(err)
}
})
或者可以直接使用
var data = await RapidQuery.query(ctx.request.body.query)
定义一个超简单的 Model
var users = RapidQuery.define({
name: "users",
description: "用户数据",
fields: {
name: String,
email: String,
gender: String,
age: Number
}
})
系统会自动添加以下字段
_id
meta:{
createdAt
updatedAt
}
来定义一个较完整功能的
var users = RapidQuery.define({
name: "users",
description: "用户数据",
fields: {
id: {
type: RapidQuery.ObjectId,
default: RapidQuery.NewObjectId //RapidQuery.NewObjectId可以生成一段新的id
},
firstname: String,
lastname: String,
email: {
type: String,
unique: true,
lowercase: true,
trim: true,
required: [true, "Email为必填项"], //required说明该field不能为空
//自定义验证
validate: {
validator: value => {
return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(
value
);
},
message: "{VALUE} 不是一个有效的Email地址!"
}
},
userType:{
type: String,
default: "学生",
enum:["学生","老师"]
},
age: {
type: Number,
//数值验证, 最小值为15, 最大值为30
min: 15,
max: [30, "年龄不能超过30岁"]
},
job:{
type: String,
required: function() {
return this.age > 23; //required可变, 比如当age大于23岁时, job才是必填项
}
},
school: {
name: String
}
},
options: {
timestamp: true, //可以不填,默认为true, model会自动添加 meta: {createdAt, updatedAt}
paranoid: true, //可以不填,默认为true, 当使用delete时, 使用逻辑删除(并不真正删除)
discriminatorKey: "kind"
}
});
所有可用的数据类型
String
Number
Date
Buffer
Boolean
Array
Mixed // 一个啥都可以放的 SchemaType, 虽然便利,但也会让数据难以维护。在声明中使用 RapidQuery.Mixed
ObjectId // 要指定类型为 ObjectId,在声明中使用 RapidQuery.ObjectId
Decimal128 // 在声明中使用 RapidQuery.Decimal128
所有可用的选项
所有类型相关
required: 布尔值或函数 如果值为真,为此属性添加 required 验证器
default: 任何值或函数 设置此路径默认值。如果是函数,函数返回值为默认值
validate: 函数,自定义验证
get: 函数 使用 Object.defineProperty() 定义自定义 getter
set: 函数 使用 Object.defineProperty() 定义自定义 setter
alias: 字符串 仅mongoose >= 4.10.0。 为该字段路径定义虚拟值 gets/sets
索引相关
index: 布尔值 是否对这个属性创建索引
unique: 布尔值 是否对这个属性创建唯一索引
sparse: 布尔值 是否对这个属性创建稀疏索引
String相关
lowercase: 布尔值 是否在保存前对此值调用 .toLowerCase()
uppercase: 布尔值 是否在保存前对此值调用 .toUpperCase()
trim: 布尔值 是否在保存前对此值调用 .trim()
match: 正则表达式 创建验证器检查这个值是否匹配给定正则表达式
enum: 数组 创建验证器检查这个值是否包含于给定数组
Number相关
min: 数值 创建验证器检查属性是否大于或等于该值
max: 数值 创建验证器检查属性是否小于或等于该值
Date相关
min: Date 创建验证器检查属性是否大于或等于该Date
max: Date 创建验证器检查属性是否小于或等于该Date
因 ORM 部分采用的是 Mongoose 的代码,数据类型的详细说明, 请访问: http://www.mongoosejs.net/docs/schematypes.html
validate: {
validator: (v) => {
return /\d{3}-\d{3}-\d{4}/.test(v);
},
message: '{VALUE} is not a valid phone number!'
},
自定义检验器可以是异步的
validate: (v) => {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(false);
}, 5);
});
}
因 ORM 部分采用的是 Mongoose 的代码,关于验证的详细说明, 请访问: http://www.mongoosejs.net/docs/validation.html
timestamp: true // 默认为true, model会自动添加 meta: {createdAt, updatedAt}
paranoid: true // 默认为true, 当使用delete时, 使用逻辑删除(并不真正删除),删除时添加deletedAt
使用GET方法:
http://localhost:8080/rapidquery?query={"create user":{"firstname":"tt"}}
推荐使用POST方法(参数为query)
{
"create users": {
firstname: "tt",
lastname: "zhang",
age: 29,
school: {
name: "UCLA"
}
}
}
{
"create users": [
{
firstname: "tt",
lastname: "zhang",
age: 29,
school: {
name: "UCLA"
}
},
{
firstname: "jinchuang",
lastname: "huang",
age: 21,
school: {
name: "MIT"
}
}
]
}
查询名为"tt"的user
{
"read users": {
firstname: "tt"
}
}
结果:
{
users:[
{
_id: 5e4b97490cc84609513cf8fa,
firstname: 'tt',
lastname: 'zhang',
age: 29,
school: { name: 'UCLA' },
__v: 0
}
]
}
查询所有Users
{
"read users": {
}
}
{
"read users": {
age: {
$lt: 25
}
}
}
还有其他比较运算符可以使用.
```key
$gt: greater than 大于
$lt: less than 小于
$gte: greater than equal 大于等于
$lte: less than equal 小于等于
$ne: not equal 不等于
{
"read users": {
$or:[
{age: 21},
{age: 23}
]
}
}
{
"read users": {
firstname: /t/
}
}
按年龄进行倒序
{
"read users": {
$order:{
age: -1
}
}
}
pageSize来控制每页返回数据的行数,pageNum来控制第几页
注意:pageNum从1开始,而不是0
{
"read users": {
firstname: /t/,
$pageSize: 1,
$pageNum: 1
}
}
$select可以选择只要查询的字段
{
"read users": {
$select:"firstname age school.name"
}
}
或者使用 "-" 排除字段
{
"read users": {
$select:"-firstname -age"
}
}
$in:
{
"read users": {
"school.name": {
$in: ["MIT"]
}
}
}
$nin:
{
"read users": {
"school.name": {
$nin: ["UCLA"]
}
}
}
使用 $count 计算学校名为MIT用户数量
{
"count users": {
"school.name": {
$in: ["MIT"]
}
}
}
结果:
{
count_users: 1
}
查询名为"tt"的user和"alice"的student
注意:查询为并行,所以不能有先后顺序。
{
"read users": {
firstname: "tt"
},
"read students": {
name: "alice"
}
}
结果:
{
users:[
{...}
],
students:[
{...}
]
}
更新名字为 "tt" 的用户的年龄为 35
{
"update users": {
age: 35,
$update:{
firstname: "tt"
}
}
}
结果:
{
update_users: { n: 1, nModified: 1, ok: 1 }
}
更改名字为 "tt" 的用户
{
"update users": {
firstname: "tt",
$update:{
$push: {
cars: ["porsche", "ford f-150"]
}
}
}
}
更改名字为 "tt" 的用户,age+1
{
"update users": {
firstname: "tt",
$update:{
$inc:{
age:1
}
}
}
}
删除年龄为35的一个用户.
{
"delete users": {
age: 35
}
}
***为Collection名
{
create_***:{},
***:[],
update_***:{},
delete_***:{},
count_***:{},
aggregate_***:{},
}
db中包含了所有定义的Models, Schemas, Connections等,如果需要进行扩展开发,可以使用await RapidQuery.connect来获取。只能通过异步来获取。
var db = await RapidQuery.connect({
host: "mongodb://localhost:27017/rapid"
});
FAQs
an API protocol with json style to CRUD mongdo database. use only one api url.
The npm package rapidquery receives a total of 0 weekly downloads. As such, rapidquery popularity was classified as not popular.
We found that rapidquery demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.