Skip to content

Commit

Permalink
add missing JS code for account migration tutorial
Browse files Browse the repository at this point in the history
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
danphilibin committed Aug 16, 2022
1 parent 5d56c77 commit 1a4df9a
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/account-migration/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Contains example starter code for our [account migration tutorial](#). Uses a SQ
Initialize this project using npx:

```bash
npx create-interval-app --language typescript --template account-migration-tutorial
npx create-interval-app --language javascript --template account-migration
```

After the project is created, `cd` into its directory and run `yarn setup` to install dependencies and initialize the Prisma database.
2 changes: 1 addition & 1 deletion examples/account-migration/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"prisma": "3.15.2"
},
"prisma": {
"seed": "node --loader ts-node/esm prisma/seed.ts"
"seed": "node prisma/seed.js"
}
}
10 changes: 5 additions & 5 deletions examples/account-migration/javascript/prisma/seed.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { faker } from "@faker-js/faker";
import { Prisma, PrismaClient } from "@prisma/client";
import { generateSlug } from "../src/util";
const { faker } = require('@faker-js/faker');
const { PrismaClient } = require('@prisma/client');
const { generateSlug } = require('../src/util');

const prisma = new PrismaClient();

Expand All @@ -14,14 +14,14 @@ async function main() {
firstName,
lastName,
email: faker.internet.email(firstName, lastName),
username: generateSlug([firstName, lastName].join(" ")),
username: generateSlug([firstName, lastName].join(' ')),
},
});
}
}

main()
.catch((e) => {
.catch(e => {
console.error(e);
process.exit(1);
})
Expand Down
69 changes: 66 additions & 3 deletions examples/account-migration/javascript/src/index.js
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();
24 changes: 14 additions & 10 deletions examples/account-migration/javascript/src/util.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { PrismaClient } from '@prisma/client';
const { PrismaClient } = require('@prisma/client');

export const prisma = new PrismaClient();
const prisma = new PrismaClient();

module.exports.prisma = prisma;

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

export async function generateThumbnail(url) {
module.exports.generateThumbnail = async function generateThumbnail(url) {
await sleep(500);
return '';
}
};

export function generateSlug(input) {
module.exports.generateSlug = function generateSlug(input) {
return input.toLowerCase().replace(/[^-_.a-zA-Z\d]+/, '-');
}
};

export async function getCollisionSafeSlug(desiredSlug) {
module.exports.getCollisionSafeSlug = async function getCollisionSafeSlug(
desiredSlug
) {
const existingSlugs = (
await prisma.video.findMany({
where: {
Expand All @@ -42,9 +46,9 @@ export async function getCollisionSafeSlug(desiredSlug) {
}

return slug;
}
};

export async function findUsers(query) {
module.exports.findUsers = async function findUsers(query) {
return prisma.user.findMany({
where: {
OR: [
Expand All @@ -55,4 +59,4 @@ export async function findUsers(query) {
],
},
});
}
};
2 changes: 1 addition & 1 deletion examples/account-migration/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Contains example starter code for our [account migration tutorial](#). Uses a SQ
Initialize this project using npx:

```bash
npx create-interval-app --language typescript --template account-migration-tutorial
npx create-interval-app --language typescript --template account-migration
```

After the project is created, `cd` into its directory and run `yarn setup` to install dependencies and initialize the Prisma database.

0 comments on commit 1a4df9a

Please sign in to comment.