Skip to content

Commit 5de9e2a

Browse files
committed
Support esnext typescript target on latest typescript versions
1 parent a06f9e6 commit 5de9e2a

File tree

4 files changed

+277
-3
lines changed

4 files changed

+277
-3
lines changed

lib/model/modelClone.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ function cloneWithOpt(model, shallow, stripInternal) {
5656
const key = keys[i];
5757
const value = model[key];
5858

59-
if ((shallow && relationNames.includes(key)) || (stripInternal && isInternalProp(key))) {
59+
if (shallow && relationNames.includes(key)) {
60+
// The constructor may have given default values for relations.
61+
delete clone[key];
62+
continue;
63+
}
64+
65+
if (stripInternal && isInternalProp(key)) {
6066
continue;
6167
}
6268

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "objection",
3-
"version": "3.0.0-rc.3",
3+
"version": "3.0.0-rc.4",
44
"description": "An SQL-friendly ORM for Node.js",
55
"main": "lib/objection.js",
66
"license": "MIT",

tests/integration/insertGraph.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const chai = require('chai');
33
const utils = require('../../lib/utils/knexUtils');
44
const expect = require('expect.js');
55
const Promise = require('bluebird');
6-
const { transaction, ValidationError } = require('../../');
6+
const { transaction, ValidationError, Model } = require('../../');
77

88
module.exports = (session) => {
99
let Model1 = session.models.Model1;
@@ -545,6 +545,40 @@ module.exports = (session) => {
545545
});
546546
});
547547

548+
it('should work with a model class that has undefined values as defaults for each column and relation', async () => {
549+
class TestModel extends Model {
550+
id;
551+
model1Id;
552+
model1Prop1;
553+
model1Prop2;
554+
model1Relation1;
555+
556+
static tableName = 'Model1';
557+
static relationMappings = {
558+
model1Relation1: {
559+
relation: TestModel.BelongsToOneRelation,
560+
modelClass: TestModel,
561+
join: {
562+
from: 'Model1.model1Id',
563+
to: 'Model1.id',
564+
},
565+
},
566+
};
567+
}
568+
569+
const result = await TestModel.query(session.knex).insertGraph({
570+
model1Prop1: 'foo',
571+
model1Relation1: {
572+
model1Prop2: 100,
573+
},
574+
});
575+
576+
expect(result.id).to.be.a('number');
577+
expect(result.model1Prop1).to.equal('foo');
578+
expect(result.model1Relation1).to.be.a(TestModel);
579+
expect(result.model1Relation1.model1Prop2).to.equal(100);
580+
});
581+
548582
if (utils.isPostgres(session.knex)) {
549583
it('query building methods should be applied to the root models', () => {
550584
return Model1.query()
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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

Comments
 (0)