Replies: 4 comments 5 replies
-
Hi @daawaan. We're also working on a directus project and asked ourselves the exact same thing. Till now I think it's only possible to receive the global presets by using the API and export them manually. We're in the middle of implementing a little script which should do that. It's far from ready to be released (and not even in a repository right now) but here is the current state. Maybe you can reuse parts of it: import { IDirectus, TypeMap } from '@directus/sdk';
import { writeFileSync } from 'fs';
import chalk, { Chalk } from 'chalk';
export enum Level {
INFO = 'INFO',
SUCCESS = 'SUCCESS',
WARN = 'WARN',
ERROR = 'ERROR',
}
const levelToChalkColorMap: {
[key in Level]: Chalk;
} = {
[Level.SUCCESS]: chalk.green,
[Level.INFO]: chalk.blue,
[Level.WARN]: chalk.yellow,
[Level.ERROR]: chalk.red,
};
const log = (message: string, level: Level) => {
console.log(levelToChalkColorMap[level](message));
};
export const exportDefaultPresets = async (
directus: IDirectus<TypeMap>,
collection: string,
target?: string
) => {
log(`Exporting default presets for collection ${collection}`, Level.INFO);
try {
const presets = await directus.presets.readByQuery({
filter: {
collection: {
_eq: collection,
},
user: {
_null: true,
},
bookmark: {
_null: true,
},
},
limit: -1,
});
if (!presets.data || presets.data.length === 0) {
log(`No presets found for ${collection}`, Level.WARN);
return;
} else {
log(
`Found ${presets.data?.length} ${
presets.data && presets.data?.length > 1 ? 'presets' : 'preset'
}`,
Level.INFO
);
const targetFile = `${
target ? `${target}/` : ''
}presets-${collection}.json`;
try {
writeFileSync(targetFile, JSON.stringify(presets.data, null, 2));
log(`Sucessfully exported presets to ${targetFile}`, Level.SUCCESS);
} catch (error) {
log(`Error while writing ${targetFile}: ${error}`, Level.ERROR);
}
}
} catch (error) {
log(`Failed to export presets: ${error}`, Level.ERROR);
}
}; Use the helper function like this: import { Directus } from '@directus/sdk';
import { exportDefaultPresets } from './export-default-presets';
const run = async () => {
const directus = new Directus('http://localhost:8055');
await directus.auth.login({
email: 'email',
password: 'xyz',
});
exportDefaultPresets(directus, '<collection_name>', './presets');
};
run(); It basically exports all global presets for a given collection and saves them in a file called Maybe the filter query needs to be adjusted to fit more use cases. And we didn't have time to create an import script yet. Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
The above code example relies on the SDK, which under the hood uses the |
Beta Was this translation helpful? Give feedback.
-
We finally found the time to open-source our first utility functions: https://github.com/liip/directus-utilities. We will continue to add helpful utilities to it. Right now we created a separate repository for this to keep up the pace implementing our projects with it but as soon as we have some spare time we'll try to bring it back to the core sdk. |
Beta Was this translation helpful? Give feedback.
-
@daawaan4U |
Beta Was this translation helpful? Give feedback.
-
During development, I create a lot of global presets & bookmarks and global dashboards so our end-users don't have to deal with the hassle anymore of setting up their own ones when viewing data. However, our problem is we can't find a way to programmatically migrate our changes to the production instance in this case. The
directus schema snapshot
command exists but it seems like it only takes a snapshot of your custom models' schema in the database but doesn't include system configurations for directus. Are we missing something? Looking forward for any help!Beta Was this translation helpful? Give feedback.
All reactions