# koa 增删改查

# 1.数据库

let mongoose = require("mongoose");
const Schema = mongoose.Schema;
let { MONGO_URL, _AUTHSOURCE, _USER, _PASS, LOCAL } = process.env;
let config = { useNewUrlParser: true, useUnifiedTopology: true };
if (!LOCAL) {
    config.authSource = _AUTHSOURCE; // 权限认证(添加这个属性!!!!!)
    config.user = _USER;
    config.pass = _PASS;
}
let db = mongoose.createConnection(MONGO_URL, config);
//规定数据库中集合的字段和类型
let UserSchema = new Schema(
    {
        name: { type: String },
        label: { type: String },
        type: { type: String },
        image: { type: String },
        link: { type: String }, // user admin
    },
    {
        timestamps: true,
        toJSON: {
            transform(doc, ret) {
                ret.id = ret._id;
                delete ret._id;
                delete ret.__v;
                return ret;
            },
        },
    }
);
const UserModel = db.model("User", UserSchema);

const sql_add = (params) => UserModel.create(params)
const sql_delete = ({ id, name }) => UserModel.findByIdAndDelete(id, { name });
const sql_update = ({ id, ...update }) => UserModel.findByIdAndUpdate(id, update);
const sql_query_total = (query) => UserModel.countDocuments(query);
const sql_query = ({ query, sorter, current, pageSize }) => UserModel.find(query)
    .sort(sorter)
    .skip((current - 1) * pageSize)
    .limit(pageSize);
module.exports = { sql_add, sql_delete, sql_update, sql_query_total, sql_query };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

# 2.后端接口

这里以 node 为例,使用 node 启动数据库

# 3.启动

接口地址:

# 4.接口查看