其实明道云就是一个对开发者友好的数据库,希望官方可以推出数据操作层的库让开发者更好地接入使用

分享 明道云数据库开发者友好  收藏
1 / 671

每个明道云应用就是一个数据库 DB,里面有许多的表 TABLE,表里面的字段都是可以在网页端自定义可视化编辑的,其实这个就可以用于许多企业内部涉及到简单 CRUD 操作的小应用的开发。

比如用 Node JavaScript 的开发为例,我用 axios 做 request 的处理,简单就做了一个 Class:

import axios from "axios";
import { MingdaoFilterRowResult } from "../../types/Mingdao";

export interface MingdaoRowBasic {
    rowid: string
}

export interface MingdaoWorkSheetCredential {
    appKey: string;
    sign: string
}

export enum MingdaoDataTypeEnum {
    TEXT = 2,
    MOBILE, // 手机
    TEL, // 座机
    EMAIL,
    NUMBER,
    IDENTITY_CARD, // 证件
    AMOUNT, //金额
    SINGLE_SELECT_FLAT, // 单选 平铺
    MULTI_SELECT,
    SINGLE_SELECT_DROPDOWN, // 单选 下拉
    FILE,
    DATE_DAY, // 日期: 年-月-日
    DATE_TIME, // 日期: 年-月-日 时:分
    // 其余还有很多
}

export enum MingdaoSpliceTypeEnum {
    AND = 1,
    OR
}

export enum MingdaoFilterTypeEnum {
    DEFAULT = 0,
    LIKE,
    EQ,
    START,
    END,
    NCONTAIN,
    NE,
    ISNULL,
    HASVALUE,
    BETWEEN,
    NBETWEEN,
    GT,
    GTE,
    LT,
    LTE,
    // 其余还有很多
}

export interface MingdaoDbFilter {
    controlId?: string;
    dataType?: MingdaoDataTypeEnum;
    spliceType?: MingdaoSpliceTypeEnum;
    filterType?: MingdaoFilterTypeEnum;
    values?: string[];
    value?: string;
    dateRange?: number;
    isGroup?: boolean;
    groupFilters?: MingdaoDbFilter[];
}

const mingdaoQueryApi = axios.create({
    baseURL: 'https://api.mingdao.com',
    headers: { 'Content-Type': 'application/json' },
});

type updateValue = {
    value: string;
    editType: string;
} | string

export default class MingdaoDB<DataType> {

    private worksheetId: string;
    private credential: MingdaoWorkSheetCredential;

    constructor(worksheetId: string, credential: MingdaoWorkSheetCredential) {
        this.worksheetId = worksheetId
        this.credential = credential
    }

    async search(keywords: string, config: { pageSize: number, pageIndex: number, isAsc: boolean }): Promise<Array<DataType & MingdaoRowBasic> | null> {
        var queryRowsBody = {
            "appKey": this.credential.appKey,
            "sign": this.credential.sign,
            "worksheetId": this.worksheetId,
            "pageSize": config.pageSize,
            "pageIndex": config.pageIndex,
            "isAsc": config.isAsc,
            "keyWords": keywords
        }

        const { data: axiosData, status } = await mingdaoQueryApi.post<MingdaoFilterRowResult>("/v2/open/worksheet/getFilterRows", queryRowsBody)
        if (status !== 200) {
            return null
        }

        const { data, success } = axiosData

        if (!success) {
            return null
        }

        const { rows } = data

        if (rows.length === 0) {
            return null
        }

        return rows as any as Array<DataType & { rowid: string }>
    }

    async filterRow(filters: MingdaoDbFilter[], config: { pageSize: number, pageIndex: number, isAsc: boolean }): Promise<Array<DataType & MingdaoRowBasic> | null> {
        var queryRowsBody = {
            "appKey": this.credential.appKey,
            "sign": this.credential.sign,
            "worksheetId": this.worksheetId,
            "pageSize": config.pageSize,
            "pageIndex": config.pageIndex,
            "isAsc": config.isAsc,
            "filters": filters
        }

        const { data: axiosData, status } = await mingdaoQueryApi.post<MingdaoFilterRowResult>("/v2/open/worksheet/getFilterRows", queryRowsBody)
        if (status !== 200) {
            return null
        }

        const { data, success } = axiosData

        if (!success) {
            return null
        }

        const { rows } = data

        if (rows.length === 0) {
            return null
        }

        return rows as any as Array<DataType & { rowid: string }>
    }

    async findByKey(value: string, fieldName: keyof (DataType & MingdaoRowBasic) = 'rowid'): Promise<DataType & MingdaoRowBasic | null> {
        const queryRowsBody = {
            "appKey": this.credential.appKey,
            "sign": this.credential.sign,
            "worksheetId": this.worksheetId,
            "pageSize": 1,
            "pageIndex": 1,
            "isAsc": "true",
            "filters": [
                {
                    "controlId": fieldName,
                    "dataType": 2,
                    "spliceType": 1,
                    "filterType": 2,
                    "value": value
                }
            ]
        }

        const { data: axiosData, status } = await mingdaoQueryApi.post<MingdaoFilterRowResult>("/v2/open/worksheet/getFilterRows", queryRowsBody)
        if (status !== 200) {
            return null
        }

        const { data, success } = axiosData

        if (!success) {
            return null
        }

        const { rows } = data

        if (rows.length === 0) {
            return null
        }

        return rows[0] as any as (DataType & MingdaoRowBasic)
    }

    async create(data: DataType): Promise<DataType & MingdaoRowBasic | null> {
        const addRowBody = {
            "appKey": this.credential.appKey,
            "sign": this.credential.sign,
            "worksheetId": this.worksheetId,
            "controls": Object.keys(data as { [x: string]: string }).map(function (key) {
                const controlValue = (data as { [x: string]: string })[key]

                if (Array.isArray(controlValue)) {
                    return {
                        "controlId": key,
                        "value": JSON.stringify(controlValue),
                    }
                }
                return {
                    "controlId": key,
                    "value": (data as { [x: string]: string })[key],
                }

            }),
            "triggerWorkflow": true
        }

        console.log('MingdaoDb:create:addRowBody', addRowBody)

        const { data: axiosData, status } = await mingdaoQueryApi.post("/v2/open/worksheet/addRow", addRowBody)

        console.log('MingdaoDb:create:axiosDataResponse', axiosData, status)

        if (status !== 200) {
            return axiosData
        }

        const { data: rowId, success, error_code } = axiosData

        const reFetchData = await this.findByKey(rowId, 'rowid')

        if (reFetchData) {
            return reFetchData
        }

        return null
    }

    async update(rowId: string, rawUpdateData: { [K in keyof Partial<DataType>]: updateValue }): Promise<DataType> {
        const updateData: { [K in keyof Partial<DataType>]: updateValue } = rawUpdateData

        Object.keys(rawUpdateData).forEach(key => {
            if (rawUpdateData[key as keyof Partial<DataType>] === undefined) {
                delete rawUpdateData[key as keyof Partial<DataType>];
            }
        });

        const editRowBody = {
            "appKey": this.credential.appKey,
            "sign": this.credential.sign,
            "worksheetId": this.worksheetId,
            "rowId": rowId,
            "controls": Object.keys(updateData).map(function (key) {
                const updateValue: updateValue = updateData[key as keyof Partial<DataType>]
                if (typeof updateValue === 'string') {
                    return {
                        "controlId": key,
                        "value": updateValue,
                    }
                }

                if ('editType' in updateValue) {
                    return {
                        "controlId": key,
                        "value": updateValue.value,
                        "editType": updateValue.editType
                    }
                }

            }),
            "triggerWorkflow": true
        }
        const { data: axiosData, status } = await mingdaoQueryApi.post("/v2/open/worksheet/editRow", editRowBody)

        if (status !== 200) {
            return axiosData
        }

        const reFetchData = await this.findByKey(rowId)

        return reFetchData!
    }
}

但是其实我也仔细阅读了 API 开发文档,并不是所有的 API 操作以及相关的细节都是有详细的文档,如果官方可以提供类似的库(海外的 AirTable 就有类似的库),可以让更多的企业内部应用甚至是其他的对数据库要求并不高的应用场景可以利用明道云做数据库的支持。