2.0.0.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.json('toc')
  120. table.string('contentType').notNullable()
  121. table.string('createdAt').notNullable()
  122. table.string('updatedAt').notNullable()
  123. })
  124. // PAGE TREE ---------------------------
  125. .createTable('pageTree', table => {
  126. table.charset('utf8mb4')
  127. table.increments('id').primary()
  128. table.string('path').notNullable()
  129. table.integer('depth').unsigned().notNullable()
  130. table.string('title').notNullable()
  131. table.boolean('isPrivate').notNullable().defaultTo(false)
  132. table.boolean('isFolder').notNullable().defaultTo(false)
  133. table.string('privateNS')
  134. })
  135. // RENDERERS ---------------------------
  136. .createTable('renderers', table => {
  137. table.charset('utf8mb4')
  138. table.string('key').notNullable().primary()
  139. table.boolean('isEnabled').notNullable().defaultTo(false)
  140. table.json('config')
  141. })
  142. // SEARCH ------------------------------
  143. .createTable('searchEngines', table => {
  144. table.charset('utf8mb4')
  145. table.string('key').notNullable().primary()
  146. table.boolean('isEnabled').notNullable().defaultTo(false)
  147. table.json('config')
  148. })
  149. // SETTINGS ----------------------------
  150. .createTable('settings', table => {
  151. table.charset('utf8mb4')
  152. table.string('key').notNullable().primary()
  153. table.json('value')
  154. table.string('updatedAt').notNullable()
  155. })
  156. // STORAGE -----------------------------
  157. .createTable('storage', table => {
  158. table.charset('utf8mb4')
  159. table.string('key').notNullable().primary()
  160. table.boolean('isEnabled').notNullable().defaultTo(false)
  161. table.string('mode', ['sync', 'push', 'pull']).notNullable().defaultTo('push')
  162. table.json('config')
  163. })
  164. // TAGS --------------------------------
  165. .createTable('tags', table => {
  166. table.charset('utf8mb4')
  167. table.increments('id').primary()
  168. table.string('tag').notNullable().unique()
  169. table.string('title')
  170. table.string('createdAt').notNullable()
  171. table.string('updatedAt').notNullable()
  172. })
  173. // USER KEYS ---------------------------
  174. .createTable('userKeys', table => {
  175. table.charset('utf8mb4')
  176. table.increments('id').primary()
  177. table.string('kind').notNullable()
  178. table.string('token').notNullable()
  179. table.string('createdAt').notNullable()
  180. table.string('validUntil').notNullable()
  181. })
  182. // USERS -------------------------------
  183. .createTable('users', table => {
  184. table.charset('utf8mb4')
  185. table.increments('id').primary()
  186. table.string('email').notNullable()
  187. table.string('name').notNullable()
  188. table.string('providerId')
  189. table.string('password')
  190. table.boolean('tfaIsActive').notNullable().defaultTo(false)
  191. table.string('tfaSecret')
  192. table.string('jobTitle').defaultTo('')
  193. table.string('location').defaultTo('')
  194. table.string('pictureUrl')
  195. table.string('timezone').notNullable().defaultTo('America/New_York')
  196. table.boolean('isSystem').notNullable().defaultTo(false)
  197. table.boolean('isActive').notNullable().defaultTo(false)
  198. table.boolean('isVerified').notNullable().defaultTo(false)
  199. table.string('createdAt').notNullable()
  200. table.string('updatedAt').notNullable()
  201. })
  202. // =====================================
  203. // RELATION TABLES
  204. // =====================================
  205. // PAGE HISTORY TAGS ---------------------------
  206. .createTable('pageHistoryTags', table => {
  207. table.charset('utf8mb4')
  208. table.increments('id').primary()
  209. table.integer('pageId').unsigned().references('id').inTable('pageHistory').onDelete('CASCADE')
  210. table.integer('tagId').unsigned().references('id').inTable('tags').onDelete('CASCADE')
  211. })
  212. // PAGE TAGS ---------------------------
  213. .createTable('pageTags', table => {
  214. table.charset('utf8mb4')
  215. table.increments('id').primary()
  216. table.integer('pageId').unsigned().references('id').inTable('pages').onDelete('CASCADE')
  217. table.integer('tagId').unsigned().references('id').inTable('tags').onDelete('CASCADE')
  218. })
  219. // USER GROUPS -------------------------
  220. .createTable('userGroups', table => {
  221. table.charset('utf8mb4')
  222. table.increments('id').primary()
  223. table.integer('userId').unsigned().references('id').inTable('users').onDelete('CASCADE')
  224. table.integer('groupId').unsigned().references('id').inTable('groups').onDelete('CASCADE')
  225. })
  226. // =====================================
  227. // REFERENCES
  228. // =====================================
  229. .table('assets', table => {
  230. table.integer('folderId').unsigned().references('id').inTable('assetFolders')
  231. table.integer('authorId').unsigned().references('id').inTable('users')
  232. })
  233. .table('comments', table => {
  234. table.integer('pageId').unsigned().references('id').inTable('pages')
  235. table.integer('authorId').unsigned().references('id').inTable('users')
  236. })
  237. .table('pageHistory', table => {
  238. table.integer('pageId').unsigned().references('id').inTable('pages')
  239. table.string('editorKey').references('key').inTable('editors')
  240. table.string('localeCode', 2).references('code').inTable('locales')
  241. table.integer('authorId').unsigned().references('id').inTable('users')
  242. })
  243. .table('pages', table => {
  244. table.string('editorKey').references('key').inTable('editors')
  245. table.string('localeCode', 2).references('code').inTable('locales')
  246. table.integer('authorId').unsigned().references('id').inTable('users')
  247. table.integer('creatorId').unsigned().references('id').inTable('users')
  248. })
  249. .table('pageTree', table => {
  250. table.integer('parent').unsigned().references('id').inTable('pageTree')
  251. table.integer('pageId').unsigned().references('id').inTable('pages')
  252. table.string('localeCode', 2).references('code').inTable('locales')
  253. })
  254. .table('userKeys', table => {
  255. table.integer('userId').unsigned().references('id').inTable('users')
  256. })
  257. .table('users', table => {
  258. table.string('providerKey').references('key').inTable('authentication').notNullable().defaultTo('local')
  259. table.string('localeCode', 2).references('code').inTable('locales').notNullable().defaultTo('en')
  260. table.string('defaultEditor').references('key').inTable('editors').notNullable().defaultTo('markdown')
  261. table.unique(['providerKey', 'email'])
  262. })
  263. }
  264. exports.down = knex => {
  265. return knex.schema
  266. .dropTableIfExists('userGroups')
  267. .dropTableIfExists('pageHistoryTags')
  268. .dropTableIfExists('pageHistory')
  269. .dropTableIfExists('pageTags')
  270. .dropTableIfExists('assets')
  271. .dropTableIfExists('assetFolders')
  272. .dropTableIfExists('comments')
  273. .dropTableIfExists('editors')
  274. .dropTableIfExists('groups')
  275. .dropTableIfExists('locales')
  276. .dropTableIfExists('navigation')
  277. .dropTableIfExists('pages')
  278. .dropTableIfExists('renderers')
  279. .dropTableIfExists('settings')
  280. .dropTableIfExists('storage')
  281. .dropTableIfExists('tags')
  282. .dropTableIfExists('userKeys')
  283. .dropTableIfExists('users')
  284. }