index.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import {defineStore} from 'pinia'
  2. /**
  3. * 表信息
  4. */
  5. export interface Schema {
  6. module: string
  7. name: string
  8. comment: string
  9. engine: string
  10. charset: string
  11. collaction: string
  12. created_at: boolean
  13. creator_id: boolean
  14. updated_at: boolean
  15. deleted_at: boolean
  16. }
  17. /**
  18. * 表结构信息
  19. */
  20. export interface Structure {
  21. id: number
  22. field: string
  23. length: number
  24. type: string
  25. nullable: boolean
  26. unique: boolean
  27. default: number | string
  28. comment: string
  29. }
  30. /**
  31. * generate
  32. */
  33. interface CreateSchema {
  34. schema: Schema
  35. structures: Structure[]
  36. is_finished: boolean
  37. }
  38. /**
  39. * useSchemaStore
  40. */
  41. export const useSchemaStore = defineStore('schemaStore', {
  42. state(): CreateSchema {
  43. return {
  44. // schema
  45. schema: Object.assign({
  46. module: '',
  47. name: '',
  48. comment: '',
  49. engine: 'InnoDB',
  50. charset: 'utf8mb4',
  51. collection: 'utf8mb4_unicode_ci',
  52. created_at: true,
  53. creator_id: true,
  54. updated_at: true,
  55. deleted_at: true,
  56. }),
  57. // structures
  58. structures: [] as Structure[],
  59. // is finished
  60. is_finished: false,
  61. }
  62. },
  63. // store getters
  64. getters: {
  65. getSchema(): Schema {
  66. return this.schema
  67. },
  68. getStructures(): Structure[] {
  69. return this.structures
  70. },
  71. getFinished(): boolean {
  72. return this.is_finished
  73. },
  74. },
  75. // store actions
  76. actions: {
  77. // set schema
  78. setSchema(schema: Schema): void {
  79. this.schema = schema
  80. },
  81. setStructures(structures: Array<Structure>): void {
  82. this.structures = structures
  83. },
  84. // add structure
  85. addStructure(structure: Structure): void {
  86. if (structure.id) {
  87. this.structures = this.structures.filter((s: Structure) => {
  88. if (s.id === structure.id) {
  89. s = structure
  90. }
  91. return s
  92. })
  93. } else {
  94. structure.id = this.structures.length + 1
  95. this.structures.push(structure)
  96. }
  97. },
  98. // filter structures
  99. filterStructures(id: number) {
  100. this.structures = this.structures.filter((s: Structure) => {
  101. return !(s.id === id)
  102. })
  103. },
  104. // init structure
  105. initStructure(): Structure {
  106. return Object.assign({
  107. id: 0,
  108. field: '',
  109. label: '',
  110. type: '',
  111. length: 0,
  112. nullable: false,
  113. unique: false,
  114. default: '',
  115. comment: '',
  116. })
  117. },
  118. /**
  119. * finished
  120. */
  121. finished(): void {
  122. this.$reset()
  123. this.is_finished = true
  124. },
  125. /**
  126. * unfinished
  127. */
  128. start(): void {
  129. this.is_finished = false
  130. },
  131. },
  132. })