Open JonathonRP opened 1 year ago
await db.insert(markets).values({ organisationUuid: '324583a3-51b0-4ca9-976b-4394e82833ae', city: 'Gent', country: 'Belgium', startDate: new Date('2021-05-01T10:00:00.000Z'), endDate: new Date('2021-05-01T18:00:00.000Z'), province: 'Oost-Vlaanderen', size: 100, streetname: 'Korenmarkt', streetnumber: '1', type: ['FLEE_MARKET', 'ANTIQUES_MARKET', 'CLOTHES_MARKET'], zipcode: '9000', });
console.log('Seed complete!');
And have a npm script to run it:
"drizzle:seed": "dotenv -e .env.dev tsx src/database/seed.ts"
You can just create a typescript file:
await db.insert(markets).values({ organisationUuid: '324583a3-51b0-4ca9-976b-4394e82833ae', city: 'Gent', country: 'Belgium', startDate: new Date('2021-05-01T10:00:00.000Z'), endDate: new Date('2021-05-01T18:00:00.000Z'), province: 'Oost-Vlaanderen', size: 100, streetname: 'Korenmarkt', streetnumber: '1', type: ['FLEE_MARKET', 'ANTIQUES_MARKET', 'CLOTHES_MARKET'], zipcode: '9000', });
console.log('Seed complete!');
To execute
And have a npm script to run it:
"drizzle:seed": "dotenv -e .env.dev tsx src/database/seed.ts"
What technology are you using if I may ask? I tried a similar approach with NextJS (13.4 App router). Unfortunately I keep getting an error. I'll try to get it to work in a standalone package.
You can just create a typescript file:
await db.insert(markets).values({ organisationUuid: '324583a3-51b0-4ca9-976b-4394e82833ae', city: 'Gent', country: 'Belgium', startDate: new Date('2021-05-01T10:00:00.000Z'), endDate: new Date('2021-05-01T18:00:00.000Z'), province: 'Oost-Vlaanderen', size: 100, streetname: 'Korenmarkt', streetnumber: '1', type: ['FLEE_MARKET', 'ANTIQUES_MARKET', 'CLOTHES_MARKET'], zipcode: '9000', }); console.log('Seed complete!');
To execute
And have a npm script to run it:
"drizzle:seed": "dotenv -e .env.dev tsx src/database/seed.ts"
What technology are you using if I may ask? I tried a similar approach with NextJS (13.4 App router). Unfortunately I keep getting an error. I'll try to get it to work in a standalone package.
My idea of seeding the database, is a development or testing purpose action. The .ts (or .js) file with the inserts is simply called and ran with tsx (you can just use "node ./seed.js" for regular JS). So it has nothing to do with Next.js.
But fyi, I have a node express project. A drizzle client is created and exported as "db". That is used to seed the database.
Would be better if you could execute some code together with the migrations transactions. That way your seed can follow your migrations. Since the migrations are defined In SQL, I guess you can define insertions over there. But it would be more comfortable to have them in TS, if somehow we could get a callback before/after each migration running to associate the seed with that.
Is this actionable @AndriiSherman ?
We have a use case for seeding in production, and it would be great if we could create them using ts.
We store some settings in a table, and the app doesn't expose an API to manage them, so our idea is to seed the database initially and (maybe) do some transformation later, using the same (or similar) migration system to prevent a seeding from running twice on a DB.
It would be great if the migration system allowed us to manage the schema and mutate the data. We are fixing issues in the data with "migrations", and we want to move to drizzle for new microservices, but this is a blocker for us now. We may need to implement our system if this is not a priority.
I seed it like this -> https://github.com/deadcoder0904/next-14-lucia-v3-sqlite-drizzle-conform-zod-email-verification-otp-server-actions/blob/main/seed/insert.ts
import { generateId } from 'lucia'
import { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3'
import { db } from '@/app/db/index'
import { userTable } from '@/app/db/drizzle.schema'
const seedUsers = (db: BetterSQLite3Database<Record<string, never>>) => {
const userData: (typeof userTable.$inferInsert)[] = [
{
email: 'a@a.com',
emailVerified: 1,
},
{
email: 'b@b.com',
emailVerified: 1,
},
{
email: 'c@c.com',
emailVerified: 1,
},
]
try {
db.insert(userTable).values(userData).run()
const users = db.select().from(userTable).all()
console.log({ users })
} catch (err) {
console.error('Something went wrong...')
console.error(err)
}
}
const main = () => {
console.log('🧨 Started seeding the database...\n')
seedUsers(db)
console.log('\n🧨 Done seeding the database successfully...\n')
}
main()
And in package.json
, I use:
"db:seed": "node --import tsx --env-file .env ./seed/insert.ts",
"db:delete": "node --import tsx --env-file .env ./seed/delete.ts",
You can check the delete script here -> https://github.com/deadcoder0904/next-14-lucia-v3-sqlite-drizzle-conform-zod-email-verification-otp-server-actions/blob/main/seed/delete.ts
Describe what you want
Would love to be able to seed database like migrations.