@directus/api 31.0.0 → 32.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 (135) hide show
  1. package/dist/app.js +2 -0
  2. package/dist/auth/auth.d.ts +2 -1
  3. package/dist/auth/auth.js +7 -2
  4. package/dist/auth/drivers/ldap.d.ts +0 -2
  5. package/dist/auth/drivers/ldap.js +9 -7
  6. package/dist/auth/drivers/oauth2.d.ts +0 -2
  7. package/dist/auth/drivers/oauth2.js +11 -8
  8. package/dist/auth/drivers/openid.d.ts +0 -2
  9. package/dist/auth/drivers/openid.js +11 -8
  10. package/dist/auth/drivers/saml.d.ts +0 -2
  11. package/dist/auth/drivers/saml.js +5 -5
  12. package/dist/auth.js +1 -2
  13. package/dist/cli/commands/bootstrap/index.js +12 -33
  14. package/dist/cli/commands/init/index.js +1 -1
  15. package/dist/cli/commands/schema/apply.d.ts +4 -0
  16. package/dist/cli/commands/schema/apply.js +26 -3
  17. package/dist/controllers/collections.js +7 -2
  18. package/dist/controllers/fields.js +31 -8
  19. package/dist/controllers/server.js +26 -1
  20. package/dist/controllers/settings.js +9 -2
  21. package/dist/controllers/users.js +2 -2
  22. package/dist/database/helpers/fn/types.js +3 -3
  23. package/dist/database/helpers/schema/dialects/cockroachdb.d.ts +2 -1
  24. package/dist/database/helpers/schema/dialects/cockroachdb.js +13 -0
  25. package/dist/database/helpers/schema/dialects/mssql.d.ts +2 -1
  26. package/dist/database/helpers/schema/dialects/mssql.js +23 -0
  27. package/dist/database/helpers/schema/dialects/mysql.d.ts +2 -1
  28. package/dist/database/helpers/schema/dialects/mysql.js +25 -0
  29. package/dist/database/helpers/schema/dialects/oracle.d.ts +2 -1
  30. package/dist/database/helpers/schema/dialects/oracle.js +13 -0
  31. package/dist/database/helpers/schema/dialects/postgres.d.ts +2 -1
  32. package/dist/database/helpers/schema/dialects/postgres.js +13 -0
  33. package/dist/database/helpers/schema/types.d.ts +5 -0
  34. package/dist/database/helpers/schema/types.js +6 -0
  35. package/dist/database/migrations/20251012A-add-field-searchable.d.ts +3 -0
  36. package/dist/database/migrations/20251012A-add-field-searchable.js +10 -0
  37. package/dist/database/migrations/20251014A-add-project-owner.d.ts +3 -0
  38. package/dist/database/migrations/20251014A-add-project-owner.js +37 -0
  39. package/dist/database/migrations/20251028A-add-retention-indexes.d.ts +3 -0
  40. package/dist/database/migrations/20251028A-add-retention-indexes.js +42 -0
  41. package/dist/database/run-ast/lib/apply-query/add-join.js +2 -2
  42. package/dist/database/run-ast/lib/apply-query/filter/get-filter-type.d.ts +2 -2
  43. package/dist/database/run-ast/lib/apply-query/index.d.ts +0 -1
  44. package/dist/database/run-ast/lib/apply-query/index.js +4 -6
  45. package/dist/database/run-ast/lib/apply-query/search.js +2 -0
  46. package/dist/database/run-ast/lib/get-db-query.js +7 -6
  47. package/dist/database/run-ast/utils/generate-alias.d.ts +6 -0
  48. package/dist/database/run-ast/utils/generate-alias.js +57 -0
  49. package/dist/flows.js +1 -0
  50. package/dist/mcp/schema.d.ts +14 -14
  51. package/dist/mcp/schema.js +6 -6
  52. package/dist/mcp/server.d.ts +9 -3
  53. package/dist/mcp/server.js +1 -1
  54. package/dist/mcp/tools/collections.d.ts +1 -1
  55. package/dist/mcp/tools/fields.d.ts +1 -1
  56. package/dist/mcp/tools/files.d.ts +25 -25
  57. package/dist/mcp/tools/flows.d.ts +36 -36
  58. package/dist/mcp/tools/folders.d.ts +18 -18
  59. package/dist/mcp/tools/items.d.ts +18 -18
  60. package/dist/mcp/tools/operations.d.ts +19 -19
  61. package/dist/mcp/tools/prompts/items.md +1 -1
  62. package/dist/metrics/lib/create-metrics.js +16 -25
  63. package/dist/middleware/collection-exists.js +2 -2
  64. package/dist/operations/mail/index.js +3 -1
  65. package/dist/operations/mail/rate-limiter.d.ts +1 -0
  66. package/dist/operations/mail/rate-limiter.js +29 -0
  67. package/dist/permissions/modules/process-payload/process-payload.js +3 -10
  68. package/dist/permissions/modules/validate-access/validate-access.js +2 -3
  69. package/dist/schedules/metrics.js +6 -2
  70. package/dist/schedules/project.d.ts +4 -0
  71. package/dist/schedules/project.js +27 -0
  72. package/dist/services/collections.d.ts +3 -3
  73. package/dist/services/collections.js +16 -1
  74. package/dist/services/fields.d.ts +21 -5
  75. package/dist/services/fields.js +105 -28
  76. package/dist/services/graphql/resolvers/query.js +1 -1
  77. package/dist/services/graphql/resolvers/system-admin.js +49 -5
  78. package/dist/services/graphql/schema/parse-query.js +8 -8
  79. package/dist/services/graphql/utils/aggregate-query.d.ts +1 -1
  80. package/dist/services/graphql/utils/aggregate-query.js +5 -1
  81. package/dist/services/graphql/utils/filter-replace-m2a.js +2 -1
  82. package/dist/services/import-export.d.ts +9 -1
  83. package/dist/services/import-export.js +287 -101
  84. package/dist/services/items.d.ts +1 -1
  85. package/dist/services/items.js +36 -20
  86. package/dist/services/mail/index.js +2 -0
  87. package/dist/services/mail/rate-limiter.d.ts +1 -0
  88. package/dist/services/mail/rate-limiter.js +29 -0
  89. package/dist/services/meta.js +28 -24
  90. package/dist/services/schema.js +4 -1
  91. package/dist/services/server.d.ts +1 -0
  92. package/dist/services/server.js +14 -18
  93. package/dist/services/settings.d.ts +2 -1
  94. package/dist/services/settings.js +15 -0
  95. package/dist/services/tus/server.js +14 -9
  96. package/dist/telemetry/lib/get-report.js +4 -4
  97. package/dist/telemetry/lib/send-report.d.ts +6 -1
  98. package/dist/telemetry/lib/send-report.js +3 -1
  99. package/dist/telemetry/types/report.d.ts +17 -1
  100. package/dist/telemetry/utils/get-settings.d.ts +9 -0
  101. package/dist/telemetry/utils/get-settings.js +14 -0
  102. package/dist/test-utils/README.md +760 -0
  103. package/dist/test-utils/cache.d.ts +51 -0
  104. package/dist/test-utils/cache.js +59 -0
  105. package/dist/test-utils/database.d.ts +48 -0
  106. package/dist/test-utils/database.js +52 -0
  107. package/dist/test-utils/emitter.d.ts +35 -0
  108. package/dist/test-utils/emitter.js +38 -0
  109. package/dist/test-utils/fields-service.d.ts +28 -0
  110. package/dist/test-utils/fields-service.js +36 -0
  111. package/dist/test-utils/items-service.d.ts +23 -0
  112. package/dist/test-utils/items-service.js +37 -0
  113. package/dist/test-utils/knex.d.ts +164 -0
  114. package/dist/test-utils/knex.js +268 -0
  115. package/dist/test-utils/schema.d.ts +26 -0
  116. package/dist/test-utils/schema.js +35 -0
  117. package/dist/types/auth.d.ts +0 -2
  118. package/dist/utils/apply-diff.js +15 -0
  119. package/dist/utils/create-admin.d.ts +11 -0
  120. package/dist/utils/create-admin.js +50 -0
  121. package/dist/utils/get-schema.js +5 -3
  122. package/dist/utils/get-snapshot-diff.js +49 -5
  123. package/dist/utils/get-snapshot.js +13 -7
  124. package/dist/utils/sanitize-schema.d.ts +11 -4
  125. package/dist/utils/sanitize-schema.js +9 -6
  126. package/dist/utils/schedule.js +15 -19
  127. package/dist/utils/validate-diff.js +31 -0
  128. package/dist/utils/validate-snapshot.js +7 -0
  129. package/dist/websocket/controllers/hooks.js +12 -20
  130. package/dist/websocket/messages.d.ts +3 -3
  131. package/package.json +63 -65
  132. package/dist/cli/utils/defaults.d.ts +0 -4
  133. package/dist/cli/utils/defaults.js +0 -17
  134. package/dist/telemetry/utils/get-project-id.d.ts +0 -2
  135. package/dist/telemetry/utils/get-project-id.js +0 -4
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Cache mocking utilities for service tests
3
+ * Provides simplified mocks for src/cache module used in service testing
4
+ */
5
+ /**
6
+ * Creates a standard cache mock for service tests
7
+ * Returns mock functions for vi.mock() and spies for testing cache behavior
8
+ *
9
+ * @returns Object with mock functions at root level and spies nested under 'spies' property
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * // Standard usage for vi.mock()
14
+ * vi.mock('../cache.js', async () => {
15
+ * const { mockCache } = await import('../__mocks__/cache.js');
16
+ * return mockCache();
17
+ * });
18
+ *
19
+ * // Testing cache clearing with spies
20
+ * import { getCache } from '../cache.js';
21
+ * import { mockCache } from '../__mocks__/cache.js';
22
+ *
23
+ * test('should clear cache after update', async () => {
24
+ * const { spies } = mockCache();
25
+ * vi.mocked(getCache).mockReturnValue(spies.mockCacheReturn as any);
26
+ *
27
+ * const service = new YourService({ knex: db, schema });
28
+ * await service.updateOne('1', { name: 'Updated' });
29
+ *
30
+ * expect(spies.clearSpy).toHaveBeenCalled();
31
+ * });
32
+ * ```
33
+ */
34
+ export declare function mockCache(): {
35
+ getCache: import("vitest").Mock<(...args: any[]) => any>;
36
+ getCacheValue: import("vitest").Mock<(...args: any[]) => any>;
37
+ setCacheValue: import("vitest").Mock<(...args: any[]) => any>;
38
+ clearSystemCache: import("vitest").Mock<(...args: any[]) => any>;
39
+ spies: {
40
+ clearSpy: import("vitest").Mock<(...args: any[]) => any>;
41
+ systemClearSpy: import("vitest").Mock<(...args: any[]) => any>;
42
+ getCacheSpy: import("vitest").Mock<(...args: any[]) => any>;
43
+ setCacheSpy: import("vitest").Mock<(...args: any[]) => any>;
44
+ mockCacheReturn: {
45
+ cache: any;
46
+ systemCache: any;
47
+ localSchemaCache: any;
48
+ lockCache: undefined;
49
+ };
50
+ };
51
+ };
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Cache mocking utilities for service tests
3
+ * Provides simplified mocks for src/cache module used in service testing
4
+ */
5
+ import { vi } from 'vitest';
6
+ /**
7
+ * Creates a standard cache mock for service tests
8
+ * Returns mock functions for vi.mock() and spies for testing cache behavior
9
+ *
10
+ * @returns Object with mock functions at root level and spies nested under 'spies' property
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * // Standard usage for vi.mock()
15
+ * vi.mock('../cache.js', async () => {
16
+ * const { mockCache } = await import('../__mocks__/cache.js');
17
+ * return mockCache();
18
+ * });
19
+ *
20
+ * // Testing cache clearing with spies
21
+ * import { getCache } from '../cache.js';
22
+ * import { mockCache } from '../__mocks__/cache.js';
23
+ *
24
+ * test('should clear cache after update', async () => {
25
+ * const { spies } = mockCache();
26
+ * vi.mocked(getCache).mockReturnValue(spies.mockCacheReturn as any);
27
+ *
28
+ * const service = new YourService({ knex: db, schema });
29
+ * await service.updateOne('1', { name: 'Updated' });
30
+ *
31
+ * expect(spies.clearSpy).toHaveBeenCalled();
32
+ * });
33
+ * ```
34
+ */
35
+ export function mockCache() {
36
+ const clearSpy = vi.fn();
37
+ const systemClearSpy = vi.fn();
38
+ const getCacheSpy = vi.fn();
39
+ const setCacheSpy = vi.fn();
40
+ const mockCacheReturn = {
41
+ cache: { clear: clearSpy },
42
+ systemCache: { clear: systemClearSpy },
43
+ localSchemaCache: { get: getCacheSpy, set: setCacheSpy },
44
+ lockCache: undefined,
45
+ };
46
+ return {
47
+ getCache: vi.fn().mockReturnValue(mockCacheReturn),
48
+ getCacheValue: vi.fn().mockResolvedValue(null),
49
+ setCacheValue: vi.fn().mockResolvedValue(undefined),
50
+ clearSystemCache: vi.fn(),
51
+ spies: {
52
+ clearSpy,
53
+ systemClearSpy,
54
+ getCacheSpy,
55
+ setCacheSpy,
56
+ mockCacheReturn,
57
+ },
58
+ };
59
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Database mocking utilities for service tests
3
+ * Provides simplified mocks for src/database/index module used in service testing
4
+ */
5
+ import type { DatabaseClient } from '@directus/types';
6
+ /**
7
+ * Creates a standard database mock for service tests
8
+ * This matches the pattern used across all service test files
9
+ *
10
+ * @param client Database client to mock (default: 'postgres')
11
+ * @returns Mock module object for vi.mock()
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * // Standard usage
16
+ * vi.mock('../../src/database/index', () => mockDatabase());
17
+ *
18
+ * // For MySQL-specific tests
19
+ * vi.mock('../../src/database/index', () => mockDatabase('mysql'));
20
+ *
21
+ * // To dynamically change the client during tests, import and mock directly:
22
+ * import { getDatabaseClient } from '../database/index.js';
23
+ * vi.mocked(getDatabaseClient).mockReturnValue('mssql');
24
+ * ```
25
+ */
26
+ export declare function mockDatabase(client?: DatabaseClient): {
27
+ default: import("vitest").Mock<(...args: any[]) => any>;
28
+ getDatabaseClient: import("vitest").Mock<(...args: any[]) => any>;
29
+ getSchemaInspector: import("vitest").Mock<(...args: any[]) => any>;
30
+ };
31
+ /**
32
+ * Creates a mock for the transaction utility
33
+ * By default, the mock simply executes the callback with the provided knex instance
34
+ * (no actual transaction wrapper, which is fine for most service tests)
35
+ *
36
+ * @returns Mock module object for vi.mock()
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * vi.mock('../utils/transaction.js', async () => {
41
+ * const { mockTransaction } = await import('../__mocks__/database.js');
42
+ * return mockTransaction();
43
+ * });
44
+ * ```
45
+ */
46
+ export declare function mockTransaction(): {
47
+ transaction: import("vitest").Mock<(knex: any, callback: any) => any>;
48
+ };
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Database mocking utilities for service tests
3
+ * Provides simplified mocks for src/database/index module used in service testing
4
+ */
5
+ import { vi } from 'vitest';
6
+ /**
7
+ * Creates a standard database mock for service tests
8
+ * This matches the pattern used across all service test files
9
+ *
10
+ * @param client Database client to mock (default: 'postgres')
11
+ * @returns Mock module object for vi.mock()
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * // Standard usage
16
+ * vi.mock('../../src/database/index', () => mockDatabase());
17
+ *
18
+ * // For MySQL-specific tests
19
+ * vi.mock('../../src/database/index', () => mockDatabase('mysql'));
20
+ *
21
+ * // To dynamically change the client during tests, import and mock directly:
22
+ * import { getDatabaseClient } from '../database/index.js';
23
+ * vi.mocked(getDatabaseClient).mockReturnValue('mssql');
24
+ * ```
25
+ */
26
+ export function mockDatabase(client = 'postgres') {
27
+ return {
28
+ default: vi.fn(),
29
+ getDatabaseClient: vi.fn().mockReturnValue(client),
30
+ getSchemaInspector: vi.fn(),
31
+ };
32
+ }
33
+ /**
34
+ * Creates a mock for the transaction utility
35
+ * By default, the mock simply executes the callback with the provided knex instance
36
+ * (no actual transaction wrapper, which is fine for most service tests)
37
+ *
38
+ * @returns Mock module object for vi.mock()
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * vi.mock('../utils/transaction.js', async () => {
43
+ * const { mockTransaction } = await import('../__mocks__/database.js');
44
+ * return mockTransaction();
45
+ * });
46
+ * ```
47
+ */
48
+ export function mockTransaction() {
49
+ return {
50
+ transaction: vi.fn((knex, callback) => callback(knex)),
51
+ };
52
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Emitter mocking utilities for service tests
3
+ * Provides simplified mocks for src/emitter module used in service testing
4
+ */
5
+ /**
6
+ * Creates a standard emitter mock for service tests
7
+ * This matches the pattern used across all service test files
8
+ *
9
+ * @returns Mock module object for vi.mock()
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * // Standard usage
14
+ * vi.mock('../emitter.js', () => mockEmitter());
15
+ *
16
+ * // To dynamically change emitter behavior during tests, import and mock directly:
17
+ * import emitter from '../emitter.js';
18
+ * vi.mocked(emitter.emitAction).mockResolvedValue(undefined);
19
+ * vi.mocked(emitter.emitFilter).mockResolvedValue(customPayload);
20
+ * ```
21
+ */
22
+ export declare function mockEmitter(): {
23
+ default: {
24
+ emitAction: import("vitest").Mock<(...args: any[]) => any>;
25
+ emitFilter: import("vitest").Mock<(_: any, payload: any) => Promise<any>>;
26
+ emitInit: import("vitest").Mock<(...args: any[]) => any>;
27
+ onFilter: import("vitest").Mock<(...args: any[]) => any>;
28
+ onAction: import("vitest").Mock<(...args: any[]) => any>;
29
+ onInit: import("vitest").Mock<(...args: any[]) => any>;
30
+ offFilter: import("vitest").Mock<(...args: any[]) => any>;
31
+ offAction: import("vitest").Mock<(...args: any[]) => any>;
32
+ offInit: import("vitest").Mock<(...args: any[]) => any>;
33
+ offAll: import("vitest").Mock<(...args: any[]) => any>;
34
+ };
35
+ };
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Emitter mocking utilities for service tests
3
+ * Provides simplified mocks for src/emitter module used in service testing
4
+ */
5
+ import { vi } from 'vitest';
6
+ /**
7
+ * Creates a standard emitter mock for service tests
8
+ * This matches the pattern used across all service test files
9
+ *
10
+ * @returns Mock module object for vi.mock()
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * // Standard usage
15
+ * vi.mock('../emitter.js', () => mockEmitter());
16
+ *
17
+ * // To dynamically change emitter behavior during tests, import and mock directly:
18
+ * import emitter from '../emitter.js';
19
+ * vi.mocked(emitter.emitAction).mockResolvedValue(undefined);
20
+ * vi.mocked(emitter.emitFilter).mockResolvedValue(customPayload);
21
+ * ```
22
+ */
23
+ export function mockEmitter() {
24
+ return {
25
+ default: {
26
+ emitAction: vi.fn(),
27
+ emitFilter: vi.fn((_, payload) => Promise.resolve(payload)),
28
+ emitInit: vi.fn(),
29
+ onFilter: vi.fn(),
30
+ onAction: vi.fn(),
31
+ onInit: vi.fn(),
32
+ offFilter: vi.fn(),
33
+ offAction: vi.fn(),
34
+ offInit: vi.fn(),
35
+ offAll: vi.fn(),
36
+ },
37
+ };
38
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * FieldsService mocking utilities for service tests
3
+ * Provides simplified mocks for src/services/fields module used in service testing
4
+ */
5
+ /**
6
+ * Creates a standard FieldsService mock for service tests
7
+ * This matches the pattern used in CollectionsService tests
8
+ *
9
+ * @returns Mock module object for vi.mock()
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * // Standard usage
14
+ * vi.mock('./fields.js', async () => {
15
+ * const { mockFieldsService } = await import('../__mocks__/fields-service.js');
16
+ * return mockFieldsService();
17
+ * });
18
+ *
19
+ * // To dynamically change FieldsService behavior during tests:
20
+ * import { FieldsService } from './fields.js';
21
+ * vi.spyOn(FieldsService.prototype, 'addColumnToTable').mockImplementation((table, collection, field) => {
22
+ * // custom implementation
23
+ * });
24
+ * ```
25
+ */
26
+ export declare function mockFieldsService(): {
27
+ FieldsService: import("vitest").Mock<(...args: any[]) => any>;
28
+ };
@@ -0,0 +1,36 @@
1
+ /**
2
+ * FieldsService mocking utilities for service tests
3
+ * Provides simplified mocks for src/services/fields module used in service testing
4
+ */
5
+ import { vi } from 'vitest';
6
+ /**
7
+ * Creates a standard FieldsService mock for service tests
8
+ * This matches the pattern used in CollectionsService tests
9
+ *
10
+ * @returns Mock module object for vi.mock()
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * // Standard usage
15
+ * vi.mock('./fields.js', async () => {
16
+ * const { mockFieldsService } = await import('../__mocks__/fields-service.js');
17
+ * return mockFieldsService();
18
+ * });
19
+ *
20
+ * // To dynamically change FieldsService behavior during tests:
21
+ * import { FieldsService } from './fields.js';
22
+ * vi.spyOn(FieldsService.prototype, 'addColumnToTable').mockImplementation((table, collection, field) => {
23
+ * // custom implementation
24
+ * });
25
+ * ```
26
+ */
27
+ export function mockFieldsService() {
28
+ const FieldsService = vi.fn();
29
+ // Mock common methods used by other services (like CollectionsService)
30
+ FieldsService.prototype.addColumnToTable = vi.fn().mockImplementation(() => { });
31
+ FieldsService.prototype.addColumnIndex = vi.fn().mockResolvedValue(undefined);
32
+ FieldsService.prototype.deleteField = vi.fn().mockResolvedValue(undefined);
33
+ FieldsService.prototype.createField = vi.fn().mockResolvedValue(undefined);
34
+ FieldsService.prototype.updateField = vi.fn().mockResolvedValue('field');
35
+ return { FieldsService };
36
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * ItemsService mocking utilities for service tests
3
+ * Provides simplified mocks for src/services/items module used in service testing
4
+ */
5
+ /**
6
+ * Creates a standard ItemsService mock for service tests
7
+ * This matches the pattern used across all service test files
8
+ *
9
+ * @returns Mock module object for vi.mock()
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * // Standard usage
14
+ * vi.mock('./items.js', () => mockItemsService());
15
+ *
16
+ * // To dynamically change ItemsService behavior during tests, import and mock directly:
17
+ * import { ItemsService } from './items.js';
18
+ * vi.spyOn(ItemsService.prototype, 'createOne').mockResolvedValue('new-id');
19
+ * ```
20
+ */
21
+ export declare function mockItemsService(): {
22
+ ItemsService: import("vitest").Mock<(...args: any[]) => any>;
23
+ };
@@ -0,0 +1,37 @@
1
+ /**
2
+ * ItemsService mocking utilities for service tests
3
+ * Provides simplified mocks for src/services/items module used in service testing
4
+ */
5
+ import { vi } from 'vitest';
6
+ /**
7
+ * Creates a standard ItemsService mock for service tests
8
+ * This matches the pattern used across all service test files
9
+ *
10
+ * @returns Mock module object for vi.mock()
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * // Standard usage
15
+ * vi.mock('./items.js', () => mockItemsService());
16
+ *
17
+ * // To dynamically change ItemsService behavior during tests, import and mock directly:
18
+ * import { ItemsService } from './items.js';
19
+ * vi.spyOn(ItemsService.prototype, 'createOne').mockResolvedValue('new-id');
20
+ * ```
21
+ */
22
+ export function mockItemsService() {
23
+ const ItemsService = vi.fn();
24
+ // Provide sensible default return values for common operations
25
+ ItemsService.prototype.createOne = vi.fn().mockResolvedValue(1);
26
+ ItemsService.prototype.createMany = vi.fn().mockResolvedValue([1]);
27
+ ItemsService.prototype.readByQuery = vi.fn().mockResolvedValue([]);
28
+ ItemsService.prototype.readOne = vi.fn().mockResolvedValue({});
29
+ ItemsService.prototype.readMany = vi.fn().mockResolvedValue([]);
30
+ ItemsService.prototype.updateOne = vi.fn().mockResolvedValue(1);
31
+ ItemsService.prototype.updateMany = vi.fn().mockResolvedValue([1]);
32
+ ItemsService.prototype.updateByQuery = vi.fn().mockResolvedValue([1]);
33
+ ItemsService.prototype.deleteOne = vi.fn().mockResolvedValue(1);
34
+ ItemsService.prototype.deleteMany = vi.fn().mockResolvedValue([1]);
35
+ ItemsService.prototype.deleteByQuery = vi.fn().mockResolvedValue([1]);
36
+ return { ItemsService };
37
+ }
@@ -0,0 +1,164 @@
1
+ /**
2
+ * Knex mocking utilities for service tests
3
+ * Provides mock knex instances, table builders, and tracker utilities
4
+ */
5
+ import knex from 'knex';
6
+ import { type Tracker } from 'knex-mock-client';
7
+ /**
8
+ * Creates a mocked knex instance with tracker and schema builder support
9
+ *
10
+ * @returns Object containing the mocked db instance, tracker, and mockSchema
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const { db, tracker, mockSchema } = createMockKnex();
15
+ *
16
+ * // Use tracker to mock query responses
17
+ * tracker.on.select('users').response([{ id: 1, name: 'John' }]);
18
+ *
19
+ * // Verify schema operations
20
+ * expect(mockSchema.createTable).toHaveBeenCalled();
21
+ * ```
22
+ */
23
+ export declare function createMockKnex(): {
24
+ db: import("vitest").MockedFunction<knex.Knex<any, unknown[]>>;
25
+ tracker: Tracker;
26
+ mockSchemaBuilder: {
27
+ createTable: import("vitest").Mock<(_tableName: any, callback: any) => Promise<void>>;
28
+ dropTable: import("vitest").Mock<(...args: any[]) => any>;
29
+ hasTable: import("vitest").Mock<(...args: any[]) => any>;
30
+ table: import("vitest").Mock<(_tableName: any, callback: any) => Promise<void>>;
31
+ alterTable: import("vitest").Mock<(_tableName: any, callback: any) => Promise<void>>;
32
+ dropTableIfExists: import("vitest").Mock<(...args: any[]) => any>;
33
+ renameTable: import("vitest").Mock<(...args: any[]) => any>;
34
+ raw: import("vitest").Mock<(...args: any[]) => any>;
35
+ };
36
+ };
37
+ /**
38
+ * Creates a mock table builder for schema operations
39
+ * Used for testing column creation and alteration
40
+ *
41
+ * @returns Mock table builder with chainable methods
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * const table = createMockTableBuilder();
46
+ * table.string('name', 255).notNullable().index();
47
+ * ```
48
+ */
49
+ export declare function createMockTableBuilder(): {
50
+ string: import("vitest").Mock<(...args: any[]) => any>;
51
+ text: import("vitest").Mock<(...args: any[]) => any>;
52
+ integer: import("vitest").Mock<(...args: any[]) => any>;
53
+ bigInteger: import("vitest").Mock<(...args: any[]) => any>;
54
+ float: import("vitest").Mock<(...args: any[]) => any>;
55
+ decimal: import("vitest").Mock<(...args: any[]) => any>;
56
+ boolean: import("vitest").Mock<(...args: any[]) => any>;
57
+ date: import("vitest").Mock<(...args: any[]) => any>;
58
+ dateTime: import("vitest").Mock<(...args: any[]) => any>;
59
+ timestamp: import("vitest").Mock<(...args: any[]) => any>;
60
+ json: import("vitest").Mock<(...args: any[]) => any>;
61
+ jsonb: import("vitest").Mock<(...args: any[]) => any>;
62
+ uuid: import("vitest").Mock<(...args: any[]) => any>;
63
+ increments: import("vitest").Mock<(...args: any[]) => any>;
64
+ bigIncrements: import("vitest").Mock<(...args: any[]) => any>;
65
+ defaultTo: import("vitest").Mock<(...args: any[]) => any>;
66
+ notNullable: import("vitest").Mock<(...args: any[]) => any>;
67
+ nullable: import("vitest").Mock<(...args: any[]) => any>;
68
+ primary: import("vitest").Mock<(...args: any[]) => any>;
69
+ unique: import("vitest").Mock<(...args: any[]) => any>;
70
+ index: import("vitest").Mock<(...args: any[]) => any>;
71
+ alter: import("vitest").Mock<(...args: any[]) => any>;
72
+ dropColumn: import("vitest").Mock<(...args: any[]) => any>;
73
+ dropUnique: import("vitest").Mock<(...args: any[]) => any>;
74
+ dropIndex: import("vitest").Mock<(...args: any[]) => any>;
75
+ };
76
+ /**
77
+ * Sets up common database operation mock handlers for all system collections
78
+ * Automatically mocks CRUD operations (select, insert, update, delete) for all Directus system collections
79
+ *
80
+ * @param tracker The knex-mock-client tracker instance
81
+ *
82
+ * @example
83
+ * ```typescript
84
+ * const { db, tracker, mockSchema } = createMockKnex();
85
+ * setupSystemCollectionMocks(tracker);
86
+ * // Now all CRUD operations on system collections are mocked
87
+ * ```
88
+ */
89
+ export declare function setupSystemCollectionMocks(tracker: Tracker): void;
90
+ /**
91
+ * Resets all mock states
92
+ * Should be called in afterEach hooks to clean up between tests
93
+ *
94
+ * @param tracker The knex-mock-client tracker instance
95
+ * @param mockSchema The mock schema object from createMockKnex
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * const { db, tracker, mockSchema } = createMockKnex();
100
+ *
101
+ * afterEach(() => {
102
+ * resetMocks(tracker, mockSchema);
103
+ * });
104
+ * ```
105
+ */
106
+ export declare function resetKnexMocks(tracker: Tracker, mockSchema: ReturnType<typeof createMockKnex>['mockSchemaBuilder']): void;
107
+ /**
108
+ * Creates a mock createTable function for testing table creation
109
+ * Returns a vi.fn() that calls the callback with a mock table builder
110
+ *
111
+ * @returns Mock function for db.schema.createTable
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * const { db } = createMockKnex();
116
+ * const createTableSpy = mockCreateTable();
117
+ * db.schema.createTable = createTableSpy as any;
118
+ *
119
+ * // Now when createTable is called, it will invoke the callback with a mock table builder
120
+ * await db.schema.createTable('users', (table) => {
121
+ * table.increments('id');
122
+ * table.string('name');
123
+ * });
124
+ * ```
125
+ */
126
+ export declare function mockCreateTable(): import("vitest").Mock<(_tableName: any, callback: any) => Promise<void>>;
127
+ /**
128
+ * Creates a mock alterTable function for testing schema alterations
129
+ * Returns a vi.fn() that calls the callback with a mock table builder
130
+ *
131
+ * @returns Mock function for db.schema.alterTable
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * const { db } = createMockKnex();
136
+ * const alterTableSpy = mockAlterTable();
137
+ * db.schema.alterTable = alterTableSpy as any;
138
+ *
139
+ * // Now when alterTable is called, it will invoke the callback with a mock table builder
140
+ * await db.schema.alterTable('users', (table) => {
141
+ * table.string('name');
142
+ * });
143
+ * ```
144
+ */
145
+ export declare function mockAlterTable(): import("vitest").Mock<(_tableName: any, callback: any) => Promise<void>>;
146
+ /**
147
+ * Creates a mock schema.table function for testing schema operations
148
+ * Returns a vi.fn() that calls the callback with a mock table builder
149
+ *
150
+ * @returns Mock function for db.schema.table
151
+ *
152
+ * @example
153
+ * ```typescript
154
+ * const { db } = createMockKnex();
155
+ * const schemaTableSpy = mockSchemaTable();
156
+ * db.schema.table = schemaTableSpy as any;
157
+ *
158
+ * // Now when schema.table is called, it will invoke the callback with a mock table builder
159
+ * await db.schema.table('users', (table) => {
160
+ * table.dropColumn('name');
161
+ * });
162
+ * ```
163
+ */
164
+ export declare function mockSchemaTable(): import("vitest").Mock<(_tableName: any, callback: any) => Promise<void>>;