@contember/schema-definition 1.3.0-beta.4 → 1.3.0-rc.2

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 (28) hide show
  1. package/dist/src/acl/definition/internal/AclFactory.d.ts.map +1 -1
  2. package/dist/src/acl/definition/internal/AclFactory.js +2 -5
  3. package/dist/src/acl/definition/internal/AclFactory.js.map +1 -1
  4. package/dist/src/acl/definition/internal/EntityPredicateResolver.d.ts +1 -1
  5. package/dist/src/acl/definition/internal/EntityPredicateResolver.d.ts.map +1 -1
  6. package/dist/src/acl/definition/internal/EntityPredicateResolver.js +8 -5
  7. package/dist/src/acl/definition/internal/EntityPredicateResolver.js.map +1 -1
  8. package/dist/src/acl/definition/permissions.d.ts +3 -3
  9. package/dist/src/acl/definition/permissions.d.ts.map +1 -1
  10. package/dist/src/model/definition/OrderByDefinition.d.ts +4 -2
  11. package/dist/src/model/definition/OrderByDefinition.d.ts.map +1 -1
  12. package/dist/src/model/definition/OrderByDefinition.js +18 -5
  13. package/dist/src/model/definition/OrderByDefinition.js.map +1 -1
  14. package/dist/src/tsconfig.tsbuildinfo +1 -1
  15. package/dist/tests/cases/unit/orderBy.test.d.ts +2 -0
  16. package/dist/tests/cases/unit/orderBy.test.d.ts.map +1 -0
  17. package/dist/tests/cases/unit/orderBy.test.js +102 -0
  18. package/dist/tests/cases/unit/orderBy.test.js.map +1 -0
  19. package/dist/tests/cases/unit/readAclDefinitions.test.js +26 -0
  20. package/dist/tests/cases/unit/readAclDefinitions.test.js.map +1 -1
  21. package/dist/tests/tsconfig.tsbuildinfo +1 -1
  22. package/package.json +3 -3
  23. package/src/acl/definition/internal/AclFactory.ts +2 -5
  24. package/src/acl/definition/internal/EntityPredicateResolver.ts +13 -5
  25. package/src/acl/definition/permissions.ts +3 -3
  26. package/src/model/definition/OrderByDefinition.ts +23 -8
  27. package/tests/cases/unit/orderBy.test.ts +88 -0
  28. package/tests/cases/unit/readAclDefinitions.test.ts +24 -0
@@ -3,22 +3,37 @@ import { extendEntity } from './extensions'
3
3
  import { DecoratorFunction } from '../../utils'
4
4
 
5
5
 
6
- export type OrderByOptions = { path: string[]; direction?: Model.OrderDirection | `${Model.OrderDirection}` }
6
+ export type OrderByDirection = Model.OrderDirection | `${Model.OrderDirection}`
7
+ export type OrderByOptions = { path: string[]; direction?: OrderByDirection }
7
8
 
8
9
 
9
10
  export function OrderBy<T>(options: OrderByOptions): DecoratorFunction<T>
10
- export function OrderBy<T>(path: (keyof T), direction?: Model.OrderDirection | `${Model.OrderDirection}`): DecoratorFunction<T>
11
- export function OrderBy<T>(options: OrderByOptions | keyof T, direction?: `${Model.OrderDirection}`): DecoratorFunction<T> {
11
+ export function OrderBy<T>(options: OrderByOptions[]): DecoratorFunction<T>
12
+ export function OrderBy<T>(path: (keyof T) & string, direction?: OrderByDirection): DecoratorFunction<T>
13
+ export function OrderBy<T>(options: OrderByOptions | OrderByOptions[] | ((keyof T) & string), direction?: `${Model.OrderDirection}`): DecoratorFunction<T> {
12
14
  return extendEntity(({ entity }) => {
13
- const path = (typeof options !== 'object' ? [options] : options.path) as string[]
14
- const dir = (((typeof options === 'object' && options.direction)
15
- ? options.direction
16
- : direction) ?? Model.OrderDirection.asc) as Model.OrderDirection
15
+ const normalize = (path: string[], direction?: OrderByDirection): Model.OrderBy => ({
16
+ path,
17
+ direction: (direction ?? Model.OrderDirection.asc) as Model.OrderDirection,
18
+ })
19
+ const orderBy = ((): Model.OrderBy[] => {
20
+ if (Array.isArray(options)) {
21
+ return options.map(it => normalize(it.path, it.direction))
22
+ }
23
+ if (typeof options === 'object') {
24
+ return [normalize(options.path, options.direction)]
25
+ }
26
+ return [normalize([options], direction)]
27
+ })()
17
28
 
29
+ if (entity.orderBy?.length) {
30
+ // eslint-disable-next-line no-console
31
+ console.warn(`DEPRECATED: The "order by" property for the entity ${entity.name} has already been defined. Using multiple decorators can lead to unexpected order. Please provide an array containing all the 'order by' items as an input.`)
32
+ }
18
33
 
19
34
  return {
20
35
  ...entity,
21
- orderBy: [...(entity.orderBy ?? []), { path, direction: dir }],
36
+ orderBy: [...(entity.orderBy ?? []), ...orderBy],
22
37
  }
23
38
  })
24
39
  }
@@ -0,0 +1,88 @@
1
+ import { Model } from '@contember/schema'
2
+ import { assert, describe, expect, test, vi } from 'vitest'
3
+ import { c, createSchema } from '../../../src'
4
+
5
+ namespace OrderByModel {
6
+ @c.OrderBy('title')
7
+ export class Entity1 {
8
+ title = c.stringColumn()
9
+ }
10
+
11
+ @c.OrderBy('title', 'ascNullsFirst')
12
+ export class Entity2 {
13
+ title = c.stringColumn()
14
+ }
15
+
16
+ @c.OrderBy({ path: ['title'], direction: 'descNullsLast' })
17
+ export class Entity3 {
18
+ title = c.stringColumn()
19
+ }
20
+
21
+ @c.OrderBy([
22
+ { path: ['order'], direction: 'asc' },
23
+ { path: ['title'], direction: 'descNullsLast' },
24
+ ])
25
+ export class Entity4 {
26
+ order = c.intColumn()
27
+ title = c.stringColumn()
28
+ }
29
+ }
30
+
31
+ namespace LegacyOrderBy {
32
+ @c.OrderBy('order')
33
+ @c.OrderBy('title')
34
+ export class Entity5 {
35
+ order = c.intColumn()
36
+ title = c.stringColumn()
37
+ }
38
+ }
39
+
40
+ describe('order by', () => {
41
+
42
+ test('simple order by', () => {
43
+ const schema = createSchema(OrderByModel)
44
+
45
+ assert.deepEqual(schema.model.entities.Entity1.orderBy, [{ path: ['title'], direction: Model.OrderDirection.asc }])
46
+ })
47
+
48
+ test('custom direction', () => {
49
+ const schema = createSchema(OrderByModel)
50
+
51
+ assert.deepEqual(schema.model.entities.Entity2.orderBy, [{ path: ['title'], direction: Model.OrderDirection.ascNullsFirst }])
52
+ })
53
+
54
+ test('object input order by', () => {
55
+ const schema = createSchema(OrderByModel)
56
+
57
+ assert.deepEqual(schema.model.entities.Entity3.orderBy, [{ path: ['title'], direction: Model.OrderDirection.descNullsLast }])
58
+ })
59
+
60
+ test('multiple order by', () => {
61
+ const schema = createSchema(OrderByModel)
62
+
63
+ assert.deepEqual(schema.model.entities.Entity4.orderBy, [
64
+ { path: ['order'], direction: Model.OrderDirection.asc },
65
+ { path: ['title'], direction: Model.OrderDirection.descNullsLast },
66
+ ])
67
+ })
68
+
69
+ test('deprecated orderBy', () => {
70
+
71
+ const consoleMock = vi.spyOn(console, 'warn').mockImplementation(() => {
72
+ })
73
+
74
+ const schema = createSchema(LegacyOrderBy)
75
+
76
+
77
+ assert.deepEqual(schema.model.entities.Entity5.orderBy, [
78
+ { path: ['title'], direction: Model.OrderDirection.asc },
79
+ { path: ['order'], direction: Model.OrderDirection.asc },
80
+ ])
81
+
82
+ expect(consoleMock).toHaveBeenCalledOnce()
83
+ expect(consoleMock).toHaveBeenLastCalledWith('DEPRECATED: The "order by" property for the entity Entity5 has already been defined. Using multiple decorators can lead to unexpected order. Please provide an array containing all the \'order by\' items as an input.')
84
+
85
+ consoleMock.mockReset()
86
+ })
87
+ })
88
+
@@ -553,3 +553,27 @@ namespace InvalidModel {
553
553
  test('invalid column', () => {
554
554
  expect(() => createSchema(InvalidModel)).toThrow('Field "bar" does not exist on entity "Book" in read ACL definition.')
555
555
  })
556
+
557
+
558
+ namespace ExplicitIdPredicate {
559
+ export const publicRole = c.createRole('public')
560
+
561
+ @c.Allow(publicRole, { read: ['id'] })
562
+ export class Book {
563
+ title = c.stringColumn()
564
+ }
565
+ }
566
+
567
+ test('explicit ID predicate', () => {
568
+ const schema = createSchema(ExplicitIdPredicate)
569
+ expect(schema.acl.roles.public.entities.Book).toMatchInlineSnapshot(`
570
+ {
571
+ "operations": {
572
+ "read": {
573
+ "id": true,
574
+ },
575
+ },
576
+ "predicates": {},
577
+ }
578
+ `)
579
+ })