相关文章推荐
淡定的乒乓球  ·  内江方言_百度百科·  1 年前    · 
风流倜傥的凳子  ·  Yamy郭颖的音乐主页·  1 年前    · 

node.js 生成postgresql数据字典 并存储到markdown文件

const fs = require('fs')
const path = require('path')
dataDictionary(`
CREATE TABLE "public"."work" (
  "id" int4 NOT NULL DEFAULT nextval('gm_work_order_id_seq'::regclass),
  "status" int4 NOT NULL DEFAULT 1,
  "create_time" timestamptz(6) DEFAULT CURRENT_TIMESTAMP,
  "create_user" varchar(255) COLLATE "pg_catalog"."default",
  "update_time" timestamptz(6) DEFAULT CURRENT_TIMESTAMP
COMMENT ON COLUMN "public"."work"."status" IS '是否删除';
COMMENT ON COLUMN "public"."work"."create_time" IS '创建时间';
COMMENT ON COLUMN "public"."work"."create_user" IS '创建人';
COMMENT ON COLUMN "public"."work"."update_time" IS '最后更新时间';
//数据字典
function dataDictionary(strAll) {
  const index = strAll.indexOf('(') //第一次出现
  const last = strAll.lastIndexOf(')') //最后一次出现
  const tArr = strAll.slice(0, index).split(' ')[2].split('"') //分割
  const tLen = tArr.length
  const tableName = tArr[tLen - 2] //表名称
  const notesObj = filterNotes(strAll.slice(last + 1).split(';'), tableName) //生成注释obj
  const objArr = getObj(strAll.slice(index + 1, last).split(','), notesObj) //生成obj
  const str = table(objArr, tableName) //生成table
  const paths = `${path.join(
    __dirname,
    'data-dictionary',
    `${tableName}.md`
  )}` //存储路径
  setFile(paths, str)
//生成注释obj
function filterNotes(arr, tableName) {
  const obj = {}
  arr.forEach((value) => {
    const v = value.trim().split(' ')
    const len = v.indexOf('IS')
    if (len > -1) {
      const m = v[len - 1].split('"').filter((v) => {
        if (v != '\n' && v != '' && v != '.' && v != tableName && v != 'public')
          return v
      })[0]
      obj[m] = v[len + 1].split("'").filter((v) => {
        return v != ''
      })[0]
  return obj
//生成obj
function getObj(arr, notesObj) {
  const objArr = []
  arr.forEach((value) => {
    let obj = {
      field: '',
      type: '',
      length: 0,
      point: 0,
      notIsNull: 'N',
      primary: 'N',
      notes: '',
      default: '',
    const vArr = value.trim().split(' ')
    //console.log(vArr)
    obj.field = vArr[0].split('"').filter((v) => {
      if (v != '\n' && v != '') return v
    })[0]
    if (vArr[1].includes('(') && vArr[1].includes(')')) {
      const ts = vArr[1].split('(')
      const ts1 = ts[1].split(')')
      obj.type = ts[0]
      obj.point = 0
      obj.length = +ts1[0]
    } else {
      obj.type = vArr[1]
      obj.point = 0
      obj.length = 0
      if (obj.type === 'int2') obj.length = 16
      if (obj.type === 'int4') obj.length = 32
      if (obj.type === 'int8') obj.length = 64
    obj.notIsNull = vArr.includes('NOT') && vArr.includes('NULL') ? 'Y' : 'N'
    const defaultLen = vArr.indexOf('DEFAULT')
    obj.default = defaultLen > -1 ? vArr[defaultLen + 1] : ''
    obj.notes = obj.field in notesObj ? notesObj[obj.field] : ''
    obj.primary = obj.default.includes('nextval') ? 'Y' : 'N'
    objArr.push(obj)
  return objArr
function table(objArr, tableName) {
  let str = `
  #### 表名称:${tableName}
  |字段|类型|长度|小数点|不是null|主键|注释|默认值|
  |:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
  objArr.forEach((v) => {
    str += `|${v.field}|${v.type}|${v.length}|${v.point}|${v.notIsNull}|${v.primary}|${v.notes}|${v.default}|\n`
  return str
function setFile(paths, str) {
  //写入内容
  fs.writeFile(paths, str, (err) => {
    if (err) {