猫史档案馆


NomenPostgre-Base:神岛首个PG数据库动态多表格轻量级便携式管理器

用户:NomenNomen查看:4 回复:3 评论:4 创建时间:2023-05-09T11:47:57


Nomen小队Easy Coding项目正式启动!

这个项目的目的是为所有萌新建立一个完整的包装库,可以快速简单的执行各种复杂操作,节省时间不需要造轮子

而NomenPostgre-Base则是这个项目里第二个完成的库

这个NomenPostgre-Base实际上是在NomenSQLite的基础上继续开发和修改的版本,适用于Postgre也就是PG数据库,有着基础的增删改查功能,以及更多的约束和数据类型,但是这只是PG数据库的冰山一角,目前Base版本只支持简单的增删改查功能以及一部分的约束,我们将会在未来的版本开发更多的功能,实现复杂的技术,例如LIKE,LIMIT,WITH,GROUP以及更多支持的数据类型等等

同时这些功能也会第一时间更新到Box3-Tools上,期待大家的投稿

 

代码本体:

class NomenPostgre {
    constructor() {
        if (NomenPostgre.instance) return NomenPostgre.instance;
        NomenPostgre.instance = this;
    }
    static launched = false;
    static async launch(config, action) {
        if (!config) throw new Error('请传入配置参数后重试');
        if (!this.instance) this.instance = new NomenPostgre();
        else return this.instance;
        this.tables = config || {};
        this._data = {};
        if (!Object.keys(this.tables).length) this._data = null;
        Object.keys(this.tables).forEach(v => {
            if (Array.isArray(this.tables[v]) && this.tables[v].length) {
                this._data[v] = [];
                this.tables[v].forEach(r => {
                    if (typeof r.name == "string" && Object.values(this.DataType).includes(r.type)) {
                        this._data[v].push({ name: r.name, type: r.type, notnull: !!r.notnull, unique: !!r.unique, check: r.check || false });
                    }
                })
            }
        })
        console.log(JSON.stringify(this._data));
        if (!Object.keys(this._data).length) return this.instance;
        let errMessage = null;
        if (typeof action == "string" && action.includes('create')) {
            for (let i = 0; i < Object.keys(this._data).length; i++) {
                let eString = `CREATE TABLE IF NOT EXISTS "${Object.keys(this._data)[i]}" 
                (${this._data[Object.keys(this._data)[i]].reduce((p, c) => {
                    let str = `"${c.name}" ${c.type} ${c.notnull ? 'NOT NULL' : ''} ${c.unique ? 'UNIQUE' : ''} ${c.check ? 'CHECK(' + c.check + ')' : ''}`;
                    return p ? p.concat(',', str) : str;
                }, false)})`;
                await (async function (t) {
                    while (true) {
                        try { await t.instance._exec(eString); break } catch (err) {
                            if (!err.message.includes('timeout')) {
                                throw err;
                            }
                        }
                    }
                    return;
                })(this)
            }
        }
        this.launched = true;
        return this.instance;
    }
    _exec(cmd) {
        return (typeof cmd == 'string') ? db.sql([cmd]) : new Promise(resolve => resolve(new TypeError('传入参数有误,预期应该为string')))
    }
    getAllTables() {
        return Object.keys(NomenPostgre._data);
    }
    operate(name) {
        let t = this;
        if (typeof name == "string" && NomenPostgre.launched) return {
            select(column, value) {
                return t._exec(`SELECT * FROM "${name}" ${column == "*" ? `` : (`WHERE "${column}" = ` + `'${value}'`)}`)
            },
            remove(column, value) {
                return t._exec(`DELETE FROM "${name}" ${column == "*" ? `` : (`WHERE "${column}" = ` + `'${value}'`)}`)
            },
            insert(value) {
                if (Object.prototype.toString.call(value) == '[object Object]' && Object.keys(value).length) {
                    let eString = `(${Object.keys(value).map(v => `"${v}"`).join(',')}) VALUES(${Object.values(value).map(v => (typeof v == "number") ? v : `'${v}'`).join(',')})`;
                    return t._exec(`INSERT INTO "${name}" ${eString}`);
                } else return null;
            },
            update(column, value) {
                if (Object.prototype.toString.call(value) == '[object Object]' && Object.keys(value).length && typeof column == "string" && column.includes('=')) {
                    let eArr = [];
                    Object.entries(value).forEach(([k, v]) => eArr.push(`"${k}" = '${v}'`));
                    let aString = `${column == "*" ? "" : 'WHERE "' + column.split('=')[0] + '" = \'' + column.split('=')[1] + '\''}`;
                    return t._exec(`UPDATE "${name}" SET ${eArr} ${aString}`);
                } else return null;
            }
        }
        return {};
    }
    static instance = null;
    static LaunchAction = {
        CREATE_OR_LAUNCH: 'createlaunch',
        LAUNCH: 'launch'
    }
    static DataType = {
        NUMBER: 'INTEGER',
        BIGINT: 'BIGINT',
        MONEY: 'MONEY',
        FLOAT: 'REAL',
        TEXT: 'TEXT',
        NULL: 'NULL',
        BOOLEAN: 'BOOLEAN',
        POINT: 'POINT',
        LINE: 'LINE',
        LSEG: 'LSEG',
        BOX: 'BOX',
        PATH: 'PATH',
        POLYGON: 'POLYGON',
        CIRCLE: 'CIRCLE',
        CIDR: 'CIDR',
        INET: 'INET',
        JSON: 'JSON'
    }
}
module.exports = {
    NomenPostgre
}

代码示例:

const Tables = {
    'Nomenawa': [
        { name: 'coin', type: NomenPostgre.DataType.NUMBER, notnull: true },
        { name: 'bag', type: NomenPostgre.DataType.TEXT, notnull: false }
    ],
    'Nomenawa2': [
        { name: 'coin', type: NomenPostgre.DataType.NUMBER, notnull: true, check: 'coin > 10' },
        // check属性,如果coin大于10则可以插入,否则抛出错误
        // notnull属性,如果为true,则该列无法插入null值,这意味着insert方法中为插入该列的值则会抛出错误
        { name: 'bignumber', type: NomenPostgre.DataType.BIGINT },
        // BIGINT类型,大型整数
        { name: 'uniquecolumn', type: NomenPostgre.DataType.TEXT, unique: true }
        // unique值,唯一,如果插入已经存在的值会抛出错误
    ],
};


(async () => {
    let Nomen = await NomenPostgre.launch(Tables, NomenPostgre.LaunchAction.CREATE_OR_LAUNCH);

    let NomenTable = Nomen.operate('Nomenawa2'); // 初始化该表格的管理器

    await NomenTable.insert({ coin: 11, bignumber: '1145141919810', uniquecolumn:'NOMENYYDS' }); 
    // 插入一个数据,请注意,如果缺少具有unique或not null约束列的值的话会抛出报错

    await NomenTable.update('coin=11',{bignumber:'1234567890',uniquecolumn:'NomenYYDS!'}) // 更新coin=11的列上的两个属性

    console.log(JSON.stringify(await NomenTable.select('coin','11'))) // 查询coin为11的值

    await NomenTable.remove('coin',11);  // 删除coin为11的所有列

    console.log(JSON.stringify(await NomenTable.select('*'))) // 查看所有的数据

    console.log(Nomen.getAllTables()) // 查看所有的受管理的表格
})()

 

======Nomen速讯======

Nomen小队招人啦!小队内大佬云集,为萌新答疑解惑

Box3-Tools同步了官方发布的各类教程,内涵大量的实例代码可供参考

Nomen小队Easy Coding项目正式启动,这个项目的目标是更好,更快,更方便

欢迎你的加入与参与!


回复

上一页1 页 / 共 1下一页
NomenNomen

惯例沙发

点赞0


评论


神岛吉吉喵神岛吉吉喵

那么Box3-Tools在哪里可以看到呢~

点赞0


评论


烟魂烟魂

再来看看:)

链接:https://shequ.codemao.cn/community/539851

点赞0


评论