Nouns
Nouns represent and manage entities within your workflows clearly and effectively. Entities include customers, products, locations, resources, and other critical business objects.
Entities in Your Business
Nouns provides a powerful framework for defining and managing the entities that make up your business domain. It enables you to create a structured representation of your business objects and their relationships.
Features
- Domain Modeling: Define the core entities in your business domain
- Relationship Management: Establish connections between different entity types
- Property Definitions: Define the properties and attributes of each entity
- Validation Rules: Ensure data integrity with validation constraints
- Inheritance: Create hierarchies of entity types with shared properties
- Extensibility: Extend entity definitions with custom properties and behaviors
- Type Safety: Full TypeScript support for reliable development
Usage
import { defineNoun } from 'nouns.do'
// Define a Customer entity
const Customer = defineNoun({
name: 'Customer',
description: 'A person or organization that purchases products or services',
// Define the properties of a Customer
properties: {
name: {
type: 'string',
required: true,
description: 'Full name of the customer',
},
email: {
type: 'string',
format: 'email',
required: true,
unique: true,
description: 'Primary email address',
},
phone: {
type: 'string',
pattern: '^\\+?[1-9]\\d{1,14}$',
required: false,
description: 'Contact phone number in E.164 format',
},
type: {
type: 'string',
enum: ['individual', 'business'],
default: 'individual',
description: 'Type of customer',
},
status: {
type: 'string',
enum: ['active', 'inactive', 'pending'],
default: 'pending',
description: 'Current status of the customer',
},
address: {
type: 'object',
properties: {
street: { type: 'string' },
city: { type: 'string' },
state: { type: 'string' },
postalCode: { type: 'string' },
country: { type: 'string' },
},
required: false,
description: 'Mailing address',
},
createdAt: {
type: 'string',
format: 'date-time',
default: () => new Date().toISOString(),
description: 'When the customer was created',
},
},
// Define relationships to other entities
relationships: {
orders: {
type: 'hasMany',
target: 'Order',
foreignKey: 'customerId',
description: 'Orders placed by this customer',
},
subscriptions: {
type: 'hasMany',
target: 'Subscription',
foreignKey: 'customerId',
description: 'Active subscriptions for this customer',
},
primaryContact: {
type: 'hasOne',
target: 'Contact',
foreignKey: 'customerId',
description: 'Primary contact person for this customer',
},
},
// Define validation rules
validations: [
{
rule: 'email must be unique',
message: 'A customer with this email already exists',
},
{
rule: 'if type is business, name must not be empty',
message: 'Business customers must have a company name',
},
],
// Define methods that can be performed on this entity
methods: {
activate: {
description: 'Activate the customer account',
handler: async (customer) => {
customer.status = 'active'
customer.activatedAt = new Date().toISOString()
return customer
},
},
deactivate: {
description: 'Deactivate the customer account',
handler: async (customer) => {
customer.status = 'inactive'
customer.deactivatedAt = new Date().toISOString()
return customer
},
},
},
})
// Use the Customer entity in a workflow
import { AI } from 'workflows.do'
export default AI({
onNewSignup: async ({ db, event }) => {
const { name, email, company } = event
// Create a new customer
const customer = await db.Customer.create({
name: company ? company : name,
email,
type: company ? 'business' : 'individual',
status: 'pending',
})
// Activate the customer
const activatedCustomer = await customer.activate()
return {
customerId: activatedCustomer.id,
status: activatedCustomer.status,
}
},
})
Entity Relationships
Nouns.do supports various types of relationships between entities:
One-to-One Relationships
const User = defineNoun({
name: 'User',
// ...
relationships: {
profile: {
type: 'hasOne',
target: 'Profile',
foreignKey: 'userId',
},
},
})
const Profile = defineNoun({
name: 'Profile',
// ...
relationships: {
user: {
type: 'belongsTo',
target: 'User',
foreignKey: 'userId',
},
},
})
One-to-Many Relationships
const Post = defineNoun({
name: 'Post',
// ...
relationships: {
comments: {
type: 'hasMany',
target: 'Comment',
foreignKey: 'postId',
},
},
})
const Comment = defineNoun({
name: 'Comment',
// ...
relationships: {
post: {
type: 'belongsTo',
target: 'Post',
foreignKey: 'postId',
},
},
})
Many-to-Many Relationships
const Student = defineNoun({
name: 'Student',
// ...
relationships: {
courses: {
type: 'belongsToMany',
target: 'Course',
through: 'Enrollment',
foreignKey: 'studentId',
targetKey: 'courseId',
},
},
})
const Course = defineNoun({
name: 'Course',
// ...
relationships: {
students: {
type: 'belongsToMany',
target: 'Student',
through: 'Enrollment',
foreignKey: 'courseId',
targetKey: 'studentId',
},
},
})
Noun Templates
Discover and use pre-built noun templates from the marketplace to accelerate your development process.
Next Steps
Last updated on