forked from pantharshit00/fcc_dbmigrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.js
96 lines (85 loc) · 2.81 KB
/
migrate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// !!!!!!!!!!!!!! EDIT CONFIG FILE FIRST !!!!!!!!!!
// IMPORTANT IMPORTANT INSTALL wp-basic auth in your wordpress install
// Download it from github https://github.com/WP-API/Basic-Auth and install it as a plugin
// JUST CHANGING THING A BIT WE ARE GONNA USE THE DATABASE
// INSTEAD OF JSON FILE FOR MIGRATION
// AS IT IS EFFICIENT AND PAINLESS
// YOU CAN CONNECT TO DB EASILY AS YOU CAN CONNECT FROM SQL STUDIO
// PLACE INFO IN CONFIG JS AS YOU MIGHT LEAK IT
// IMPORTS (JUST FOR YOUR INFO YOU CAN USE import from syntax USING BABEL-NODE)
// WE ARE GOING TO USE SEQUELIZE FROM OUR ORM
const Sequelize = require('sequelize')
const fetch = require('node-fetch')
const config = require('./config')
const sequelize = new Sequelize(
config.DB_NAME,
config.DB_USER,
config.DB_PASS,
{
host: config.DB_HOST,
dialect: config.DB_DIALECT,
}
)
let counter = 0
// important add one which you are using in the original db
// $ yarn add pg pg-hstore
// $ yarn add mysql2
// $ yarn add sqlite3
// $ yarn add tedious // MSSQL
// see sequelize docs for more info
// Modify query as you like to get the data required
async function getData() {
// might you need to test the query in sql studio
const res = await sequelize.query(
`SELECT content_id,content_title,content_html,content_teaser,date_created FROM content WHERE (folder_id = '109' AND DATALENGTH(content_html) > 0)`
)
const posts = res[0]
for (post of posts) {
//MAKE API CALL TO WP
const url = config.WP_URI + 'student-profile-page'
// ADD DATA HERE IF YOU REQUIRE CUSTOM POST TYPES CATEGORIES ETC
// Regexp
// let name = post.content_title
// Make it lower case
// name = name.toLowerCase()
// Now Magic!
// name = name.replace(/(\s?\bdr\b|\s\bfr\b|\s\btor\b|\s?[$&+,:;=?@#|'<>.^*()%!-])/g, '');
//replace space by dash
// name = name.replace(' ', '-');
// const isoDate = new Date(post.date_created).toISOString()
const postData = {
// slug: name,
content: post.content_html,
title: post.content_title,
status: 'publish',
excerpt: post.content_teaser,
// date: isoDate,
// 'faculty-department': [4],
// 'institute-name': [26],
// 'staff-department': [28],
// 'household-type': [29],
}
// FOR AUTHENTICATION FOR WP
const base64User = new Buffer(
`${config.WP_USER}:${config.WP_PASS}`
).toString('base64')
fetch(url, {
body: JSON.stringify(postData),
method: 'POST',
headers: {
'Content-type': 'application/json',
Authorization: `Basic ${base64User}`,
},
})
.then(res => res.json())
.then(res => {
// LOGING HOW MUCH DONE
counter += 1
console.log('POST INSERTED ' + counter)
})
}
}
getData()
sequelize.sync().then(() => {
console.log('YOOO UP n RUNNING')
})