|
| 1 | +import path from 'path' |
| 2 | +import dotenv from 'dotenv' |
| 3 | + |
| 4 | +dotenv.config({ path: path.resolve(__dirname, '../.env') }) |
| 5 | + |
| 6 | +import RushDB from '../src/index.node' |
| 7 | + |
| 8 | +jest.setTimeout(60_000) |
| 9 | + |
| 10 | +describe('records.importJson upsert nested linking (e2e)', () => { |
| 11 | + const apiKey = process.env.RUSHDB_API_KEY |
| 12 | + const apiUrl = process.env.RUSHDB_API_URL || 'http://localhost:3000' |
| 13 | + |
| 14 | + if (!apiKey) { |
| 15 | + it('skips because RUSHDB_API_KEY is not set', () => { |
| 16 | + expect(true).toBe(true) |
| 17 | + }) |
| 18 | + return |
| 19 | + } |
| 20 | + |
| 21 | + const db = new RushDB(apiKey, { url: apiUrl }) |
| 22 | + |
| 23 | + it('reuses child record and links it to new parent when parent upsert creates a new record', async () => { |
| 24 | + const tenantId = `nested-upsert-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}` |
| 25 | + |
| 26 | + const payloadA = { |
| 27 | + name: 'Abshire - Farrell', |
| 28 | + address: '928 Conroy Village Suite 785', |
| 29 | + foundedAt: '1949-10-06T22:07:28.709Z', |
| 30 | + rating: 1.9, |
| 31 | + tenantId, |
| 32 | + department: [ |
| 33 | + { |
| 34 | + name: 'Sports', |
| 35 | + description: 'The sleek and filthy Gloves comes with sky blue LED lighting for smart functionality', |
| 36 | + tenantId |
| 37 | + } |
| 38 | + ] |
| 39 | + } |
| 40 | + |
| 41 | + const payloadB = { |
| 42 | + ...payloadA, |
| 43 | + rating: 2 // slight change should force new top-level record when mergeBy is all keys |
| 44 | + } |
| 45 | + |
| 46 | + // First import creates parent and child |
| 47 | + await db.records.importJson({ label: 'Company', data: payloadA, options: { suggestTypes: true } }) |
| 48 | + |
| 49 | + // Second import should create new parent record but reuse existing child and link it |
| 50 | + await db.records.importJson({ |
| 51 | + label: 'Company', |
| 52 | + data: payloadB, |
| 53 | + options: { suggestTypes: true, mergeBy: [] } |
| 54 | + }) |
| 55 | + |
| 56 | + // Verify there are two Company records and one Department record linked to both via default relation |
| 57 | + const companies = await db.records.find({ labels: ['Company'], where: { tenantId } }) |
| 58 | + expect(companies.total).toBe(2) |
| 59 | + |
| 60 | + const departments = await db.records.find({ labels: ['department'], where: { tenantId } }) |
| 61 | + // label normalization in service uses original key; depending on capitalization option it might be 'department' |
| 62 | + // The label comes from the original key 'department' in the payload |
| 63 | + expect(departments.total).toBe(1) |
| 64 | + |
| 65 | + // Fetch relations and ensure both companies are connected to the same department |
| 66 | + const relationsResp = await db.relationships.find({ where: { tenantId } }) |
| 67 | + const rels = relationsResp.data.filter( |
| 68 | + (r) => |
| 69 | + r.type && |
| 70 | + (r.type.includes('RUSHDB_DEFAULT_RELATION') || r.type.includes('__RUSHDB__RELATION__DEFAULT__')) |
| 71 | + ) |
| 72 | + |
| 73 | + const departmentId = departments.data[0].id() |
| 74 | + const companyIds = companies.data.map((c) => c.id()) |
| 75 | + |
| 76 | + // For each company, there must be at least one relation to the department (either direction) |
| 77 | + const relatedPairs = new Set(rels.map((r) => `${r.sourceId}->${r.targetId}`)) |
| 78 | + for (const cid of companyIds) { |
| 79 | + const has = relatedPairs.has(`${cid}->${departmentId}`) || relatedPairs.has(`${departmentId}->${cid}`) |
| 80 | + expect(has).toBe(true) |
| 81 | + } |
| 82 | + |
| 83 | + // Cleanup |
| 84 | + await db.records.delete({ where: { tenantId } }) |
| 85 | + }) |
| 86 | +}) |
0 commit comments