Skip to content

Commit

Permalink
refactor seed script and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
cherylli committed Sep 19, 2023
1 parent f414614 commit 5520db2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 36 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ To install all the project's dependencies run:
$ yarn install
```

## Prisma

```bash
# reset database/schema, and seed
$ prisma migrate reset

$ prisma db seed
```

## Running the app

```bash
Expand Down
37 changes: 3 additions & 34 deletions prisma/seed/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import {PrismaClient} from "@prisma/client";
import * as process from "process";
import {populateTablesWithRelations} from "./relations";
import {populateTables} from "./tables";

const prisma = new PrismaClient()

const Genders = require('./data/genders')
const Tiers = require('./data/tiers')
const VoyageRoles = require('./data/voyage-roles')
const VoyageStatus = require('./data/voyage-status')
const TechStackCategories = require('./data/tech-stack-categories')

const Users = require('./data/users')
const Voyages = require('./data/voyages')
const VoyageTeams = require('./data/voyage-teams')
const VoyageTeamMembers = require('./data/voyage-team-members')
const TechStackItems = require('./data/tech-stack-items')
const TeamTechStackItems = require('./data/team-tech-stack-items')

const deleteAllTables = async () => {
const tablenames = await prisma.$queryRaw<
Array<{ tablename: string }>
Expand All @@ -36,35 +24,16 @@ const deleteAllTables = async () => {
console.log('===\nAll tables deleted.\n===')
}

const populateTable = async (tableName: string, data) => {
await Promise.all(data.map(row => prisma[tableName].create({
data: row
})))
console.log(`${tableName} table populated.`)
}

(async function () {
try {
await deleteAllTables()
await populateTable("tier", Tiers)
await populateTable("gender", Genders)
await populateTable("voyageRole", VoyageRoles)
await populateTable("voyageStatus", VoyageStatus)
await populateTable("techStackCategory", TechStackCategories)
await populateTable("user", Users)
await populateTable("voyage", Voyages)
await populateTable("voyageTeam", VoyageTeams)
await populateTable("voyageTeamMember", VoyageTeamMembers)
await populateTable("techStackItem", TechStackItems)
await populateTable("teamTechStackItem", TeamTechStackItems)

await populateTables()
await populateTablesWithRelations()

console.log('===\nDatabase seeding completed.\n===')
} catch (e) {
console.error(e);
process.exit(1);
} finally {
await prisma.$disconnect();
}
})()
})();
2 changes: 0 additions & 2 deletions prisma/seed/relations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {PrismaClient} from "@prisma/client";


const prisma = new PrismaClient()

export const populateTablesWithRelations = async()=>{
Expand Down
36 changes: 36 additions & 0 deletions prisma/seed/tables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {PrismaClient} from "@prisma/client";
const prisma = new PrismaClient()

const Genders = require('./data/genders')
const Tiers = require('./data/tiers')
const VoyageRoles = require('./data/voyage-roles')
const VoyageStatus = require('./data/voyage-status')
const TechStackCategories = require('./data/tech-stack-categories')

const Users = require('./data/users')
const Voyages = require('./data/voyages')
const VoyageTeams = require('./data/voyage-teams')
const VoyageTeamMembers = require('./data/voyage-team-members')
const TechStackItems = require('./data/tech-stack-items')
const TeamTechStackItems = require('./data/team-tech-stack-items')

const populateTable = async (tableName: string, data) => {
await Promise.all(data.map(row => prisma[tableName].create({
data: row
})))
console.log(`${tableName} table populated.`)
}

export const populateTables = async()=> {
await populateTable("tier", Tiers)
await populateTable("gender", Genders)
await populateTable("voyageRole", VoyageRoles)
await populateTable("voyageStatus", VoyageStatus)
await populateTable("techStackCategory", TechStackCategories)
await populateTable("user", Users)
await populateTable("voyage", Voyages)
await populateTable("voyageTeam", VoyageTeams)
await populateTable("voyageTeamMember", VoyageTeamMembers)
await populateTable("techStackItem", TechStackItems)
await populateTable("teamTechStackItem", TeamTechStackItems)
}

0 comments on commit 5520db2

Please sign in to comment.