This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
forked from reactjs/server-components-demo
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathseed.js
64 lines (56 loc) · 1.84 KB
/
seed.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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
'use strict';
const path = require('path');
const {readdir, unlink, writeFile} = require('fs/promises');
const {PrismaClient} = require('@prisma/client');
const prisma = new PrismaClient();
const NOTES_PATH = './notes';
const seedData = [
{
title: 'Meeting Notes',
body: 'This is an example note. It contains **Markdown**!',
},
{
title: 'Make a thing',
body: `It's very easy to make some words **bold** and other words *italic* with
Markdown. You can even [link to React's website!](https://www.reactjs.org).`,
},
{
title:
'A note with a very long title because sometimes you need more words',
body: `You can write all kinds of [amazing](https://en.wikipedia.org/wiki/The_Amazing)
notes in this app! These note live on the server in the \`notes\` folder.
`,
},
{title: 'I wrote this note today', body: 'It was an excellent note.'},
];
async function seed() {
await prisma.note.deleteMany();
const res = await Promise.all(
seedData.map((data) => prisma.note.create({data}))
);
const oldNotes = await readdir(path.resolve(NOTES_PATH));
await Promise.all(
oldNotes
.filter((filename) => filename.endsWith('.md'))
.map((filename) => unlink(path.resolve(NOTES_PATH, filename)))
);
await Promise.all(
res.map(({id, body}) => {
const data = new Uint8Array(Buffer.from(body));
return writeFile(path.resolve(NOTES_PATH, `${id}.md`), data, (err) => {
if (err) {
throw err;
}
});
})
);
prisma.$disconnect();
}
seed();