
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
accel-record-factory
Advanced tools
Language: [English](https://github.com/koyopro/accella/blob/main/packages/accel-record-factory/README.md) | [日本語](https://github.com/koyopro/accella/blob/main/packages/accel-record-factory/README-ja.md)
This is a factory library for Accel Record.
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,
});
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
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"
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
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"
FAQs
Language: [English](https://github.com/koyopro/accella/blob/main/packages/accel-record-factory/README.md) | [日本語](https://github.com/koyopro/accella/blob/main/packages/accel-record-factory/README-ja.md)
We found that accel-record-factory demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.