Skip to content

Commit 332fdb9

Browse files
authored
0.15.1 (#333)
## Documentation: * docs(ancestry-collection): fixed spacing * docs: fix jsdoc return types * docs(internal): commented exception classes * docs(query-builder): adjusted `when`'s grammar * docs: document `StaticToThis` type helper * docs(ancestry-collection): add flatten tip ## Fix: * fix(api-calls): include @ts-expect-error in declaration file * fix(api-calls): added missing `RequestMiddleware` type export * fix(relations): fix `morphTo` definition * fix(ancestry-collection): update constructor to be protected * Protected to discourage its usage from the outside. * fix(model-collection): improve argument of `findByKey` ## Performance: * perf(model): remove unnecessary `make` call ## Refactor: * refactor: rename FileModel to File * refactor: further File name changes ## Continuous Delivery: * ci: update testing node matrix * ci: fixed rollup config ## Style: * style(services): fixed too long line issue * style(relations): fixed too long line issue * style: fix eslint issues * style: fixed eslint issues ## Chore: * chore(deps-dev): updated dependencies * chore(deps-dev): update semantic-release * chore(deps-dev): updated packages * chore: increment version ## Testing: * test: update JSON parse error * test(helpers): improve variable name * test(relations): added missing test * test: add test for untested logic paths * test(ancestry-collection): fixed test name * test(services): fix json test for node 18
1 parent 8e8d2b3 commit 332fdb9

27 files changed

Lines changed: 2355 additions & 11106 deletions

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
strategy:
2222
matrix:
2323
# current and active LTS
24-
node: [ 16, 18 ]
24+
node: [ 18, 19 ]
2525
concurrency:
2626
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
2727
cancel-in-progress: true

docs/calliope/ancestry-collection.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ This string determines the name of the attribute that is set on the models when
1818
#### treeOf
1919
<Badge text="static" type="warning"/>
2020

21-
The `treeOf` static method creates the AncestryCollection from the given [ModelCollection](./model-collection.md). This arranges the models to as the child of their respective models. Optionally the method takes 2 more arguments:
21+
The `treeOf` static method creates the AncestryCollection from the given [ModelCollection](./model-collection.md). This arranges the models as the child of their respective models.
22+
Optionally the method takes 2 more arguments:
2223
- `parentKey` (default: `'parentId'`) - the name of the attribute that contains the parent's identifier.
2324
- `childrenRelation` (default: `'children'`): - the name of the relation the child models are nested under.
2425

@@ -45,6 +46,10 @@ const folderTree = AncestryCollection.treeOf(folders);
4546
folderTree.flatten(); // ModelCollection
4647
```
4748

49+
::: tip
50+
This allows to implement a simple `find()` or `contains()` logic by calling `!!folderTree.flatten().findByKey(1)`.
51+
:::
52+
4853
#### leaves
4954

5055
The `leaves` method returns a [ModelCollection](./model-collection.md) containing all the models that does not have any children. With the analogy of a tree, it will not include roots, branches, only the models at the end of the bloodline.

docs/calliope/relationships.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,24 +203,36 @@ const userPassport = await user.$passport().get();
203203

204204
#### morphTo
205205

206-
The `morphTo` method describes a polymorphic relation where the model can describe to multiple entities.
206+
The `morphTo` method describes a polymorphic relation and expects one argument. A callback where the correct related model constructor is returned depending on the provided logic. This callback receives the polymorphic parent and the attributes of the relation to help choosing the correct model.
207+
208+
::: tip
209+
`morphTo` is a special case as this method returns the morph parent itself as opposed to the relation's model. This is because the morphed model is not expected to implement the standard REST endpoints.
210+
:::
207211

208212
```ts
209-
// Rate.ts
213+
// Contract.ts
210214
import { Model } from '@upfrontjs/framework';
211215
import Car from '@models/Car';
216+
import Team from '@models/Team';
212217

213-
export default class Rate extends Model {
214-
public $rateables(): Car {
215-
return this.morphTo();
218+
export default class Contract extends Model {
219+
public contractableId?: number;
220+
public contractableType?: 'team' | 'car';
221+
public contractable?: Team | Car;
222+
223+
public $contractable(): this {
224+
return this.morphTo((self, attributesOfRelation) => {
225+
return self.contractableType === 'team' ? Team : Car;
226+
});
216227
}
217228
}
218229

219230
// myScript.ts
220-
import Rate from '@models/Rate'
231+
import Contract from '@models/Contract'
221232

222-
const rating = await Rate.limit(1).get();
223-
const ratingWithRatedEntities = await rating.$rateables().get();
233+
const contract = await Contract.find(1);
234+
// same contract as above fetched from the API, with the relation set
235+
const contractedEntity = await contract.$contractable().get().then(contract => contract.contractable);
224236
```
225237

226238
## Manage Relations

package-lock.json

Lines changed: 1964 additions & 10896 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@upfrontjs/framework",
3-
"version": "0.15.0",
3+
"version": "0.15.1",
44
"description": "Data handling framework complementary to backend model systems.",
55
"main": "index.min.js",
66
"module": "index.es.min.js",
@@ -85,14 +85,15 @@
8585
"@commitlint/config-conventional": "^17.0.0",
8686
"@commitlint/prompt-cli": "^17.0.0",
8787
"@commitlint/types": "^17.0.0",
88-
"@rollup/plugin-typescript": "^8.1.0",
88+
"@rollup/plugin-terser": "^0.4.0",
89+
"@rollup/plugin-typescript": "^11.0.0",
8990
"@semantic-release/git": "^10.0.0",
9091
"@types/jest": "^29.0.1",
9192
"@types/lodash": "^4.14.167",
9293
"@types/pluralize": "^0.0.29",
9394
"@types/qs": "^6.9.5",
94-
"@types/semantic-release": "^17.2.1",
95-
"@types/uuid": "^8.3.0",
95+
"@types/semantic-release": "^20.0.1",
96+
"@types/uuid": "^9.0.0",
9697
"@typescript-eslint/eslint-plugin": "^5.38.0",
9798
"@typescript-eslint/parser": "^5.38.0",
9899
"commitlint": "^17.0.2",
@@ -105,10 +106,9 @@
105106
"jest": "^29.0.3",
106107
"jest-environment-jsdom": "^29.0.3",
107108
"lint-staged": "^13.0.1",
108-
"rollup": "^2.35.1",
109+
"rollup": "^3.13.0",
109110
"rollup-plugin-bundle-size": "^1.0.3",
110-
"rollup-plugin-terser": "^7.0.2",
111-
"semantic-release": "^19.0.2",
111+
"semantic-release": "^20.1.0",
112112
"ts-jest": "^29.0.0",
113113
"tslib": "^2.2.0",
114114
"typedoc": "^0.23.5",
@@ -132,6 +132,6 @@
132132
}
133133
],
134134
"engines": {
135-
"node": ">=14.13.1"
135+
"node": ">=18.0.0"
136136
}
137137
}
Lines changed: 102 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,102 @@
1-
import typescript from "@rollup/plugin-typescript";
2-
import pkg from './package.json';
3-
import { terser } from "rollup-plugin-terser";
4-
import bundleSize from 'rollup-plugin-bundle-size';
5-
6-
const banner = `
7-
/*! ================================
8-
${pkg.name} v${pkg.version}
9-
(c) 2020-present ${pkg.author}
10-
Released under ${pkg.license} License
11-
================================== */
12-
`;
13-
14-
/**
15-
* @type {import('rollup/dist/rollup').InputOptions}
16-
*/
17-
const commonConfig = {
18-
external: [
19-
...Object.keys(pkg.dependencies ?? {}),
20-
...Object.keys(pkg.optionalDependencies ?? {}),
21-
...Object.keys(pkg.peerDependencies ?? {})
22-
],
23-
plugins: [
24-
// it doesn't find the config by default and doesn't emit interface files
25-
// todo - https://github.com/rollup/plugins/pull/791/files#diff-77ceb76f06466d761730b952567396e6b5c292cc4044441cdfdf048b4614881dR83 check those tests
26-
typescript({ tsconfig: './tsconfig.json' }),
27-
terser({
28-
format: {
29-
comments: (node, comment) => {
30-
if (comment.type === "comment2") {
31-
return /@upfront/.test(comment.value);
32-
}
33-
}
34-
}
35-
}),
36-
bundleSize()
37-
]
38-
};
39-
40-
/**
41-
* @type {import('rollup/dist/rollup').RollupOptions[]}
42-
*/
43-
const rollupConfig = [
44-
{
45-
input: 'src/index.ts',
46-
output: [
47-
{
48-
file: pkg.main,
49-
format: 'cjs',
50-
sourcemap: true,
51-
banner
52-
},
53-
{
54-
file: pkg.module,
55-
format: 'es',
56-
sourcemap: true,
57-
banner
58-
}
59-
],
60-
...commonConfig
61-
},
62-
63-
{
64-
input: 'src/array.ts',
65-
output: [
66-
{
67-
file: 'array.min.js',
68-
format: 'cjs',
69-
sourcemap: true,
70-
banner
71-
},
72-
{
73-
file: 'array.es.min.js',
74-
format: 'es',
75-
sourcemap: true,
76-
banner
77-
}
78-
],
79-
...commonConfig
80-
},
81-
82-
{
83-
input: 'src/string.ts',
84-
output: [
85-
{
86-
file: 'string.min.js',
87-
format: 'cjs',
88-
sourcemap: true,
89-
banner
90-
},
91-
{
92-
file: 'string.es.min.js',
93-
format: 'es',
94-
sourcemap: true,
95-
banner
96-
}
97-
],
98-
...commonConfig
99-
}
100-
];
101-
102-
export default rollupConfig;
1+
import typescript from '@rollup/plugin-typescript';
2+
import pkg from './package.json' assert { type: 'json' };
3+
import terser from '@rollup/plugin-terser';
4+
import bundleSize from 'rollup-plugin-bundle-size';
5+
6+
const banner = `
7+
/*! ================================
8+
${pkg.name} v${pkg.version}
9+
(c) 2020-present ${pkg.author}
10+
Released under ${pkg.license} License
11+
================================== */
12+
`;
13+
14+
/**
15+
* @type {import('rollup/dist/rollup').InputOptions}
16+
*/
17+
const commonConfig = {
18+
external: [
19+
...Object.keys(pkg.dependencies ?? {}),
20+
...Object.keys(pkg.optionalDependencies ?? {}),
21+
...Object.keys(pkg.peerDependencies ?? {})
22+
],
23+
plugins: [
24+
// it doesn't find the config by default and doesn't emit interface files
25+
// todo - https://github.com/rollup/plugins/pull/791/files#diff-77ceb76f06466d761730b952567396e6b5c292cc4044441cdfdf048b4614881dR83 check those tests
26+
typescript({ tsconfig: './tsconfig.json' }),
27+
terser({
28+
format: {
29+
comments: (node, comment) => {
30+
if (comment.type === "comment2") {
31+
return /@upfront/.test(comment.value);
32+
}
33+
}
34+
}
35+
}),
36+
bundleSize()
37+
]
38+
};
39+
40+
/**
41+
* @type {import('rollup/dist/rollup').RollupOptions[]}
42+
*/
43+
const rollupConfig = [
44+
{
45+
input: 'src/index.ts',
46+
output: [
47+
{
48+
file: pkg.main,
49+
format: 'cjs',
50+
sourcemap: true,
51+
banner
52+
},
53+
{
54+
file: pkg.module,
55+
format: 'es',
56+
sourcemap: true,
57+
banner
58+
}
59+
],
60+
...commonConfig
61+
},
62+
63+
{
64+
input: 'src/array.ts',
65+
output: [
66+
{
67+
file: 'array.min.js',
68+
format: 'cjs',
69+
sourcemap: true,
70+
banner
71+
},
72+
{
73+
file: 'array.es.min.js',
74+
format: 'es',
75+
sourcemap: true,
76+
banner
77+
}
78+
],
79+
...commonConfig
80+
},
81+
82+
{
83+
input: 'src/string.ts',
84+
output: [
85+
{
86+
file: 'string.min.js',
87+
format: 'cjs',
88+
sourcemap: true,
89+
banner
90+
},
91+
{
92+
file: 'string.es.min.js',
93+
format: 'es',
94+
sourcemap: true,
95+
banner
96+
}
97+
],
98+
...commonConfig
99+
}
100+
];
101+
102+
export default rollupConfig;

src/Calliope/AncestryCollection.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ export default class AncestryCollection<T extends Model> extends ModelCollection
2222
*/
2323
protected childrenRelation: string;
2424

25-
public constructor(
25+
/**
26+
* @param models - The models already arranged in an ancestry tree format.
27+
* @param parentKey - The key that identifies the parent's id.
28+
* @param childrenRelation - The key that will include descendants.
29+
*
30+
*/
31+
protected constructor(
2632
models?: MaybeArray<T>,
2733
parentKey = 'parentId',
2834
childrenRelation = 'children'
@@ -124,7 +130,7 @@ export default class AncestryCollection<T extends Model> extends ModelCollection
124130
models.forEach(model => {
125131
const children = model.getAttribute(this.childrenRelation) as ModelCollection<T> | T[] | undefined;
126132

127-
if (!children || !children.length) {
133+
if (!children?.length) {
128134
leaves.push(model);
129135
}
130136

src/Calliope/Concerns/BuildsQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ export default class BuildsQuery extends HasAttributes {
778778
}
779779

780780
/**
781-
* The static version of the when method.
781+
* The static version of the `when` method.
782782
*
783783
* @param {any} value
784784
* @param {function} closure

0 commit comments

Comments
 (0)