Skip to content

Commit

Permalink
version/1.3.3 (#19)
Browse files Browse the repository at this point in the history
* feat/deploy-automation

* feat/multi-schema-support

* feat/dep-update
  • Loading branch information
Nicolae-Rares Ailincai authored Mar 17, 2023
1 parent bd0a85d commit cd06fcb
Show file tree
Hide file tree
Showing 13 changed files with 785 additions and 427 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Publish to NPM

on:
release:
types: [created]

jobs:
build:
name: Build & Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 19.0
uses: actions/setup-node@v3
with:
node-version: 19.0
cache: "yarn"
- name: Install Dependencies
run: yarn install --frozen-lockfile
- name: Tests
run: yarn test
- name: Build
run: yarn build

publish-npm:
name: Publish
needs: build
runs-on: ubuntu-latest
environment:
name: NPM
steps:
- uses: actions/checkout@v3
- name: Use Node.js 19.0
uses: actions/setup-node@v3
with:
node-version: 19.0
cache: "yarn"
registry-url: https://registry.npmjs.org/
- name: Install Dependencies
run: yarn install --frozen-lockfile
- name: Build
run: yarn build
- name: Publish
run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@ bin
prisma
prisma-mapper.json
.DS_Store
out.prisma
out*.prisma
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"trailingComma": "none",
"singleQuote": true
}
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prisma-mapper",
"version": "1.3.2",
"version": "1.3.3",
"description": "A CLI that adds @Map, @@Map, @updatedAt based on a json or camel case for the prisma schema",
"main": "bin/index.js",
"preferGlobal": true,
Expand Down Expand Up @@ -52,19 +52,19 @@
},
"devDependencies": {
"@types/inquirer": "^9.0.3",
"@types/jest": "^29.4.0",
"@types/node": "^18.14.1",
"@typescript-eslint/eslint-plugin": "^5.53.0",
"@typescript-eslint/parser": "^5.53.0",
"eslint": "^8.34.0",
"@types/jest": "^29.5.0",
"@types/node": "^18.15.3",
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
"eslint": "^8.36.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-n": "^15.6.1",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-security": "^1.7.1",
"jest": "^29.4.3",
"jest": "^29.5.0",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
"typescript": "4.9.5"
"typescript": "5.0.2"
}
}
90 changes: 90 additions & 0 deletions src/__tests__/expected-multi-schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
generator client {
provider = "mock"
previewFeatures = ["metrics", "multiSchema"]
binaryTargets = [env("mock"), "mock"]
engineType = "mock"
output = env("mock")
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
schemas = ["base", "transactional"]
}

model ModelOne {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
someId Int @unique @map("some_id")
json Json? @db.Json
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
updatedAt DateTime? @updatedAt @map("updated_at") @db.Timestamptz(6)
deletedAt DateTime? @map("deleted_at") @db.Timestamptz(6)
@@index([deletedAt, someId], map: "index_name")
@@map("model_one")
@@schema("base")
}

model ModelTwo {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
string String? @db.VarChar(255)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
updatedAt DateTime? @updatedAt @map("updated_at") @db.Timestamptz(6)
deletedAt DateTime? @map("deleted_at") @db.Timestamptz(6)
someInt Int @map("some_int")
someUuid String? @map("some_uuid") @db.Uuid
ModelThree ModelThree[]
@@index([deletedAt, createdAt(sort: Desc)], map: "index_name_2")
@@map("model_two")
@@schema("transactional")
}

model ModelThree {
modelTwoId String @db.Uuid
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
updatedAt DateTime? @updatedAt @map("updated_at") @db.Timestamptz(6)
deletedAt DateTime? @map("deleted_at") @db.Timestamptz(6)
ModelTwo ModelTwo @relation(fields: [modelTwoId], references: [id], onDelete: Cascade)
@@id([modelTwoId], map: "index_name_3")
@@map("model_three")
@@schema("base")
}

model ModelFour {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
enumOne EnumOne @default(SomeValue) @map("enum_one")
enumTwo EnumTwo? @map("enum_two")
enumThree EnumThree? @map("enum_three")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
updatedAt DateTime? @updatedAt @map("updated_at") @db.Timestamptz(6)
deletedAt DateTime? @map("deleted_at") @db.Timestamptz(6)
@@map("model_four")
@@schema("transactional")
}

enum EnumOne {
SomeValue @map("some_value")
Single @map("single")
@@map("enum_one")
@@schema("base")
}

enum EnumTwo {
SOME_VALUE
SINGLE
@@map("enum_two")
@@schema("transactional")
}

enum EnumThree {
SomeValue @map("someValue")
Single @map("single")
@@map("enum_three")
@@schema("base")
}
10 changes: 6 additions & 4 deletions src/__tests__/expected.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ model ModelTwo {
someUuid String? @map("some_uuid") @db.Uuid
ModelThree ModelThree[]
@@index([deletedAt, createdAt(sort: Desc)], map: "index_name_2")
@@map("model_two")
}

model ModelThree {
modelTwoId String @id @map("model_two_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime? @updatedAt @map("updated_at")
deletedAt DateTime? @map("deleted_at")
modelTwoId String @db.Uuid
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
updatedAt DateTime? @updatedAt @map("updated_at") @db.Timestamptz(6)
deletedAt DateTime? @map("deleted_at") @db.Timestamptz(6)
ModelTwo ModelTwo @relation(fields: [modelTwoId], references: [id], onDelete: Cascade)
@@id([modelTwoId], map: "index_name_3")
@@map("model_three")
}

Expand Down
51 changes: 51 additions & 0 deletions src/__tests__/map-multi-schema.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Command } from 'commander';
import fs from 'fs/promises';
import path from 'path';

import mapCommand, { action } from '../commands/map';
import CLIError from '../CLIError';

const EXPECTED_PRISMA = path.join(__dirname, 'expected-multi-schema.prisma');
const OUTPUT_PRISMA = path.join(__dirname, 'out-multi-schema.prisma');
const MOCK_PRISMA = path.join(__dirname, 'mock-multi-schema.prisma');

describe('Test map command with multiSchema feature', () => {
let expectedPrisma = '';

beforeAll(async () => {
const programMock = {
command: jest.fn().mockReturnValue({
description: jest.fn().mockReturnValue({
action: jest.fn()
})
})
} as unknown as Command;

mapCommand(programMock);

expectedPrisma = await fs.readFile(EXPECTED_PRISMA, 'utf-8');
});

it('command should pass', async () => {
await action(
{ schema: MOCK_PRISMA, output: OUTPUT_PRISMA, camel: true },
true
);

expect(expectedPrisma).not.toEqual('');

const outputPrisma = await fs.readFile(OUTPUT_PRISMA, 'utf-8');
expect(outputPrisma).toEqual(expectedPrisma);
});

it('command should fail with no schema found', async () => {
try {
await action({ schema: '', camel: true });
throw new Error('Should not reach here');
} catch (e) {
const error = e as CLIError;
expect(error.message).toEqual('No prisma schema found');
expect(error.name).toEqual('CLIError');
}
});
});
79 changes: 79 additions & 0 deletions src/__tests__/mock-multi-schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
generator client {
provider = "mock"
previewFeatures = ["metrics", "multiSchema"]
binaryTargets = [env("mock"), "mock"]
engineType = "mock"
output = env("mock")
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
schemas = ["base", "transactional"]
}

model model_one {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
some_id Int @unique
json Json? @db.Json
created_at DateTime @default(now()) @db.Timestamptz(6)
updated_at DateTime? @db.Timestamptz(6)
deleted_at DateTime? @db.Timestamptz(6)
@@index([deleted_at, some_id], map: "index_name")
@@schema("base")
}

model model_two {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
string String? @db.VarChar(255)
created_at DateTime @default(now()) @db.Timestamptz(6)
updated_at DateTime? @db.Timestamptz(6)
deleted_at DateTime? @db.Timestamptz(6)
some_int Int
some_uuid String? @db.Uuid
model_three model_three[]
@@index([deleted_at, created_at(sort: Desc)], map: "index_name_2")
@@schema("transactional")
}

model model_three {
model_two_id String @db.Uuid
created_at DateTime @default(now()) @db.Timestamptz(6)
updated_at DateTime? @db.Timestamptz(6)
deleted_at DateTime? @db.Timestamptz(6)
model_two model_two @relation(fields: [model_two_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
@@id([model_two_id], map: "index_name_3")
@@schema("base")
}

model model_four {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
enum_one enum_one @default(some_value)
enum_two enum_two?
enum_three enum_three?
created_at DateTime @default(now()) @db.Timestamptz(6)
updated_at DateTime? @db.Timestamptz(6)
deleted_at DateTime? @db.Timestamptz(6)
@@schema("transactional")
}

enum enum_one {
some_value
single
@@schema("base")
}

enum enum_two {
SOME_VALUE
SINGLE
@@schema("transactional")
}

enum enum_three {
someValue
single
@@schema("base")
}
20 changes: 14 additions & 6 deletions src/commands/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import colors from 'colors/safe';
import nodePath from 'path';
import fs from 'fs';

import { ExtendedField, ExtendedModel, Models } from '../types';
import { getModelElements } from '../functions/getModelElements';
import { ExtendedEnum, ExtendedField, ExtendedModel, Models } from '../types';
import { getElements } from '../functions/getElements';
import { generateJson } from '../functions/generate';
import CLIError from '../CLIError';
import {
Expand Down Expand Up @@ -71,7 +71,8 @@ export const action = async (
colors.green(`+${Date.now() - nowTime}ms`)
);
nowTime = Date.now();
const modelElements = await getModelElements(datamodel);
const modelElements = await getElements(datamodel, 'model');
const enumElements = await getElements(datamodel, 'enum');

log(
colors.cyan(Config.logPrefix),
Expand Down Expand Up @@ -214,11 +215,15 @@ export const action = async (
return model;
});

const mappedEnums = enums.map((enumModel) => {
const mappedEnums = enums.map((enumModel): ExtendedEnum => {
const jsonModel = jsonModels[enumModel.dbName || enumModel.name];
const elementsParent = enumElements[enumModel.name];

if (!jsonModel) {
return enumModel;
return {
...enumModel,
elementsParent
};
}

if (!jsonModel.hasMap && !!jsonModel.name) {
Expand All @@ -237,7 +242,10 @@ export const action = async (
return value;
});

return enumModel;
return {
...enumModel,
elementsParent
};
});

log(
Expand Down
Loading

0 comments on commit cd06fcb

Please sign in to comment.