-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: sql func migrations, computed field sql gen, test chore: migrations proper' chore: computed fields optional in model api creates chore: computed fields not required in create action inputs' chore: cleanup, op precedence impro chore: lint chore: computed fns migrations reworked chore: comments and clean up chore: bool supports, more tests chore: negation, fixed migration type chore: prettier chore: type compatibility between decimal and number chore: consistent generation of migrations' chore: completions chore: validations
- Loading branch information
Showing
42 changed files
with
1,654 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
model ComputedDecimal { | ||
fields { | ||
price Decimal | ||
quantity Number | ||
total Decimal @computed(computedDecimal.quantity * computedDecimal.price) | ||
totalWithShipping Decimal @computed(5 + computedDecimal.quantity * computedDecimal.price) | ||
totalWithDiscount Decimal @computed(computedDecimal.quantity * (computedDecimal.price - (computedDecimal.price / 100 * 10))) | ||
} | ||
} | ||
|
||
model ComputedNumber { | ||
fields { | ||
price Decimal | ||
quantity Number | ||
total Number @computed(computedNumber.quantity * computedNumber.price) | ||
totalWithShipping Number @computed(5 + computedNumber.quantity * computedNumber.price) | ||
totalWithDiscount Number @computed(computedNumber.quantity * (computedNumber.price - (computedNumber.price / 100 * 10))) | ||
} | ||
} | ||
|
||
model ComputedBool { | ||
fields { | ||
price Decimal? | ||
isActive Boolean | ||
isExpensive Boolean @computed(computedBool.price > 100 && computedBool.isActive) | ||
isCheap Boolean @computed(!computedBool.isExpensive) | ||
} | ||
} | ||
|
||
model ComputedNulls { | ||
fields { | ||
price Decimal? | ||
quantity Number? | ||
total Decimal? @computed(computedNulls.quantity * computedNulls.price) | ||
} | ||
} | ||
|
||
model ComputedDepends { | ||
fields { | ||
price Decimal | ||
quantity Number | ||
totalWithDiscount Decimal? @computed(computedDepends.totalWithShipping - (computedDepends.totalWithShipping / 100 * 10)) | ||
totalWithShipping Decimal? @computed(computedDepends.total + 5) | ||
total Decimal? @computed(computedDepends.quantity * computedDepends.price) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { test, expect, beforeEach } from "vitest"; | ||
import { models, resetDatabase } from "@teamkeel/testing"; | ||
|
||
beforeEach(resetDatabase); | ||
|
||
test("computed fields - decimal", async () => { | ||
const item = await models.computedDecimal.create({ price: 5, quantity: 2 }); | ||
expect(item.total).toEqual(10); | ||
expect(item.totalWithShipping).toEqual(15); | ||
expect(item.totalWithDiscount).toEqual(9); | ||
|
||
const get = await models.computedDecimal.findOne({ id: item.id }); | ||
expect(get!.total).toEqual(10); | ||
expect(get!.totalWithShipping).toEqual(15); | ||
expect(get!.totalWithDiscount).toEqual(9); | ||
|
||
const updatePrice = await models.computedDecimal.update( | ||
{ id: item.id }, | ||
{ price: 10 } | ||
); | ||
expect(updatePrice.total).toEqual(20); | ||
expect(updatePrice.totalWithShipping).toEqual(25); | ||
expect(updatePrice.totalWithDiscount).toEqual(18); | ||
|
||
const updateQuantity = await models.computedDecimal.update( | ||
{ id: item.id }, | ||
{ quantity: 3 } | ||
); | ||
expect(updateQuantity.total).toEqual(30); | ||
expect(updateQuantity.totalWithShipping).toEqual(35); | ||
expect(updateQuantity.totalWithDiscount).toEqual(27); | ||
|
||
const updateBoth = await models.computedDecimal.update( | ||
{ id: item.id }, | ||
{ price: 12, quantity: 4 } | ||
); | ||
expect(updateBoth.total).toEqual(48); | ||
expect(updateBoth.totalWithShipping).toEqual(53); | ||
expect(updateBoth.totalWithDiscount).toEqual(43.2); | ||
}); | ||
|
||
test("computed fields - number", async () => { | ||
const item = await models.computedNumber.create({ price: 5, quantity: 2 }); | ||
expect(item.total).toEqual(10); | ||
expect(item.totalWithShipping).toEqual(15); | ||
expect(item.totalWithDiscount).toEqual(9); | ||
|
||
const get = await models.computedNumber.findOne({ id: item.id }); | ||
expect(get!.total).toEqual(10); | ||
expect(get!.totalWithShipping).toEqual(15); | ||
expect(get!.totalWithDiscount).toEqual(9); | ||
|
||
const updatePrice = await models.computedNumber.update( | ||
{ id: item.id }, | ||
{ price: 10 } | ||
); | ||
expect(updatePrice.total).toEqual(20); | ||
expect(updatePrice.totalWithShipping).toEqual(25); | ||
expect(updatePrice.totalWithDiscount).toEqual(18); | ||
|
||
const updateQuantity = await models.computedNumber.update( | ||
{ id: item.id }, | ||
{ quantity: 3 } | ||
); | ||
expect(updateQuantity.total).toEqual(30); | ||
expect(updateQuantity.totalWithShipping).toEqual(35); | ||
expect(updateQuantity.totalWithDiscount).toEqual(27); | ||
|
||
const updateBoth = await models.computedNumber.update( | ||
{ id: item.id }, | ||
{ price: 12, quantity: 4 } | ||
); | ||
expect(updateBoth.total).toEqual(48); | ||
expect(updateBoth.totalWithShipping).toEqual(53); | ||
expect(updateBoth.totalWithDiscount).toEqual(43); | ||
}); | ||
|
||
test("computed fields - boolean", async () => { | ||
const expensive = await models.computedBool.create({ | ||
price: 200, | ||
isActive: true, | ||
}); | ||
expect(expensive.isExpensive).toBeTruthy(); | ||
expect(expensive.isCheap).toBeFalsy(); | ||
|
||
const notExpensive = await models.computedBool.create({ | ||
price: 90, | ||
isActive: true, | ||
}); | ||
expect(notExpensive.isExpensive).toBeFalsy(); | ||
expect(notExpensive.isCheap).toBeTruthy(); | ||
|
||
const notActive = await models.computedBool.create({ | ||
price: 200, | ||
isActive: false, | ||
}); | ||
expect(notActive.isExpensive).toBeFalsy(); | ||
expect(notActive.isCheap).toBeTruthy(); | ||
}); | ||
|
||
test("computed fields - with nulls", async () => { | ||
const item = await models.computedNulls.create({ price: 5 }); | ||
expect(item.total).toBeNull(); | ||
|
||
const updateQty = await models.computedNulls.update( | ||
{ id: item.id }, | ||
{ quantity: 10 } | ||
); | ||
expect(updateQty!.total).toEqual(50); | ||
|
||
const updatePrice2 = await models.computedNulls.update( | ||
{ id: item.id }, | ||
{ price: null } | ||
); | ||
expect(updatePrice2!.total).toBeNull(); | ||
}); | ||
|
||
test("computed fields - with dependencies", async () => { | ||
const item = await models.computedDepends.create({ price: 5, quantity: 2 }); | ||
expect(item.total).toEqual(10); | ||
expect(item.totalWithShipping).toEqual(15); | ||
expect(item.totalWithDiscount).toEqual(13.5); | ||
|
||
const updatedQty = await models.computedDepends.update( | ||
{ id: item.id }, | ||
{ quantity: 10 } | ||
); | ||
expect(updatedQty.total).toEqual(50); | ||
expect(updatedQty.totalWithShipping).toEqual(55); | ||
expect(updatedQty.totalWithDiscount).toEqual(49.5); | ||
|
||
const updatePrice = await models.computedDepends.update( | ||
{ id: item.id }, | ||
{ price: 8 } | ||
); | ||
expect(updatePrice.total).toEqual(80); | ||
expect(updatePrice.totalWithShipping).toEqual(85); | ||
expect(updatePrice.totalWithDiscount).toEqual(76.5); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
SELECT | ||
routine_name | ||
FROM | ||
information_schema.routines | ||
WHERE | ||
routine_type = 'FUNCTION' | ||
AND | ||
routine_schema = 'public' AND routine_name LIKE '%__computed'; |
Oops, something went wrong.