Skip to content

Commit

Permalink
feat: data faker (#69)
Browse files Browse the repository at this point in the history
* feat: added data faker

* fix: added the truncation before upserting

* chore: install faker

* refactor: remove redundant function

the onConflictDoNothing is redundant
as we are not expecting any conflict when seeding with faker

* fix: added password hashing

---------

Co-authored-by: yamyam263 <[email protected]>
  • Loading branch information
aruaycodes and yamcodes authored Oct 18, 2023
1 parent 5414329 commit 528bf3d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
Binary file modified bun.lockb
Binary file not shown.
29 changes: 18 additions & 11 deletions db/seed.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { exit } from 'process';
import { db } from '@/database.providers';
import { users } from '@/users/users.model';
import { faker } from '@faker-js/faker';

const data = {
id: 1, //do not use 'users.id.default', seed should be idempotent
email: '[email protected]',
username: 'test',
password: 'test',
bio: 'test',
image: 'test',
};
console.log('Upserting user: ', data);
await db.insert(users).values(data).onConflictDoNothing();
console.log('User upserted');
console.log('Truncating the user database');
await db.delete(users);
console.log('The database is empty: ', await db.select().from(users));

for (let i = 0; i < 10; i++) {
const data = {
id: faker.datatype.number(),
email: faker.internet.email(),
username: faker.internet.userName(),
password: await Bun.password.hash(faker.internet.password()),
bio: faker.lorem.text(),
image: faker.image.imageUrl(),
};
console.log('Upserting user:', data);

await db.insert(users).values(data);
console.log('User upserted');
}
const userResult = await db.select().from(users);
console.log('User result: ', userResult);

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"devDependencies": {
"@biomejs/biome": "1.2.2",
"@faker-js/faker": "^8.2.0",
"@types/node": "^20.8.3",
"bun-types": "latest",
"drizzle-kit": "^0.19.13",
Expand Down

0 comments on commit 528bf3d

Please sign in to comment.