echobind / prisma-factory Goto Github PK
View Code? Open in Web Editor NEWFactories ❤️ Prisma
Factories ❤️ Prisma
Thanks for the great library! I think it would be great if there was a way to set defaults for the generated factory. Something like:
import {createUserFactory} from 'prisma-factory/generated';
const UserFactory = createUserFactory({
name: 'fake name'
});
How do you guys get around this limitation at echobind at the moment?
This parent/children schema
model Page {
id Int @id @default(autoincrement())
title String?
parentId Int?
parent Page? @relation("Parent", fields: [parentId], references: [id])
children Page[] @relation("Parent")
}
will cause the following error:
Error: missing relation dest error, model: Page, field: parent
at /projects/app/api/node_modules/prisma-factory-generator/dist/relation.js:22:19
at Array.map (<anonymous>)
at Object.getRelationFields (/projects/app/api/node_modules/prisma-factory-generator/dist/relation.js:10:8)
at Object.args (/projects/app/api/node_modules/prisma-factory-generator/dist/args.js:38:41)
at Object.addModelFactoryDeclaration (/projects/app/api/node_modules/prisma-factory-generator/dist/generator.js:159:16)
at /projects/app/api/node_modules/prisma-factory-generator/dist/index.js:97:45
at Array.forEach (<anonymous>)
at _callee$ (/projects/app/api/node_modules/prisma-factory-generator/dist/index.js:96:55)
at tryCatch (/projects/app/api/node_modules/regenerator-runtime/runtime.js:63:40)
at Generator.invoke [as _invoke] (/projects/app/api/node_modules/regenerator-runtime/runtime.js:294:22)
I noticed that requiredAttrs
is using a partial. The problem is it doesnt reflect the required attrs that .create({ data: {} })
has and wont be caught by TS. For example, if column name
is required, prisma.user.create()
would catch it but the factory would not. Suggestion is to remove partial but remove associations (still trying to figure out a way to do that)
Might not want to allow it, but if so should update the type definition to explicitly disallow it or throw a more helpful error
Amazing package. Great Work. Well built.
Very appreciated.
Hey there,
Thanks for the work on this package - looks great! 😁
Any chance you'd know why Jest is struggling to find the generated index.js
? I get the following error:
ENOENT: no such file or directory, open '/{path to code}/node_modules/prisma-factory/dist/generated.js'
Example of my code:
import { createUserFactory } from 'prisma-factory/generated';
export const UserFactory = createUserFactory({
email: '[email protected]',
password: '123',
verified: true,
});
I've checked that the generated code is there inside node_modules
– not sure if this is an issue with my jest/typescript config or a package issue?
Thanks again
After installing prisma-factory
I can't run npx prisma generate
anymore. It just hangs.
I added DEBUG=*
to it and got the following output:
prisma:engines binaries to download libquery-engine, migration-engine, introspection-engine, prisma-fmt +0ms
prisma:loadEnv project root found at /Users/{path-omitted}/package.json +0ms
prisma:tryLoadEnv Environment variables loaded from /Users/{path-omitted}/.env +0ms
prisma:getConfig Using CLI Query Engine (Node-API Library) at: /Users/{path-omitted}/node_modules/prisma/libquery_engine-darwin.dylib.node +0ms
prisma:getConfig Loaded Node-API Library +8ms
prisma:getConfig config data retrieved without errors in getConfigNodeAPI +5ms
prisma:getDMMF Using CLI Query Engine (Node-API Library) at: /Users/{path-omitted}/node_modules/prisma/libquery_engine-darwin.dylib.node +0ms
prisma:getDMMF Loaded Node-API Library +0ms
prisma:getDMMF unserialized dmmf result ready +87ms
prisma:getDMMF dmmf retrieved without errors in getDmmfNodeAPI +36ms
prisma:generator prismaCLIDir /Users/{path-omitted}/node_modules/prisma +0ms
prisma:generator prismaClientDir /Users/{path-omitted}/node_modules/@prisma/client +0ms
prisma:generator baseDir /Users/{path-omitted}/prisma +0ms
prisma:generator typescriptPath /Users/{path-omitted}/node_modules/typescript +2ms
prisma:GeneratorProcess 2022-06-16T18:45:39.441Z prisma:client:generator requiredEngine: libqueryEngine +0ms
prisma:GeneratorProcess node:internal/modules/cjs/loader:942 +53ms
prisma:GeneratorProcess throw err; +0ms
prisma:GeneratorProcess ^ +0ms
prisma:GeneratorProcess Error: Cannot find module './lib/stringify' +0ms
prisma:GeneratorProcess Require stack: +0ms
prisma:GeneratorProcess - /Users/{path-omitted}/node_modules/braces/index.js +0ms
prisma:GeneratorProcess - /Users/{path-omitted}/node_modules/micromatch/index.js +0ms
prisma:GeneratorProcess - /Users/{path-omitted}/node_modules/fast-glob/out/utils/pattern.js +0ms
prisma:GeneratorProcess - /Users/{path-omitted}/node_modules/fast-glob/out/utils/index.js +0ms
prisma:GeneratorProcess - /Users/{path-omitted}/node_modules/fast-glob/out/managers/tasks.js +1ms
prisma:GeneratorProcess - /Users/{path-omitted}/node_modules/fast-glob/out/index.js +0ms
prisma:GeneratorProcess - /Users/{path-omitted}/node_modules/@ts-morph/common/dist/ts-morph-common.js +0ms
prisma:GeneratorProcess - /Users/{path-omitted}/node_modules/ts-morph/dist/ts-morph.js +0ms
prisma:GeneratorProcess - /Users/{path-omitted}/node_modules/prisma-factory/dist/generator.js +0ms
prisma:GeneratorProcess - /Users/{path-omitted}/node_modules/prisma-factory/bin/generator.js +0ms
prisma:GeneratorProcess at Module._resolveFilename (node:internal/modules/cjs/loader:939:15) +0ms
prisma:GeneratorProcess at Module._load (node:internal/modules/cjs/loader:780:27) +0ms
prisma:GeneratorProcess at Module.require (node:internal/modules/cjs/loader:1005:19) +0ms
prisma:GeneratorProcess at require (node:internal/modules/cjs/helpers:102:18) +0ms
prisma:GeneratorProcess at Object.<anonymous> (/Users/{path-omitted}/node_modules/braces/index.js:3:19) +0ms
prisma:GeneratorProcess at Module._compile (node:internal/modules/cjs/loader:1105:14) +0ms
prisma:GeneratorProcess at Module._extensions..js (node:internal/modules/cjs/loader:1159:10) +0ms
prisma:GeneratorProcess at Module.load (node:internal/modules/cjs/loader:981:32) +0ms
prisma:GeneratorProcess at Module._load (node:internal/modules/cjs/loader:827:12) +0ms
prisma:GeneratorProcess at Module.require (node:internal/modules/cjs/loader:1005:19) { +0ms
prisma:GeneratorProcess code: 'MODULE_NOT_FOUND', +0ms
prisma:GeneratorProcess requireStack: [ +1ms
prisma:GeneratorProcess '/Users/{path-omitted}/node_modules/braces/index.js', +0ms
prisma:GeneratorProcess '/Users/{path-omitted}/node_modules/micromatch/index.js', +0ms
prisma:GeneratorProcess '/Users/{path-omitted}/node_modules/fast-glob/out/utils/pattern.js', +5ms
prisma:GeneratorProcess '/Users/{path-omitted}/node_modules/fast-glob/out/utils/index.js', +0ms
prisma:GeneratorProcess '/Users/{path-omitted}/node_modules/fast-glob/out/managers/tasks.js', +0ms
prisma:GeneratorProcess '/Users/{path-omitted}/node_modules/fast-glob/out/index.js', +0ms
prisma:GeneratorProcess '/Users/{path-omitted}/node_modules/@ts-morph/common/dist/ts-morph-common.js', +0ms
prisma:GeneratorProcess '/Users/{path-omitted}/node_modules/ts-morph/dist/ts-morph.js', +0ms
prisma:GeneratorProcess '/Users/{path-omitted}/node_modules/prisma-factory/dist/generator.js', +0ms
prisma:GeneratorProcess '/Users/{path-omitted}/node_modules/prisma-factory/bin/generator.js' +0ms
prisma:GeneratorProcess ] +0ms
prisma:GeneratorProcess } +0ms
prisma:GeneratorProcess Node.js v18.0.0 +0ms
Honestly, I don't know what's missing to make it work. It seems to be missing a dependency, but I can't tell how to fix it.
Can someone help me with that?
I really appreciate any help you can provide.
Since typeof Date
returns object
it attempts to call the function here
Based on the buildIncludeFromAttrs function, additional fields are returned on the created data but TypeScript is not showing them as existing
Hello, thanks for making this - it's super useful!
The Prisma types for nested creates allow for passing an array in the case of a has-many relation, but prisma-factory
appears not to handle this case gracefully. Instead, I'm seeing something like the following:
Error:
Invalid `prisma[prismaModel].create()` invocation in
/Users/solomonhawk/Code/Work/mainframe/node_modules/prisma-factory/dist/index.js:109:44
106 data = hooks.beforeCreate(data);
107 }
108 const prismaModel = (0, import_camel_case.camelCase)(modelName);
→ 109 let result = await prisma[prismaModel].create({
data: {
name: 'Name',
slug: 'name',
description: 'description',
author: {
connect: {
email: '[email protected]'
}
},
homepageUrl: 'https://known-bike.info',
sourceUrl: 'https://growing-eavesdropper.biz',
tags: {
create: {
'0': {
name: 'facere',
description: 'Rem nemo repudiandae veritatis enim totam aperiam ipsam nam. Inventore tempore exercitationem labore in dicta saepe eveniet asperiores excepturi. Enim voluptatibus commodi molestiae enim odit doloribus in a sunt.'
},
'1': {
name: 'aliquam',
description: 'Perferendis porro quas natus. Quasi enim sed enim tempora unde amet eveniet aliquam. Eaque reiciendis vel sit placeat repudiandae a dolore incidunt quasi.'
},
+ name: String,
? id?: String,
? description?: String | null
}
}
},
include: {
author: true
}
})
Unknown arg `0` in data.tags.create.0 for type TagCreateWithoutTopicsInput.
Unknown arg `1` in data.tags.create.1 for type TagCreateWithoutTopicsInput.
Argument name for data.tags.create.name is missing.
My factory looks like:
export const TopicFactory = createTopicFactory({
name: () => faker.lorem.word(),
slug: () => faker.lorem.slug(),
description: () => faker.lorem.paragraphs({ min: 1, max: 3 }),
author: () => ({ create: UserFactory.build() }),
homepageUrl: () => faker.internet.url(),
sourceUrl: () => faker.internet.url(),
tags: () => ({ create: () => [TagFactory.build(), TagFactory.build()] }),
});
I also tried without the inner lambda { create: [TagFactory.build(), TagFactory.build()] }
, but the result is the same.
Without delving too deep, perhaps the typeof value === 'object'
checks here are matching array values erroneously.
The following patch seems to resolve the issue:
diff --git a/node_modules/prisma-factory/dist/index.js b/node_modules/prisma-factory/dist/index.js
index 9293749..de9df4a 100644
--- a/node_modules/prisma-factory/dist/index.js
+++ b/node_modules/prisma-factory/dist/index.js
@@ -46,11 +46,17 @@ var import_camel_case = require("camel-case");
// src/utils/getAttrs.ts
var getAttrs = (attrs) => {
return Object.fromEntries(Object.entries(attrs).map(([key, value]) => {
+ if (Array.isArray(value)) {
+ return [key, value.map(inner => getAttrs(inner))]
+ }
if (typeof value === "object") {
return [key, getAttrs(value)];
}
if (typeof value === "function") {
const result = value();
+ if (Array.isArray(value)) {
+ return [key, value.map(inner => getAttrs(inner))]
+ }
if (typeof result === "object") {
return [key, getAttrs(result)];
}
diff --git a/node_modules/prisma-factory/dist/index.mjs b/node_modules/prisma-factory/dist/index.mjs
index a13ad42..470e8d3 100644
--- a/node_modules/prisma-factory/dist/index.mjs
+++ b/node_modules/prisma-factory/dist/index.mjs
@@ -10,11 +10,17 @@ import { camelCase } from "camel-case";
// src/utils/getAttrs.ts
var getAttrs = (attrs) => {
return Object.fromEntries(Object.entries(attrs).map(([key, value]) => {
+ if (Array.isArray(value)) {
+ return [key, value.map(inner => getAttrs(inner))]
+ }
if (typeof value === "object") {
return [key, getAttrs(value)];
}
if (typeof value === "function") {
const result = value();
+ if (Array.isArray(value)) {
+ return [key, value.map(inner => getAttrs(inner))]
+ }
if (typeof result === "object") {
return [key, getAttrs(result)];
}
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.