2.0.0.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. exports.up = knex => {
  2. return knex.schema
  3. // =====================================
  4. // MODEL TABLES
  5. // =====================================
  6. // ASSETS ------------------------------
  7. .createTable('assets', table => {
  8. table.charset('utf8mb4')
  9. table.increments('id').primary()
  10. table.string('filename').notNullable()
  11. table.string('basename').notNullable()
  12. table.string('ext').notNullable()
  13. table.enum('kind', ['binary', 'image']).notNullable().defaultTo('binary')
  14. table.string('mime').notNullable().defaultTo('application/octet-stream')
  15. table.integer('fileSize').unsigned().comment('In kilobytes')
  16. table.json('metadata')
  17. table.string('createdAt').notNullable()
  18. table.string('updatedAt').notNullable()
  19. })
  20. // ASSET FOLDERS -----------------------
  21. .createTable('assetFolders', table => {
  22. table.charset('utf8mb4')
  23. table.increments('id').primary()
  24. table.string('name').notNullable()
  25. table.string('slug').notNullable()
  26. table.integer('parentId').unsigned().references('id').inTable('assetFolders')
  27. })
  28. // AUTHENTICATION ----------------------
  29. .createTable('authentication', table => {
  30. table.charset('utf8mb4')
  31. table.string('key').notNullable().primary()
  32. table.boolean('isEnabled').notNullable().defaultTo(false)
  33. table.json('config').notNullable()
  34. table.boolean('selfRegistration').notNullable().defaultTo(false)
  35. table.json('domainWhitelist').notNullable()
  36. table.json('autoEnrollGroups').notNullable()
  37. })
  38. // COMMENTS ----------------------------
  39. .createTable('comments', table => {
  40. table.charset('utf8mb4')
  41. table.increments('id').primary()
  42. table.text('content').notNullable()
  43. table.string('createdAt').notNullable()
  44. table.string('updatedAt').notNullable()
  45. })
  46. // EDITORS -----------------------------
  47. .createTable('editors', table => {
  48. table.charset('utf8mb4')
  49. table.string('key').notNullable().primary()
  50. table.boolean('isEnabled').notNullable().defaultTo(false)
  51. table.json('config').notNullable()
  52. })
  53. // GROUPS ------------------------------
  54. .createTable('groups', table => {
  55. table.charset('utf8mb4')
  56. table.increments('id').primary()
  57. table.string('name').notNullable()
  58. table.json('permissions').notNullable()
  59. table.boolean('isSystem').notNullable().defaultTo(false)
  60. table.string('createdAt').notNullable()
  61. table.string('updatedAt').notNullable()
  62. })
  63. // LOCALES -----------------------------
  64. .createTable('locales', table => {
  65. table.charset('utf8mb4')
  66. table.string('code', 2).notNullable().primary()
  67. table.json('strings')
  68. table.boolean('isRTL').notNullable().defaultTo(false)
  69. table.string('name').notNullable()
  70. table.string('nativeName').notNullable()
  71. table.string('createdAt').notNullable()
  72. table.string('updatedAt').notNullable()
  73. })
  74. // LOGGING ----------------------------
  75. .createTable('loggers', table => {
  76. table.charset('utf8mb4')
  77. table.string('key').notNullable().primary()
  78. table.boolean('isEnabled').notNullable().defaultTo(false)
  79. table.string('level').notNullable().defaultTo('warn')
  80. table.json('config')
  81. })
  82. // NAVIGATION ----------------------------
  83. .createTable('navigation', table => {
  84. table.charset('utf8mb4')
  85. table.string('key').notNullable().primary()
  86. table.json('config')
  87. })
  88. // PAGE HISTORY ------------------------
  89. .createTable('pageHistory', table => {
  90. table.charset('utf8mb4')
  91. table.increments('id').primary()
  92. table.string('path').notNullable()
  93. table.string('hash').notNullable()
  94. table.string('title').notNullable()
  95. table.string('description')
  96. table.boolean('isPrivate').notNullable().defaultTo(false)
  97. table.boolean('isPublished').notNullable().defaultTo(false)
  98. table.string('publishStartDate')
  99. table.string('publishEndDate')
  100. table.text('content')
  101. table.string('contentType').notNullable()
  102. table.string('createdAt').notNullable()
  103. })
  104. // PAGES -------------------------------
  105. .createTable('pages', table => {
  106. table.charset('utf8mb4')
  107. table.increments('id').primary()
  108. table.string('path').notNullable()
  109. table.string('hash').notNullable()
  110. table.string('title').notNullable()
  111. table.string('description')
  112. table.boolean('isPrivate').notNullable().defaultTo(false)
  113. table.boolean('isPublished').notNullable().defaultTo(false)
  114. table.string('privateNS')
  115. table.string('publishStartDate')
  116. table.string('publishEndDate')
  117. table.text('content')
  118. table.text('render')
  119. table.string('contentType').notNullable()
  120. table.string('createdAt').notNullable()
  121. table.string('updatedAt').notNullable()
  122. })
  123. // PAGE TREE ---------------------------
  124. .createTable('pageTree', table => {
  125. table.charset('utf8mb4')
  126. table.increments('id').primary()
  127. table.string('path').notNullable()
  128. table.integer('depth').unsigned().notNullable()
  129. table.string('title').notNullable()
  130. table.boolean('isPrivate').notNullable().defaultTo(false)
  131. table.boolean('isFolder').notNullable().defaultTo(false)
  132. table.string('privateNS')
  133. })
  134. // RENDERERS ---------------------------
  135. .createTable('renderers', table => {
  136. table.charset('utf8mb4')
  137. table.string('key').notNullable().primary()
  138. table.boolean('isEnabled').notNullable().defaultTo(false)
  139. table.json('config')
  140. })
  141. // SEARCH ------------------------------
  142. .createTable('searchEngines', table => {
  143. table.charset('utf8mb4')
  144. table.string('key').notNullable().primary()
  145. table.boolean('isEnabled').notNullable().defaultTo(false)
  146. table.json('config')
  147. })
  148. // SETTINGS ----------------------------
  149. .createTable('settings', table => {
  150. table.charset('utf8mb4')
  151. table.string('key').notNullable().primary()
  152. table.json('value')
  153. table.string('updatedAt').notNullable()
  154. })
  155. // STORAGE -----------------------------
  156. .createTable('storage', table => {
  157. table.charset('utf8mb4')
  158. table.string('key').notNullable().primary()
  159. table.boolean('isEnabled').notNullable().defaultTo(false)
  160. table.string('mode', ['sync', 'push', 'pull']).notNullable().defaultTo('push')
  161. table.json('config')
  162. })
  163. // TAGS --------------------------------
  164. .createTable('tags', table => {
  165. table.charset('utf8mb4')
  166. table.increments('id').primary()
  167. table.string('tag').notNullable().unique()
  168. table.string('title')
  169. table.string('createdAt').notNullable()
  170. table.string('updatedAt').notNullable()
  171. })
  172. // USERS -------------------------------
  173. .createTable('users', table => {
  174. table.charset('utf8mb4')
  175. table.increments('id').primary()
  176. table.string('email').notNullable()
  177. table.string('name').notNullable()
  178. table.string('providerId')
  179. table.string('password')
  180. table.boolean('tfaIsActive').notNullable().defaultTo(false)
  181. table.string('tfaSecret')
  182. table.string('jobTitle').defaultTo('')
  183. table.string('location').defaultTo('')
  184. table.string('pictureUrl')
  185. table.string('timezone').notNullable().defaultTo('America/New_York')
  186. table.string('createdAt').notNullable()
  187. table.string('updatedAt').notNullable()
  188. })
  189. // =====================================
  190. // RELATION TABLES
  191. // =====================================
  192. // PAGE HISTORY TAGS ---------------------------
  193. .createTable('pageHistoryTags', table => {
  194. table.charset('utf8mb4')
  195. table.increments('id').primary()
  196. table.integer('pageId').unsigned().references('id').inTable('pageHistory').onDelete('CASCADE')
  197. table.integer('tagId').unsigned().references('id').inTable('tags').onDelete('CASCADE')
  198. })
  199. // PAGE TAGS ---------------------------
  200. .createTable('pageTags', table => {
  201. table.charset('utf8mb4')
  202. table.increments('id').primary()
  203. table.integer('pageId').unsigned().references('id').inTable('pages').onDelete('CASCADE')
  204. table.integer('tagId').unsigned().references('id').inTable('tags').onDelete('CASCADE')
  205. })
  206. // USER GROUPS -------------------------
  207. .createTable('userGroups', table => {
  208. table.charset('utf8mb4')
  209. table.increments('id').primary()
  210. table.integer('userId').unsigned().references('id').inTable('users').onDelete('CASCADE')
  211. table.integer('groupId').unsigned().references('id').inTable('groups').onDelete('CASCADE')
  212. })
  213. // =====================================
  214. // REFERENCES
  215. // =====================================
  216. .table('assets', table => {
  217. table.integer('folderId').unsigned().references('id').inTable('assetFolders')
  218. table.integer('authorId').unsigned().references('id').inTable('users')
  219. })
  220. .table('comments', table => {
  221. table.integer('pageId').unsigned().references('id').inTable('pages')
  222. table.integer('authorId').unsigned().references('id').inTable('users')
  223. })
  224. .table('pageHistory', table => {
  225. table.integer('pageId').unsigned().references('id').inTable('pages')
  226. table.string('editorKey').references('key').inTable('editors')
  227. table.string('localeCode', 2).references('code').inTable('locales')
  228. table.integer('authorId').unsigned().references('id').inTable('users')
  229. })
  230. .table('pages', table => {
  231. table.string('editorKey').references('key').inTable('editors')
  232. table.string('localeCode', 2).references('code').inTable('locales')
  233. table.integer('authorId').unsigned().references('id').inTable('users')
  234. table.integer('creatorId').unsigned().references('id').inTable('users')
  235. })
  236. .table('pageTree', table => {
  237. table.integer('parent').unsigned().references('id').inTable('pageTree')
  238. table.integer('pageId').unsigned().references('id').inTable('pages')
  239. table.string('localeCode', 2).references('code').inTable('locales')
  240. })
  241. .table('users', table => {
  242. table.string('providerKey').references('key').inTable('authentication').notNullable().defaultTo('local')
  243. table.string('localeCode', 2).references('code').inTable('locales').notNullable().defaultTo('en')
  244. table.string('defaultEditor').references('key').inTable('editors').notNullable().defaultTo('markdown')
  245. table.unique(['providerKey', 'email'])
  246. })
  247. }
  248. exports.down = knex => {
  249. return knex.schema
  250. .dropTableIfExists('userGroups')
  251. .dropTableIfExists('pageHistoryTags')
  252. .dropTableIfExists('pageHistory')
  253. .dropTableIfExists('pageTags')
  254. .dropTableIfExists('assets')
  255. .dropTableIfExists('assetFolders')
  256. .dropTableIfExists('comments')
  257. .dropTableIfExists('editors')
  258. .dropTableIfExists('groups')
  259. .dropTableIfExists('locales')
  260. .dropTableIfExists('navigation')
  261. .dropTableIfExists('pages')
  262. .dropTableIfExists('renderers')
  263. .dropTableIfExists('settings')
  264. .dropTableIfExists('storage')
  265. .dropTableIfExists('tags')
  266. .dropTableIfExists('users')
  267. }