import { IFetchers, Query, Where } from '@tcwd/jsonschema-graphql'; import FlakeId from 'flakeid'; const flake = new FlakeId(); function getQuery(t, query: Query, needCount = false) { const { where, skip, limit, sort = '' } = query; const [orderField, orderType = 'asc'] = sort.split(':'); let model = t; if (where) { model = model.where(where); } let q = model.clone(), c; if (skip) { q = q.offset(skip); } if (limit) { q = q.limit(limit); } if (orderField) { q = q.orderBy(orderField, orderType); } q = q.select(); if (needCount) { c = model.clone().count(); } let res = needCount ? Promise.all([ q, c.then((r) => { return r[0]['count(*)']; }), ]) : Promise.all([q]); return res; } export function getTableName(datasource) { const { name } = datasource; return name; } // 关系型数据库中 json 字段在 kenx 中需要使用 json.stringify 转化成字符串来提交 function formatData(data = {}, { properties = {} }, dataType: 'input' | 'output') { return Object.keys(data).reduce((x, key) => { if (properties[key]) { const { type } = properties[key]; if (type === 'array' || type === 'object') { if (dataType === 'input') { x[key] = JSON.stringify(data[key]); } else { x[key] = JSON.parse(data[key]); } } else { x[key] = data[key]; } } return x; }, {} as any); } export enum DB_TYPE { MYSQL = 'mysql', SQLITE3 = 'sqlite3', } export function getKnexFetchers(knex, dbType: DB_TYPE = DB_TYPE.MYSQL) { function getInstanceByDatasource(datatsource) { const name = getTableName(datatsource); return knex(name); } const fetchers: IFetchers = { async create(data, datasource) { const t = getInstanceByDatasource(datasource); const _id = flake.gen(); const now = Date.now(); await t.insert({ ...formatData(data, datasource.schema, 'input'), _id, createdAt: now, updatedAt: now }); return { _id, }; }, async getItem(where, datasource) { const t = getInstanceByDatasource(datasource); const res = await t.where(where).limit(1).select('*'); return dbType === DB_TYPE.SQLITE3 ? formatData(res[0], datasource.schema, 'output') : res[0]; }, async update(args, datasource) { const t = getInstanceByDatasource(datasource); const now = Date.now(); const { where, data } = args; const res = await t.where(where).update({ ...formatData(data, datasource.schema, 'input'), updatedAt: now }); return { updated: res, }; }, async getList(args: Query, datasource) { const t = getInstanceByDatasource(datasource); const [records] = await getQuery(t, args); return records.map((x) => (dbType === DB_TYPE.SQLITE3 ? formatData(x, datasource.schema, 'output') : x)); }, async getRecords(args: Query, datasource) { const t = getInstanceByDatasource(datasource); const { skip, limit, sort = '' } = args; const [records, total] = await getQuery(t, args, true); return { total, records: records.map((x) => (dbType === DB_TYPE.SQLITE3 ? formatData(x, datasource.schema, 'output') : x)), skip, limit, sort, }; }, async delete(args: Where, datasource) { const t = getInstanceByDatasource(datasource); const res = await t.where(args).delete(); return { deleted: res, }; }, }; return fetchers; }