farreldarian / prisma-generator-drizzle Goto Github PK
View Code? Open in Web Editor NEWA Prisma generator for generating Drizzle schema with ease
A Prisma generator for generating Drizzle schema with ease
Simply separate all definitions into a single module
This issue raised from these comments:
#1 (comment)
#1 (comment)
Using schema.ts
for drizzle-kit doesn't expose any schema.
export default defineConfig({
schema: './drizzle/schema/schema.ts',
})
It can't find any tables from the schema.ts
, but glob works fine
export default defineConfig({
schema: './drizzle/schema/*',
})
Originally posted by @hilja in #18 (comment)
First off, Im so thankful for this lib. Not yet using it in production, but it's allowing me to properly test Drizzle with my prod schemas and will make migrating off Prisma so much easier. Onto my idea...
My Prisma schema is pretty massive... I would love to have an option in the generator to suppress the output of the generator. I know where the files are going and it just ends up making a lot of noise and I run prisma generate
Add support for @ignore and @@ignore, by filtering out any columns that has the @ignore decorator, or simply don't generate when the model has the @@ignore decorator
Raised from:
#1 (comment)
When we have timestampTZ from postgres we should add withTimezone true to the drizzle schema.
here is how it should look like:
Description of drizzle timestamps
Related tweet https://twitter.com/Jaaneek/status/1751655615271727251
I followed the instructions in the README and executed the relevant commands.
However, when I reached the following command, an error occurred.
I searched extensively on search engines but couldn't find a solution.
generator client {
provider = "prisma-generator-drizzle"
}
datasource db {
provider = "sqlite"
url = "file:../prisma.db"
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}
prisma:engines binaries to download libquery-engine, schema-engine +156ms
prisma:get-platform Found distro info:
{
"targetDistro": "debian",
"familyDistro": "debian",
"originalDistro": "ubuntu"
} +4ms
prisma:get-platform Trying platform-specific paths for "debian" (and "ubuntu") +0ms
prisma:get-platform Found libssl.so file using platform-specific paths: libssl.so.3 +0ms
prisma:get-platform The parsed libssl version is: 3.0.x +0ms
prisma:loadEnv project root found at /home/work/package.json +246ms
prisma:tryLoadEnv Environment variables loaded from /home/work/.env +5ms
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
prisma:getConfig Using getConfig Wasm +3ms
prisma:getConfig config data retrieved without errors in getConfig Wasm +7ms
prisma:get-platform Found distro info:
{
"targetDistro": "debian",
"familyDistro": "debian",
"originalDistro": "ubuntu"
} +1ms
prisma:get-platform Trying platform-specific paths for "debian" (and "ubuntu") +0ms
prisma:get-platform Found libssl.so file using platform-specific paths: libssl.so.3 +1ms
prisma:get-platform The parsed libssl version is: 3.0.x +0ms
prisma:getConfig Using getConfig Wasm +34ms
prisma:getConfig config data retrieved without errors in getConfig Wasm +1ms
prisma:getDMMF Using getDmmf Wasm +0ms
prisma:getDMMF Using given datamodel +0ms
prisma:getDMMF dmmf data retrieved without errors in getDmmf Wasm +36ms
prisma:getGenerators neededVersions {} +294ms
Error: Error:
Expected property name or '}' in JSON at position 29
at Mf.parse (/home/work/node_modules/.pnpm/[email protected]/node_modules/prisma/build/index.js:1413:71)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Rgt (/home/work/node_modules/.pnpm/[email protected]/node_modules/prisma/build/index.js:1892:280)
Given a simple Prisma model:
model Bar {
foo String
createdAT DateTime @default(now())
}
It outputs something like:
import { pgTable, text, timestamp } from 'drizzle-orm/pg-core'
export const sites = pgTable('Bar', {
foo: text('foo').notNull(),
createdAT: timestamp('createdAT', { mode: 'date', precision: 3 }).notNull(),
})
Should it have a default value?
import { pgTable, text, timestamp } from 'drizzle-orm/pg-core'
export const sites = pgTable('Bar', {
foo: text('foo').notNull(),
createdAt: timestamp('createdAt', { mode: 'date', precision: 3 })
.defaultNow()
.notNull(),
})
Or default(sql`CURRENT_TIMESTAMP`)
. There seems to be some issues with it. Is it intentionally ambiguous?
These are the essential features needed for querying and mutating purposes, providing 1:1 feature mapping from Prisma. Please mention if there's anything missed from this list.
notNull
(when no ? modifier)@db.
field$type<...>()
defined from Prisma schemav1 will concentrates exclusively on query and mutation operations, these parts are currently not on consideration:
I am trying to create an internal package in my codebase that needs access to everything in my schema. Right now all the models are imported to build the schema, but I need access to those types outside of that one file and I don't want to have to seek into the individual files.
Maybe the generated directory should include an index.ts
which exports out the schema
as well as all the individual models?
I gotta say first I am beyond stoked on this library. My prisma schema is ~600 lines and the drizzle schema generation happened without error. However, I do get the following error when trying to load Drizzle Studio.
Error: There is not enough information to infer relation "__public__.catalogProducts.salesChannels"
CatalogProduct:
import {
mysqlTable,
text,
datetime,
boolean,
int,
} from "drizzle-orm/mysql-core";
import { catalogProductTypeEnum } from "./catalog-product-type-enum";
import { catalogProductCategoryEnum } from "./catalog-product-category-enum";
import { relations } from "drizzle-orm";
import { variants } from "./variants";
import { externalProducts } from "./external-products";
import { salesChannels } from "./sales-channels";
import { externalUsers } from "./external-users";
export const catalogProducts = mysqlTable("CatalogProduct", {
id: text("id").primaryKey(),
createdAt: datetime("createdAt", { mode: "date", fsp: 3 }).notNull(),
updatedAt: datetime("updatedAt", { mode: "date", fsp: 3 }).notNull(),
title: text("title").notNull(),
description: text("description"),
productType: catalogProductTypeEnum("productType").notNull(),
productCategory: catalogProductCategoryEnum("productCategory").notNull(),
isActive: boolean("isActive").notNull(),
retailPrice: int("retailPrice").notNull(),
imageUrl: text("imageUrl"),
isCustom: boolean("isCustom").notNull(),
customExternalUserId: text("customExternalUserId"),
customSkuIdentifier: text("customSkuIdentifier"),
dielineTemplateUrl: text("dielineTemplateUrl"),
});
export const catalogProductsRelations = relations(
catalogProducts,
(helpers) => {
return {
variants: helpers.many(variants, {
relationName: "CatalogProductToVariant",
}),
ExternalProduct: helpers.many(externalProducts, {
relationName: "CatalogProductToExternalProduct",
}),
salesChannels: helpers.many(salesChannels, {
relationName: "CatalogProductToSalesChannel",
}),
customExternalUser: helpers.one(externalUsers, {
relationName: "CatalogProductToExternalUser",
fields: [catalogProducts.customExternalUserId],
references: [externalUsers.id],
}),
};
},
);
SalesChannel Drizzle:
import { mysqlTable, text } from "drizzle-orm/mysql-core";
import { availableSalesChannelEnum } from "./available-sales-channel-enum";
import { relations } from "drizzle-orm";
import { catalogProducts } from "./catalog-products";
export const salesChannels = mysqlTable("SalesChannel", {
id: text("id").primaryKey(),
salesChannels: availableSalesChannelEnum("salesChannels").notNull(),
});
export const salesChannelsRelations = relations(salesChannels, (helpers) => {
return {
CatalogProduct: helpers.many(catalogProducts, {
relationName: "CatalogProductToSalesChannel",
}),
};
});
Prisma:
model SalesChannel {
id String @id @default(cuid())
salesChannels AvailableSalesChannel
CatalogProduct CatalogProduct[]
}
model CatalogProduct {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String @db.Text
description String? @db.Text
productType CatalogProductType
productCategory CatalogProductCategory
isActive Boolean @default(false)
retailPrice Int
imageUrl String?
variants Variant[]
ExternalProduct ExternalProduct[]
salesChannels SalesChannel[]
isCustom Boolean @default(false)
customExternalUser ExternalUser? @relation(fields: [customExternalUserId], references: [id])
customExternalUserId String?
customSkuIdentifier String?
dielineTemplateUrl String?
@@index([customExternalUserId])
}
I saw that in your V1 notes, you didn't call out CockroachDB support. For the sake of this lib, I think it would just be simple enough to generate CockroachDB models in the same way you do Postgres. CockroachDB is designed and meant to be Postgres compatible.
datasource db {
provider = "cockroachdb"
url = env("DATABASE_URL")
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.