|
| 1 | +const { expect } = require('chai'); |
| 2 | +const { Model } = require('../../../'); |
| 3 | + |
| 4 | +module.exports = (session) => { |
| 5 | + const { knex } = session; |
| 6 | + |
| 7 | + // Typescript adds undefined default values for all declared class fields |
| 8 | + // when the there's `target: 'esnext'` in tsconfig.json. This test set |
| 9 | + // makes sure objection plays nice with those undefineds. |
| 10 | + describe('default undefined model field values', () => { |
| 11 | + class Person extends Model { |
| 12 | + firstName; |
| 13 | + pets; |
| 14 | + |
| 15 | + static jsonSchema = { |
| 16 | + type: 'object', |
| 17 | + properties: { |
| 18 | + firstName: { |
| 19 | + type: 'string', |
| 20 | + }, |
| 21 | + }, |
| 22 | + }; |
| 23 | + |
| 24 | + static tableName = 'person'; |
| 25 | + static relationMappings = () => ({ |
| 26 | + pets: { |
| 27 | + modelClass: Pet, |
| 28 | + relation: Model.HasManyRelation, |
| 29 | + join: { |
| 30 | + from: 'person.id', |
| 31 | + to: 'pet.ownerId', |
| 32 | + }, |
| 33 | + }, |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + class Pet extends Model { |
| 38 | + name; |
| 39 | + ownerId; |
| 40 | + owner; |
| 41 | + toys; |
| 42 | + |
| 43 | + static jsonSchema = { |
| 44 | + type: 'object', |
| 45 | + properties: { |
| 46 | + name: { |
| 47 | + type: 'string', |
| 48 | + }, |
| 49 | + ownerId: { |
| 50 | + type: 'integer', |
| 51 | + }, |
| 52 | + }, |
| 53 | + }; |
| 54 | + |
| 55 | + static tableName = 'pet'; |
| 56 | + static relationMappings = () => ({ |
| 57 | + owner: { |
| 58 | + modelClass: Person, |
| 59 | + relation: Model.BelongsToOneRelation, |
| 60 | + join: { |
| 61 | + from: 'pet.ownerId', |
| 62 | + to: 'person.id', |
| 63 | + }, |
| 64 | + }, |
| 65 | + |
| 66 | + toys: { |
| 67 | + modelClass: Toy, |
| 68 | + relation: Model.ManyToManyRelation, |
| 69 | + join: { |
| 70 | + from: 'pet.id', |
| 71 | + through: { |
| 72 | + from: 'petToy.petId', |
| 73 | + to: 'petToy.toyId', |
| 74 | + }, |
| 75 | + to: 'toy.id', |
| 76 | + }, |
| 77 | + }, |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + class Toy extends Model { |
| 82 | + toyName; |
| 83 | + price; |
| 84 | + |
| 85 | + static jsonSchema = { |
| 86 | + type: 'object', |
| 87 | + properties: { |
| 88 | + toyName: { |
| 89 | + type: 'string', |
| 90 | + }, |
| 91 | + price: { |
| 92 | + type: 'number', |
| 93 | + }, |
| 94 | + }, |
| 95 | + }; |
| 96 | + |
| 97 | + static tableName = 'toy'; |
| 98 | + } |
| 99 | + |
| 100 | + before(() => { |
| 101 | + return knex.schema |
| 102 | + .dropTableIfExists('petToy') |
| 103 | + .dropTableIfExists('pet') |
| 104 | + .dropTableIfExists('toy') |
| 105 | + .dropTableIfExists('person') |
| 106 | + .createTable('person', (table) => { |
| 107 | + table.increments('id').primary(); |
| 108 | + table.string('firstName'); |
| 109 | + }) |
| 110 | + .createTable('pet', (table) => { |
| 111 | + table.increments('id').primary(); |
| 112 | + table.string('name'); |
| 113 | + table.integer('ownerId').references('person.id').onDelete('cascade'); |
| 114 | + }) |
| 115 | + .createTable('toy', (table) => { |
| 116 | + table.increments('id').primary(); |
| 117 | + table.string('toyName'); |
| 118 | + table.float('price'); |
| 119 | + }) |
| 120 | + .createTable('petToy', (table) => { |
| 121 | + table.integer('petId').references('pet.id').notNullable().onDelete('cascade'); |
| 122 | + table.integer('toyId').references('toy.id').notNullable().onDelete('cascade'); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + after(() => { |
| 127 | + return knex.schema |
| 128 | + .dropTableIfExists('petToy') |
| 129 | + .dropTableIfExists('pet') |
| 130 | + .dropTableIfExists('toy') |
| 131 | + .dropTableIfExists('person'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('insertGraph', async () => { |
| 135 | + const result = await Person.query(knex) |
| 136 | + .allowGraph('pets.toys') |
| 137 | + .insertGraph({ |
| 138 | + firstName: 'Arnold', |
| 139 | + pets: [{ name: 'Catto' }, { name: 'Doggo', toys: [{ toyName: 'Bone' }] }], |
| 140 | + }); |
| 141 | + |
| 142 | + expect(result).to.containSubset({ |
| 143 | + firstName: 'Arnold', |
| 144 | + pets: [ |
| 145 | + { name: 'Catto', owner: undefined, toys: undefined }, |
| 146 | + { |
| 147 | + name: 'Doggo', |
| 148 | + owner: undefined, |
| 149 | + toys: [{ toyName: 'Bone' }], |
| 150 | + }, |
| 151 | + ], |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + it('withGraphFetched', async () => { |
| 156 | + const { id } = await Person.query(knex).insertGraph({ |
| 157 | + firstName: 'Arnold', |
| 158 | + pets: [{ name: 'Catto' }, { name: 'Doggo', toys: [{ toyName: 'Bone' }] }], |
| 159 | + }); |
| 160 | + |
| 161 | + const result = await Person.query(knex) |
| 162 | + .findById(id) |
| 163 | + .withGraphFetched({ |
| 164 | + pets: { |
| 165 | + toys: true, |
| 166 | + }, |
| 167 | + }); |
| 168 | + |
| 169 | + expect(result).to.containSubset({ |
| 170 | + firstName: 'Arnold', |
| 171 | + pets: [ |
| 172 | + { name: 'Catto', owner: undefined, toys: [] }, |
| 173 | + { |
| 174 | + name: 'Doggo', |
| 175 | + owner: undefined, |
| 176 | + toys: [{ toyName: 'Bone' }], |
| 177 | + }, |
| 178 | + ], |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + it('withGraphJoined', async () => { |
| 183 | + const { id } = await Person.query(knex).insertGraph({ |
| 184 | + firstName: 'Arnold', |
| 185 | + pets: [{ name: 'Catto' }, { name: 'Doggo', toys: [{ toyName: 'Bone' }] }], |
| 186 | + }); |
| 187 | + |
| 188 | + const result = await Person.query(knex) |
| 189 | + .findById(id) |
| 190 | + .withGraphJoined({ |
| 191 | + pets: { |
| 192 | + toys: true, |
| 193 | + }, |
| 194 | + }); |
| 195 | + |
| 196 | + expect(result).to.containSubset({ |
| 197 | + firstName: 'Arnold', |
| 198 | + pets: [ |
| 199 | + { name: 'Catto', owner: undefined, toys: [] }, |
| 200 | + { |
| 201 | + name: 'Doggo', |
| 202 | + owner: undefined, |
| 203 | + toys: [{ toyName: 'Bone' }], |
| 204 | + }, |
| 205 | + ], |
| 206 | + }); |
| 207 | + }); |
| 208 | + |
| 209 | + it('relatedQuery: find', async () => { |
| 210 | + const { |
| 211 | + id: personId, |
| 212 | + pets: [{ id: cattoId }, { id: doggoId }], |
| 213 | + } = await Person.query(knex).insertGraph({ |
| 214 | + firstName: 'Arnold', |
| 215 | + pets: [{ name: 'Catto' }, { name: 'Doggo', toys: [{ toyName: 'Bone' }] }], |
| 216 | + }); |
| 217 | + |
| 218 | + // HasManyRelation |
| 219 | + const catto = await Person.relatedQuery('pets', knex).for(personId).findById(cattoId); |
| 220 | + expect(catto).to.containSubset({ name: 'Catto' }); |
| 221 | + const doggo = await Person.relatedQuery('pets', knex).for(personId).findById(doggoId); |
| 222 | + expect(doggo).to.containSubset({ name: 'Doggo' }); |
| 223 | + |
| 224 | + // BelongsToOneRelation |
| 225 | + const person = await doggo.$relatedQuery('owner', knex); |
| 226 | + expect(person).to.containSubset({ firstName: 'Arnold' }); |
| 227 | + |
| 228 | + // ManyToManyRelation |
| 229 | + const toys = await Pet.relatedQuery('toys', knex).for(doggo); |
| 230 | + expect(toys).to.have.length(1); |
| 231 | + expect(toys).to.containSubset([{ toyName: 'Bone' }]); |
| 232 | + }); |
| 233 | + }); |
| 234 | +}; |
0 commit comments