-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from Quramy/screen_before_create
Screen built input before create execution
- Loading branch information
Showing
16 changed files
with
469 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import ts from "typescript"; | ||
import { ast } from "./astShorthand"; | ||
|
||
type Primitive = string | boolean | number; | ||
|
||
type JSONObjLike = { | ||
[key: string]: Primitive | JSONObjLike | JSONObjLike[]; | ||
}; | ||
|
||
type JSONLike = JSONObjLike | JSONObjLike[]; | ||
|
||
function createArrayLitreral(obj: ReadonlyArray<JSONObjLike>): ts.ArrayLiteralExpression { | ||
return ast.arrayLiteralExpression(obj.map(createObjectLiteral)); | ||
} | ||
|
||
function createObjectLiteral(obj: JSONObjLike): ts.ObjectLiteralExpression { | ||
return ast.objectLiteralExpression( | ||
Object.entries(obj).map(([k, v]) => | ||
ast.propertyAssignment( | ||
k, | ||
typeof v === "string" | ||
? ast.stringLiteral(v) | ||
: typeof v === "number" | ||
? ast.numericLiteral(v) | ||
: typeof v === "boolean" | ||
? v | ||
? ast.true() | ||
: ast.false() | ||
: Array.isArray(v) | ||
? createArrayLitreral(v) | ||
: typeof v === "object" | ||
? createObjectLiteral(v) | ||
: (null as never), | ||
), | ||
), | ||
true, | ||
); | ||
} | ||
|
||
export function createJSONLiteral(obj: JSONLike): ts.Expression { | ||
return Array.isArray(obj) ? createArrayLitreral(obj) : createObjectLiteral(obj); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function byName<T extends { readonly name: string }>(name: string | { readonly name: string }) { | ||
return (x: T) => x.name === (typeof name === "string" ? name : name.name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./screen"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { DMMF } from "@prisma/generator-helper"; | ||
import { getDMMF } from "@prisma/internals"; | ||
import { createFieldDefinitions, createScreener } from "./screen"; | ||
|
||
describe(createScreener, () => { | ||
let document: DMMF.Document; | ||
let subject: <T>(data: T) => T; | ||
describe("For User - Post releation", () => { | ||
beforeEach(async () => { | ||
document = await getDMMF({ | ||
datamodel: ` | ||
model User { | ||
id Int @id | ||
posts Post[] | ||
} | ||
model Post { | ||
id Int @id | ||
authorId Int | ||
author User @relation(fields: [authorId], references: [id]) | ||
} | ||
`, | ||
}); | ||
}); | ||
|
||
describe("User screen", () => { | ||
beforeEach(() => { | ||
subject = createScreener("User", createFieldDefinitions(document.datamodel.models)); | ||
}); | ||
it("screens author in posts create relation", () => { | ||
expect( | ||
subject({ | ||
id: "", | ||
posts: { | ||
create: [{ id: "", author: {} }], | ||
}, | ||
}), | ||
).toEqual({ | ||
id: "", | ||
posts: { | ||
create: [{ id: "" }], | ||
}, | ||
}); | ||
}); | ||
|
||
it("screens author in posts connectOrCreate relation", () => { | ||
expect( | ||
subject({ | ||
id: "", | ||
posts: { | ||
connectOrCreate: [{ where: { id: "" }, create: { id: "", author: {} } }], | ||
}, | ||
}), | ||
).toEqual({ | ||
id: "", | ||
posts: { | ||
connectOrCreate: [{ where: { id: "" }, create: { id: "" } }], | ||
}, | ||
}); | ||
}); | ||
|
||
it("does nothing for connect", () => { | ||
expect( | ||
subject({ | ||
id: "", | ||
posts: { | ||
connect: [{ id: "", name: "" }], | ||
}, | ||
}), | ||
).toEqual({ | ||
id: "", | ||
posts: { | ||
connect: [{ id: "", name: "" }], | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("Post screen", () => { | ||
beforeEach(() => { | ||
subject = createScreener("Post", createFieldDefinitions(document.datamodel.models)); | ||
}); | ||
|
||
it("screening posts in user create relation", () => { | ||
expect( | ||
subject({ | ||
id: "", | ||
author: { | ||
create: { id: "", posts: [] }, | ||
}, | ||
}), | ||
).toEqual({ | ||
id: "", | ||
author: { | ||
create: { id: "" }, | ||
}, | ||
}); | ||
}); | ||
|
||
it("screening posts in user connectOrCreate relation", () => { | ||
expect( | ||
subject({ | ||
id: "", | ||
author: { | ||
connectOrCreate: { | ||
where: { id: "" }, | ||
create: { id: "", posts: [] }, | ||
}, | ||
}, | ||
}), | ||
).toEqual({ | ||
id: "", | ||
author: { | ||
connectOrCreate: { | ||
where: { id: "" }, | ||
create: { id: "" }, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it("does nothing for connect", () => { | ||
expect( | ||
subject({ | ||
id: "", | ||
author: { | ||
connect: { id: "", name: "" }, | ||
}, | ||
}), | ||
).toEqual({ | ||
id: "", | ||
author: { | ||
connect: { id: "", name: "" }, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.