@ditojs/server 0.275.0 → 1.0.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/package.json +25 -43
  2. package/src/app/Application.js +105 -111
  3. package/src/app/Validator.js +6 -3
  4. package/src/app/index.js +2 -2
  5. package/src/cli/db/createMigration.js +1 -1
  6. package/src/cli/db/index.js +7 -7
  7. package/src/cli/db/listAssetConfig.js +1 -1
  8. package/src/cli/db/reset.js +1 -1
  9. package/src/cli/index.js +14 -5
  10. package/src/controllers/AdminController.js +164 -158
  11. package/src/controllers/CollectionController.js +8 -29
  12. package/src/controllers/Controller.js +71 -76
  13. package/src/controllers/ControllerAction.js +37 -18
  14. package/src/controllers/MemberAction.js +2 -2
  15. package/src/controllers/ModelController.js +4 -4
  16. package/src/controllers/RelationController.js +3 -3
  17. package/src/controllers/{UserController.js → UsersController.js} +8 -13
  18. package/src/controllers/index.js +5 -5
  19. package/src/decorators/action.js +3 -3
  20. package/src/decorators/authorize.js +1 -1
  21. package/src/decorators/index.js +6 -6
  22. package/src/decorators/parameters.js +1 -1
  23. package/src/decorators/returns.js +1 -1
  24. package/src/decorators/scope.js +1 -1
  25. package/src/decorators/transacted.js +1 -1
  26. package/src/errors/AssetError.js +1 -1
  27. package/src/errors/AuthenticationError.js +1 -1
  28. package/src/errors/AuthorizationError.js +1 -1
  29. package/src/errors/ControllerError.js +1 -1
  30. package/src/errors/DatabaseError.js +12 -3
  31. package/src/errors/GraphError.js +1 -1
  32. package/src/errors/ModelError.js +1 -1
  33. package/src/errors/NotFoundError.js +1 -1
  34. package/src/errors/NotImplementedError.js +1 -1
  35. package/src/errors/QueryBuilderError.js +1 -1
  36. package/src/errors/RelationError.js +1 -1
  37. package/src/errors/ValidationError.js +1 -1
  38. package/src/errors/WrappedError.js +1 -1
  39. package/src/errors/index.js +14 -14
  40. package/src/graph/DitoGraphProcessor.js +2 -1
  41. package/src/graph/graph.js +1 -1
  42. package/src/graph/index.js +3 -3
  43. package/src/index.js +11 -9
  44. package/src/lib/index.js +2 -2
  45. package/src/middleware/createTransaction.js +1 -1
  46. package/src/middleware/findRoute.js +3 -2
  47. package/src/middleware/handleConnectMiddleware.js +88 -0
  48. package/src/middleware/handleError.js +1 -1
  49. package/src/middleware/handleRoute.js +3 -3
  50. package/src/middleware/index.js +8 -7
  51. package/src/mixins/AssetMixin.js +1 -1
  52. package/src/mixins/UserMixin.js +1 -1
  53. package/src/mixins/index.js +4 -4
  54. package/src/models/AssetModel.js +2 -2
  55. package/src/models/Model.js +16 -12
  56. package/src/models/RelationAccessor.js +1 -1
  57. package/src/models/SessionModel.js +2 -2
  58. package/src/models/TimeStampedModel.js +2 -2
  59. package/src/models/UserModel.js +2 -2
  60. package/src/models/definitions/assets.js +1 -1
  61. package/src/models/definitions/filters.js +57 -44
  62. package/src/models/definitions/hooks.js +1 -1
  63. package/src/models/definitions/index.js +9 -9
  64. package/src/models/definitions/modifiers.js +1 -1
  65. package/src/models/definitions/options.js +1 -1
  66. package/src/models/definitions/properties.js +2 -2
  67. package/src/models/definitions/relations.js +1 -1
  68. package/src/models/definitions/schema.js +1 -1
  69. package/src/models/definitions/scopes.js +2 -2
  70. package/src/models/index.js +5 -5
  71. package/src/query/QueryBuilder.js +5 -5
  72. package/src/query/QueryFilters.js +50 -50
  73. package/src/query/QueryParameters.js +2 -2
  74. package/src/query/index.js +3 -3
  75. package/src/schema/formats/index.js +2 -2
  76. package/src/schema/index.js +4 -4
  77. package/src/schema/keywords/_relate.js +1 -1
  78. package/src/schema/keywords/index.js +12 -12
  79. package/src/schema/properties.test.js +1 -1
  80. package/src/schema/relations.js +1 -1
  81. package/src/schema/relations.test.js +2 -2
  82. package/src/services/index.js +1 -1
  83. package/src/storage/DiskStorage.js +1 -1
  84. package/src/storage/S3Storage.js +4 -4
  85. package/src/storage/Storage.js +1 -1
  86. package/src/storage/index.js +4 -4
  87. package/src/utils/function.test.js +1 -1
  88. package/src/utils/handler.js +17 -0
  89. package/src/utils/index.js +8 -7
  90. package/src/utils/object.test.js +1 -1
  91. package/lib/app/Application.js +0 -961
  92. package/lib/app/SessionStore.js +0 -40
  93. package/lib/app/Validator.js +0 -355
  94. package/lib/app/index.js +0 -26
  95. package/lib/cli/console.js +0 -175
  96. package/lib/cli/db/createMigration.js +0 -237
  97. package/lib/cli/db/index.js +0 -66
  98. package/lib/cli/db/listAssetConfig.js +0 -16
  99. package/lib/cli/db/migrate.js +0 -15
  100. package/lib/cli/db/reset.js +0 -27
  101. package/lib/cli/db/rollback.js +0 -15
  102. package/lib/cli/db/seed.js +0 -104
  103. package/lib/cli/db/unlock.js +0 -15
  104. package/lib/cli/index.js +0 -90
  105. package/lib/controllers/AdminController.js +0 -258
  106. package/lib/controllers/CollectionController.js +0 -263
  107. package/lib/controllers/Controller.js +0 -462
  108. package/lib/controllers/ControllerAction.js +0 -276
  109. package/lib/controllers/MemberAction.js +0 -22
  110. package/lib/controllers/ModelController.js +0 -64
  111. package/lib/controllers/RelationController.js +0 -82
  112. package/lib/controllers/UserController.js +0 -98
  113. package/lib/controllers/index.js +0 -50
  114. package/lib/decorators/action.js +0 -14
  115. package/lib/decorators/authorize.js +0 -13
  116. package/lib/decorators/index.js +0 -58
  117. package/lib/decorators/parameters.js +0 -35
  118. package/lib/decorators/returns.js +0 -26
  119. package/lib/decorators/scope.js +0 -14
  120. package/lib/decorators/transacted.js +0 -12
  121. package/lib/errors/AssetError.js +0 -19
  122. package/lib/errors/AuthenticationError.js +0 -19
  123. package/lib/errors/AuthorizationError.js +0 -19
  124. package/lib/errors/ControllerError.js +0 -24
  125. package/lib/errors/DatabaseError.js +0 -27
  126. package/lib/errors/GraphError.js +0 -19
  127. package/lib/errors/ModelError.js +0 -24
  128. package/lib/errors/NotFoundError.js +0 -19
  129. package/lib/errors/NotImplementedError.js +0 -19
  130. package/lib/errors/QueryBuilderError.js +0 -19
  131. package/lib/errors/RelationError.js +0 -36
  132. package/lib/errors/ResponseError.js +0 -46
  133. package/lib/errors/ValidationError.js +0 -19
  134. package/lib/errors/WrappedError.js +0 -24
  135. package/lib/errors/index.js +0 -122
  136. package/lib/graph/DitoGraphProcessor.js +0 -185
  137. package/lib/graph/expression.js +0 -76
  138. package/lib/graph/graph.js +0 -300
  139. package/lib/graph/index.js +0 -34
  140. package/lib/index.js +0 -82
  141. package/lib/lib/EventEmitter.js +0 -76
  142. package/lib/lib/KnexHelper.js +0 -40
  143. package/lib/lib/index.js +0 -26
  144. package/lib/middleware/attachLogger.js +0 -16
  145. package/lib/middleware/createTransaction.js +0 -36
  146. package/lib/middleware/findRoute.js +0 -35
  147. package/lib/middleware/handleError.js +0 -26
  148. package/lib/middleware/handleRoute.js +0 -29
  149. package/lib/middleware/handleUser.js +0 -36
  150. package/lib/middleware/index.js +0 -66
  151. package/lib/middleware/logRequests.js +0 -122
  152. package/lib/mixins/AssetMixin.js +0 -81
  153. package/lib/mixins/SessionMixin.js +0 -22
  154. package/lib/mixins/TimeStampedMixin.js +0 -47
  155. package/lib/mixins/UserMixin.js +0 -151
  156. package/lib/mixins/index.js +0 -42
  157. package/lib/models/AssetModel.js +0 -12
  158. package/lib/models/Model.js +0 -953
  159. package/lib/models/RelationAccessor.js +0 -41
  160. package/lib/models/SessionModel.js +0 -12
  161. package/lib/models/TimeStampedModel.js +0 -12
  162. package/lib/models/UserModel.js +0 -12
  163. package/lib/models/definitions/assets.js +0 -11
  164. package/lib/models/definitions/filters.js +0 -101
  165. package/lib/models/definitions/hooks.js +0 -11
  166. package/lib/models/definitions/index.js +0 -38
  167. package/lib/models/definitions/modifiers.js +0 -11
  168. package/lib/models/definitions/options.js +0 -11
  169. package/lib/models/definitions/properties.js +0 -87
  170. package/lib/models/definitions/relations.js +0 -11
  171. package/lib/models/definitions/schema.js +0 -11
  172. package/lib/models/definitions/scopes.js +0 -51
  173. package/lib/models/index.js +0 -50
  174. package/lib/query/QueryBuilder.js +0 -745
  175. package/lib/query/QueryFilters.js +0 -82
  176. package/lib/query/QueryParameters.js +0 -77
  177. package/lib/query/Registry.js +0 -40
  178. package/lib/query/index.js +0 -34
  179. package/lib/schema/formats/_empty.js +0 -10
  180. package/lib/schema/formats/_required.js +0 -10
  181. package/lib/schema/formats/index.js +0 -26
  182. package/lib/schema/index.js +0 -49
  183. package/lib/schema/keywords/_computed.js +0 -11
  184. package/lib/schema/keywords/_foreign.js +0 -11
  185. package/lib/schema/keywords/_hidden.js +0 -11
  186. package/lib/schema/keywords/_index.js +0 -11
  187. package/lib/schema/keywords/_instanceof.js +0 -49
  188. package/lib/schema/keywords/_primary.js +0 -11
  189. package/lib/schema/keywords/_range.js +0 -26
  190. package/lib/schema/keywords/_relate.js +0 -19
  191. package/lib/schema/keywords/_specificType.js +0 -11
  192. package/lib/schema/keywords/_unique.js +0 -11
  193. package/lib/schema/keywords/_unsigned.js +0 -11
  194. package/lib/schema/keywords/_validate.js +0 -80
  195. package/lib/schema/keywords/index.js +0 -106
  196. package/lib/schema/properties.js +0 -227
  197. package/lib/schema/properties.test.js +0 -573
  198. package/lib/schema/relations.js +0 -274
  199. package/lib/schema/relations.test.js +0 -155
  200. package/lib/services/Service.js +0 -34
  201. package/lib/services/index.js +0 -18
  202. package/lib/storage/AssetFile.js +0 -97
  203. package/lib/storage/DiskStorage.js +0 -125
  204. package/lib/storage/S3Storage.js +0 -171
  205. package/lib/storage/Storage.js +0 -209
  206. package/lib/storage/index.js +0 -34
  207. package/lib/utils/decorator.js +0 -16
  208. package/lib/utils/deprecate.js +0 -46
  209. package/lib/utils/emitter.js +0 -13
  210. package/lib/utils/function.js +0 -14
  211. package/lib/utils/function.test.js +0 -49
  212. package/lib/utils/index.js +0 -66
  213. package/lib/utils/json.js +0 -9
  214. package/lib/utils/object.js +0 -92
  215. package/lib/utils/object.test.js +0 -65
  216. package/lib/utils/scope.js +0 -14
package/package.json CHANGED
@@ -1,24 +1,17 @@
1
1
  {
2
+ "type": "module",
2
3
  "name": "@ditojs/server",
3
- "version": "0.275.0",
4
+ "version": "1.0.0-rc.0",
4
5
  "description": "Dito.js Server – Dito.js is a declarative and modern web framework, based on Objection.js, Koa.js and Vue.js",
5
- "main": "lib/index.js",
6
6
  "repository": "https://github.com/ditojs/dito/tree/master/packages/server",
7
7
  "author": "Jürg Lehni <juerg@scratchdisk.com> (http://scratchdisk.com)",
8
8
  "license": "MIT",
9
+ "main": "./src/index.js",
9
10
  "files": [
10
- "src/",
11
- "lib/"
11
+ "src/"
12
12
  ],
13
- "scripts": {
14
- "knex": "babel-node node_modules/.bin/knex",
15
- "clean": "rimraf lib",
16
- "build": "babel src --out-dir lib --copy-files",
17
- "watch": "yarn build --watch",
18
- "prepare": "yarn clean && yarn build"
19
- },
20
13
  "bin": {
21
- "dito": "./lib/cli/index.js"
14
+ "dito": "./src/cli/index.js"
22
15
  },
23
16
  "engines": {
24
17
  "node": ">= 14.0.0",
@@ -28,27 +21,23 @@
28
21
  "node >= 14"
29
22
  ],
30
23
  "dependencies": {
31
- "@babel/core": "^7.17.5",
32
- "@babel/runtime": "^7.17.2",
33
- "@ditojs/admin": "^0.275.0",
34
- "@ditojs/router": "^0.275.0",
35
- "@ditojs/utils": "^0.275.0",
36
- "@koa/cors": "^3.1.0",
24
+ "@ditojs/admin": "^1.0.0-rc.0",
25
+ "@ditojs/router": "^1.0.0-rc.0",
26
+ "@ditojs/utils": "^1.0.0-rc.0",
27
+ "@koa/cors": "^3.2.0",
37
28
  "@koa/multer": "^3.0.0",
38
- "@vue/cli-service": "^4.5.15",
29
+ "@originjs/vite-plugin-commonjs": "^1.0.3",
39
30
  "ajv": "^7.2.4",
40
31
  "ajv-formats": "^1.6.1",
41
- "aws-sdk": "^2.1087.0",
42
- "axios": "^0.26.0",
43
- "babel-loader": "^8.2.3",
32
+ "aws-sdk": "^2.1093.0",
33
+ "axios": "^0.26.1",
44
34
  "bcryptjs": "^2.4.3",
45
35
  "bytes": "^3.1.2",
46
- "core-js": "^3.21.1",
47
- "data-uri-to-buffer": "^3.0.1",
36
+ "data-uri-to-buffer": "^4.0.0",
48
37
  "eventemitter2": "^6.4.5",
49
- "file-type": "^16.5.3",
38
+ "file-type": "^17.1.1",
39
+ "find-up": "^6.3.0",
50
40
  "fs-extra": "^10.0.1",
51
- "html-webpack-tags-plugin": "^2.0.17",
52
41
  "image-size": "^1.0.1",
53
42
  "is-svg": "^4.3.2",
54
43
  "koa": "^2.13.4",
@@ -56,7 +45,6 @@
56
45
  "koa-compose": "^4.1.0",
57
46
  "koa-compress": "^5.1.0",
58
47
  "koa-conditional-get": "^3.0.0",
59
- "koa-connect-history-api-fallback": "^0.3.1",
60
48
  "koa-etag": "^4.0.0",
61
49
  "koa-helmet": "^6.1.0",
62
50
  "koa-mount": "^4.0.0",
@@ -64,8 +52,8 @@
64
52
  "koa-response-time": "^2.1.0",
65
53
  "koa-session": "^6.2.0",
66
54
  "koa-static": "^5.0.0",
67
- "koa-webpack": "^6.0.0",
68
- "mime-types": "^2.1.34",
55
+ "mime-types": "^2.1.35",
56
+ "minimatch": "^5.0.1",
69
57
  "multer": "^1.4.4",
70
58
  "multer-s3": "^2.10.0",
71
59
  "nanoid": "^3.3.1",
@@ -73,32 +61,26 @@
73
61
  "passport-local": "^1.0.0",
74
62
  "passthrough-counter": "^1.0.0",
75
63
  "picocolors": "^1.0.0",
76
- "pino": "^6.14.0",
77
- "pino-pretty": "^6.0.0",
64
+ "picomatch": "^2.3.1",
65
+ "pino": "^7.8.1",
66
+ "pino-pretty": "^7.5.3",
78
67
  "pluralize": "^8.0.0",
79
68
  "repl": "^0.1.3",
80
- "source-map-loader": "1.1.3",
81
69
  "uuid": "^8.3.2",
70
+ "vite": "^2.8.6",
71
+ "vite-plugin-vue2": "^1.9.3",
82
72
  "vue": "^2.6.14",
83
- "vue-template-compiler": "^2.6.14",
84
- "webpack": "^4.46.0",
85
- "webpack-hot-client": "^4.2.0"
73
+ "vue-template-compiler": "^2.6.14"
86
74
  },
87
75
  "peerDependencies": {
88
76
  "knex": "^0.21.0",
89
77
  "objection": "^2.2.0"
90
78
  },
91
79
  "devDependencies": {
92
- "@babel/cli": "^7.17.6",
93
- "@babel/preset-env": "^7.16.11",
94
- "@ditojs/babel-preset": "^0.275.0",
95
- "babel-plugin-dynamic-import-node": "^2.3.3",
96
- "babel-plugin-module-resolver": "^4.1.0",
97
- "knex": "^0.21.21",
80
+ "knex": "^0.21.17",
98
81
  "objection": "^2.2.18",
99
82
  "pg": "^8.7.3",
100
- "rimraf": "^3.0.2",
101
83
  "sqlite3": "^5.0.2"
102
84
  },
103
- "gitHead": "b1ee07dad73b0e3408cd90798deb7608c8ef096d"
85
+ "gitHead": "e643586e4b26a850f54c52a1f1bf8f0421c05d31"
104
86
  }
@@ -12,26 +12,31 @@ import cors from '@koa/cors'
12
12
  import compose from 'koa-compose'
13
13
  import compress from 'koa-compress'
14
14
  import conditional from 'koa-conditional-get'
15
+ import mount from 'koa-mount'
15
16
  import passport from 'koa-passport'
16
17
  import session from 'koa-session'
17
18
  import etag from 'koa-etag'
18
19
  import helmet from 'koa-helmet'
19
20
  import responseTime from 'koa-response-time'
20
21
  import Router from '@ditojs/router'
21
- import { EventEmitter } from '@/lib'
22
- import { Controller, AdminController } from '@/controllers'
23
- import { Service } from '@/services'
24
- import { Storage } from '@/storage'
25
- import { convertSchema } from '@/schema'
26
- import { formatJson } from '@/utils'
22
+ import {
23
+ isArray, isObject, isString, asArray, isPlainObject, hyphenate, clone, merge,
24
+ parseDataPath, normalizeDataPath, isModule
25
+ } from '@ditojs/utils'
26
+ import SessionStore from './SessionStore.js'
27
+ import { Validator } from './Validator.js'
28
+ import { EventEmitter } from '../lib/index.js'
29
+ import { Controller, AdminController } from '../controllers/index.js'
30
+ import { Service } from '../services/index.js'
31
+ import { Storage } from '../storage/index.js'
32
+ import { convertSchema } from '../schema/index.js'
33
+ import { formatJson } from '../utils/index.js'
27
34
  import {
28
35
  ResponseError,
29
36
  ValidationError,
30
37
  DatabaseError,
31
38
  AssetError
32
- } from '@/errors'
33
- import SessionStore from './SessionStore'
34
- import { Validator } from './Validator'
39
+ } from '../errors/index.js'
35
40
  import {
36
41
  attachLogger,
37
42
  createTransaction,
@@ -40,15 +45,12 @@ import {
40
45
  handleRoute,
41
46
  handleUser,
42
47
  logRequests
43
- } from '@/middleware'
44
- import {
45
- isArray, isObject, isString, asArray, isPlainObject, hyphenate, clone, merge,
46
- parseDataPath, normalizeDataPath
47
- } from '@ditojs/utils'
48
- import {
48
+ } from '../middleware/index.js'
49
+ import objection, {
49
50
  Model,
50
51
  BelongsToOneRelation,
51
- knexSnakeCaseMappers,
52
+ // TODO: Import directly once we can move to Objection 3
53
+ // knexSnakeCaseMappers,
52
54
  ref
53
55
  } from 'objection'
54
56
 
@@ -85,13 +87,10 @@ export class Application extends Koa {
85
87
  this.models = Object.create(null)
86
88
  this.services = Object.create(null)
87
89
  this.controllers = Object.create(null)
88
- this.hasControllerMiddleware = false
89
90
  this.setupLogger()
90
91
  this.setupKnex()
91
- this.setupGlobalMiddleware()
92
- if (middleware) {
93
- this.use(middleware)
94
- }
92
+ this.setupMiddleware(middleware)
93
+
95
94
  if (config.storages) {
96
95
  this.addStorages(config.storages)
97
96
  }
@@ -106,20 +105,24 @@ export class Application extends Koa {
106
105
  }
107
106
  }
108
107
 
109
- addRoute(verb, path, transacted, handlers, controller = null, action = null) {
110
- handlers = asArray(handlers)
111
- const handler = handlers.length > 1 ? compose(handlers) : handlers[0]
108
+ addRoute(
109
+ method, path, transacted, middlewares, controller = null, action = null
110
+ ) {
111
+ middlewares = asArray(middlewares)
112
+ const middleware = middlewares.length > 1
113
+ ? compose(middlewares)
114
+ : middlewares[0]
112
115
  // Instead of directly passing `handler`, pass a `route` object that also
113
116
  // will be exposed through `ctx.route`, see `routerHandler()`:
114
117
  const route = {
115
- verb,
118
+ method,
116
119
  path,
117
120
  transacted,
118
- handler,
121
+ middleware,
119
122
  controller,
120
123
  action
121
124
  }
122
- this.router[verb](path, route)
125
+ this.router[method](path, route)
123
126
  }
124
127
 
125
128
  addModels(models) {
@@ -235,12 +238,7 @@ export class Application extends Koa {
235
238
 
236
239
  addServices(services) {
237
240
  for (const [name, service] of Object.entries(services)) {
238
- // Handle ES6 module weirdness that can happen, apparently:
239
- if (name === 'default' && isPlainObject(service)) {
240
- this.addServices(service)
241
- } else {
242
- this.addService(service, name)
243
- }
241
+ this.addService(service, name)
244
242
  }
245
243
  }
246
244
 
@@ -285,7 +283,7 @@ export class Application extends Koa {
285
283
 
286
284
  addControllers(controllers, namespace) {
287
285
  for (const [key, value] of Object.entries(controllers)) {
288
- if (isPlainObject(value)) {
286
+ if (isModule(value) || isPlainObject(value)) {
289
287
  this.addControllers(value, namespace ? `${namespace}/${key}` : key)
290
288
  } else {
291
289
  this.addController(value, namespace)
@@ -294,8 +292,6 @@ export class Application extends Koa {
294
292
  }
295
293
 
296
294
  addController(controller, namespace) {
297
- // Controllers require additional middleware to be installed once.
298
- this.setupControllerMiddleware()
299
295
  // Auto-instantiate controller classes:
300
296
  if (Controller.isPrototypeOf(controller)) {
301
297
  // eslint-disable-next-line new-cap
@@ -311,11 +307,11 @@ export class Application extends Koa {
311
307
  // Now that the controller is set up, call `initialize()` which can be
312
308
  // overridden by controllers.
313
309
  controller.initialize()
314
- // Each controller can also provide further middleware, e.g.
315
- // `AdminController`:
316
- const middleware = controller.compose()
317
- if (middleware) {
318
- this.use(middleware)
310
+ // Each controller can also compose their own middleware (or app), e.g. as
311
+ // used in `AdminController`:
312
+ const composed = controller.compose()
313
+ if (composed) {
314
+ this.use(mount(controller.url, composed))
319
315
  }
320
316
  }
321
317
 
@@ -333,8 +329,8 @@ export class Application extends Koa {
333
329
  )
334
330
  }
335
331
 
336
- getAdminVueConfig() {
337
- return this.getAdminController()?.getVueConfig() || null
332
+ getAdminViteConfig(config) {
333
+ return this.getAdminController()?.getViteConfig(config) || null
338
334
  }
339
335
 
340
336
  getAssetConfig({
@@ -501,18 +497,19 @@ export class Application extends Koa {
501
497
  })
502
498
  }
503
499
 
504
- setupGlobalMiddleware() {
500
+ setupMiddleware(middleware) {
505
501
  const { app, log } = this.config
506
502
 
507
- this.use(attachLogger(this.logger))
503
+ // Setup global middleware
508
504
 
505
+ this.use(attachLogger(this.logger))
509
506
  if (app.responseTime !== false) {
510
507
  this.use(responseTime(getOptions(app.responseTime)))
511
508
  }
512
509
  if (log.requests) {
513
510
  this.use(logRequests())
514
511
  }
515
- // Needs to be positioned after the request logger to log the correct
512
+ // This needs to be positioned after the request logger to log the correct
516
513
  // response status.
517
514
  this.use(handleError())
518
515
  if (app.helmet !== false) {
@@ -539,50 +536,44 @@ export class Application extends Koa {
539
536
  this.use(conditional())
540
537
  this.use(etag())
541
538
  }
542
- }
543
539
 
544
- setupControllerMiddleware() {
545
- // NOTE: This is not part of the automatic `setupGlobalMiddleware()` so that
546
- // apps can set up the static serving of assets before installing the
547
- // session and passport middleware. It is called from `addController()`.
548
- // Use a flag to only install the middleware once:
549
- if (!this.hasControllerMiddleware) {
550
- const { app } = this.config
551
- // Sequence is important:
552
- // 1. body parser
553
- this.use(bodyParser(getOptions(app.bodyParser)))
554
- // 2. find route from routes installed by controllers.
555
- this.use(findRoute(this.router))
556
- // 3. respect transacted settings, create and handle transactions.
557
- this.use(createTransaction())
558
- // 4. session
559
- if (app.session) {
560
- const {
561
- modelClass,
562
- ...options
563
- } = getOptions(app.session)
564
- if (modelClass) {
565
- // Create a ContextStore that resolved the specified model class,
566
- // uses it to persist and retrieve the session, and automatically
567
- // binds all db operations to `ctx.transaction`, if it is set.
568
- // eslint-disable-next-line new-cap
569
- options.ContextStore = SessionStore(modelClass)
570
- }
571
- this.use(session(options, this))
540
+ // Controller-specific middleware
541
+
542
+ // 1. Find route from routes installed by controllers.
543
+ this.use(findRoute(this.router))
544
+ // 2. Additional, user-space application-level middleware.
545
+ if (middleware) {
546
+ this.use(middleware)
547
+ }
548
+ // 3. body parser
549
+ this.use(bodyParser(getOptions(app.bodyParser)))
550
+ // 4. respect transacted settings, create and handle transactions.
551
+ this.use(createTransaction())
552
+ // 5. session
553
+ if (app.session) {
554
+ const {
555
+ modelClass,
556
+ ...options
557
+ } = getOptions(app.session)
558
+ if (modelClass) {
559
+ // Create a ContextStore that resolved the specified model class,
560
+ // uses it to persist and retrieve the session, and automatically
561
+ // binds all db operations to `ctx.transaction`, if it is set.
562
+ // eslint-disable-next-line new-cap
563
+ options.ContextStore = SessionStore(modelClass)
572
564
  }
573
- // 5. passport
574
- if (app.passport) {
575
- this.use(passport.initialize())
576
- if (app.session) {
577
- this.use(passport.session())
578
- }
579
- this.use(handleUser())
565
+ this.use(session(options, this))
566
+ }
567
+ // 6. passport
568
+ if (app.passport) {
569
+ this.use(passport.initialize())
570
+ if (app.session) {
571
+ this.use(passport.session())
580
572
  }
581
-
582
- // 6. finally handle the found route, or set status / allow accordingly.
583
- this.use(handleRoute())
584
- this.hasControllerMiddleware = true
573
+ this.use(handleUser())
585
574
  }
575
+ // 6. finally handle the found route, or set status / allow accordingly.
576
+ this.use(handleRoute())
586
577
  }
587
578
 
588
579
  setupLogger() {
@@ -591,26 +582,32 @@ export class Application extends Koa {
591
582
  const user = user => ({ id: user.id })
592
583
  const serializers = { err, req, res, user }
593
584
 
594
- const logger = pino(merge(
595
- {
596
- level: 'info',
597
- serializers,
598
- prettyPrint: {
599
- // List of keys to ignore in pretty mode.
600
- ignore: 'req,res,durationMs,user,requestId',
601
- // SYS to use system time and not UTC.
602
- translateTime: 'SYS:HH:MM:ss.l'
603
- },
604
- // Redact common sensitive headers.
605
- redact: [
606
- '*.headers["cookie"]',
607
- '*.headers["set-cookie"]',
608
- '*.headers["authorization"]'
609
- ],
610
- base: null // no pid,hostname,name
585
+ const { prettyPrint, ...options } = merge({
586
+ level: 'info',
587
+ serializers,
588
+ prettyPrint: {
589
+ colorize: true,
590
+ // List of keys to ignore in pretty mode.
591
+ ignore: 'req,res,durationMs,user,requestId',
592
+ // SYS to use system time and not UTC.
593
+ translateTime: 'SYS:HH:MM:ss.l'
611
594
  },
612
- getOptions(this.config.logger)
613
- ))
595
+ // Redact common sensitive headers.
596
+ redact: [
597
+ '*.headers["cookie"]',
598
+ '*.headers["set-cookie"]',
599
+ '*.headers["authorization"]'
600
+ ],
601
+ base: null // no pid,hostname,name
602
+ }, getOptions(this.config.logger))
603
+
604
+ const transport = prettyPrint
605
+ ? pino.transport({
606
+ target: 'pino-pretty',
607
+ options: prettyPrint
608
+ }) : null
609
+
610
+ const logger = pino(options, transport)
614
611
 
615
612
  this.logger = logger.child({ name: 'app' })
616
613
  }
@@ -624,7 +621,7 @@ export class Application extends Koa {
624
621
  if (snakeCaseOptions) {
625
622
  knex = {
626
623
  ...knex,
627
- ...knexSnakeCaseMappers(snakeCaseOptions)
624
+ ...objection.knexSnakeCaseMappers(snakeCaseOptions)
628
625
  }
629
626
  }
630
627
  this.knex = Knex(knex)
@@ -716,15 +713,12 @@ export class Application extends Koa {
716
713
  }
717
714
  await this.emit('before:start')
718
715
  await this.forEachService(service => service.start())
719
- const {
720
- server: { host, port },
721
- env
722
- } = this.config
716
+ const { server: { host, port } } = this.config
723
717
  this.server = await new Promise((resolve, reject) => {
724
718
  const server = this.listen(port, host, () => {
725
719
  const { port } = server.address()
726
720
  console.info(
727
- `${env} server started at http://${host}:${port}`
721
+ `Dito server started at http://${host}:${port}`
728
722
  )
729
723
  resolve(server)
730
724
  })
@@ -1,9 +1,12 @@
1
1
  import objection from 'objection'
2
- import Ajv from 'ajv'
2
+ import ajv from 'ajv'
3
3
  import addFormats from 'ajv-formats'
4
4
  import { isArray, isObject, clone, isAsync, isPromise } from '@ditojs/utils'
5
- import { formatJson } from '@/utils'
6
- import * as schema from '@/schema'
5
+ import { formatJson } from '../utils/index.js'
6
+ import * as schema from '../schema/index.js'
7
+
8
+ // TODO: Switch to direct import once Ajv is updated / supports ESM.
9
+ const Ajv = ajv.default
7
10
 
8
11
  // Dito does not rely on objection.AjvValidator but instead implements its own
9
12
  // validator instance that is shared across the whole app and handles schema
package/src/app/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export * from './Application'
2
- export * from './Validator'
1
+ export * from './Application.js'
2
+ export * from './Validator.js'
@@ -1,7 +1,7 @@
1
1
  import path from 'path'
2
2
  import fs from 'fs-extra'
3
3
  import pico from 'picocolors'
4
- import { getRelationClass, isThroughRelationClass } from '@/schema'
4
+ import { getRelationClass, isThroughRelationClass } from '@ditojs/server'
5
5
  import {
6
6
  isObject, isArray, isString, deindent, capitalize
7
7
  } from '@ditojs/utils'
@@ -1,7 +1,7 @@
1
- export * from './createMigration'
2
- export * from './listAssetConfig'
3
- export * from './migrate'
4
- export * from './rollback'
5
- export * from './reset'
6
- export * from './seed'
7
- export * from './unlock'
1
+ export * from './createMigration.js'
2
+ export * from './listAssetConfig.js'
3
+ export * from './migrate.js'
4
+ export * from './rollback.js'
5
+ export * from './reset.js'
6
+ export * from './seed.js'
7
+ export * from './unlock.js'
@@ -1,4 +1,4 @@
1
- import { formatJson } from '@/utils'
1
+ import { formatJson } from '@ditojs/server'
2
2
 
3
3
  export async function listAssetConfig(app, ...args) {
4
4
  const assetConfig = app.getAssetConfig({
@@ -1,5 +1,5 @@
1
1
  import pico from 'picocolors'
2
- import { migrate } from './migrate'
2
+ import { migrate } from './migrate.js'
3
3
 
4
4
  export async function reset(knex) {
5
5
  const batches = []
package/src/cli/index.js CHANGED
@@ -1,11 +1,16 @@
1
- #!/usr/bin/env babel-node
1
+ #!/usr/bin/env node
2
2
 
3
3
  import path from 'path'
4
+ import fs from 'fs'
4
5
  import pico from 'picocolors'
5
6
  import Knex from 'knex'
6
7
  import { isPlainObject, isFunction, camelize } from '@ditojs/utils'
7
- import * as db from './db'
8
- import startConsole from './console'
8
+ import * as db from './db/index.js'
9
+ import startConsole from './console.js'
10
+
11
+ // Tell Knex to use ESM. TODO: Fix this properly in Knex itself, so it works
12
+ // with Yarn too.
13
+ process.env.npm_package_type = 'module'
9
14
 
10
15
  const commands = { db, console: startConsole }
11
16
 
@@ -56,8 +61,12 @@ async function execute() {
56
61
  }
57
62
  }
58
63
 
59
- // Start the console if `node ./lib/cli/index.js`
60
- if (require.main === module) {
64
+ // Start the console if `node ./cli/index.js`
65
+
66
+ // See module was not imported but called directly
67
+ const path1 = fs.realpathSync(import.meta.url.replace(/^file:\/\//, ''))
68
+ const path2 = fs.realpathSync(process.argv[1])
69
+ if (path1 === path2) {
61
70
  execute()
62
71
  }
63
72