Rorsch
Rorsch
DTDrizzle Team
Created by Rorsch on 7/31/2024 in #help
findMany with custom field
Thank you as always @Raphaël M (@rphlmr) ⚡ , I decided to use raw sql to handle this feature instead
5 replies
DTDrizzle Team
Created by Rorsch on 7/31/2024 in #help
findMany with custom field
What i want is for it to look something like this
{
...
orderLineItems: [
{ serviceName: 'Service A', qty: 1 },
{ serviceName: 'Service B', qty: 2 }
]
}
{
...
orderLineItems: [
{ serviceName: 'Service A', qty: 1 },
{ serviceName: 'Service B', qty: 2 }
]
}
5 replies
DTDrizzle Team
Created by Rorsch on 7/23/2024 in #help
Fetch data based on client's timezone.
interesting approach. yes, i think i can do that for now. thank you for giving me some insights!
3 replies
DTDrizzle Team
Created by Rorsch on 7/15/2024 in #help
How to Normalize Database using Drizzle?
thank you for your insights! at least i think I'm on the right track 😄
13 replies
DTDrizzle Team
Created by Rorsch on 7/15/2024 in #help
How to Normalize Database using Drizzle?
@Raphaël M (@rphlmr) ⚡ thank you! it's working now, but I need to use dotenv on my DB config to read the env file. I have another question just for my improvements. Am I doing the database normalization the right way as it should be? Are people in the industry doing it this way as well? Or there should be a better way to tackle this kind of problem? I'm currently saving the migration script as /db/migration/YYYYMMDD_migrate_orders_to_new_schema.ts and I will be commiting the script to git.
13 replies
DTDrizzle Team
Created by Rorsch on 7/15/2024 in #help
How to Normalize Database using Drizzle?
This is the error that I got. I tried to use require instead but it's not working as well.
➜ admin-dashboard git:(order-migration) ✗ node app/db/migration/20240712_154500_migrate_orders_to_new_schema.ts
(node:5815) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/user/Developer/Projects/laundry-admin-dashboard/app/db/migration/20240712_154500_migrate_orders_to_new_schema.ts:1
import { db } from "../config"; // Import your DB connection
^^^^^^

SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:77:18)
at wrapSafe (node:internal/modules/cjs/loader:1288:20)
at Module._compile (node:internal/modules/cjs/loader:1340:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
at node:internal/main/run_main_module:28:49

Node.js v20.11.0
➜ admin-dashboard git:(order-migration) ✗ node app/db/migration/20240712_154500_migrate_orders_to_new_schema.ts
(node:5815) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/user/Developer/Projects/laundry-admin-dashboard/app/db/migration/20240712_154500_migrate_orders_to_new_schema.ts:1
import { db } from "../config"; // Import your DB connection
^^^^^^

SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:77:18)
at wrapSafe (node:internal/modules/cjs/loader:1288:20)
at Module._compile (node:internal/modules/cjs/loader:1340:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
at node:internal/main/run_main_module:28:49

Node.js v20.11.0
13 replies
DTDrizzle Team
Created by Rorsch on 7/15/2024 in #help
How to Normalize Database using Drizzle?
and this is what I wrote for the migration
import { db } from "../config";
import { newOrders, orderLineItems, orders } from "../schema";

async function migrateOrders() {
const oldOrders = await db.select().from(orders);

await db.transaction(async (tx) => {
for (const oldOrder of oldOrders) {
// Insert into newOrders table
const newOrder = {
id: oldOrder.id,
customerId: oldOrder.customerId,
status: oldOrder.status,
note: oldOrder.note,
totalPrice: oldOrder.totalPrice,
paymentMethodId: oldOrder.paymentMethodId,
creditId: oldOrder.creditId,
createdAt: oldOrder.createdAt,
updatedAt: oldOrder.updatedAt,
};
await tx.insert(newOrders).values(newOrder);

// Insert into orderLineItems table
const lineItem = {
id: "ordli_" + nanoid(),
orderId: oldOrder.id,
serviceId: oldOrder.serviceId,
qty: oldOrder.qty,
createdAt: oldOrder.createdAt,
updatedAt: oldOrder.updatedAt,
};
await tx.insert(orderLineItems).values(lineItem);
}
});
}

// Run the migration
migrateOrders()
.then(() => {
console.log("Migration complete!");
})
.catch((err) => {
console.error("Migration failed:", err);
});
import { db } from "../config";
import { newOrders, orderLineItems, orders } from "../schema";

async function migrateOrders() {
const oldOrders = await db.select().from(orders);

await db.transaction(async (tx) => {
for (const oldOrder of oldOrders) {
// Insert into newOrders table
const newOrder = {
id: oldOrder.id,
customerId: oldOrder.customerId,
status: oldOrder.status,
note: oldOrder.note,
totalPrice: oldOrder.totalPrice,
paymentMethodId: oldOrder.paymentMethodId,
creditId: oldOrder.creditId,
createdAt: oldOrder.createdAt,
updatedAt: oldOrder.updatedAt,
};
await tx.insert(newOrders).values(newOrder);

// Insert into orderLineItems table
const lineItem = {
id: "ordli_" + nanoid(),
orderId: oldOrder.id,
serviceId: oldOrder.serviceId,
qty: oldOrder.qty,
createdAt: oldOrder.createdAt,
updatedAt: oldOrder.updatedAt,
};
await tx.insert(orderLineItems).values(lineItem);
}
});
}

// Run the migration
migrateOrders()
.then(() => {
console.log("Migration complete!");
})
.catch((err) => {
console.error("Migration failed:", err);
});
13 replies
DTDrizzle Team
Created by Rorsch on 7/15/2024 in #help
How to Normalize Database using Drizzle?
Here is my table
export const orders = pgTable("order", {
id: varchar("id", { length: 25 }).primaryKey(),
customerId: varchar("customer_id", { length: 26 }).notNull(),
serviceId: varchar("service_id", { length: 25 }).notNull(),
qty: smallint("qty").notNull(),
status: statusEnum("status").notNull(),
note: varchar("note", { length: 256 }),
price: bigint("price", { mode: "number" }).notNull(),
totalPrice: bigint("total_price", { mode: "number" }).notNull(),
paymentMethodId: varchar("payment_method_id", { length: 27 }).notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").defaultNow().notNull(),
});

export const newOrders = pgTable("new_order", {
id: varchar("id", { length: 25 }).primaryKey(),
customerId: varchar("customer_id", { length: 26 }).notNull(),
status: statusEnum("status").notNull(),
note: varchar("note", { length: 256 }),
totalPrice: bigint("total_price", { mode: "number" }).notNull(),
paymentMethodId: varchar("payment_method_id", { length: 27 }).notNull(),
creditId: varchar("credit_id", { length: 24 }),
createdAt: timestamp("created_at", { withTimezone: true })
.defaultNow()
.notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.defaultNow()
.notNull(),
});

export const orderLineItems = pgTable("order_line_item", {
id: varchar("id", { length: 27 }).primaryKey(), //ordli_
orderId: varchar("order_id", { length: 25 })
.references(() => newOrders.id)
.notNull(),
serviceId: varchar("service_id", { length: 26 })
.references(() => services.id)
.notNull(),
qty: smallint("qty").notNull(),
createdAt: timestamp("created_at", { withTimezone: true })
.defaultNow()
.notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.defaultNow()
.notNull(),
});
export const orders = pgTable("order", {
id: varchar("id", { length: 25 }).primaryKey(),
customerId: varchar("customer_id", { length: 26 }).notNull(),
serviceId: varchar("service_id", { length: 25 }).notNull(),
qty: smallint("qty").notNull(),
status: statusEnum("status").notNull(),
note: varchar("note", { length: 256 }),
price: bigint("price", { mode: "number" }).notNull(),
totalPrice: bigint("total_price", { mode: "number" }).notNull(),
paymentMethodId: varchar("payment_method_id", { length: 27 }).notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").defaultNow().notNull(),
});

export const newOrders = pgTable("new_order", {
id: varchar("id", { length: 25 }).primaryKey(),
customerId: varchar("customer_id", { length: 26 }).notNull(),
status: statusEnum("status").notNull(),
note: varchar("note", { length: 256 }),
totalPrice: bigint("total_price", { mode: "number" }).notNull(),
paymentMethodId: varchar("payment_method_id", { length: 27 }).notNull(),
creditId: varchar("credit_id", { length: 24 }),
createdAt: timestamp("created_at", { withTimezone: true })
.defaultNow()
.notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.defaultNow()
.notNull(),
});

export const orderLineItems = pgTable("order_line_item", {
id: varchar("id", { length: 27 }).primaryKey(), //ordli_
orderId: varchar("order_id", { length: 25 })
.references(() => newOrders.id)
.notNull(),
serviceId: varchar("service_id", { length: 26 })
.references(() => services.id)
.notNull(),
qty: smallint("qty").notNull(),
createdAt: timestamp("created_at", { withTimezone: true })
.defaultNow()
.notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true })
.defaultNow()
.notNull(),
});
13 replies
DTDrizzle Team
Created by Rorsch on 4/16/2024 in #help
Selecting from 2 subqueries without using magic `sql()`
both currentMonthRevenue and lastMonthRevenue is identical. this is the return
SQL {
decoder: { mapFromDriverValue: [Function: mapFromDriverValue] },
shouldInlineParams: false,
queryChunks: [
StringChunk { value: [Array] },
undefined,
StringChunk { value: [Array] },
undefined,
StringChunk { value: [Array] },
SQL {
decoder: [Object],
shouldInlineParams: false,
queryChunks: [Array]
},
StringChunk { value: [Array] },
PgTable {
id: [PgVarchar],
date: [PgTimestamp],
debitAccountId: [PgVarchar],
creditAccountId: [PgVarchar],
amount: [PgBigInt53],
description: [PgVarchar],
createdAt: [PgTimestamp],
updatedAt: [PgTimestamp],
[Symbol(drizzle:IsAlias)]: false,
[Symbol(drizzle:ExtraConfigBuilder)]: undefined,
[Symbol(drizzle:IsDrizzleTable)]: true,
[Symbol(drizzle:OriginalName)]: 'accounting_transaction',
[Symbol(drizzle:Name)]: 'accounting_transaction',
[Symbol(drizzle:Schema)]: undefined,
[Symbol(drizzle:BaseName)]: 'accounting_transaction',
[Symbol(drizzle:PgInlineForeignKeys)]: [],
[Symbol(drizzle:Columns)]: [Object]
},
StringChunk { value: [Array] },
SQL {
decoder: [Object],
shouldInlineParams: false,
queryChunks: [Array]
},
StringChunk { value: [Array] },
SQL {
decoder: [Object],
shouldInlineParams: false,
queryChunks: [Array]
},
StringChunk { value: [Array] },
undefined,
StringChunk { value: [Array] },
undefined,
StringChunk { value: [Array] },
undefined,
StringChunk { value: [Array] },
undefined,
StringChunk { value: [Array] },
undefined,
StringChunk { value: [Array] },
SQL {
decoder: [Object],
shouldInlineParams: false,
queryChunks: []
},
StringChunk { value: [Array] }
]
}
SQL {
decoder: { mapFromDriverValue: [Function: mapFromDriverValue] },
shouldInlineParams: false,
queryChunks: [
StringChunk { value: [Array] },
undefined,
StringChunk { value: [Array] },
undefined,
StringChunk { value: [Array] },
SQL {
decoder: [Object],
shouldInlineParams: false,
queryChunks: [Array]
},
StringChunk { value: [Array] },
PgTable {
id: [PgVarchar],
date: [PgTimestamp],
debitAccountId: [PgVarchar],
creditAccountId: [PgVarchar],
amount: [PgBigInt53],
description: [PgVarchar],
createdAt: [PgTimestamp],
updatedAt: [PgTimestamp],
[Symbol(drizzle:IsAlias)]: false,
[Symbol(drizzle:ExtraConfigBuilder)]: undefined,
[Symbol(drizzle:IsDrizzleTable)]: true,
[Symbol(drizzle:OriginalName)]: 'accounting_transaction',
[Symbol(drizzle:Name)]: 'accounting_transaction',
[Symbol(drizzle:Schema)]: undefined,
[Symbol(drizzle:BaseName)]: 'accounting_transaction',
[Symbol(drizzle:PgInlineForeignKeys)]: [],
[Symbol(drizzle:Columns)]: [Object]
},
StringChunk { value: [Array] },
SQL {
decoder: [Object],
shouldInlineParams: false,
queryChunks: [Array]
},
StringChunk { value: [Array] },
SQL {
decoder: [Object],
shouldInlineParams: false,
queryChunks: [Array]
},
StringChunk { value: [Array] },
undefined,
StringChunk { value: [Array] },
undefined,
StringChunk { value: [Array] },
undefined,
StringChunk { value: [Array] },
undefined,
StringChunk { value: [Array] },
undefined,
StringChunk { value: [Array] },
SQL {
decoder: [Object],
shouldInlineParams: false,
queryChunks: []
},
StringChunk { value: [Array] }
]
}
4 replies
DTDrizzle Team
Created by Rorsch on 12/17/2023 in #help
net::ERR_NAME_NOT_RESOLVED showing multiple times on the console
right right, thanks a lot! I'm new to next js and these kind of stuffs, and I've been stuck for 2 days hahaha
32 replies
DTDrizzle Team
Created by Rorsch on 12/17/2023 in #help
net::ERR_NAME_NOT_RESOLVED showing multiple times on the console
that way, the Modal and Button can be a client component, and the page still can be a server component
32 replies
DTDrizzle Team
Created by Rorsch on 12/17/2023 in #help
net::ERR_NAME_NOT_RESOLVED showing multiple times on the console
Ah, then I probably will just move the Button inside the Modal component, instead of having it as 2 separate components
32 replies
DTDrizzle Team
Created by Rorsch on 12/17/2023 in #help
net::ERR_NAME_NOT_RESOLVED showing multiple times on the console
1 question, in my case, the dialog won't be opened until the Add New Customer button is clicked. In your case, Button to show the Dialog is inside the Dialog component itself.
32 replies
DTDrizzle Team
Created by Rorsch on 12/17/2023 in #help
net::ERR_NAME_NOT_RESOLVED showing multiple times on the console
like /dashboard/customers?open-modal=true
32 replies
DTDrizzle Team
Created by Rorsch on 12/17/2023 in #help
net::ERR_NAME_NOT_RESOLVED showing multiple times on the console
unless there is a way to control modal view without using useState and onClick, like using query string, there's no other way to handle this problem
32 replies
DTDrizzle Team
Created by Rorsch on 12/17/2023 in #help
net::ERR_NAME_NOT_RESOLVED showing multiple times on the console
did i get it right?
32 replies
DTDrizzle Team
Created by Rorsch on 12/17/2023 in #help
net::ERR_NAME_NOT_RESOLVED showing multiple times on the console
so, since i need to have a usestate on the page to handle the modal, probably the approach that i can do is: - make the whole page as a client component - make the table a client component - create function to fetch the data from drizzle db, set this as a server function - pass the customers data to table as a prop
32 replies
DTDrizzle Team
Created by Rorsch on 12/17/2023 in #help
net::ERR_NAME_NOT_RESOLVED showing multiple times on the console
32 replies
DTDrizzle Team
Created by Rorsch on 12/17/2023 in #help
net::ERR_NAME_NOT_RESOLVED showing multiple times on the console
because if i force the whole page to be a client component, the table won't work, as it is a server component
32 replies
DTDrizzle Team
Created by Rorsch on 12/17/2023 in #help
net::ERR_NAME_NOT_RESOLVED showing multiple times on the console
No description
32 replies