All files / tcb-admin-node/lib/database query.js

48.42% Statements 46/95
47.5% Branches 19/40
45% Functions 9/20
48.42% Lines 46/95
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203  8x 8x 8x 8x 8x 8x 8x 8x     10x 10x 10x 10x 10x 10x     5x 5x 5x       5x     5x 5x   5x     5x     5x         5x   5x     5x 5x 5x 5x                                                       7x                                                                                                                                         2x         2x 2x 2x               7x 7x 2x     5x 5x 2x   3x 1x             2x                             2x       7x     8x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const request_1 = require("./request");
const validate_1 = require("./validate");
const util_1 = require("./util");
const command_1 = require("./command");
const regexp_1 = require("./regexp");
const isRegExp = require("is-regex");
const actionName = require("../constants/actionName");
class Query {
    constructor(db, coll, fieldFilters, fieldOrders, queryOptions) {
        this._db = db;
        this._coll = coll;
        this._fieldFilters = fieldFilters;
        this._fieldOrders = fieldOrders || [];
        this._queryOptions = queryOptions || {};
        this._request = new request_1.Request(this._db);
    }
    get() {
        let newOder = [];
        Eif (this._fieldOrders) {
            this._fieldOrders.forEach(order => {
                newOder.push(order);
            });
        }
        let param = {
            CollectionName: this._coll
        };
        Eif (this._fieldFilters) {
            param.Query = JSON.stringify(this._fieldFilters);
        }
        Iif (newOder.length > 0) {
            param.Order = newOder;
        }
        Iif (this._queryOptions.offset) {
            param.Offset = this._queryOptions.offset;
        }
        Iif (this._queryOptions.limit) {
            param.Limit =
                this._queryOptions.limit < 100 ? this._queryOptions.limit : 100;
        }
        else {
            param.Limit = 100;
        }
        Iif (this._queryOptions.projection) {
            param.Projection = this._queryOptions.projection;
        }
        return new Promise(resolve => {
            this._request.send(actionName.DescribeDocument, param).then((res) => {
                const documents = util_1.Util.formatResDocumentData(res.DocumentList);
                resolve({
                    data: documents,
                    requestId: res.RequestId,
                    total: res.TotalCount,
                    limit: res.Limit,
                    offset: res.Offset
                });
            });
        });
    }
    count() {
        let param = {
            CollectionName: this._coll
        };
        if (this._fieldFilters) {
            param.Query = JSON.stringify(this._fieldFilters);
        }
        return new Promise(resolve => {
            this._request.send(actionName.DescribeDocument, param).then((res) => {
                resolve({
                    requestId: res.RequestId,
                    total: res.TotalCount
                });
                console.log(res);
            });
        });
    }
    where(query) {
        return new Query(this._db, this._coll, this.convertParams(query), this._fieldOrders, this._queryOptions);
    }
    orderBy(fieldPath, directionStr) {
        validate_1.Validate.isFieldPath(fieldPath);
        validate_1.Validate.isFieldOrder(directionStr);
        const newOrder = {
            field: fieldPath,
            direction: directionStr
        };
        const combinedOrders = this._fieldOrders.concat(newOrder);
        return new Query(this._db, this._coll, this._fieldFilters, combinedOrders, this._queryOptions);
    }
    limit(limit) {
        validate_1.Validate.isInteger("limit", limit);
        let option = Object.assign({}, this._queryOptions);
        option.limit = limit;
        return new Query(this._db, this._coll, this._fieldFilters, this._fieldOrders, option);
    }
    skip(offset) {
        validate_1.Validate.isInteger("offset", offset);
        let option = Object.assign({}, this._queryOptions);
        option.offset = offset;
        return new Query(this._db, this._coll, this._fieldFilters, this._fieldOrders, option);
    }
    update(data) {
        if (!data || typeof data !== "object") {
            return Promise.resolve({
                code: "INVALID_PARAM",
                message: "参数必需是非空对象"
            });
        }
        if (data.hasOwnProperty("_id")) {
            return Promise.resolve({
                code: "INVALID_PARAM",
                message: "不能更新_id的值"
            });
        }
        let param = {
            CollectionName: this._coll,
            Query: JSON.stringify(this._fieldFilters),
            Multi: true,
            Merge: true,
            Upsert: false,
            Data: JSON.stringify(util_1.Util.encodeDocumentDataForReq(data, true))
        };
        return new Promise(resolve => {
            this._request.send(actionName.ModifyDocument, param).then((res) => {
                resolve({
                    requestId: res.RequestId,
                    updated: res.Updated,
                    upsertId: res.UpsertedId
                });
            });
        });
    }
    field(projection) {
        for (let k in projection) {
            if (projection[k]) {
                projection[k] = 1;
            }
            else {
                projection[k] = 0;
            }
        }
        let option = Object.assign({}, this._queryOptions);
        option.projection = projection;
        return new Query(this._db, this._coll, this._fieldFilters, this._fieldOrders, option);
    }
    remove() {
        const param = {
            CollectionName: this._coll,
            Query: JSON.stringify(this._fieldFilters),
            Multi: true
        };
        return new Promise(resolve => {
            this._request.send(actionName.DeleteDocument, param).then((res) => {
                resolve({
                    requestId: res.RequestId,
                    deleted: res.Deleted
                });
            });
        });
    }
    convertParams(query) {
        let queryParam = {};
        if (query instanceof command_1.Command) {
            queryParam = query.parse();
        }
        else {
            for (let key in query) {
                if (query[key] instanceof command_1.Command || query[key] instanceof regexp_1.RegExp) {
                    queryParam = Object.assign({}, queryParam, query[key].parse(key));
                }
                else if (isRegExp(query[key])) {
                    queryParam = {
                        [key]: {
                            $regex: query[key].source,
                            $options: query[key].flags
                        }
                    };
                }
                else Iif (typeof query[key] === "object") {
                    let command = new command_1.Command();
                    let tmp = {};
                    command.concatKeys({ [key]: query[key] }, "", tmp);
                    let keys = Object.keys(tmp)[0];
                    let value = tmp[keys];
                    if (value instanceof command_1.Command) {
                        value = value.parse(keys);
                    }
                    else {
                        value = { [keys]: value };
                    }
                    queryParam = Object.assign({}, queryParam, value);
                }
                else {
                    queryParam = Object.assign({}, queryParam, { [key]: query[key] });
                }
            }
        }
        return queryParam;
    }
}
exports.Query = Query;