Last active
November 16, 2020 15:28
-
-
Save UnbearableBear/fbc0fa5c939cd365c5ab00861e2023fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require("isomorphic-unfetch"); | |
const { createClient } = require("@urql/core"); | |
const HASURA_GRAPHQL_ADMIN_SECRET = process.env.HASURA_GRAPHQL_ADMIN_SECRET; | |
const HASURA_GRAPHQL_ENDPOINT = process.env.GRAPHQL_ENDPOINT; | |
const client = createClient({ | |
url: HASURA_GRAPHQL_ENDPOINT, | |
requestPolicy: "network-only", | |
fetchOptions: { | |
headers: { | |
"Content-Type": "application/json", | |
"x-hasura-admin-secret": HASURA_GRAPHQL_ADMIN_SECRET, | |
}, | |
}, | |
fetch, | |
}); | |
const data = require("../../cdtn/packages/code-du-travail-data/dist/dump.data.json"); | |
const { SOURCES, getSourceByRoute } = require("../../cdtn/packages/sources"); | |
const rawThemes = data.filter((document) => document.source === "themes"); | |
const rawThemeRelations = []; | |
const rawThemeContentRelations = []; | |
const findReferenceQuery = ` | |
query FindReference($slug: String!, $source: String!) { | |
documents(where: {slug: {_eq: $slug}, source: {_eq: $source}}) { | |
cdtnId: cdtn_id | |
} | |
} | |
`; | |
async function main() { | |
for (const { breadcrumbs, cdtnId, position, refs } of rawThemes) { | |
// prepare theme to theme relations | |
const normalizedPosition = position ? Number.parseInt(position.toString().split("").pop(), 10) : 0; | |
let parentId = null; | |
if (breadcrumbs.length) { | |
const parentThemeSlug = breadcrumbs.pop().slug; | |
parentId = rawThemes.find( | |
(theme) => `/themes/${theme.slug}` === parentThemeSlug | |
).cdtnId; | |
} | |
rawThemeRelations.push({ | |
data: { | |
position: normalizedPosition, | |
}, | |
document_a: parentId, | |
document_b: cdtnId, | |
type: "theme" | |
}); | |
// prepare theme to content relations | |
for ( const [i, { url }] of refs.entries()) { | |
const urlArray = url.split("#").shift().split("/"); | |
const slug = urlArray.pop(); | |
// if it starts with http then it's an external url ! | |
const source = getSourceByRoute(urlArray.pop()); | |
// query ends here | |
const { data: { documents }} = await client | |
.query(findReferenceQuery, { slug, source: source === SOURCES['SHEET_MT'] ? SOURCES['SHEET_MT_PAGE'] : source}) | |
.toPromise(); | |
const relatedDocument = documents[0]; | |
if (relatedDocument && relatedDocument.cdtnId) { | |
rawThemeContentRelations.push({ | |
data: { | |
position: i, | |
}, | |
document_a: cdtnId, | |
document_b: relatedDocument.cdtnId, | |
type: "theme-content" | |
}); | |
} | |
}; | |
}; | |
console.log(` | |
INSERT INTO public.documents (cdtn_id, initial_id, title, meta_description, source, slug, text, document, is_published, is_searchable, is_available) | |
VALUES ${rawThemes.map(insertIntoDocuments).join(",\n")}; | |
INSERT INTO public.document_relations (document_a, document_b, type, data) | |
VALUES ${rawThemeRelations | |
.concat(rawThemeContentRelations) | |
.map(insertIntoRelations) | |
.join(",\n")}; | |
`); | |
} | |
function insertIntoDocuments(document) { | |
const { | |
breadcrumbs: _, | |
position: __, | |
excludeFromSearch: ___, | |
refs: ____, | |
children: _____, | |
cdtnId, | |
id: initialId, | |
source, | |
slug, | |
title, | |
text, | |
...props | |
} = document; | |
let formattedSlug = slug; | |
if(source === "themes") { | |
const splittedSlug = slug.split("-"); | |
splittedSlug.shift(); | |
formattedSlug = splittedSlug.join("-"); | |
} | |
return `('${cdtnId}', '${initialId}', '${title.replace(/'/g, "’")}', '${ | |
props.description ? props.description.replace(/'/g, "’") : "" | |
}', '${source}', '${formattedSlug}', $$${text || title}$$, $$${JSON.stringify(props)}$$, TRUE, TRUE, TRUE)`; | |
} | |
function insertIntoRelations(relation) { | |
const { document_a, document_b, data, type } = relation; | |
return `(${ | |
document_a ? `'${document_a}'` : "NULL" | |
}, '${document_b}', '${type}', '${JSON.stringify(data)}')`; | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment