Skip to content

Commit

Permalink
upgrade to prisma 2.14
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedElywa committed Jan 5, 2021
1 parent ea88760 commit e5309de
Show file tree
Hide file tree
Showing 31 changed files with 117 additions and 94 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "root",
"private": true,
"devDependencies": {
"husky": "4.3.0",
"husky": "4.3.6",
"lerna": "3.22.1",
"prettier": "2.1.2",
"prettier": "2.2.1",
"pretty-quick": "3.1.0"
},
"workspaces": ["packages/*"],
Expand Down
20 changes: 10 additions & 10 deletions packages/admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,33 @@
},
"dependencies": {
"@paljs/types": "^2.9.0",
"dompurify": "^2.2.3",
"dompurify": "^2.2.6",
"moment": "2.29.1",
"prettier": "2.2.1",
"query-string": "6.13.7",
"query-string": "6.13.8",
"react-beautiful-dnd": "13.0.0",
"react-datepicker": "3.3.0",
"react-hook-form": "6.12.2",
"react-hook-form": "6.14.0",
"react-quill": "1.3.5",
"react-table": "7.6.2"
},
"devDependencies": {
"@apollo/client": "3.3.4",
"@apollo/client": "3.3.6",
"@paljs/ui": "1.0.4",
"@types/dompurify": "^2.0.4",
"@types/node": "14.14.12",
"@types/prettier": "2.1.5",
"@types/dompurify": "^2.2.0",
"@types/node": "14.14.20",
"@types/prettier": "2.1.6",
"@types/react": "17.0.0",
"@types/react-beautiful-dnd": "13.0.0",
"@types/react-datepicker": "3.1.2",
"@types/react-dom": "17.0.0",
"@types/react-table": "7.0.25",
"@types/styled-components": "5.1.5",
"@types/react-table": "7.0.26",
"@types/styled-components": "5.1.7",
"graphql-tag": "2.11.0",
"react": "17.0.1",
"react-dom": "17.0.1",
"styled-components": "5.2.1",
"typescript": "4.1.2"
"typescript": "4.1.3"
},
"files": [
"dist"
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@paljs/display": "^2.5.4",
"@paljs/generator": "^2.10.0",
"@paljs/schema": "^2.9.0",
"@prisma/sdk": "2.13.0",
"@prisma/sdk": "2.14.0",
"cross-spawn": "^7.0.3",
"enquirer": "^2.3.6",
"fs-extra": "^9.0.1",
Expand All @@ -27,18 +27,18 @@
"@oclif/dev-cli": "^1",
"@oclif/test": "^1",
"@types/chai": "^4",
"@types/fs-extra": "^9.0.4",
"@types/fs-extra": "^9.0.6",
"@types/mocha": "^8",
"@types/node": "14.14.12",
"@types/node": "14.14.20",
"chai": "^4",
"eslint": "^7.15",
"eslint": "^7.17",
"eslint-config-oclif": "^3.1",
"eslint-config-oclif-typescript": "^0.2",
"globby": "^11",
"mocha": "^8",
"nyc": "^15",
"ts-node": "^9",
"typescript": "4.1.2"
"typescript": "4.1.3"
},
"engines": {
"node": ">=8.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const schema = makeSchema({
typegen: join(process.cwd(), 'nexus-typegen.ts'),
},
contextType: {
module: join(__dirname, 'context.ts'),
module: join(process.cwd(), 'src', 'Api', 'context.ts'),
export: 'Context',
},
})
4 changes: 2 additions & 2 deletions packages/create/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"devDependencies": {
"@types/cross-spawn": "^6.0.2",
"cross-spawn": "^7.0.3",
"typescript": "4.1.2",
"wait-on": "^5.2.0"
"typescript": "4.1.3",
"wait-on": "^5.2.1"
},
"files": [
"dist"
Expand Down
2 changes: 1 addition & 1 deletion packages/display/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"dependencies": {
"chalk": "4.1.0",
"ora": "5.1.0"
"ora": "5.2.0"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 3 additions & 0 deletions packages/generator/__tests__/graphql-modules/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ generator client {
provider = "prisma-client-js"
}

/// User Model
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
email String @unique
name String?
password String
/// user Posts relation
/// Must delete if user delete
posts Post[]
}

Expand Down
6 changes: 6 additions & 0 deletions packages/generator/__tests__/nexus/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ generator client {
provider = "prisma-client-js"
}

/// user docs
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
/// email docs
email String @unique
name String?
password String
/// @onDelete(CASCADE)
/// posts docs
posts Post[]
}

/// posts docs
model Post {
id Int @id @default(autoincrement())
published Boolean @default(false)
title String
/// @PrismaSelect.map([Account, Profile]) auther of posts
author User? @relation(fields: [authorId], references: [id])
authorId Int?
createdAt DateTime @default(now())
Expand Down
4 changes: 4 additions & 0 deletions packages/generator/__tests__/sdl/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ generator client {
provider = "prisma-client-js"
}

/// User Model
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
email String @unique
name String?
password String
/// user Posts relation
/// Must delete if user delete
posts Post[]
}

/// Posts Model
model Post {
id Int @id @default(autoincrement())
published Boolean @default(false)
Expand Down
10 changes: 5 additions & 5 deletions packages/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
},
"dependencies": {
"@paljs/types": "^2.9.0",
"@prisma/sdk": "2.13.0",
"@prisma/sdk": "2.14.0",
"pkg-dir": "^5.0.0",
"pluralize": "^8.0.0",
"prettier": "^2.2.1"
},
"devDependencies": {
"@prisma/client": "2.13.0",
"@types/node": "14.14.12",
"@prisma/client": "2.14.0",
"@types/node": "14.14.20",
"@types/pluralize": "^0.0.29",
"@types/prettier": "2.1.5",
"typescript": "4.1.2"
"@types/prettier": "2.1.6",
"typescript": "4.1.3"
},
"files": [
"dist"
Expand Down
14 changes: 14 additions & 0 deletions packages/generator/src/Generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export class Generators {
return datamodel;
}

protected dataModel(models: DMMF.Model[], name: string) {
return models.find((m) => m.name === name);
}

protected dataField(name: string, model?: DMMF.Model) {
return model?.fields.find((f) => f.name === name);
}

protected async models() {
const { schema }: { schema: DMMF.Schema } = await this.dmmf();
return schema.outputObjectTypes.model.filter(
Expand Down Expand Up @@ -124,6 +132,12 @@ export class Generators {
: `import ${content} from '${path}'`;
}

protected filterDocs(docs?: string) {
return docs
?.replace(/@PrismaSelect.map\(\[(.*?)\]\)/, '')
.replace(/@onDelete\((.*?)\)/, '');
}

protected createFileIfNotfound(
path: string,
fileName: string,
Expand Down
23 changes: 11 additions & 12 deletions packages/generator/src/graphql-modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,30 @@ export class GenerateModules extends Generators {
this.appModules.push(model.name + 'Module');
this.index = `import { ${model.name}Module } from './${model.name}/${model.name}.module';${this.index}`;
}
const dataModel = this.dataModel(datamodel.models, model.name);
const modelDocs = this.filterDocs(dataModel?.documentation);
let fileContent = `${modelDocs ? `"""${modelDocs}"""\n` : ''}type ${
model.name
} {`;

let fileContent = `type ${model.name} {`;

const dataModel = datamodel.models.find(
(item) => item.name === model.name,
);
model.fields.forEach((field) => {
if (!this.excludeFields(model.name).includes(field.name)) {
const dataField = dataModel?.fields.find(
(item) => item.name === field.name,
);
const dataField = this.dataField(field.name, dataModel);
const fieldDocs = this.filterDocs(dataField?.documentation);
if (dataField?.kind === 'object' && model.name !== dataField.type) {
if (!extendsTypes.includes(`extend type ${dataField.type}`)) {
extendsTypes += `extend type ${dataField.type} {`;
models
.find((item) => item.name === dataField.type)
?.fields.filter((item) => item.outputType.type === model.name)
.forEach((item) => {
extendsTypes = getField(item, extendsTypes);
extendsTypes = getField(item, extendsTypes, fieldDocs);
});

extendsTypes += `}\n\n`;
}
} else {
fileContent = getField(field, fileContent);
fileContent = getField(field, fileContent, fieldDocs);
}
}
});
Expand Down Expand Up @@ -151,9 +150,9 @@ export const ${name}Module = createModule({
`;
};

const getField = (field: DMMF.SchemaField, content: string) => {
const getField = (field: DMMF.SchemaField, content: string, docs?: string) => {
content += `
${field.name}`;
${docs ? `"""${docs}"""\n` : ''}${field.name}`;
if (field.args.length > 0) {
content += '(';
field.args.forEach((arg) => {
Expand Down
18 changes: 12 additions & 6 deletions packages/generator/src/nexus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class GenerateNexus extends Generators {

private async createModels() {
const models = await this.models();
const dataModels = await this.datamodel();
models.forEach((model) => {
if (this.isJS) {
this.indexJS.push(model.name);
Expand All @@ -30,9 +31,9 @@ export class GenerateNexus extends Generators {
this.indexTS = `export * from './${model.name}'\n${this.indexTS}`;
}
}

const dataModel = this.dataModel(dataModels.models, model.name);
const modelDocs = this.filterDocs(dataModel?.documentation);
let fileContent = `${this.getImport('{ objectType }', 'nexus')}\n\n`;

fileContent += `${!this.isJS ? 'export ' : ''}const ${
model.name
} = objectType({
Expand All @@ -41,17 +42,22 @@ export class GenerateNexus extends Generators {
input: false,
},
name: '${model.name}',
${modelDocs ? `description: '${modelDocs}',` : ''}
definition(t) {
`;
model.fields.forEach((field) => {
if (!this.excludeFields(model.name).includes(field.name)) {
const options = this.getOptions(field);
const dataField = this.dataField(field.name, dataModel);
const fieldDocs = this.filterDocs(dataField?.documentation);
const options = this.getOptions(field, fieldDocs);
if (
field.outputType.location === 'scalar' &&
field.outputType.type !== 'DateTime'
) {
fileContent += `t${this.getNullOrList(field)}.${(field.outputType
.type as String).toLowerCase()}('${field.name}')\n`;
.type as String).toLowerCase()}('${field.name}'${
fieldDocs ? `, {description: '${fieldDocs}'}` : ''
})\n`;
} else {
fileContent += `t${this.getNullOrList(field)}.field('${
field.name
Expand Down Expand Up @@ -162,8 +168,8 @@ export class GenerateNexus extends Generators {
: '';
}

private getOptions(field: DMMF.SchemaField) {
const options: any = {};
private getOptions(field: DMMF.SchemaField, docs?: string) {
const options: any = docs ? { description: docs } : {};
if (
field.outputType.location !== 'scalar' ||
field.outputType.type === 'DateTime'
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/src/nexus/templates/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default `
type: 'Aggregate#{Model}',
args: {
where: '#{Model}WhereInput',
orderBy: list(arg({ type: '#{Model}OrderByInput' })),
orderBy: list('#{Model}OrderByInput'),
cursor: '#{Model}WhereUniqueInput',
distinct: '#{Model}ScalarFieldEnum',
skip: 'Int',
Expand Down
4 changes: 1 addition & 3 deletions packages/generator/src/nexus/templates/createOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ export default `
#{exportTs}const #{Model}CreateOneMutation = mutationField('createOne#{Model}', {
type: nonNull('#{Model}'),
args: {
data: nonNull(arg({
type: '#{Model}CreateInput'
})),
data: nonNull('#{Model}CreateInput'),
},
resolve(_parent, { data }, { prisma, select }) {
return prisma.#{model}.create({
Expand Down
4 changes: 1 addition & 3 deletions packages/generator/src/nexus/templates/deleteMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ export default `
#{exportTs}const #{Model}DeleteManyMutation = mutationField('deleteMany#{Model}', {
type: nonNull('BatchPayload'),
args: {
where: arg({
type: '#{Model}WhereInput',
}),
where: '#{Model}WhereInput',
},
resolve: async (_parent, { where }, { prisma }) => {
#{onDelete}
Expand Down
4 changes: 1 addition & 3 deletions packages/generator/src/nexus/templates/deleteOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ export default `
#{exportTs}const #{Model}DeleteOneMutation = mutationField('deleteOne#{Model}', {
type: '#{Model}',
args: {
where: nonNull(arg({
type: '#{Model}WhereUniqueInput',
})),
where: nonNull('#{Model}WhereUniqueInput'),
},
resolve: async (_parent, { where }, { prisma, select }) => {
#{onDelete}
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/src/nexus/templates/findCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default `
type: nonNull('Int'),
args: {
where: '#{Model}WhereInput',
orderBy: list(arg({ type: '#{Model}OrderByInput' })),
orderBy: list('#{Model}OrderByInput'),
cursor: '#{Model}WhereUniqueInput',
distinct: '#{Model}ScalarFieldEnum',
skip: 'Int',
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/src/nexus/templates/findFirst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default `
type: '#{Model}',
args: {
where: '#{Model}WhereInput',
orderBy: list(arg({ type: '#{Model}OrderByInput' })),
orderBy: list('#{Model}OrderByInput'),
cursor: '#{Model}WhereUniqueInput',
distinct: '#{Model}ScalarFieldEnum',
skip: 'Int',
Expand Down
Loading

0 comments on commit e5309de

Please sign in to comment.