2
0

2.0.0.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.json('pageRules').notNullable()
  60. table.boolean('isSystem').notNullable().defaultTo(false)
  61. table.string('createdAt').notNullable()
  62. table.string('updatedAt').notNullable()
  63. })
  64. // LOCALES -----------------------------
  65. .createTable('locales', table => {
  66. table.charset('utf8mb4')
  67. table.string('code', 2).notNullable().primary()
  68. table.json('strings')
  69. table.boolean('isRTL').notNullable().defaultTo(false)
  70. table.string('name').notNullable()
  71. table.string('nativeName').notNullable()
  72. table.string('createdAt').notNullable()
  73. table.string('updatedAt').notNullable()
  74. })
  75. // LOGGING ----------------------------
  76. .createTable('loggers', table => {
  77. table.charset('utf8mb4')
  78. table.string('key').notNullable().primary()
  79. table.boolean('isEnabled').notNullable().defaultTo(false)
  80. table.string('level').notNullable().defaultTo('warn')
  81. table.json('config')
  82. })
  83. // NAVIGATION ----------------------------
  84. .createTable('navigation', table => {
  85. table.charset('utf8mb4')
  86. table.string('key').notNullable().primary()
  87. table.json('config')
  88. })
  89. // PAGE HISTORY ------------------------
  90. .createTable('pageHistory', table => {
  91. table.charset('utf8mb4')
  92. table.increments('id').primary()
  93. table.string('path').notNullable()
  94. table.string('hash').notNullable()
  95. table.string('title').notNullable()
  96. table.string('description')
  97. table.boolean('isPrivate').notNullable().defaultTo(false)
  98. table.boolean('isPublished').notNullable().defaultTo(false)
  99. table.string('publishStartDate')
  100. table.string('publishEndDate')
  101. table.text('content')
  102. table.string('contentType').notNullable()
  103. table.string('createdAt').notNullable()
  104. })
  105. // PAGES -------------------------------
  106. .createTable('pages', table => {
  107. table.charset('utf8mb4')
  108. table.increments('id').primary()
  109. table.string('path').notNullable()
  110. table.string('hash').notNullable()
  111. table.string('title').notNullable()
  112. table.string('description')
  113. table.boolean('isPrivate').notNullable().defaultTo(false)
  114. table.boolean('isPublished').notNullable().defaultTo(false)
  115. table.string('privateNS')
  116. table.string('publishStartDate')
  117. table.string('publishEndDate')
  118. table.text('content')
  119. table.text('render')
  120. table.json('toc')
  121. table.string('contentType').notNullable()
  122. table.string('createdAt').notNullable()
  123. table.string('updatedAt').notNullable()
  124. })
  125. // PAGE TREE ---------------------------
  126. .createTable('pageTree', table => {
  127. table.charset('utf8mb4')
  128. table.increments('id').primary()
  129. table.string('path').notNullable()
  130. table.integer('depth').unsigned().notNullable()
  131. table.string('title').notNullable()
  132. table.boolean('isPrivate').notNullable().defaultTo(false)
  133. table.boolean('isFolder').notNullable().defaultTo(false)
  134. table.string('privateNS')
  135. })
  136. // RENDERERS ---------------------------
  137. .createTable('renderers', table => {
  138. table.charset('utf8mb4')
  139. table.string('key').notNullable().primary()
  140. table.boolean('isEnabled').notNullable().defaultTo(false)
  141. table.json('config')
  142. })
  143. // SEARCH ------------------------------
  144. .createTable('searchEngines', table => {
  145. table.charset('utf8mb4')
  146. table.string('key').notNullable().primary()
  147. table.boolean('isEnabled').notNullable().defaultTo(false)
  148. table.json('config')
  149. })
  150. // SETTINGS ----------------------------
  151. .createTable('settings', table => {
  152. table.charset('utf8mb4')
  153. table.string('key').notNullable().primary()
  154. table.json('value')
  155. table.string('updatedAt').notNullable()
  156. })
  157. // STORAGE -----------------------------
  158. .createTable('storage', table => {
  159. table.charset('utf8mb4')
  160. table.string('key').notNullable().primary()
  161. table.boolean('isEnabled').notNullable().defaultTo(false)
  162. table.string('mode', ['sync', 'push', 'pull']).notNullable().defaultTo('push')
  163. table.json('config')
  164. })
  165. // TAGS --------------------------------
  166. .createTable('tags', table => {
  167. table.charset('utf8mb4')
  168. table.increments('id').primary()
  169. table.string('tag').notNullable().unique()
  170. table.string('title')
  171. table.string('createdAt').notNullable()
  172. table.string('updatedAt').notNullable()
  173. })
  174. // USER KEYS ---------------------------
  175. .createTable('userKeys', table => {
  176. table.charset('utf8mb4')
  177. table.increments('id').primary()
  178. table.string('kind').notNullable()
  179. table.string('token').notNullable()
  180. table.string('createdAt').notNullable()
  181. table.string('validUntil').notNullable()
  182. })
  183. // USERS -------------------------------
  184. .createTable('users', table => {
  185. table.charset('utf8mb4')
  186. table.increments('id').primary()
  187. table.string('email').notNullable()
  188. table.string('name').notNullable()
  189. table.string('providerId')
  190. table.string('password')
  191. table.boolean('tfaIsActive').notNullable().defaultTo(false)
  192. table.string('tfaSecret')
  193. table.string('jobTitle').defaultTo('')
  194. table.string('location').defaultTo('')
  195. table.string('pictureUrl')
  196. table.string('timezone').notNullable().defaultTo('America/New_York')
  197. table.boolean('isSystem').notNullable().defaultTo(false)
  198. table.boolean('isActive').notNullable().defaultTo(false)
  199. table.boolean('isVerified').notNullable().defaultTo(false)
  200. table.string('createdAt').notNullable()
  201. table.string('updatedAt').notNullable()
  202. })
  203. // =====================================
  204. // RELATION TABLES
  205. // =====================================
  206. // PAGE HISTORY TAGS ---------------------------
  207. .createTable('pageHistoryTags', table => {
  208. table.charset('utf8mb4')
  209. table.increments('id').primary()
  210. table.integer('pageId').unsigned().references('id').inTable('pageHistory').onDelete('CASCADE')
  211. table.integer('tagId').unsigned().references('id').inTable('tags').onDelete('CASCADE')
  212. })
  213. // PAGE TAGS ---------------------------
  214. .createTable('pageTags', table => {
  215. table.charset('utf8mb4')
  216. table.increments('id').primary()
  217. table.integer('pageId').unsigned().references('id').inTable('pages').onDelete('CASCADE')
  218. table.integer('tagId').unsigned().references('id').inTable('tags').onDelete('CASCADE')
  219. })
  220. // USER GROUPS -------------------------
  221. .createTable('userGroups', table => {
  222. table.charset('utf8mb4')
  223. table.increments('id').primary()
  224. table.integer('userId').unsigned().references('id').inTable('users').onDelete('CASCADE')
  225. table.integer('groupId').unsigned().references('id').inTable('groups').onDelete('CASCADE')
  226. })
  227. // =====================================
  228. // REFERENCES
  229. // =====================================
  230. .table('assets', table => {
  231. table.integer('folderId').unsigned().references('id').inTable('assetFolders')
  232. table.integer('authorId').unsigned().references('id').inTable('users')
  233. })
  234. .table('comments', table => {
  235. table.integer('pageId').unsigned().references('id').inTable('pages')
  236. table.integer('authorId').unsigned().references('id').inTable('users')
  237. })
  238. .table('pageHistory', table => {
  239. table.integer('pageId').unsigned().references('id').inTable('pages')
  240. table.string('editorKey').references('key').inTable('editors')
  241. table.string('localeCode', 2).references('code').inTable('locales')
  242. table.integer('authorId').unsigned().references('id').inTable('users')
  243. })
  244. .table('pages', table => {
  245. table.string('editorKey').references('key').inTable('editors')
  246. table.string('localeCode', 2).references('code').inTable('locales')
  247. table.integer('authorId').unsigned().references('id').inTable('users')
  248. table.integer('creatorId').unsigned().references('id').inTable('users')
  249. })
  250. .table('pageTree', table => {
  251. table.integer('parent').unsigned().references('id').inTable('pageTree')
  252. table.integer('pageId').unsigned().references('id').inTable('pages')
  253. table.string('localeCode', 2).references('code').inTable('locales')
  254. })
  255. .table('userKeys', table => {
  256. table.integer('userId').unsigned().references('id').inTable('users')
  257. })
  258. .table('users', table => {
  259. table.string('providerKey').references('key').inTable('authentication').notNullable().defaultTo('local')
  260. table.string('localeCode', 2).references('code').inTable('locales').notNullable().defaultTo('en')
  261. table.string('defaultEditor').references('key').inTable('editors').notNullable().defaultTo('markdown')
  262. table.unique(['providerKey', 'email'])
  263. })
  264. }
  265. exports.down = knex => {
  266. return knex.schema
  267. .dropTableIfExists('userGroups')
  268. .dropTableIfExists('pageHistoryTags')
  269. .dropTableIfExists('pageHistory')
  270. .dropTableIfExists('pageTags')
  271. .dropTableIfExists('assets')
  272. .dropTableIfExists('assetFolders')
  273. .dropTableIfExists('comments')
  274. .dropTableIfExists('editors')
  275. .dropTableIfExists('groups')
  276. .dropTableIfExists('locales')
  277. .dropTableIfExists('navigation')
  278. .dropTableIfExists('pages')
  279. .dropTableIfExists('renderers')
  280. .dropTableIfExists('settings')
  281. .dropTableIfExists('storage')
  282. .dropTableIfExists('tags')
  283. .dropTableIfExists('userKeys')
  284. .dropTableIfExists('users')
  285. }