2.0.0 • Published 5 months ago

accel-record-factory v2.0.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 months ago

Language: English | 日本語

Accel Record Factory

This is a factory library for Accel Record.

Getting Started

npm install -D accel-record-factory

Add the configuration for prisma-generator-accel-record to prisma/schema.prisma.

generator client {
  provider = "prisma-client-js"
  output   = "../src/prisma/client"
}

generator accelRecord {
  provider    = "prisma-generator-accel-record"
  output      = "../src/models"
  factoryPath = "../tests/factories" // Add the output destination for factory files
}

After modifying prisma/schema.prisma, run the following command:

npx prisma generate

For example, if you define a User model like this:

// prisma/schema.prisma
model User {
  id        Int    @id @default(autoincrement())
  firstName String
  lastName  String
  age       Int?
}

The following factory file will be automatically generated:

// tests/factories/user.ts

import { defineFactory } from "accel-record-factory";
import { User } from "../../src/models/index.js";

export const UserFactory = defineFactory(User, {
  // firstName: "MyString",
  // lastName: "MyString",
  // age: 1,
});

export { UserFactory as $User };

You can import and use the factory:

// tests/user.test.ts

import { $User } from "./factories/user";

const newUser = $User.build();

const user = $User.create({
  firstName: "John",
  lastName: "Doe",
  age: 20,
});

Usage

Setting Default Values

You can set default values by passing them as the second argument to defineFactory.

// tests/factories/user.ts

import { defineFactory } from "accel-record-factory";
import { User } from "../../src/models/index.js";

export const UserFactory = defineFactory(User, {
  firstName: "John", // Set default value
  lastName: "Doe",
  age: 20,
});

export { UserFactory as $User };
// tests/user.test.ts

import { $User } from "./factories/user";

const newUser = $User.build();
newUser.firstName; // => "John"
newUser.lastName; // => "Doe"
newUser.age; // => 20

Using Sequential Numbers

When setting default values with defineFactory, you can use a function to utilize sequential numbers.

// tests/factories/user.ts

import { defineFactory } from "accel-record-factory";
import { User } from "../../src/models/index.js";

export const UserFactory = defineFactory(User, {
  firstName: (seq) => `User${seq}`, // Specify a function to use sequential numbers
});

export { UserFactory as $User };
// tests/user.test.ts

import { $User } from "./factories/user";

const user1 = $User.build();
user1.firstName; // => "User1"

const user2 = $User.build();
user2.firstName; // => "User2"

Generating Associations

By specifying a function for the default value, you can generate models with associations.

// prisma/schema.prisma
model User {
  id        Int    @id @default(autoincrement())
  firstName String
  lastName  String
  age       Int?
  setting   Setting? // Associated model
}

model Setting {
  id     Int    @id @default(autoincrement())
  userId Int
  user   User   @relation(fields: [userId], references: [id])
  notify Boolean
}
// tests/factories/user.ts

import { defineFactory } from "accel-record-factory";
import { User, Setting } from "../../src/models/index.js";

export const UserFactory = defineFactory(User, {
  setting: () => Setting.build({ notify: true }), // Specify a function to generate the association
});

export { UserFactory as $User };
// tests/user.test.ts

import { $User } from "./factories/user";

const user = $User.build();
user.setting.notify; // => true

Traits

By specifying traits as the third argument to defineFactory, you can set multiple default values.

// tests/factories/user.ts

import { defineFactory } from "accel-record-factory";
import { User } from "../../src/models/index.js";

export const UserFactory = defineFactory(
  User,
  {
    firstName: "John",
    lastName: "Doe",
  },
  {
    traits: {
      // Specify the trait name
      foo: {
        firstName: "Foo",
        lastName: "Bar",
      },
    },
  }
);

export { UserFactory as $User };
// tests/user.test.ts

import { $User } from "./factories/user";

const john = $User.build({});
john.firstName; // => "John"
john.lastName; // => "Doe"

const foo = $User.build({}, "foo"); // Use the trait
foo.firstName; // => "Foo"
foo.lastName; // => "Bar"
2.0.0

5 months ago

1.17.0

7 months ago

1.16.0

7 months ago

1.15.1

7 months ago

1.15.0

9 months ago

1.14.1

9 months ago

1.14.0

10 months ago

1.13.0

10 months ago

1.12.0

10 months ago

1.11.0

10 months ago

1.10.1

11 months ago

1.9.1

11 months ago

1.9.0

11 months ago

1.8.0

12 months ago

1.7.0

12 months ago

1.6.0

12 months ago

1.10.0

11 months ago

1.5.0

1 year ago

1.4.0

1 year ago

1.3.0

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago

1.0.0-beta.4

1 year ago

1.0.0-beta.3

1 year ago

1.0.0-beta.2

1 year ago

1.0.0-beta.0

1 year ago

1.0.0-beta.1

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago