|
| 1 | +/** |
| 2 | + * Relations example - REFERENCE, LOOKUP, and EMBED |
| 3 | + */ |
| 4 | + |
| 5 | +import { |
| 6 | + mongoCollection, |
| 7 | + objectId, |
| 8 | + string, |
| 9 | + number, |
| 10 | + date, |
| 11 | + createMongoOrm, |
| 12 | +} from '../src/index'; |
| 13 | + |
| 14 | +// ============ COLLECTIONS ============ |
| 15 | + |
| 16 | +// Organizations |
| 17 | +const organizations = mongoCollection('organizations', { |
| 18 | + name: string(), |
| 19 | + createdAt: date().defaultNow(), |
| 20 | +}); |
| 21 | + |
| 22 | +// Users with REFERENCE to organizations |
| 23 | +const users = mongoCollection( |
| 24 | + 'users', |
| 25 | + { |
| 26 | + email: string().email(), |
| 27 | + name: string(), |
| 28 | + orgId: objectId(), // Foreign key to organizations |
| 29 | + createdAt: date().defaultNow(), |
| 30 | + }, |
| 31 | + { |
| 32 | + relations: (r) => ({ |
| 33 | + // REFERENCE: Validates that orgId points to existing organization |
| 34 | + organization: r.reference(organizations, { |
| 35 | + localField: 'orgId', |
| 36 | + foreignField: '_id', |
| 37 | + }), |
| 38 | + // LOOKUP: Populates organization data |
| 39 | + organizationData: r.lookup(organizations, { |
| 40 | + localField: 'orgId', |
| 41 | + foreignField: '_id', |
| 42 | + one: true, // Single document |
| 43 | + }), |
| 44 | + }), |
| 45 | + } |
| 46 | +); |
| 47 | + |
| 48 | +// Posts with REFERENCE and LOOKUP to users |
| 49 | +const posts = mongoCollection( |
| 50 | + 'posts', |
| 51 | + { |
| 52 | + title: string(), |
| 53 | + content: string(), |
| 54 | + authorId: objectId(), // Foreign key to users |
| 55 | + likes: number().default(0), |
| 56 | + createdAt: date().defaultNow(), |
| 57 | + }, |
| 58 | + { |
| 59 | + relations: (r) => ({ |
| 60 | + // REFERENCE: Validates authorId exists |
| 61 | + author: r.reference(users, { |
| 62 | + localField: 'authorId', |
| 63 | + foreignField: '_id', |
| 64 | + }), |
| 65 | + // LOOKUP: Populates author data |
| 66 | + authorData: r.lookup(users, { |
| 67 | + localField: 'authorId', |
| 68 | + foreignField: '_id', |
| 69 | + one: true, |
| 70 | + }), |
| 71 | + }), |
| 72 | + } |
| 73 | +); |
| 74 | + |
| 75 | +// Comments with nested LOOKUP |
| 76 | +const comments = mongoCollection( |
| 77 | + 'comments', |
| 78 | + { |
| 79 | + postId: objectId(), |
| 80 | + authorId: objectId(), |
| 81 | + content: string(), |
| 82 | + createdAt: date().defaultNow(), |
| 83 | + }, |
| 84 | + { |
| 85 | + relations: (r) => ({ |
| 86 | + post: r.lookup(posts, { |
| 87 | + localField: 'postId', |
| 88 | + foreignField: '_id', |
| 89 | + one: true, |
| 90 | + }), |
| 91 | + author: r.lookup(users, { |
| 92 | + localField: 'authorId', |
| 93 | + foreignField: '_id', |
| 94 | + one: true, |
| 95 | + }), |
| 96 | + }), |
| 97 | + } |
| 98 | +); |
| 99 | + |
| 100 | +// ============ USAGE EXAMPLE ============ |
| 101 | + |
| 102 | +async function relationsExample() { |
| 103 | + // Connect to MongoDB |
| 104 | + const orm = await createMongoOrm({ |
| 105 | + uri: process.env.MONGO_URI || 'mongodb://localhost:27017', |
| 106 | + dbName: 'mizzle_relations_example', |
| 107 | + collections: { organizations, users, posts, comments }, |
| 108 | + }); |
| 109 | + |
| 110 | + const ctx = orm.createContext({}); |
| 111 | + const db = orm.withContext(ctx); |
| 112 | + |
| 113 | + try { |
| 114 | + // Note: Using `as any` type assertions to work around TypeScript's |
| 115 | + // complexity with union types in multi-collection ORMs. |
| 116 | + // The code is fully type-safe at runtime. |
| 117 | + |
| 118 | + // 1. REFERENCE VALIDATION |
| 119 | + console.log('\n=== REFERENCE Validation ==='); |
| 120 | + |
| 121 | + // Create an organization |
| 122 | + const org = await db.organizations.create({ |
| 123 | + name: 'Acme Corp', |
| 124 | + }); |
| 125 | + console.log('Created org:', org.name); |
| 126 | + |
| 127 | + // Create a user with valid orgId (REFERENCE validates this) |
| 128 | + const user = await db.users.create({ |
| 129 | + email: 'alice@acme.com', |
| 130 | + name: 'Alice', |
| 131 | + orgId: org._id, // Must reference existing organization |
| 132 | + }); |
| 133 | + console.log('Created user:', user.name); |
| 134 | + |
| 135 | + // Try to create user with invalid orgId - will throw error |
| 136 | + try { |
| 137 | + const { ObjectId } = await import('mongodb'); |
| 138 | + await db.users.create({ |
| 139 | + email: 'invalid@example.com', |
| 140 | + name: 'Invalid User', |
| 141 | + orgId: new ObjectId(), // Non-existent org |
| 142 | + }); |
| 143 | + } catch (err) { |
| 144 | + console.log('✓ Reference validation caught invalid orgId'); |
| 145 | + } |
| 146 | + |
| 147 | + // 2. LOOKUP POPULATION |
| 148 | + console.log('\n=== LOOKUP Population ==='); |
| 149 | + |
| 150 | + // Create a post |
| 151 | + const post = await db.posts.create({ |
| 152 | + title: 'My First Post', |
| 153 | + content: 'Hello, World!', |
| 154 | + authorId: user._id, |
| 155 | + }); |
| 156 | + |
| 157 | + // Fetch post and populate author |
| 158 | + const foundPosts = await db.posts.findMany({ _id: post._id }); |
| 159 | + const postsWithAuthor = await db.posts.populate(foundPosts, 'authorData'); |
| 160 | + |
| 161 | + console.log('Post:', postsWithAuthor[0].title); |
| 162 | + console.log('Author:', postsWithAuthor[0].authorData.name); |
| 163 | + console.log('Author Email:', postsWithAuthor[0].authorData.email); |
| 164 | + |
| 165 | + // 3. MULTIPLE POPULATIONS |
| 166 | + console.log('\n=== Multiple Populations ==='); |
| 167 | + |
| 168 | + // Create some comments |
| 169 | + await db.comments.create({ |
| 170 | + postId: post._id, |
| 171 | + authorId: user._id, |
| 172 | + content: 'Great post!', |
| 173 | + }); |
| 174 | + |
| 175 | + // Fetch comments and populate both post and author |
| 176 | + const foundComments = await db.comments.findMany({}); |
| 177 | + const populatedComments = await db.comments.populate(foundComments, [ |
| 178 | + 'post', |
| 179 | + 'author', |
| 180 | + ]); |
| 181 | + |
| 182 | + console.log('Comment:', populatedComments[0].content); |
| 183 | + console.log('On post:', populatedComments[0].post?.title); |
| 184 | + console.log('By:', populatedComments[0].author?.name); |
| 185 | + |
| 186 | + // 4. NESTED POPULATION (manually) |
| 187 | + console.log('\n=== Nested Population ==='); |
| 188 | + |
| 189 | + // Get posts with authors |
| 190 | + const allPosts = await db.posts.findMany({}); |
| 191 | + const postsWithAuthors = await db.posts.populate(allPosts, 'authorData'); |
| 192 | + |
| 193 | + // For each author, populate their organization |
| 194 | + for (const postWithAuthor of postsWithAuthors) { |
| 195 | + const author = postWithAuthor.authorData; |
| 196 | + if (author) { |
| 197 | + const usersArray = [author]; |
| 198 | + const usersWithOrg = await db.users.populate(usersArray, 'organizationData'); |
| 199 | + postWithAuthor.authorData = usersWithOrg[0]; |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + console.log('Post:', postsWithAuthors[0].title); |
| 204 | + console.log('Author:', postsWithAuthors[0].authorData?.name); |
| 205 | + console.log('Org:', postsWithAuthors[0].authorData?.organizationData?.name); |
| 206 | + } finally { |
| 207 | + await orm.close(); |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +// Run example if called directly |
| 212 | +if (require.main === module) { |
| 213 | + relationsExample() |
| 214 | + .then(() => { |
| 215 | + console.log('\n✓ Relations example completed!'); |
| 216 | + process.exit(0); |
| 217 | + }) |
| 218 | + .catch((err) => { |
| 219 | + console.error('Error:', err); |
| 220 | + process.exit(1); |
| 221 | + }); |
| 222 | +} |
0 commit comments