Skip to content
This repository has been archived by the owner on Jan 8, 2023. It is now read-only.

Commit

Permalink
Move extractId to rules/scalar (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
yujonglee authored Dec 19, 2021
1 parent ea1302f commit 66aa2f4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 93 deletions.
50 changes: 24 additions & 26 deletions src/converters/convertScalar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ import {DMMF} from '@prisma/generator-helper';

import {PSL, SDL, Scalar} from './types';
import convertScalar from './convertScalar';
import store from '../store';

describe('convertScalar', () => {
beforeEach(() => {
store.reset();
});

it.each(['String', 'Boolean', 'Int', 'Float'])(
'does nothing for %s',
(type) => {
Expand All @@ -18,9 +13,12 @@ describe('convertScalar', () => {
type: PSL[type as T],
};

expect(convertScalar(field as DMMF.Field, {} as DMMF.Model)).toBe(
SDL[type as T],
);
expect(
convertScalar(
field as DMMF.Field,
{fields: []} as unknown as DMMF.Model,
),
).toBe(SDL[type as T]);
},
);

Expand All @@ -29,65 +27,67 @@ describe('convertScalar', () => {
type: PSL.Json,
};

expect(convertScalar(field as DMMF.Field, {} as DMMF.Model)).toBe(
SDL.String,
);
expect(
convertScalar(field as DMMF.Field, {fields: []} as unknown as DMMF.Model),
).toBe(SDL.String);
});

it('converts BigInt to Int', () => {
const field = {
type: PSL.BigInt,
};

expect(convertScalar(field as DMMF.Field, {} as DMMF.Model)).toBe(SDL.Int);
expect(
convertScalar(field as DMMF.Field, {fields: []} as unknown as DMMF.Model),
).toBe(SDL.Int);
});

it('converts Decimal to Float', () => {
const field = {
type: PSL.Decimal,
};

expect(convertScalar(field as DMMF.Field, {} as DMMF.Model)).toBe(
SDL.Float,
);
expect(
convertScalar(field as DMMF.Field, {fields: []} as unknown as DMMF.Model),
).toBe(SDL.Float);
});

it('converts Bytes to ByteArray', () => {
const field = {
type: PSL.Bytes,
};

expect(convertScalar(field as DMMF.Field, {} as DMMF.Model)).toBe(
Scalar.ByteArray,
);
expect(
convertScalar(field as DMMF.Field, {fields: []} as unknown as DMMF.Model),
).toBe(Scalar.ByteArray);
});

it('converts every type declared as @id to ID', () => {
expect(
convertScalar(
{type: PSL.String, isId: false} as DMMF.Field,
{} as DMMF.Model,
{fields: []} as unknown as DMMF.Model,
),
).toBe(SDL.String);

expect(
convertScalar(
{type: PSL.Json, isId: false} as DMMF.Field,
{} as DMMF.Model,
{fields: []} as unknown as DMMF.Model,
),
).toBe(SDL.String);

expect(
convertScalar(
{type: PSL.String, isId: true} as DMMF.Field,
{} as DMMF.Model,
{fields: []} as unknown as DMMF.Model,
),
).toBe(SDL.ID);

expect(
convertScalar(
{type: PSL.Json, isId: true} as DMMF.Field,
{} as DMMF.Model,
{fields: []} as unknown as DMMF.Model,
),
).toBe(SDL.ID);
});
Expand All @@ -96,16 +96,14 @@ describe('convertScalar', () => {
expect(
convertScalar(
{type: PSL.String, isUnique: true, isId: false} as DMMF.Field,
{name: 'User'} as DMMF.Model,
{name: 'User', fields: [{isId: false}]} as DMMF.Model,
),
).toBe(SDL.ID);

store.add({model: 'User', field: {name: 'field'} as DMMF.Field});

expect(
convertScalar(
{type: PSL.String, isUnique: true, isId: false} as DMMF.Field,
{name: 'User'} as DMMF.Model,
{name: 'User', fields: [{isId: true}]} as DMMF.Model,
),
).toBe(SDL.String);
});
Expand Down
7 changes: 4 additions & 3 deletions src/converters/rules/scalar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import store from 'store';
import {PSL, Rule, SDL, Scalar} from '../types';
import extractId from 'extractors/extractId';

const rules: Rule[] = [
{
Expand Down Expand Up @@ -60,10 +60,11 @@ const rules: Rule[] = [
},
{
matcher: (field, model) => {
const {name} = model;
const idField = extractId(model);

const {isUnique} = field;

return !store.ids[name] && isUnique;
return !idField && isUnique;
},
transformer: () => SDL.ID,
},
Expand Down
18 changes: 0 additions & 18 deletions src/store.test.ts

This file was deleted.

36 changes: 0 additions & 36 deletions src/store.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/transpile.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import parse from './parse';
import {removeWhiteSpaces} from './utils';
import transpile from './transpile';
import store from './store';

describe('transpile', () => {
beforeEach(() => {
store.reset();
});

it('adds scalars', async () => {
const prismaSchema = /* Prisma */ `
model Post {
Expand Down
5 changes: 0 additions & 5 deletions src/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {DMMF} from '@prisma/generator-helper';

import {DataModel} from './parse';

import extractId from 'extractors/extractId';
import extractScalars from './extractors/extractScalars';

import addTypeModifiers from './converters/addTypeModifiers';
Expand All @@ -11,7 +10,6 @@ import convertType from './converters/convertType';
import formatDefinition from './formatters/formatDefinition';
import formatField from './formatters/formatField';
import formatScalar from './formatters/formatScalar';
import store from 'store';

const getFieldTypePair = (model: DMMF.Model): string[] => {
if (!model) {
Expand All @@ -33,9 +31,6 @@ const getFieldTypePair = (model: DMMF.Model): string[] => {
{},
);

const idField = extractId(model);
store.add({model: model.name, field: idField});

const pairs = model.fields.map((field) => {
const {name} = field;

Expand Down

0 comments on commit 66aa2f4

Please sign in to comment.