2
0

tree.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. const _ = require('lodash')
  2. const graphHelper = require('../../helpers/graph')
  3. const typeResolvers = {
  4. folder: 'TreeItemFolder',
  5. page: 'TreeItemPage',
  6. asset: 'TreeItemAsset'
  7. }
  8. const rePathName = /^[a-z0-9-]+$/
  9. const reTitle = /^[^<>"]+$/
  10. module.exports = {
  11. Query: {
  12. async tree (obj, args, context, info) {
  13. // Offset
  14. const offset = args.offset || 0
  15. if (offset < 0) {
  16. throw new Error('Invalid Offset')
  17. }
  18. // Limit
  19. const limit = args.limit || 100
  20. if (limit < 1 || limit > 100) {
  21. throw new Error('Invalid Limit')
  22. }
  23. // Order By
  24. const orderByDirection = args.orderByDirection || 'asc'
  25. const orderBy = args.orderBy || 'title'
  26. // Parse depth
  27. const depth = args.depth || 0
  28. if (depth < 0 || depth > 10) {
  29. throw new Error('Invalid Depth')
  30. }
  31. const depthCondition = depth > 0 ? `*{,${depth}}` : '*{0}'
  32. // Get parent path
  33. let parentPath = ''
  34. if (args.parentId) {
  35. const parent = await WIKI.db.knex('tree').where('id', args.parentId).first()
  36. if (parent) {
  37. parentPath = (parent.folderPath ? `${parent.folderPath}.${parent.fileName}` : parent.fileName).replaceAll('-', '_')
  38. }
  39. } else if (args.parentPath) {
  40. parentPath = args.parentPath.replaceAll('/', '.').replaceAll('-', '_').toLowerCase()
  41. }
  42. const folderPathCondition = parentPath ? `${parentPath}.${depthCondition}` : depthCondition
  43. // Fetch Items
  44. const items = await WIKI.db.knex('tree')
  45. .select(WIKI.db.knex.raw('tree.*, nlevel(tree."folderPath") AS depth'))
  46. .where(builder => {
  47. builder.where('folderPath', '~', folderPathCondition)
  48. if (args.includeAncestors) {
  49. const parentPathParts = parentPath.split('.')
  50. for (let i = 1; i <= parentPathParts.length; i++) {
  51. builder.orWhere({
  52. folderPath: _.dropRight(parentPathParts, i).join('.'),
  53. fileName: _.nth(parentPathParts, i * -1)
  54. })
  55. }
  56. }
  57. })
  58. .andWhere(builder => {
  59. if (args.types && args.types.length > 0) {
  60. builder.whereIn('type', args.types)
  61. }
  62. })
  63. .limit(limit)
  64. .offset(offset)
  65. .orderBy([
  66. { column: 'depth' },
  67. { column: orderBy, order: orderByDirection }
  68. ])
  69. return items.map(item => ({
  70. id: item.id,
  71. depth: item.depth,
  72. type: item.type,
  73. folderPath: item.folderPath.replaceAll('.', '/').replaceAll('_', '-'),
  74. fileName: item.fileName,
  75. title: item.title,
  76. createdAt: item.createdAt,
  77. updatedAt: item.updatedAt,
  78. ...(item.type === 'folder') && {
  79. childrenCount: 0
  80. }
  81. }))
  82. }
  83. },
  84. Mutation: {
  85. async createFolder (obj, args, context) {
  86. try {
  87. // Get parent path
  88. let parentPath = ''
  89. if (args.parentId) {
  90. const parent = await WIKI.db.knex('tree').where('id', args.parentId).first()
  91. parentPath = parent ? `${parent.folderPath}.${parent.fileName}` : ''
  92. if (parent) {
  93. parentPath = parent.folderPath ? `${parent.folderPath}.${parent.fileName}` : parent.fileName
  94. }
  95. parentPath = parentPath.replaceAll('-', '_')
  96. }
  97. // Validate path name
  98. if (!rePathName.test(args.pathName)) {
  99. throw new Error('ERR_INVALID_PATH_NAME')
  100. }
  101. // Validate title
  102. if (!reTitle.test(args.title)) {
  103. throw new Error('ERR_INVALID_TITLE')
  104. }
  105. // Check for collision
  106. const existingFolder = await WIKI.db.knex('tree').where({
  107. siteId: args.siteId,
  108. folderPath: parentPath,
  109. fileName: args.pathName
  110. }).first()
  111. if (existingFolder) {
  112. throw new Error('ERR_FOLDER_ALREADY_EXISTS')
  113. }
  114. // Create folder
  115. await WIKI.db.knex('tree').insert({
  116. folderPath: parentPath,
  117. fileName: args.pathName,
  118. type: 'folder',
  119. title: args.title,
  120. siteId: args.siteId
  121. })
  122. return {
  123. operation: graphHelper.generateSuccess('Folder created successfully')
  124. }
  125. } catch (err) {
  126. return graphHelper.generateError(err)
  127. }
  128. }
  129. },
  130. TreeItem: {
  131. __resolveType (obj, context, info) {
  132. return typeResolvers[obj.type] ?? null
  133. }
  134. }
  135. }