-
Notifications
You must be signed in to change notification settings - Fork 2
Add a script that allows developpers to generate random seed data #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f509e41
Add a script that allows developpers to generate random seed data
Ked57 49bd1bb
Don't commit the seed file, skill ids are random and seeds need to be…
Ked57 d0b9aae
Fix seeds saving path
Ked57 18f5f5e
Fix review and fix userSkill created_at not sync with userDesire's
Ked57 d108b93
Remove seeds.sql from root folder
Ked57 c3da0b3
adapt script to new db layout
Ked57 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Skill.json | ||
| seeds.sql |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| [ | ||
| { | ||
| "name": "Paris" | ||
| }, | ||
| { | ||
| "name": "Nantes" | ||
| }, | ||
| { | ||
| "name": "Singapore" | ||
| }, | ||
| { | ||
| "name": "Bordeaux" | ||
| }, | ||
| { | ||
| "name": "Brest" | ||
| }, | ||
| { | ||
| "name": "Montreal" | ||
| }, | ||
| { | ||
| "name": "Grenoble" | ||
| }, | ||
| { | ||
| "name": "Lyon" | ||
| }, | ||
| { | ||
| "name": "Rennes" | ||
| }, | ||
| { | ||
| "name": "Lille" | ||
| } | ||
| ] |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| [ | ||
| { | ||
| "index": 1, | ||
| "color": "yellow", | ||
| "label": "languages-and-frameworks", | ||
| "x": "left", | ||
| "y": "top", | ||
| "id": "89780de3-4a4c-40c2-bcdf-b5d15a48437a" | ||
| }, | ||
| { | ||
| "index": 2, | ||
| "color": "violet", | ||
| "label": "platforms", | ||
| "x": "right", | ||
| "y": "top", | ||
| "id": "06420261-3e78-4a91-bc6a-1a52cad5d6a1" | ||
| }, | ||
| { | ||
| "index": 3, | ||
| "color": "blue", | ||
| "label": "tools", | ||
| "x": "left", | ||
| "y": "bot", | ||
hgwood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "id": "c3341edb-3c1f-4e3d-bf89-8e795eb13690" | ||
| }, | ||
| { | ||
| "index": 4, | ||
| "color": "cyan", | ||
| "label": "technics-and-methods", | ||
| "x": "right", | ||
| "y": "bot", | ||
| "id": "89f5e9a5-5ce6-416c-bed9-dd736546aa7f" | ||
| } | ||
| ] | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| [ | ||
| { | ||
| "name": "Frontend", | ||
| "id": "71c8f42c-7182-444d-8133-3a3a52ae7912" | ||
| }, | ||
| { | ||
| "name": "Backend", | ||
| "id": "16198ae7-40bb-4fcd-ae5d-ce371902dce2" | ||
| }, | ||
| { | ||
| "name": "Agilité", | ||
| "id": "5a83e55d-22f2-428a-a5e2-effade6b6be5" | ||
| }, | ||
| { | ||
| "name": "Maker", | ||
| "id": "b53a5cdb-6269-4965-ae5b-f42c9611664f" | ||
| }, | ||
| { | ||
| "name": "Réseau", | ||
| "id": "817e3dae-01da-446a-920b-a3a5a9f57bea" | ||
| }, | ||
| { | ||
| "name": "Web", | ||
| "id": "7e8e6f07-7844-4c1f-aa91-1dda49555ee3" | ||
| }, | ||
| { | ||
| "name": "Security", | ||
| "id": "eb4edee0-9351-4fce-b04f-acbee63634b0" | ||
| }, | ||
| { | ||
| "name": "Microservices", | ||
| "id": "c42d872f-227e-49ce-8a97-2af8aa021fbd" | ||
| }, | ||
| { | ||
| "name": "Network", | ||
| "id": "6be68073-0f12-42bc-9835-07f8e81ea4a3" | ||
| }, | ||
| { | ||
| "name": "Ops", | ||
| "id": "2117fd9e-bda8-4b0a-b1f1-6ea68bca3443" | ||
| }, | ||
| { | ||
| "name": "DevOps", | ||
| "id": "7ee06efb-f337-4541-b160-10e35cd5b574" | ||
| }, | ||
| { | ||
| "name": "IA", | ||
| "id": "5451823f-965c-40e7-85db-05ff8c7a370d" | ||
| }, | ||
| { | ||
| "name": "Data", | ||
| "id": "fa4af642-a890-4966-9224-2c607807ef68" | ||
| }, | ||
| { | ||
| "name": "Mobile", | ||
| "id": "c80dbcaa-b6d5-42fb-b375-0bb3146fcbbe" | ||
| } | ||
| ] |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import { writeFile } from "fs/promises"; | ||
| import { datatype, date, image, name, random } from "faker"; | ||
| import newJsonSql from "json-sql"; | ||
|
|
||
| const jsonSql = newJsonSql(); | ||
| jsonSql.configure({ | ||
| dialect: "postgresql", | ||
| wrappedIdentifiers: true, | ||
| separatedValues: false, | ||
| }); | ||
|
|
||
| type Agency = { name: string }; | ||
| type Skill = { id: string; name: string; categoryId: string }; | ||
|
|
||
| const agencies: Agency[] = require("./Agency.json"); | ||
| const categories = require("./Category.json"); | ||
| const skills: Skill[] = require("./Skill.json"); | ||
| const topics = require("./Topic.json"); | ||
|
|
||
| type User = { | ||
| email: string; | ||
| name: string; | ||
| picture: string; | ||
| }; | ||
|
|
||
| type UserAgency = { | ||
| userEmail: string; | ||
| agency: string; | ||
| created_at: string; | ||
| }; | ||
|
|
||
| type UserSkillDesire = { | ||
| userEmail: string; | ||
| skillId: string; | ||
| skillLevel: number; | ||
| desireLevel: number; | ||
| created_at: string; | ||
| }; | ||
|
|
||
| const config = { | ||
| nbUser: 500, | ||
| nbSkillEntriesPerUser: 30, | ||
| dateStart: "2020/01/01", | ||
| dateEnd: "2021/12/30", | ||
| }; | ||
|
|
||
| const users: User[] = []; | ||
| const userSkillDesires: UserSkillDesire[] = []; | ||
| const userAgencies: UserAgency[] = []; | ||
|
|
||
| const generateUniqueDate = ( | ||
| source: UserSkillDesire[], | ||
| userEmail: string, | ||
| skillid: string | ||
| ) => { | ||
| let proposedDate = ""; | ||
| do { | ||
| proposedDate = date | ||
| .between(config.dateStart, config.dateEnd) | ||
| .toISOString() | ||
| .split("T")[0]; | ||
| } while ( | ||
| source.find( | ||
| (row) => | ||
| row.userEmail === userEmail && | ||
| row.skillId === skillid && | ||
| row.created_at === proposedDate | ||
| ) | ||
| ); | ||
| return proposedDate; | ||
| }; | ||
|
|
||
| const generateUser = (): User => { | ||
| const firstName = name.firstName().replace("'", ""); | ||
| const lastName = name.lastName().replace("'", ""); | ||
| return { | ||
| email: `${firstName.toLowerCase()}.${lastName.toLowerCase()}@zenika.com`, | ||
| name: `${firstName} ${lastName}`, | ||
| picture: image.avatar(), | ||
| }; | ||
| }; | ||
|
|
||
| const generateUserAgency = (userEmail: string): UserAgency => { | ||
| return { | ||
| userEmail, | ||
| agency: random.arrayElement(agencies).name, | ||
| created_at: "2020/01/01", | ||
| }; | ||
| }; | ||
|
|
||
| const generateUserSkillAndDesire = ( | ||
| userEmail: string | ||
| ): UserSkillDesire => { | ||
| const skillId = | ||
| skills[datatype.number({ min: 0, max: skills.length - 1 })].id; | ||
hgwood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const created_at = generateUniqueDate(userSkillDesires, userEmail, skillId); | ||
| return { | ||
| userEmail, | ||
| skillId, | ||
| skillLevel: datatype.number({ min: 1, max: 5 }), | ||
| desireLevel: datatype.number({ min: 1, max: 5 }), | ||
| created_at, | ||
| }; | ||
| }; | ||
|
|
||
| for (let i = 0; i < config.nbUser; ++i) { | ||
| const user = generateUser(); | ||
| users.push(user); | ||
| userAgencies.push(generateUserAgency(user.email)); | ||
| for (let k = 0; k < config.nbSkillEntriesPerUser; ++k) { | ||
| const generatedSkillAndDesire = generateUserSkillAndDesire(user.email); | ||
| userSkillDesires.push(generatedSkillAndDesire); | ||
| } | ||
| } | ||
|
|
||
| const userInsertQuery = jsonSql.build({ | ||
| type: "insert", | ||
| table: "User", | ||
| values: users, | ||
| }); | ||
|
|
||
| const userAgenciesInsertQuery = jsonSql.build({ | ||
| type: "insert", | ||
| table: "UserAgency", | ||
| values: userAgencies, | ||
| }); | ||
|
|
||
| const userSkillsInsertQuery = jsonSql.build({ | ||
| type: "insert", | ||
| table: "UserSkillDesire", | ||
| values: userSkillDesires, | ||
| }); | ||
|
|
||
| const result = `${userInsertQuery.query} | ||
| ${userAgenciesInsertQuery.query} | ||
| ${userSkillsInsertQuery.query}`; | ||
|
|
||
| (async () => { | ||
| await writeFile("./hasura/seeds/seeds.sql", result); | ||
| console.log("Seeds succesfully generated into hasura/seeds/seeds.sql file"); | ||
| })(); | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.