@ditojs/server 0.275.0 → 1.0.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 +181 -158
  11. package/src/controllers/CollectionController.js +8 -29
  12. package/src/controllers/Controller.js +71 -76
  13. package/src/controllers/ControllerAction.js +39 -17
  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
@@ -1,66 +1,66 @@
1
- import Registry from './Registry'
2
- import { parameters } from '@/decorators'
3
-
1
+ import Registry from './Registry.js'
4
2
  export const QueryFilters = new Registry()
5
3
 
6
4
  QueryFilters.register({
7
- @parameters([
8
- {
9
- name: 'operator',
10
- type: 'string'
5
+ 'text': {
6
+ parameters: {
7
+ operator: {
8
+ type: 'string'
9
+ },
10
+ text: {
11
+ type: 'string'
12
+ }
11
13
  },
12
- {
13
- name: 'text',
14
- type: 'string'
15
- }
16
- ])
17
- text(query, property, operator, text) {
18
- if (text === undefined) {
19
- text = operator
20
- operator = 'contains'
21
- }
22
- const templates = {
23
- 'equals': text => text,
24
- 'contains': text => `%${text}%`,
25
- 'starts-with': text => `${text}%`,
26
- 'ends-with': text => `%${text}`
27
- }
28
- if (text) {
29
- const operand = templates[operator]?.(text)
30
- if (operand) {
31
- if (query.isPostgreSQL()) {
32
- query.where(property, 'ILIKE', operand)
33
- } else {
34
- query.whereRaw(
14
+
15
+ handler(query, property, { operator, text }) {
16
+ if (text === undefined) {
17
+ text = operator
18
+ operator = 'contains'
19
+ }
20
+ const templates = {
21
+ 'equals': text => text,
22
+ 'contains': text => `%${text}%`,
23
+ 'starts-with': text => `${text}%`,
24
+ 'ends-with': text => `%${text}`
25
+ }
26
+ if (text) {
27
+ const operand = templates[operator]?.(text)
28
+ if (operand) {
29
+ if (query.isPostgreSQL()) {
30
+ query.where(property, 'ILIKE', operand)
31
+ } else {
32
+ query.whereRaw(
35
33
  `LOWER(??) LIKE ?`,
36
34
  [property, operand.toLowerCase()]
37
- )
35
+ )
36
+ }
38
37
  }
39
38
  }
40
39
  }
41
40
  },
42
41
 
43
- @parameters([
44
- {
45
- name: 'from',
46
- type: 'datetime',
47
- nullable: true
42
+ 'date-range': {
43
+ parameters: {
44
+ from: {
45
+ type: 'datetime',
46
+ nullable: true
47
+ },
48
+ to: {
49
+ type: 'datetime',
50
+ nullable: true
51
+ }
48
52
  },
49
- {
50
- name: 'to',
51
- type: 'datetime',
52
- nullable: true
53
- }
54
- ])
55
- 'date-range'(query, property, from, to) {
56
- if (from && to) {
57
- query.whereBetween(property, [new Date(from), new Date(to)])
58
- } else if (from) {
59
- query.where(property, '>=', new Date(from))
60
- } else if (to) {
61
- query.where(property, '<=', new Date(to))
62
- } else {
53
+
54
+ handler(query, property, { from, to }) {
55
+ if (from && to) {
56
+ query.whereBetween(property, [new Date(from), new Date(to)])
57
+ } else if (from) {
58
+ query.where(property, '>=', new Date(from))
59
+ } else if (to) {
60
+ query.where(property, '<=', new Date(to))
61
+ } else {
63
62
  // TODO: Can we get validation to catch the case where both are empty?
63
+ }
64
64
  }
65
65
  }
66
66
  })
@@ -1,6 +1,6 @@
1
- import { ResponseError, QueryBuilderError } from '@/errors'
2
1
  import { isString, asArray } from '@ditojs/utils'
3
- import Registry from './Registry'
2
+ import Registry from './Registry.js'
3
+ import { ResponseError, QueryBuilderError } from '../errors/index.js'
4
4
 
5
5
  export const QueryParameters = new Registry()
6
6
 
@@ -1,3 +1,3 @@
1
- export * from './QueryBuilder'
2
- export * from './QueryFilters'
3
- export * from './QueryParameters'
1
+ export * from './QueryBuilder.js'
2
+ export * from './QueryFilters.js'
3
+ export * from './QueryParameters.js'
@@ -1,2 +1,2 @@
1
- export * from './_empty'
2
- export * from './_required'
1
+ export * from './_empty.js'
2
+ export * from './_required.js'
@@ -1,4 +1,4 @@
1
- export * as keywords from './keywords'
2
- export * as formats from './formats'
3
- export * from './properties'
4
- export * from './relations'
1
+ export * as keywords from './keywords/index.js'
2
+ export * as formats from './formats/index.js'
3
+ export * from './properties.js'
4
+ export * from './relations.js'
@@ -1,5 +1,5 @@
1
1
  // This keyword is used to validate reference-only data in relations: { id: 1 }
2
- // See @/schema/relation.js for more details
2
+ // See ../schema/relation.js for more details
3
3
  export const relate = {
4
4
  silent: true,
5
5
 
@@ -1,12 +1,12 @@
1
- export * from './_specificType'
2
- export * from './_primary'
3
- export * from './_foreign'
4
- export * from './_unique'
5
- export * from './_index'
6
- export * from './_computed'
7
- export * from './_hidden'
8
- export * from './_unsigned'
9
- export * from './_instanceof'
10
- export * from './_validate'
11
- export * from './_relate'
12
- export * from './_range'
1
+ export * from './_specificType.js'
2
+ export * from './_primary.js'
3
+ export * from './_foreign.js'
4
+ export * from './_unique.js'
5
+ export * from './_index.js'
6
+ export * from './_computed.js'
7
+ export * from './_hidden.js'
8
+ export * from './_unsigned.js'
9
+ export * from './_instanceof.js'
10
+ export * from './_validate.js'
11
+ export * from './_relate.js'
12
+ export * from './_range.js'
@@ -1,4 +1,4 @@
1
- import { convertSchema, expandSchemaShorthand } from './properties'
1
+ import { convertSchema, expandSchemaShorthand } from './properties.js'
2
2
 
3
3
  describe('convertSchema()', () => {
4
4
  it('expands objects with properties to full JSON schemas', () => {
@@ -6,10 +6,10 @@ import {
6
6
  HasManyRelation,
7
7
  ManyToManyRelation
8
8
  } from 'objection'
9
- import { RelationError } from '@/errors'
10
9
  import {
11
10
  isObject, isArray, isString, asArray, capitalize, camelize
12
11
  } from '@ditojs/utils'
12
+ import { RelationError } from '../errors/index.js'
13
13
 
14
14
  const relationLookup = {
15
15
  // one:
@@ -5,10 +5,10 @@ import {
5
5
  HasManyRelation,
6
6
  ManyToManyRelation
7
7
  } from 'objection'
8
- import { Model } from '@/models'
8
+ import { Model } from '../models.js'
9
9
  import {
10
10
  getRelationClass, convertRelation, addRelationSchemas
11
- } from './relations'
11
+ } from './relations.js'
12
12
 
13
13
  describe('getRelationClass()', () => {
14
14
  it('returns the corresponding relation classes', () => {
@@ -1 +1 @@
1
- export * from './Service'
1
+ export * from './Service.js'
@@ -1,7 +1,7 @@
1
1
  import fs from 'fs-extra'
2
2
  import path from 'path'
3
3
  import multer from '@koa/multer'
4
- import { Storage } from './Storage'
4
+ import { Storage } from './Storage.js'
5
5
 
6
6
  export class DiskStorage extends Storage {
7
7
  static type = 'disk'
@@ -1,8 +1,8 @@
1
1
  import aws from 'aws-sdk'
2
2
  import multerS3 from 'multer-s3'
3
- import FileType from 'file-type'
3
+ import { fileTypeFromBuffer } from 'file-type'
4
4
  import isSvg from 'is-svg'
5
- import { Storage } from './Storage'
5
+ import { Storage } from './Storage.js'
6
6
  import { PassThrough } from 'stream'
7
7
 
8
8
  export class S3Storage extends Storage {
@@ -49,7 +49,7 @@ export class S3Storage extends Storage {
49
49
  const onData = chunk => {
50
50
  if (!data) {
51
51
  // 2. Try reading the mimetype from the first chunk.
52
- const type = FileType.fromBuffer(chunk)?.mime
52
+ const type = fileTypeFromBuffer(chunk)?.mime
53
53
  if (type) {
54
54
  stream.off('data', onData)
55
55
  done(type)
@@ -58,7 +58,7 @@ export class S3Storage extends Storage {
58
58
  // the mimetype using the full data.
59
59
  stream.once('end', () => {
60
60
  const type = (
61
- FileType.fromBuffer(data)?.mime ||
61
+ fileTypeFromBuffer(data)?.mime ||
62
62
  (isSvg(data) ? 'image/svg+xml' : 'application/octet-stream')
63
63
  )
64
64
  done(type)
@@ -4,7 +4,7 @@ import imageSize from 'image-size'
4
4
  import { PassThrough } from 'stream'
5
5
  import { URL } from 'url'
6
6
  import { hyphenate, toPromiseCallback } from '@ditojs/utils'
7
- import { AssetFile } from './AssetFile'
7
+ import { AssetFile } from './AssetFile.js'
8
8
 
9
9
  const storageClasses = {}
10
10
 
@@ -1,9 +1,9 @@
1
- import { Storage } from './Storage'
2
- import { DiskStorage } from './DiskStorage'
3
- import { S3Storage } from './S3Storage'
1
+ import { Storage } from './Storage.js'
2
+ import { DiskStorage } from './DiskStorage.js'
3
+ import { S3Storage } from './S3Storage.js'
4
4
 
5
5
  Storage.register(DiskStorage)
6
6
  Storage.register(S3Storage)
7
7
 
8
- export * from './AssetFile'
8
+ export * from './AssetFile.js'
9
9
  export { Storage }
@@ -1,4 +1,4 @@
1
- import { describeFunction } from './function'
1
+ import { describeFunction } from './function.js'
2
2
 
3
3
  describe('describeFunction()', () => {
4
4
  it('describes normal functions', () => {
@@ -0,0 +1,17 @@
1
+ import { asArray } from '@ditojs/utils'
2
+
3
+ export function processHandlerParameters(handler, name, value) {
4
+ if (value) {
5
+ const [schema, options] = asArray(value)
6
+ handler[name] = schema
7
+
8
+ // If validation options are provided, expose them through
9
+ // `handler.options[name]`, see `ControllerAction`.
10
+ if (options) {
11
+ handler.options = {
12
+ ...handler.options,
13
+ [name]: options
14
+ }
15
+ }
16
+ }
17
+ }
@@ -1,7 +1,8 @@
1
- export * from './decorator'
2
- export * from './deprecate'
3
- export * from './emitter'
4
- export * from './function'
5
- export * from './json'
6
- export * from './object'
7
- export * from './scope'
1
+ export * from './decorator.js'
2
+ export * from './deprecate.js'
3
+ export * from './emitter.js'
4
+ export * from './function.js'
5
+ export * from './handler.js'
6
+ export * from './json.js'
7
+ export * from './object.js'
8
+ export * from './scope.js'
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  getAllKeys, getOwnProperty, createLookup, mergeReversed, mergeAsReversedArrays
3
- } from './object'
3
+ } from './object.js'
4
4
 
5
5
  const object = Object.create({ a: 1 })
6
6
  object.b = 2