Skip to content

Instantly share code, notes, and snippets.

@hx-natthawat
Last active July 12, 2023 08:08
Show Gist options
  • Select an option

  • Save hx-natthawat/8ffbebac78a1fa461f448ee0cb5cf967 to your computer and use it in GitHub Desktop.

Select an option

Save hx-natthawat/8ffbebac78a1fa461f448ee0cb5cf967 to your computer and use it in GitHub Desktop.
Directus - Schema Migration
const fetch = require("cross-fetch");
const BASE_DIRECTUS_URL = "http://localhost:8055";
const BASE_ACCESS_TOKEN = "rnIg69xxxxxxxxxxxxxxxxxxxP3F";
const TARGET_DIRECTUS_URL = "http://123.123.123.123:8055";
const TARGET_ACCESS_TOKEN = "UTrcUvyyyyyyyyyyyyyyyyyyyyyk_2";
async function main() {
const snapshot = await getSnapshot();
console.log(snapshot);
const diff = await getDiff(snapshot);
console.log(diff);
await applyDiff(diff);
}
main();
async function getSnapshot() {
const URL = `${BASE_DIRECTUS_URL}/schema/snapshot?access_token=${BASE_ACCESS_TOKEN}`;
const { data } = await fetch(URL).then((r) => r.json());
return data;
}
async function getDiff(snapshot) {
const URL = `${TARGET_DIRECTUS_URL}/schema/diff?access_token=${TARGET_ACCESS_TOKEN}`;
const { data } = await fetch(URL, {
method: "POST",
body: JSON.stringify(snapshot),
headers: {
"Content-Type": "application/json",
},
}).then((r) => r.json());
return data;
}
async function applyDiff(diff) {
const URL = `${TARGET_DIRECTUS_URL}/schema/apply?access_token=${TARGET_ACCESS_TOKEN}`;
await fetch(URL, {
method: "POST",
body: JSON.stringify(diff),
headers: {
"Content-Type": "application/json",
},
});
}
{
"name": "migration",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"migrate": "node migrate.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cross-fetch": "^4.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment