Reputation: 1
I want to insert data according to the following codes to the two tables named as productTable and discountCodeTable in the following way. These two tables have a many to many relationship and are connected to each other through a join-table called productToDiscount. I have the same syntax to import these changes through prisma, but I want to convert it to drizzle.
**create data by prisma
await db.discountCode.create({
data: {
code: data.code,
discountAmount: data.discountAmount,
discountType: data.discountType,
allProducts: data.allProducts,
products:
data.productIds != null
? { connect: data.productIds.map((id) => ({ id })) }
: undefined,
expiredAt: data.expiredAt,
limit: data.limit,
},
});
data that I want to insert
const addSchema = z
.object({
code: z.string().min(1),
discountAmount: z.coerce.number().int().min(1),
discountType: z.nativeEnum(DiscountCodeType),
allProducts: z.coerce.boolean(),
productIds: z.array(z.string()).optional(),
expiredAt: z.preprocess(
(value) => (value === '' ? undefined : value),
z.coerce.date().min(new Date()).optional()
),
limit: z.preprocess(
(value) => (value === '' ? undefined : value),
z.coerce.number().int().min(1).optional()
),
})
** product,discountCode and their join tables schema in drizzle:
export const ProductTable = pgTable('product', {
id: uuid('id').primaryKey().defaultRandom(),
name: varchar('name', { length: 70 }).notNull(),
priceInCent: integer('priceInCent').notNull(),
description: varchar('description', { length: 100 }).notNull(),
filePath: varchar('filePath').notNull(),
imagePath: varchar('imagePath').notNull(),
isAvailableForPurchase: boolean('isAvailableForPurchase')
.default(true)
.notNull(),
createdAt: timestamp('createdAt').notNull().defaultNow(),
updatedAt: timestamp('updatedAt').notNull(),
email: varchar('email', { length: 255 }).unique(),
});
export const DiscountCodeTable = pgTable('discountCode', {
//choose every columns name and features:
id: uuid('id').primaryKey().defaultRandom(),
code: varchar('code').unique(),
discountAmount: integer('discountAmount').notNull(),
discountType: DiscountCodeType('discountCodeType').default('FIXED').notNull(),
uses: integer('uses').default(0).notNull(),
isActive: boolean('isActive').default(true).notNull(),
allProducts: boolean('allProducts').default(false),
createdAt: timestamp('createdAt').notNull().defaultNow(),
expiredAt: timestamp('updatedAt'),
limit: integer('limit'),
});
export const productToDiscountCode = pgTable(
'productToDiscountCode',
{
productId: varchar('productId')
.notNull()
.references(() => ProductTable.id),
discountCodeId: varchar('discountCodeId')
.notNull()
.references(() => DiscountCodeTable.id),
},
(t) => ({
pk: primaryKey({ columns: [t.productId, t.discountCodeId] }),
})
);
how to convert it to drizzle?
Upvotes: 0
Views: 866