Paul
Paul
DTDrizzle Team
Created by Paul on 1/17/2024 in #help
Retrieving ids when in a batch?
I'm doing the following with a batch. Where I am getting stuck is the very last join insert. How can I get the resulting ids from the 2 separate inserts (categoryQuery and imageQuery) ? Thanks!
const allTransactions = [] as any[];
const categoryQuery = db
.insert(schema.medusaCategories)
.values({
id: genId(),
parentId:
parsedData.parentId !== undefined ? parsedData.parentId : undefined,
name: parsedData.name,
enabled: parsedData.enabled,
description: parsedData.description,
pageTitle: parsedData.title,
metaDescription: parsedData.metaDescription,
slug: parsedData.slug,
storeId: parsedData.storeId,
})
.returning({ id: schema.medusaCategories.id });

allTransactions.push(categoryQuery);

if (parsedData.images && parsedData.images.length > 0) {
parsedData.images.forEach(async (image: any) => {
const imageQuery = db
.insert(schema.images)
.values({
id: image.id,
name: image.name,
key: image.key,
url: image.url,
size: image.size,
extension: image.extension,
hash: image.hash,
type: image.type,
})
.returning({ id: schema.images.id });

allTransactions.push(imageQuery);

// const join = db.insert(schema.medusaCategoryImages).values({
// id: genId(),
// categoryId: [categoryQuery].id,
// imageId: [imageQuery].id,
// });

// allTransactions.push(join);
});
}

const res = await db.batch(allTransactions);
const allTransactions = [] as any[];
const categoryQuery = db
.insert(schema.medusaCategories)
.values({
id: genId(),
parentId:
parsedData.parentId !== undefined ? parsedData.parentId : undefined,
name: parsedData.name,
enabled: parsedData.enabled,
description: parsedData.description,
pageTitle: parsedData.title,
metaDescription: parsedData.metaDescription,
slug: parsedData.slug,
storeId: parsedData.storeId,
})
.returning({ id: schema.medusaCategories.id });

allTransactions.push(categoryQuery);

if (parsedData.images && parsedData.images.length > 0) {
parsedData.images.forEach(async (image: any) => {
const imageQuery = db
.insert(schema.images)
.values({
id: image.id,
name: image.name,
key: image.key,
url: image.url,
size: image.size,
extension: image.extension,
hash: image.hash,
type: image.type,
})
.returning({ id: schema.images.id });

allTransactions.push(imageQuery);

// const join = db.insert(schema.medusaCategoryImages).values({
// id: genId(),
// categoryId: [categoryQuery].id,
// imageId: [imageQuery].id,
// });

// allTransactions.push(join);
});
}

const res = await db.batch(allTransactions);
2 replies
DTDrizzle Team
Created by Paul on 1/11/2024 in #help
Type issue when creating self join
I have the following:
export const medusaCategories = sqliteTable("medusa_categories", {
id: text("id").unique(),
createdAt: text("created_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: text("updated_at").default(sql`CURRENT_TIMESTAMP`),
deletedAt: text("deleted_at"),
// relations
storeId: text("store_id")
.references(() => medusaStores.id)
.notNull(),
parentId: text("parent_id").references(() => medusaCategories.id).notNull(),
});
export const medusaCategories = sqliteTable("medusa_categories", {
id: text("id").unique(),
createdAt: text("created_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: text("updated_at").default(sql`CURRENT_TIMESTAMP`),
deletedAt: text("deleted_at"),
// relations
storeId: text("store_id")
.references(() => medusaStores.id)
.notNull(),
parentId: text("parent_id").references(() => medusaCategories.id).notNull(),
});
If I comment out parentId then I get the type for medusaCategories. Otherwise I get the error: 'medusaCategories' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer How to resolve? Thanks
3 replies
DTDrizzle Team
Created by Paul on 1/6/2024 in #help
SyntaxError: Invalid or unexpected token when trying to push:
Hey all. I've just created a turso database and want to push. I'm getting the following error: SyntaxError: Invalid or unexpected token Any ideas why? Thanks
paul@Pauls-MacBook-Pro db % pnpm db:push

> @golfcart/[email protected] db:push /Users/paul/development/src/golf_cart/packages/db
> pnpm with-env drizzle-kit push:sqlite


> @golfcart/[email protected] with-env /Users/paul/development/src/golf_cart/packages/db
> dotenv -e ../../.env -- "drizzle-kit" "push:sqlite"

No config path provided, using default path
Reading config file '/Users/paul/development/src/golf_cart/packages/db/drizzle.config.ts'
drizzle-kit: v0.20.10
drizzle-orm: v0.29.3

/Users/paul/development/src/golf_cart/packages/db/src/schema/.DS_Store:1



SyntaxError: Invalid or unexpected token
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1178:20)
at Module._compile (node:internal/modules/cjs/loader:1220:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at newLoader (/Users/paul/development/src/golf_cart/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:8648:13)
at Object.newLoader (/Users/paul/development/src/golf_cart/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:8648:13)
at extensions..js (/Users/paul/development/src/golf_cart/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:11176:28)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Module.require (node:internal/modules/cjs/loader:1143:19)
paul@Pauls-MacBook-Pro db % pnpm db:push

> @golfcart/[email protected] db:push /Users/paul/development/src/golf_cart/packages/db
> pnpm with-env drizzle-kit push:sqlite


> @golfcart/[email protected] with-env /Users/paul/development/src/golf_cart/packages/db
> dotenv -e ../../.env -- "drizzle-kit" "push:sqlite"

No config path provided, using default path
Reading config file '/Users/paul/development/src/golf_cart/packages/db/drizzle.config.ts'
drizzle-kit: v0.20.10
drizzle-orm: v0.29.3

/Users/paul/development/src/golf_cart/packages/db/src/schema/.DS_Store:1



SyntaxError: Invalid or unexpected token
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1178:20)
at Module._compile (node:internal/modules/cjs/loader:1220:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at newLoader (/Users/paul/development/src/golf_cart/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:8648:13)
at Object.newLoader (/Users/paul/development/src/golf_cart/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:8648:13)
at extensions..js (/Users/paul/development/src/golf_cart/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:11176:28)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Module.require (node:internal/modules/cjs/loader:1143:19)
5 replies