Skip to content

Instantly share code, notes, and snippets.

@tobkle
Last active August 15, 2019 22:00
Show Gist options
  • Save tobkle/bcf46bb84dcb39bcae0558b4d405d3af to your computer and use it in GitHub Desktop.
Save tobkle/bcf46bb84dcb39bcae0558b4d405d3af to your computer and use it in GitHub Desktop.
datasource db {
provider = "mysql"
url = "mysql://user:password@mysql.myserver.com:3306/mydb?sslmode=preferred"
default = true
}
generator photon {
provider = "photonjs"
}
generator nexus_prisma {
provider = "nexus-prisma"
}
model Currency {
id String @default(cuid()) @id @unique
code String?
name String?
symbol String?
}
model Language {
id String @default(cuid()) @id @unique
iso639_1 String?
iso639_2 String?
name String?
nativeName String?
}
model Translation {
id String @default(cuid()) @id @unique
from String?
iso639_1 String?
translated String?
}
model RegionalBloc {
id String @default(cuid()) @id @unique
acronym String?
name String?
otherAcronyms String[]
otherNames String[]
}
model Country {
id String @default(cuid()) @id @unique
name String
topLevelDomain String[]
alpha2Code String?
alpha3Code String?
callingCodes String[]
capital String?
altSpellings String[]
region String?
subregion String?
population Int?
latlng String[]
demonym String?
area Int?
gini String?
timezones String[]
borders String[]
nativeName String?
numericCode String?
currencies Currency[]
languages Language[]
translations Translation[]
flag String?
regionalBlocs RegionalBloc[]
cioc String?
}
// Ok, this is quick and dirty, but wanted to learn prisma2 with...
const Photon = require('@generated/photon');
const photon = new Photon.default();
const Countries = require('../data/restcountries.eu.all.json');
function translations(Translations) {
let _translations = [];
if (!Translations) return _translations;
const keys = Object.keys(Translations);
keys.forEach((key) =>
_translations.push({
iso639_1: key,
from: '',
translated: Translations[key] || ''
})
);
return _translations;
}
function regionalBlocs(RegionalBlocs) {
let _regionalBlocs = [];
if (!RegionalBlocs) return _regionalBlocs;
RegionalBlocs.forEach((block) => {
let newBlock = {};
block.acronym ? (newBlock.acronym = block.acronym) : null;
block.name ? (newBlock.name = block.name) : null;
block.otherAcronyms && block.otherAcronyms.length > 0
? (newBlock.otherAcronyms = { set: block.otherAcronyms })
: null;
block.otherNames && block.otherNames.length > 0 ? (newBlock.otherNames = { set: block.otherNames }) : null;
if (Object.keys(newBlock).length > 0) {
_regionalBlocs.push(newBlock);
}
});
return _regionalBlocs;
}
function addNotEmptyArray(target, name, arr) {
if (!arr) return target;
if (arr.length > 0) {
if (typeof arr[0] === 'object') {
target[name] = {
create: arr
};
} else if (name === 'latlng') {
const keys = Object.keys(arr);
const newArr = [];
keys.forEach((key) => {
newArr[key] = arr[key].toString();
});
target[name] = {
set: newArr
};
} else {
target[name] = {
set: arr
};
}
return target;
}
return target;
}
async function createCountry(Country) {
let _translations = translations(Country.translations);
let _regionalBlocs = regionalBlocs(Country.regionalBlocs);
const data = {};
data.name = Country.name || '';
data.alpha2Code = Country.alpha2Code || ''; //'AF'
data.alpha3Code = Country.alpha3Code || ''; //'AFG',
data.capital = Country.capital || ''; // 'Kabul',
data.region = Country.region || ''; //'Asia',
data.subregion = Country.subregion || ''; //'Southern Asia',
data.population = Country.population || 0; //27657145,
data.demonym = Country.demonym || ''; //'Afghan',
data.area = Country.area ? parseInt(Country.area) : 0; // 652230,
data.gini = Country.gini ? Country.gini.toString() : '0'; // 27.8,
data.nativeName = Country.nativeName || ''; // 'افغانستان',
data.numericCode = Country.numericCode || ''; // '004',
data.flag = Country.flag || '';
data.cioc = Country.cioc || ''; //'AFG'
addNotEmptyArray(data, 'topLevelDomain', Country.topLevelDomain);
addNotEmptyArray(data, 'callingCodes', Country.callingCodes);
addNotEmptyArray(data, 'altSpellings', Country.altSpellings);
addNotEmptyArray(data, 'latlng', Country.latlng);
addNotEmptyArray(data, 'timezones', Country.timezones);
addNotEmptyArray(data, 'borders', Country.borders);
addNotEmptyArray(data, 'currencies', Country.currencies);
addNotEmptyArray(data, 'languages', Country.languages);
addNotEmptyArray(data, 'translations', _translations);
addNotEmptyArray(data, 'regionalBlocs', _regionalBlocs);
return data;
}
function main() {
Countries.forEach(async (Country, index) => {
try {
let data = createCountry(Country);
let country = await photon.countries.create({ data });
if (country) console.log(country.name, 'added.');
} catch (error) {
console.log(error);
}
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment