-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add missing JS code for account migration tutorial
The original version of this template didn't ship with example code. Looks like we missed the JS version when updating the examples to include a full working demo.
- Loading branch information
1 parent
5d56c77
commit 1a4df9a
Showing
6 changed files
with
88 additions
and
21 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
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 |
---|---|---|
@@ -1,9 +1,72 @@ | ||
import 'dotenv/config'; | ||
import Interval from '@interval/sdk'; | ||
const { Interval, io, ctx } = require('@interval/sdk'); | ||
require('dotenv').config(); // loads environment variables from .env | ||
const { | ||
findUsers, | ||
generateThumbnail, | ||
getCollisionSafeSlug, | ||
prisma, | ||
} = require('./util'); | ||
|
||
const interval = new Interval({ | ||
apiKey: process.env.INTERVAL_KEY, | ||
actions: {}, | ||
actions: { | ||
import_videos: async () => { | ||
const user = await io.search('Select an account', { | ||
onSearch: query => { | ||
return findUsers(query); | ||
}, | ||
renderResult: u => ({ | ||
label: `${u.firstName} ${u.lastName}`, | ||
description: u.email, | ||
}), | ||
}); | ||
|
||
const videosFile = await io.experimental.input.file('Select a file', { | ||
allowedExtensions: ['.json'], | ||
}); | ||
|
||
const videos = await videosFile.json(); | ||
|
||
await io.display.table('Videos to import', { | ||
data: videos, | ||
helpText: 'Press Continue to run the import.', | ||
}); | ||
|
||
const confirmed = await io.confirm(`Import ${videos.length} videos?`); | ||
if (!confirmed) return 'Action canceled, no videos imported'; | ||
|
||
ctx.loading.start({ | ||
title: 'Uploading videos...', | ||
itemsInQueue: videos.length, | ||
}); | ||
|
||
const importedVideos = []; | ||
|
||
for (let i = 0; i < videos.length; i++) { | ||
// use our app's internal methods to create the missing inputs | ||
const thumbnailUrl = await generateThumbnail(videos[i].url); | ||
const slug = await getCollisionSafeSlug(videos[i].name); | ||
|
||
const createdAt = new Date(videos[i].createdAt * 1000); | ||
|
||
const video = await prisma.video.create({ | ||
data: { | ||
title: videos[i].name, | ||
url: videos[i].url, | ||
thumbnailUrl, | ||
slug, | ||
createdAt, | ||
user: { connect: { id: user.id } }, | ||
}, | ||
}); | ||
|
||
importedVideos.push(video); | ||
ctx.loading.completeOne(); | ||
} | ||
|
||
return `Imported ${importedVideos.length} videos for ${user.email}`; | ||
}, | ||
}, | ||
}); | ||
|
||
interval.listen(); |
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