diff --git a/packages/cli/src/lib/loader.ts b/packages/cli/src/lib/loader.ts index df13e488..da1b8be4 100644 --- a/packages/cli/src/lib/loader.ts +++ b/packages/cli/src/lib/loader.ts @@ -2,6 +2,7 @@ import { ConfigService, DashboardsCollection, FlowsCollection, + FoldersCollection, OperationsCollection, PanelsCollection, PermissionsCollection, @@ -63,6 +64,7 @@ export function loadCollections() { // The collections are populated in the same order return [ Container.get(SettingsCollection), + Container.get(FoldersCollection), Container.get(TranslationsCollection), Container.get(WebhooksCollection), Container.get(FlowsCollection), diff --git a/packages/cli/src/lib/services/collections/folders/collection.ts b/packages/cli/src/lib/services/collections/folders/collection.ts new file mode 100644 index 00000000..b0f57cc8 --- /dev/null +++ b/packages/cli/src/lib/services/collections/folders/collection.ts @@ -0,0 +1,45 @@ +import { DirectusCollection } from '../base'; +import pino from 'pino'; +import { Inject, Service } from 'typedi'; +import { FoldersDataLoader } from './data-loader'; +import { FoldersDataClient } from './data-client'; +import { FoldersIdMapperClient } from './id-mapper-client'; +import { FoldersDataDiffer } from './data-differ'; +import { getChildLogger } from '../../../helpers'; +import { FOLDERS_COLLECTION } from './constants'; +import { FoldersDataMapper } from './data-mapper'; +import { LOGGER } from '../../../constants'; +import { DirectusFolder } from './interfaces'; +import { ConfigService } from '../../config'; +import { MigrationClient } from '../../migration-client'; + +@Service() +export class FoldersCollection extends DirectusCollection { + protected readonly enableCreate = true; + protected readonly enableUpdate = true; + protected readonly enableDelete = true; + + protected readonly preserveIds = true; + + constructor( + @Inject(LOGGER) baseLogger: pino.Logger, + dataDiffer: FoldersDataDiffer, + dataLoader: FoldersDataLoader, + dataClient: FoldersDataClient, + dataMapper: FoldersDataMapper, + idMapper: FoldersIdMapperClient, + config: ConfigService, + migrationClient: MigrationClient, + ) { + super( + getChildLogger(baseLogger, FOLDERS_COLLECTION), + dataDiffer, + dataLoader, + dataClient, + dataMapper, + idMapper, + migrationClient, + config.getHooksConfig(FOLDERS_COLLECTION), + ); + } +} diff --git a/packages/cli/src/lib/services/collections/folders/constants.ts b/packages/cli/src/lib/services/collections/folders/constants.ts new file mode 100644 index 00000000..1c0ccd3e --- /dev/null +++ b/packages/cli/src/lib/services/collections/folders/constants.ts @@ -0,0 +1 @@ +export const FOLDERS_COLLECTION = 'folders'; diff --git a/packages/cli/src/lib/services/collections/folders/data-client.ts b/packages/cli/src/lib/services/collections/folders/data-client.ts new file mode 100644 index 00000000..05acdc49 --- /dev/null +++ b/packages/cli/src/lib/services/collections/folders/data-client.ts @@ -0,0 +1,36 @@ +import { DataClient, Query, WithoutIdAndSyncId } from '../base'; +import { + createFolder, + deleteFolder, + readFolders, + updateFolder, +} from '@directus/sdk'; +import { Service } from 'typedi'; +import { MigrationClient } from '../../migration-client'; +import { DirectusFolder } from './interfaces'; + +@Service() +export class FoldersDataClient extends DataClient { + constructor(migrationClient: MigrationClient) { + super(migrationClient); + } + + protected getDeleteCommand(itemId: string) { + return deleteFolder(itemId); + } + + protected getInsertCommand(item: WithoutIdAndSyncId) { + return createFolder(item); + } + + protected getQueryCommand(query: Query) { + return readFolders(query); + } + + protected getUpdateCommand( + itemId: string, + diffItem: Partial>, + ) { + return updateFolder(itemId, diffItem); + } +} diff --git a/packages/cli/src/lib/services/collections/folders/data-differ.ts b/packages/cli/src/lib/services/collections/folders/data-differ.ts new file mode 100644 index 00000000..f9821ced --- /dev/null +++ b/packages/cli/src/lib/services/collections/folders/data-differ.ts @@ -0,0 +1,31 @@ +import { DataDiffer } from '../base'; + +import { Inject, Service } from 'typedi'; +import { FOLDERS_COLLECTION } from './constants'; +import pino from 'pino'; +import { FoldersDataLoader } from './data-loader'; +import { FoldersDataClient } from './data-client'; +import { FoldersIdMapperClient } from './id-mapper-client'; +import { getChildLogger } from '../../../helpers'; +import { FoldersDataMapper } from './data-mapper'; +import { LOGGER } from '../../../constants'; +import { DirectusFolder } from './interfaces'; + +@Service() +export class FoldersDataDiffer extends DataDiffer { + constructor( + @Inject(LOGGER) baseLogger: pino.Logger, + dataLoader: FoldersDataLoader, + dataClient: FoldersDataClient, + dataMapper: FoldersDataMapper, + idMapper: FoldersIdMapperClient, + ) { + super( + getChildLogger(baseLogger, FOLDERS_COLLECTION), + dataLoader, + dataClient, + dataMapper, + idMapper, + ); + } +} diff --git a/packages/cli/src/lib/services/collections/folders/data-loader.ts b/packages/cli/src/lib/services/collections/folders/data-loader.ts new file mode 100644 index 00000000..3e3ff436 --- /dev/null +++ b/packages/cli/src/lib/services/collections/folders/data-loader.ts @@ -0,0 +1,20 @@ +import { DataLoader } from '../base'; + +import { Service } from 'typedi'; +import { FOLDERS_COLLECTION } from './constants'; +import path from 'path'; +import { DirectusFolder } from './interfaces'; +import { ConfigService } from '../../config'; +import { MigrationClient } from '../../migration-client'; + +@Service() +export class FoldersDataLoader extends DataLoader { + constructor(config: ConfigService, migrationClient: MigrationClient) { + const filePath = path.join( + config.getCollectionsConfig().dumpPath, + `${FOLDERS_COLLECTION}.json`, + ); + const hooks = config.getHooksConfig(FOLDERS_COLLECTION); + super(filePath, migrationClient, hooks); + } +} diff --git a/packages/cli/src/lib/services/collections/folders/data-mapper.ts b/packages/cli/src/lib/services/collections/folders/data-mapper.ts new file mode 100644 index 00000000..c550b6f2 --- /dev/null +++ b/packages/cli/src/lib/services/collections/folders/data-mapper.ts @@ -0,0 +1,19 @@ +import { DataMapper, IdMappers } from '../base'; + +import { Container, Inject, Service } from 'typedi'; +import pino from 'pino'; +import { getChildLogger } from '../../../helpers'; +import { LOGGER } from '../../../constants'; +import { FOLDERS_COLLECTION } from './constants'; +import { DirectusFolder } from './interfaces'; +import { FoldersIdMapperClient } from './id-mapper-client'; + +@Service() +export class FoldersDataMapper extends DataMapper { + protected idMappers: IdMappers = { + parent: Container.get(FoldersIdMapperClient), + }; + constructor(@Inject(LOGGER) baseLogger: pino.Logger) { + super(getChildLogger(baseLogger, FOLDERS_COLLECTION)); + } +} diff --git a/packages/cli/src/lib/services/collections/folders/id-mapper-client.ts b/packages/cli/src/lib/services/collections/folders/id-mapper-client.ts new file mode 100644 index 00000000..6d789f40 --- /dev/null +++ b/packages/cli/src/lib/services/collections/folders/id-mapper-client.ts @@ -0,0 +1,11 @@ +import { IdMapperClient } from '../base'; +import { Service } from 'typedi'; +import { FOLDERS_COLLECTION } from './constants'; +import { MigrationClient } from '../../migration-client'; + +@Service() +export class FoldersIdMapperClient extends IdMapperClient { + constructor(migrationClient: MigrationClient) { + super(migrationClient, FOLDERS_COLLECTION); + } +} diff --git a/packages/cli/src/lib/services/collections/folders/index.ts b/packages/cli/src/lib/services/collections/folders/index.ts new file mode 100644 index 00000000..203e6c76 --- /dev/null +++ b/packages/cli/src/lib/services/collections/folders/index.ts @@ -0,0 +1,8 @@ +export * from './interfaces'; +export * from './constants'; +export * from './collection'; +export * from './data-client'; +export * from './data-differ'; +export * from './data-mapper'; +export * from './data-loader'; +export * from './id-mapper-client'; diff --git a/packages/cli/src/lib/services/collections/folders/interfaces.ts b/packages/cli/src/lib/services/collections/folders/interfaces.ts new file mode 100644 index 00000000..d385dea3 --- /dev/null +++ b/packages/cli/src/lib/services/collections/folders/interfaces.ts @@ -0,0 +1,4 @@ +import { DirectusFolder as BaseDirectusFolder } from '@directus/sdk'; +import { BaseSchema } from '../base'; + +export type DirectusFolder = BaseDirectusFolder; diff --git a/packages/cli/src/lib/services/collections/index.ts b/packages/cli/src/lib/services/collections/index.ts index aba7a3ca..4a690a26 100644 --- a/packages/cli/src/lib/services/collections/index.ts +++ b/packages/cli/src/lib/services/collections/index.ts @@ -1,5 +1,6 @@ export * from './base'; export * from './flows'; +export * from './folders'; export * from './settings'; export * from './translations'; export * from './webhooks'; diff --git a/packages/cli/src/lib/services/config/schema.ts b/packages/cli/src/lib/services/config/schema.ts index 9ff64185..a996c38e 100644 --- a/packages/cli/src/lib/services/config/schema.ts +++ b/packages/cli/src/lib/services/config/schema.ts @@ -10,6 +10,7 @@ export const HooksSchema = z.object({ export const OptionsHooksSchema = z.object({ dashboards: HooksSchema.optional(), flows: HooksSchema.optional(), + folders: HooksSchema.optional(), operations: HooksSchema.optional(), panels: HooksSchema.optional(), permissions: HooksSchema.optional(),