Arti Villa
Arti Villa
DTDrizzle Team
Created by Arti Villa on 1/27/2025 in #help
restart increment for refNumber column starting from 1
All examples I've seen increment globally for the column. I need to restart increment from 1. how would I do that?
export const orderItems = pgTable(
"order_items",
{
id: uuid("id").defaultRandom().primaryKey(),
orderId: uuid("order_id").references(() => orders.id).notNull(),
refNumber: integer("ref_number")
.generatedAlwaysAsIdentity({ startWith: 1 }),
productType: productTypeEnum("product_type").notNull(),
color: colorEnum("color").notNull(),
size: sizeEnum("size").notNull(),
position: positionEnum("position").notNull(),
quantity: integer("quantity").notNull(),
nftData: json("nft_data").notNull().$type<ValidatedNFT>(),
unitPrice: decimal("unit_price", { precision: 10, scale: 2 }).notNull(),
createdAt: timestamp("created_at").defaultNow(),
}
);

export async function createOrder({
userId,
email,
items
}: CreateOrderParams): Promise<CreateOrderResult> {
return db.transaction(async (tx) => {
const [order] = await tx
.insert(orders)
.values({
userId,
customerEmail: email,
subtotalAmount: 0,
taxAmount: 0,
shippingAmount: 0,
totalAmount: 0,
providerFees: 0,
})
.returning();

if (!order) {
throw new Error('Order does not exist');
}

/** amke sure item.nft is a valid jsonb object through req zod validation */
const orderItems = await tx
.insert(orderItemsTable)
.values(
items.map(item => ({
orderId: order.id,
productType: item.type,
color: item.color,
size: item.size,
position: item.position,
quantity: item.quantity,
nftData: sql`${JSON.stringify(item.nft)}::jsonb`,
unitPrice: 0,
createdAt: new Date(),
}))
)
.returning();

return {
order,
orderItems
};
});
}
export const orderItems = pgTable(
"order_items",
{
id: uuid("id").defaultRandom().primaryKey(),
orderId: uuid("order_id").references(() => orders.id).notNull(),
refNumber: integer("ref_number")
.generatedAlwaysAsIdentity({ startWith: 1 }),
productType: productTypeEnum("product_type").notNull(),
color: colorEnum("color").notNull(),
size: sizeEnum("size").notNull(),
position: positionEnum("position").notNull(),
quantity: integer("quantity").notNull(),
nftData: json("nft_data").notNull().$type<ValidatedNFT>(),
unitPrice: decimal("unit_price", { precision: 10, scale: 2 }).notNull(),
createdAt: timestamp("created_at").defaultNow(),
}
);

export async function createOrder({
userId,
email,
items
}: CreateOrderParams): Promise<CreateOrderResult> {
return db.transaction(async (tx) => {
const [order] = await tx
.insert(orders)
.values({
userId,
customerEmail: email,
subtotalAmount: 0,
taxAmount: 0,
shippingAmount: 0,
totalAmount: 0,
providerFees: 0,
})
.returning();

if (!order) {
throw new Error('Order does not exist');
}

/** amke sure item.nft is a valid jsonb object through req zod validation */
const orderItems = await tx
.insert(orderItemsTable)
.values(
items.map(item => ({
orderId: order.id,
productType: item.type,
color: item.color,
size: item.size,
position: item.position,
quantity: item.quantity,
nftData: sql`${JSON.stringify(item.nft)}::jsonb`,
unitPrice: 0,
createdAt: new Date(),
}))
)
.returning();

return {
order,
orderItems
};
});
}
1 replies
DTDrizzle Team
Created by Arti Villa on 1/21/2025 in #help
password authentication failed for user "postgres"
<-- POST /api/checkout/create-checkout-session
Query: select "id", "privy_did", "created_at" from "users" where "users"."privy_did" = $1 limit $2 -- params: ["did:privy:cm4wfnsso004pw1dbh5n8ns4c", 1]
Error in getOrCreateUser: 40 | res = resolve
41 | rej = reject
42 | }).catch((err) => {
43 | // replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the
44 | // application that created the query
45 | Error.captureStackTrace(err)
^
error: password authentication failed for user "postgres"
code: "28P01"
<-- POST /api/checkout/create-checkout-session
Query: select "id", "privy_did", "created_at" from "users" where "users"."privy_did" = $1 limit $2 -- params: ["did:privy:cm4wfnsso004pw1dbh5n8ns4c", 1]
Error in getOrCreateUser: 40 | res = resolve
41 | rej = reject
42 | }).catch((err) => {
43 | // replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the
44 | // application that created the query
45 | Error.captureStackTrace(err)
^
error: password authentication failed for user "postgres"
code: "28P01"
I'm trying to run a local db using spire as the username with env DATABASE_URL=postgresql://spire:based@localhost:5432/outpaint_db
import { defineConfig } from "drizzle-kit";
import * as dotenvFlow from "dotenv-flow";

dotenvFlow.config({
node_env: process.env.NODE_ENV ?? 'development',
default_node_env: 'development',
path: process.cwd(),
silent: false,
});

console.log("process.env.DATABASE_URL", process.env.DATABASE_URL);

if (!process.env.DATABASE_URL) {
throw new Error("DATABASE_URL is undefined");
}

export default defineConfig({
schema: "./src/db/schema.ts",
out: "./drizzle",
dialect: "postgresql",
dbCredentials: {
url: process.env.DATABASE_URL!,
},
verbose: true,
strict: true,
});
import { defineConfig } from "drizzle-kit";
import * as dotenvFlow from "dotenv-flow";

dotenvFlow.config({
node_env: process.env.NODE_ENV ?? 'development',
default_node_env: 'development',
path: process.cwd(),
silent: false,
});

console.log("process.env.DATABASE_URL", process.env.DATABASE_URL);

if (!process.env.DATABASE_URL) {
throw new Error("DATABASE_URL is undefined");
}

export default defineConfig({
schema: "./src/db/schema.ts",
out: "./drizzle",
dialect: "postgresql",
dbCredentials: {
url: process.env.DATABASE_URL!,
},
verbose: true,
strict: true,
});
any ideas where this is going wrong? I can verify by conecting fine: docker exec -it postgres-latest psql -U spire
3 replies