-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…235) Closes #220. What does this PR do / solve? ----------------------------- When developing and/or testing features (e.g. the "load more" button, see #221) in a local environment, it can be handy to have some posts in the local database. Overview of changes ------------------- Introduce `./scripts/import-from-prod.js` which loads [adrien's profile and posts](https://openwhyd.org/adrien) to the local/testing database. How to test this PR? -------------------- Run this after starting a local database server (e.g. with `$ docker-compose up`): ```sh $ node scripts/import-from-prod.js ``` => http://localhost:8080/all should display several pages of tracks imported from https://openwhyd.org/adrien.
- Loading branch information
1 parent
711ee25
commit 9c6164f
Showing
3 changed files
with
129 additions
and
41 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// You may want to clear the test database with `$ npm run docker-seed` | ||
// before running this script. | ||
|
||
const request = require('request'); | ||
const mongodb = require('mongodb'); | ||
|
||
const ObjectID = id => mongodb.ObjectID.createFromHexString(id); | ||
|
||
// Parameters // TODO: get from command line arguments and/or env vars | ||
const url = 'mongodb://localhost:27117'; | ||
const dbName = 'openwhyd_test'; | ||
const username = 'adrien'; | ||
|
||
const connectToDb = ({ url, dbName }) => | ||
new Promise((resolve, reject) => | ||
mongodb.MongoClient.connect(url, (err, client) => { | ||
if (err) reject(err); | ||
else resolve({ db: client.db(dbName), client }); | ||
}) | ||
); | ||
|
||
const fetchUserData = ({ username }) => | ||
new Promise((resolve, reject) => { | ||
const url = `https://openwhyd.org/${username}?format=json`; | ||
console.log(`fetching tracks from ${url} ...`); | ||
request(url, function(error, response, body) { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve({ | ||
posts: JSON.parse(body).map(post => ({ | ||
...post, | ||
_id: ObjectID(post._id) | ||
})) | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
function genUserFromPost({ post }) { | ||
return { | ||
_id: ObjectID(post.uId), | ||
id: post.uId, | ||
name: post.uNm | ||
}; | ||
} | ||
|
||
const insertUser = ({ db, user }) => | ||
new Promise((resolve, reject) => { | ||
db.collection('user').insertOne(user, function(err, r) { | ||
if (err) reject(err); | ||
else resolve(); | ||
}); | ||
}); | ||
|
||
const insertPosts = ({ db, posts }) => | ||
new Promise((resolve, reject) => { | ||
db.collection('post').insertMany(posts, function(err, r) { | ||
if (err) reject(err); | ||
else resolve(); | ||
}); | ||
}); | ||
|
||
(async () => { | ||
console.log(`connecting to ${url}/${dbName} ...`); | ||
const { db, client } = await connectToDb({ url, dbName }); | ||
const { posts } = await fetchUserData({ username }); // or require(`./../${username}.json`); | ||
console.log(`imported ${posts.length} posts`); | ||
const user = genUserFromPost({ post: posts[0] }); | ||
// console.log('genUserFromPost =>', user); | ||
// await insertUser({ db, user }); // may cause E11000 duplicate key error collection: openwhyd_test.user, after seeding the db | ||
console.log('inserted user'); | ||
await insertPosts({ db, posts }); | ||
client.close(); | ||
})().catch(err => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |