Skip to content
프로젝트

소개

Schemix를 사용하면 TypeScript로 Prisma 스키마를 프로그래밍 방식으로 만들 수 있습니다 ⌨️

데이터 및 분석

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

Schemix

Easily create modular Prisma schemas.

Welcome to Schemix. An easy-to-use TypeScript layer over designing Prisma schemas, allowing for modularization, mixins, and other added capabilities.

Installation   ·   Setup   ·   Usage   ·   Contribute

example usage

Installation

To install Schemix, simply use your favourite Node.js package manager.

yarn add schemix
npm install schemix

Setup

You will need to create three folders that will contain your source files for your Prisma schema.

  • enums/
  • mixins/
  • models/

Your resulting file structure for your project may end up looking like what follows.

project/
├── node_modules/
│   └── *
├── prisma/
│   ├── index.ts
│   ├── schema.prisma
│   ├── enums/
│   │   └── Status.enum.ts
│   ├── mixins/
│   │   ├── DateTime.mixin.ts
│   │   └── UUID.mixin.ts
│   └── models/
│       ├── Post.model.ts
│       └── User.model.ts
├── index.ts
├── tsconfig.json
└── package.json

Once that's set up, you can add a script to your package.json in your root directory for your project which runs the prisma/index.ts file, thus generating the schema.prisma. file. Any time you generate your @prisma/client, you should first re-run the schemix script.

Usage

You can see an example usage in the examples/ folder in this repository.

Schema Index

// ./index.ts

import { createSchema } from "schemix";

createSchema({
  // basePath should be a path to the folder containing models/, enums/, and mixins/.
  basePath: __dirname,
  datasource: {
    provider: "postgresql",
    url: { env: "DATABASE_URL" },
  },
  generator: {
    provider: "prisma-client-js",
  },
}).export(__dirname, "schema");

Model Structure

// ./models/User.model.ts

import { createModel } from "schemix";

import PostModel from "./Post.model";
import UUIDMixin from "../mixins/UUID.mixin";

export default createModel((UserModel) => {
  UserModel.mixin(UUIDMixin)
    .relation("friends", UserModel, { list: true, name: "friends" })
    .relation("friendsRelation", UserModel, { list: true, name: "friends" })
    .relation("posts", PostModel, { list: true });
});

As you can see, you can self-reference, and cross-reference relations, apply mixins, etc.

Override Resulting Model Name

The model name will default to the name of the file, if you want to override this behaviour, simply pass it in as the first parameter in the createModel function.

// ./models/User.model.ts

import { createModel } from "schemix";

import PostModel from "./Post.model";
import UUIDMixin from "../mixins/UUID.mixin";

export default createModel("UserModel", (UserModel) => {
  UserModel.mixin(UUIDMixin)
    .relation("friends", UserModel, { list: true, name: "friends" })
    .relation("friendsRelation", UserModel, { list: true, name: "friends" })
    .relation("posts", PostModel, { list: true });
});

Extending Models

Sometimes one model is derivative of another, and the extension feature can be quite useful for this.

If we want to extend the PostModel to create an ImagePostModel which has all its old properties but with an imageUrl string column, we can do so without having to re-write every single other attribute.

// ./models/ImagePost.model.ts

import { extendModel } from "schemix";

import PostModel from "./Post.model";

export default extendModel(PostModel, (ImagePostModel) => {
  ImagePostModel.string("imageUrl");
});

Contribute

Feel free to contribute to the repository. Pull requests and issues with feature requests are super welcome!

관련 프로젝트 살펴보기

불러오는 중…