-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
5414329
commit 528bf3d
Showing
3 changed files
with
19 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -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); | ||
|
||
|
This file contains 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