Skip to content

Commit

Permalink
feat: handle translations collection
Browse files Browse the repository at this point in the history
  • Loading branch information
EdouardDem committed Jan 23, 2024
1 parent 9a1a3a0 commit ad5a3a1
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ going to Directus.
Hooks are defined in the configuration file using the `hooks` property. Under this property, you can define the
collection
name and the hook function to be executed.
Available collection names are: `dashboards`, `flows`, `operations`, `panels`, `permissions`, `roles`, `settings`,
Available collection names are: `dashboards`, `flows`, `operations`, `panels`, `permissions`, `roles`, `settings`, `translations`,
and `webhooks`.

For each collection, available hook functions are: `onQuery`, `onLoad`, `onSave`, and `onDump`.
Expand Down Expand Up @@ -333,6 +333,7 @@ flowchart
- permissions
- roles
- settings
- translations
- webhooks

For these collections, data changes are committed to the code, allowing for replication on other Directus instances. A
Expand Down Expand Up @@ -362,7 +363,7 @@ configurations and schema within Directus. Here is a step-by-step explanation of
Upon execution of the `pull` command, `directus-sync` will:

1. Scan the specified Directus collections, which include dashboards, flows, operations, panels, permissions, roles,
settings, and webhooks.
settings, translations and webhooks.
2. Assign a SyncID to each element within these collections if it doesn't already have one.
3. Commit the data of these collections into code, allowing for versioning and tracking of configuration changes.

Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/lib/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PermissionsCollection,
RolesCollection,
SettingsCollection,
TranslationsCollection,
WebhooksCollection,
} from './services';
import { createDumpFolders } from './helpers';
Expand Down Expand Up @@ -62,6 +63,7 @@ export function loadCollections() {
// The collections are populated in the same order
return [
Container.get(SettingsCollection),
Container.get(TranslationsCollection),
Container.get(WebhooksCollection),
Container.get(FlowsCollection),
Container.get(OperationsCollection),
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/lib/services/collections/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './base';
export * from './flows';
export * from './settings';
export * from './translations';
export * from './webhooks';
export * from './operations';
export * from './roles';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { DirectusCollection } from '../base';
import pino from 'pino';
import { Inject, Service } from 'typedi';
import { TranslationsDataLoader } from './data-loader';
import { TranslationsDataClient } from './data-client';
import { TranslationsIdMapperClient } from './id-mapper-client';
import { TranslationsDataDiffer } from './data-differ';
import { getChildLogger } from '../../../helpers';
import { TRANSLATIONS_COLLECTION } from './constants';
import { TranslationsDataMapper } from './data-mapper';
import { LOGGER } from '../../../constants';
import { DirectusTranslation } from './interfaces';
import { ConfigService } from '../../config';
import { MigrationClient } from '../../migration-client';

@Service()
export class TranslationsCollection extends DirectusCollection<DirectusTranslation> {
protected readonly enableCreate = true;
protected readonly enableUpdate = true;
protected readonly enableDelete = true;

constructor(
@Inject(LOGGER) baseLogger: pino.Logger,
dataDiffer: TranslationsDataDiffer,
dataLoader: TranslationsDataLoader,
dataClient: TranslationsDataClient,
dataMapper: TranslationsDataMapper,
idMapper: TranslationsIdMapperClient,
config: ConfigService,
migrationClient: MigrationClient,
) {
super(
getChildLogger(baseLogger, TRANSLATIONS_COLLECTION),
dataDiffer,
dataLoader,
dataClient,
dataMapper,
idMapper,
migrationClient,
config.getHooksConfig(TRANSLATIONS_COLLECTION),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const TRANSLATIONS_COLLECTION = 'translations';
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { DataClient, Query, WithoutIdAndSyncId } from '../base';
import {
createTranslation,
deleteTranslation,
readTranslations,
updateTranslation,
} from '@directus/sdk';
import { Service } from 'typedi';
import { MigrationClient } from '../../migration-client';
import { DirectusTranslation } from './interfaces';

@Service()
export class TranslationsDataClient extends DataClient<DirectusTranslation> {
constructor(migrationClient: MigrationClient) {
super(migrationClient);
}

protected getDeleteCommand(itemId: number) {
return deleteTranslation(itemId);
}

protected getInsertCommand(item: WithoutIdAndSyncId<DirectusTranslation>) {
return createTranslation(item);
}

protected getQueryCommand(query: Query<DirectusTranslation>) {
return readTranslations(query);
}

protected getUpdateCommand(
itemId: number,
diffItem: Partial<WithoutIdAndSyncId<DirectusTranslation>>,
) {
return updateTranslation(itemId, diffItem);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { DataDiffer } from '../base';

import { Inject, Service } from 'typedi';
import { TRANSLATIONS_COLLECTION } from './constants';
import pino from 'pino';
import { TranslationsDataLoader } from './data-loader';
import { TranslationsDataClient } from './data-client';
import { TranslationsIdMapperClient } from './id-mapper-client';
import { getChildLogger } from '../../../helpers';
import { TranslationsDataMapper } from './data-mapper';
import { LOGGER } from '../../../constants';
import { DirectusTranslation } from './interfaces';

@Service()
export class TranslationsDataDiffer extends DataDiffer<DirectusTranslation> {
constructor(
@Inject(LOGGER) baseLogger: pino.Logger,
dataLoader: TranslationsDataLoader,
dataClient: TranslationsDataClient,
dataMapper: TranslationsDataMapper,
idMapper: TranslationsIdMapperClient,
) {
super(
getChildLogger(baseLogger, TRANSLATIONS_COLLECTION),
dataLoader,
dataClient,
dataMapper,
idMapper,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DataLoader } from '../base';

import { Service } from 'typedi';
import { TRANSLATIONS_COLLECTION } from './constants';
import path from 'path';
import { DirectusTranslation } from './interfaces';
import { ConfigService } from '../../config';
import { MigrationClient } from '../../migration-client';

@Service()
export class TranslationsDataLoader extends DataLoader<DirectusTranslation> {
constructor(config: ConfigService, migrationClient: MigrationClient) {
const filePath = path.join(
config.getCollectionsConfig().dumpPath,
`${TRANSLATIONS_COLLECTION}.json`,
);
const hooks = config.getHooksConfig(TRANSLATIONS_COLLECTION);
super(filePath, migrationClient, hooks);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DataMapper } from '../base';

import { Inject, Service } from 'typedi';
import pino from 'pino';
import { getChildLogger } from '../../../helpers';
import { LOGGER } from '../../../constants';
import { TRANSLATIONS_COLLECTION } from './constants';
import { DirectusTranslation } from './interfaces';

@Service()
export class TranslationsDataMapper extends DataMapper<DirectusTranslation> {
constructor(@Inject(LOGGER) baseLogger: pino.Logger) {
super(getChildLogger(baseLogger, TRANSLATIONS_COLLECTION));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IdMapperClient } from '../base';
import { Service } from 'typedi';
import { TRANSLATIONS_COLLECTION } from './constants';
import { MigrationClient } from '../../migration-client';

@Service()
export class TranslationsIdMapperClient extends IdMapperClient {
constructor(migrationClient: MigrationClient) {
super(migrationClient, TRANSLATIONS_COLLECTION);
}
}
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { DirectusTranslation as BaseDirectusTranslation } from '@directus/sdk';
import { BaseSchema } from '../base';

export type DirectusTranslation = BaseDirectusTranslation<BaseSchema>;
1 change: 1 addition & 0 deletions packages/cli/src/lib/services/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const OptionsHooksSchema = z.object({
permissions: HooksSchema.optional(),
roles: HooksSchema.optional(),
settings: HooksSchema.optional(),
translations: HooksSchema.optional(),
webhooks: HooksSchema.optional(),
});

Expand Down

0 comments on commit ad5a3a1

Please sign in to comment.