Skip to content

Commit ad22da2

Browse files
committed
add seed script
1 parent 067e005 commit ad22da2

File tree

19 files changed

+2093
-0
lines changed

19 files changed

+2093
-0
lines changed

epicshop/post-set-playground.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { existsSync } from 'node:fs'
2+
import { join } from 'node:path'
3+
import { execa } from 'execa'
4+
5+
const seedPath = join(
6+
process.env.EPICSHOP_PLAYGROUND_DEST_DIR,
7+
'src',
8+
'db',
9+
'seed.ts',
10+
)
11+
12+
if (existsSync(seedPath)) {
13+
console.log('Running seed script...')
14+
try {
15+
await execa('npx', ['tsx', seedPath], {
16+
cwd: process.env.EPICSHOP_PLAYGROUND_DEST_DIR,
17+
stdio: 'inherit',
18+
})
19+
console.log('Seed script completed successfully')
20+
} catch (error) {
21+
console.error('Failed to run seed script:', error)
22+
}
23+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import path from 'node:path'
2+
import { DB } from './index.js'
3+
4+
async function seed() {
5+
const db = DB.getInstance(path.join(process.cwd(), 'db.sqlite'))
6+
7+
// Helper to run raw SQL (DB class does not expose this, so we use createTag etc. for clearing)
8+
// We'll clear by deleting all entries via getEntries/getTags and deleteEntry/deleteTag
9+
10+
// Delete all entry_tags by deleting entries and tags (should cascade if foreign keys are set)
11+
const entries = await db.getEntries()
12+
for (const entry of entries) {
13+
await db.deleteEntry(entry.id)
14+
}
15+
const tags = await db.getTags()
16+
for (const tag of tags) {
17+
await db.deleteTag(tag.id)
18+
}
19+
20+
// Create tags
21+
const tagsData = [
22+
{ name: 'work', description: 'Work-related entries' },
23+
{ name: 'personal', description: 'Personal thoughts and events' },
24+
{ name: 'travel', description: 'Travel experiences' },
25+
{ name: 'health', description: 'Health and wellness' },
26+
{ name: 'family', description: 'Family moments' },
27+
{ name: 'learning', description: 'Learning and education' },
28+
{ name: 'nature', description: 'Nature and outdoors' },
29+
{ name: 'food', description: 'Food and cooking' },
30+
]
31+
const createdTags: Array<{ id: number; name: string }> = []
32+
for (const tag of tagsData) {
33+
const created = await db.createTag(tag)
34+
createdTags.push({ id: created.id, name: created.name })
35+
}
36+
37+
// Helper to get tag ids by name
38+
const tagId = (name: string) => createdTags.find((t) => t.name === name)?.id
39+
40+
// Create entries
41+
const entriesData = [
42+
{
43+
title: 'First day at new job',
44+
content:
45+
'Started my new job today. Met the team and set up my workspace.',
46+
mood: 'excited',
47+
location: 'office',
48+
weather: 'sunny',
49+
isPrivate: 0,
50+
isFavorite: 1,
51+
tags: ['work', 'learning'],
52+
},
53+
{
54+
title: 'Family picnic',
55+
content:
56+
'Had a wonderful picnic with the family at the park. Kids played frisbee.',
57+
mood: 'happy',
58+
location: 'park',
59+
weather: 'cloudy',
60+
isPrivate: 0,
61+
isFavorite: 0,
62+
tags: ['family', 'nature', 'food'],
63+
},
64+
{
65+
title: 'Trip to Kyoto',
66+
content:
67+
'Explored temples and enjoyed local cuisine. The cherry blossoms were beautiful.',
68+
mood: 'amazed',
69+
location: 'Kyoto',
70+
weather: 'rainy',
71+
isPrivate: 0,
72+
isFavorite: 1,
73+
tags: ['travel', 'food', 'nature'],
74+
},
75+
{
76+
title: 'Morning run',
77+
content: 'Went for a 5k run. Felt energized and healthy.',
78+
mood: 'energized',
79+
location: 'neighborhood',
80+
weather: 'sunny',
81+
isPrivate: 1,
82+
isFavorite: 0,
83+
tags: ['health', 'personal'],
84+
},
85+
{
86+
title: 'Online course completed',
87+
content:
88+
'Finished a JavaScript course online. Built a small project as a final assignment.',
89+
mood: 'accomplished',
90+
location: 'home',
91+
weather: 'sunny',
92+
isPrivate: 0,
93+
isFavorite: 1,
94+
tags: ['learning', 'personal', 'work'],
95+
},
96+
]
97+
98+
for (const entry of entriesData) {
99+
const { tags, ...entryData } = entry
100+
const createdEntry = await db.createEntry(entryData)
101+
for (const tagName of tags) {
102+
const id = tagId(tagName)
103+
if (id) {
104+
await db.addTagToEntry({ entryId: createdEntry.id, tagId: id })
105+
}
106+
}
107+
}
108+
109+
console.log('Seed complete!')
110+
}
111+
112+
seed().catch((e) => {
113+
console.error(e)
114+
process.exit(1)
115+
})
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import path from 'node:path'
2+
import { DB } from './index.js'
3+
4+
async function seed() {
5+
const db = DB.getInstance(path.join(process.cwd(), 'db.sqlite'))
6+
7+
// Helper to run raw SQL (DB class does not expose this, so we use createTag etc. for clearing)
8+
// We'll clear by deleting all entries via getEntries/getTags and deleteEntry/deleteTag
9+
10+
// Delete all entry_tags by deleting entries and tags (should cascade if foreign keys are set)
11+
const entries = await db.getEntries()
12+
for (const entry of entries) {
13+
await db.deleteEntry(entry.id)
14+
}
15+
const tags = await db.getTags()
16+
for (const tag of tags) {
17+
await db.deleteTag(tag.id)
18+
}
19+
20+
// Create tags
21+
const tagsData = [
22+
{ name: 'work', description: 'Work-related entries' },
23+
{ name: 'personal', description: 'Personal thoughts and events' },
24+
{ name: 'travel', description: 'Travel experiences' },
25+
{ name: 'health', description: 'Health and wellness' },
26+
{ name: 'family', description: 'Family moments' },
27+
{ name: 'learning', description: 'Learning and education' },
28+
{ name: 'nature', description: 'Nature and outdoors' },
29+
{ name: 'food', description: 'Food and cooking' },
30+
]
31+
const createdTags: Array<{ id: number; name: string }> = []
32+
for (const tag of tagsData) {
33+
const created = await db.createTag(tag)
34+
createdTags.push({ id: created.id, name: created.name })
35+
}
36+
37+
// Helper to get tag ids by name
38+
const tagId = (name: string) => createdTags.find((t) => t.name === name)?.id
39+
40+
// Create entries
41+
const entriesData = [
42+
{
43+
title: 'First day at new job',
44+
content:
45+
'Started my new job today. Met the team and set up my workspace.',
46+
mood: 'excited',
47+
location: 'office',
48+
weather: 'sunny',
49+
isPrivate: 0,
50+
isFavorite: 1,
51+
tags: ['work', 'learning'],
52+
},
53+
{
54+
title: 'Family picnic',
55+
content:
56+
'Had a wonderful picnic with the family at the park. Kids played frisbee.',
57+
mood: 'happy',
58+
location: 'park',
59+
weather: 'cloudy',
60+
isPrivate: 0,
61+
isFavorite: 0,
62+
tags: ['family', 'nature', 'food'],
63+
},
64+
{
65+
title: 'Trip to Kyoto',
66+
content:
67+
'Explored temples and enjoyed local cuisine. The cherry blossoms were beautiful.',
68+
mood: 'amazed',
69+
location: 'Kyoto',
70+
weather: 'rainy',
71+
isPrivate: 0,
72+
isFavorite: 1,
73+
tags: ['travel', 'food', 'nature'],
74+
},
75+
{
76+
title: 'Morning run',
77+
content: 'Went for a 5k run. Felt energized and healthy.',
78+
mood: 'energized',
79+
location: 'neighborhood',
80+
weather: 'sunny',
81+
isPrivate: 1,
82+
isFavorite: 0,
83+
tags: ['health', 'personal'],
84+
},
85+
{
86+
title: 'Online course completed',
87+
content:
88+
'Finished a JavaScript course online. Built a small project as a final assignment.',
89+
mood: 'accomplished',
90+
location: 'home',
91+
weather: 'sunny',
92+
isPrivate: 0,
93+
isFavorite: 1,
94+
tags: ['learning', 'personal', 'work'],
95+
},
96+
]
97+
98+
for (const entry of entriesData) {
99+
const { tags, ...entryData } = entry
100+
const createdEntry = await db.createEntry(entryData)
101+
for (const tagName of tags) {
102+
const id = tagId(tagName)
103+
if (id) {
104+
await db.addTagToEntry({ entryId: createdEntry.id, tagId: id })
105+
}
106+
}
107+
}
108+
109+
console.log('Seed complete!')
110+
}
111+
112+
seed().catch((e) => {
113+
console.error(e)
114+
process.exit(1)
115+
})
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import path from 'node:path'
2+
import { DB } from './index.js'
3+
4+
async function seed() {
5+
const db = DB.getInstance(path.join(process.cwd(), 'db.sqlite'))
6+
7+
// Helper to run raw SQL (DB class does not expose this, so we use createTag etc. for clearing)
8+
// We'll clear by deleting all entries via getEntries/getTags and deleteEntry/deleteTag
9+
10+
// Delete all entry_tags by deleting entries and tags (should cascade if foreign keys are set)
11+
const entries = await db.getEntries()
12+
for (const entry of entries) {
13+
await db.deleteEntry(entry.id)
14+
}
15+
const tags = await db.getTags()
16+
for (const tag of tags) {
17+
await db.deleteTag(tag.id)
18+
}
19+
20+
// Create tags
21+
const tagsData = [
22+
{ name: 'work', description: 'Work-related entries' },
23+
{ name: 'personal', description: 'Personal thoughts and events' },
24+
{ name: 'travel', description: 'Travel experiences' },
25+
{ name: 'health', description: 'Health and wellness' },
26+
{ name: 'family', description: 'Family moments' },
27+
{ name: 'learning', description: 'Learning and education' },
28+
{ name: 'nature', description: 'Nature and outdoors' },
29+
{ name: 'food', description: 'Food and cooking' },
30+
]
31+
const createdTags: Array<{ id: number; name: string }> = []
32+
for (const tag of tagsData) {
33+
const created = await db.createTag(tag)
34+
createdTags.push({ id: created.id, name: created.name })
35+
}
36+
37+
// Helper to get tag ids by name
38+
const tagId = (name: string) => createdTags.find((t) => t.name === name)?.id
39+
40+
// Create entries
41+
const entriesData = [
42+
{
43+
title: 'First day at new job',
44+
content:
45+
'Started my new job today. Met the team and set up my workspace.',
46+
mood: 'excited',
47+
location: 'office',
48+
weather: 'sunny',
49+
isPrivate: 0,
50+
isFavorite: 1,
51+
tags: ['work', 'learning'],
52+
},
53+
{
54+
title: 'Family picnic',
55+
content:
56+
'Had a wonderful picnic with the family at the park. Kids played frisbee.',
57+
mood: 'happy',
58+
location: 'park',
59+
weather: 'cloudy',
60+
isPrivate: 0,
61+
isFavorite: 0,
62+
tags: ['family', 'nature', 'food'],
63+
},
64+
{
65+
title: 'Trip to Kyoto',
66+
content:
67+
'Explored temples and enjoyed local cuisine. The cherry blossoms were beautiful.',
68+
mood: 'amazed',
69+
location: 'Kyoto',
70+
weather: 'rainy',
71+
isPrivate: 0,
72+
isFavorite: 1,
73+
tags: ['travel', 'food', 'nature'],
74+
},
75+
{
76+
title: 'Morning run',
77+
content: 'Went for a 5k run. Felt energized and healthy.',
78+
mood: 'energized',
79+
location: 'neighborhood',
80+
weather: 'sunny',
81+
isPrivate: 1,
82+
isFavorite: 0,
83+
tags: ['health', 'personal'],
84+
},
85+
{
86+
title: 'Online course completed',
87+
content:
88+
'Finished a JavaScript course online. Built a small project as a final assignment.',
89+
mood: 'accomplished',
90+
location: 'home',
91+
weather: 'sunny',
92+
isPrivate: 0,
93+
isFavorite: 1,
94+
tags: ['learning', 'personal', 'work'],
95+
},
96+
]
97+
98+
for (const entry of entriesData) {
99+
const { tags, ...entryData } = entry
100+
const createdEntry = await db.createEntry(entryData)
101+
for (const tagName of tags) {
102+
const id = tagId(tagName)
103+
if (id) {
104+
await db.addTagToEntry({ entryId: createdEntry.id, tagId: id })
105+
}
106+
}
107+
}
108+
109+
console.log('Seed complete!')
110+
}
111+
112+
seed().catch((e) => {
113+
console.error(e)
114+
process.exit(1)
115+
})

0 commit comments

Comments
 (0)