Skip to content

Commit

Permalink
0.15.1 (#333)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
nandi95 authored Feb 6, 2023
1 parent 8e8d2b3 commit 332fdb9
Show file tree
Hide file tree
Showing 27 changed files with 2,355 additions and 11,106 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
strategy:
matrix:
# current and active LTS
node: [ 16, 18 ]
node: [ 18, 19 ]
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
Expand Down
7 changes: 6 additions & 1 deletion docs/calliope/ancestry-collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ This string determines the name of the attribute that is set on the models when
#### treeOf
<Badge text="static" type="warning"/>

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:
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.
Optionally the method takes 2 more arguments:
- `parentKey` (default: `'parentId'`) - the name of the attribute that contains the parent's identifier.
- `childrenRelation` (default: `'children'`): - the name of the relation the child models are nested under.

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

::: tip
This allows to implement a simple `find()` or `contains()` logic by calling `!!folderTree.flatten().findByKey(1)`.
:::

#### leaves

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.
Expand Down
28 changes: 20 additions & 8 deletions docs/calliope/relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,24 +203,36 @@ const userPassport = await user.$passport().get();

#### morphTo

The `morphTo` method describes a polymorphic relation where the model can describe to multiple entities.
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.

::: tip
`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.
:::

```ts
// Rate.ts
// Contract.ts
import { Model } from '@upfrontjs/framework';
import Car from '@models/Car';
import Team from '@models/Team';

export default class Rate extends Model {
public $rateables(): Car {
return this.morphTo();
export default class Contract extends Model {
public contractableId?: number;
public contractableType?: 'team' | 'car';
public contractable?: Team | Car;

public $contractable(): this {
return this.morphTo((self, attributesOfRelation) => {
return self.contractableType === 'team' ? Team : Car;
});
}
}

// myScript.ts
import Rate from '@models/Rate'
import Contract from '@models/Contract'

const rating = await Rate.limit(1).get();
const ratingWithRatedEntities = await rating.$rateables().get();
const contract = await Contract.find(1);
// same contract as above fetched from the API, with the relation set
const contractedEntity = await contract.$contractable().get().then(contract => contract.contractable);
```

## Manage Relations
Expand Down
12,860 changes: 1,964 additions & 10,896 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@upfrontjs/framework",
"version": "0.15.0",
"version": "0.15.1",
"description": "Data handling framework complementary to backend model systems.",
"main": "index.min.js",
"module": "index.es.min.js",
Expand Down Expand Up @@ -85,14 +85,15 @@
"@commitlint/config-conventional": "^17.0.0",
"@commitlint/prompt-cli": "^17.0.0",
"@commitlint/types": "^17.0.0",
"@rollup/plugin-typescript": "^8.1.0",
"@rollup/plugin-terser": "^0.4.0",
"@rollup/plugin-typescript": "^11.0.0",
"@semantic-release/git": "^10.0.0",
"@types/jest": "^29.0.1",
"@types/lodash": "^4.14.167",
"@types/pluralize": "^0.0.29",
"@types/qs": "^6.9.5",
"@types/semantic-release": "^17.2.1",
"@types/uuid": "^8.3.0",
"@types/semantic-release": "^20.0.1",
"@types/uuid": "^9.0.0",
"@typescript-eslint/eslint-plugin": "^5.38.0",
"@typescript-eslint/parser": "^5.38.0",
"commitlint": "^17.0.2",
Expand All @@ -105,10 +106,9 @@
"jest": "^29.0.3",
"jest-environment-jsdom": "^29.0.3",
"lint-staged": "^13.0.1",
"rollup": "^2.35.1",
"rollup": "^3.13.0",
"rollup-plugin-bundle-size": "^1.0.3",
"rollup-plugin-terser": "^7.0.2",
"semantic-release": "^19.0.2",
"semantic-release": "^20.1.0",
"ts-jest": "^29.0.0",
"tslib": "^2.2.0",
"typedoc": "^0.23.5",
Expand All @@ -132,6 +132,6 @@
}
],
"engines": {
"node": ">=14.13.1"
"node": ">=18.0.0"
}
}
204 changes: 102 additions & 102 deletions rollup.config.js → rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,102 +1,102 @@
import typescript from "@rollup/plugin-typescript";
import pkg from './package.json';
import { terser } from "rollup-plugin-terser";
import bundleSize from 'rollup-plugin-bundle-size';

const banner = `
/*! ================================
${pkg.name} v${pkg.version}
(c) 2020-present ${pkg.author}
Released under ${pkg.license} License
================================== */
`;

/**
* @type {import('rollup/dist/rollup').InputOptions}
*/
const commonConfig = {
external: [
...Object.keys(pkg.dependencies ?? {}),
...Object.keys(pkg.optionalDependencies ?? {}),
...Object.keys(pkg.peerDependencies ?? {})
],
plugins: [
// it doesn't find the config by default and doesn't emit interface files
// todo - https://github.com/rollup/plugins/pull/791/files#diff-77ceb76f06466d761730b952567396e6b5c292cc4044441cdfdf048b4614881dR83 check those tests
typescript({ tsconfig: './tsconfig.json' }),
terser({
format: {
comments: (node, comment) => {
if (comment.type === "comment2") {
return /@upfront/.test(comment.value);
}
}
}
}),
bundleSize()
]
};

/**
* @type {import('rollup/dist/rollup').RollupOptions[]}
*/
const rollupConfig = [
{
input: 'src/index.ts',
output: [
{
file: pkg.main,
format: 'cjs',
sourcemap: true,
banner
},
{
file: pkg.module,
format: 'es',
sourcemap: true,
banner
}
],
...commonConfig
},

{
input: 'src/array.ts',
output: [
{
file: 'array.min.js',
format: 'cjs',
sourcemap: true,
banner
},
{
file: 'array.es.min.js',
format: 'es',
sourcemap: true,
banner
}
],
...commonConfig
},

{
input: 'src/string.ts',
output: [
{
file: 'string.min.js',
format: 'cjs',
sourcemap: true,
banner
},
{
file: 'string.es.min.js',
format: 'es',
sourcemap: true,
banner
}
],
...commonConfig
}
];

export default rollupConfig;
import typescript from '@rollup/plugin-typescript';
import pkg from './package.json' assert { type: 'json' };
import terser from '@rollup/plugin-terser';
import bundleSize from 'rollup-plugin-bundle-size';

const banner = `
/*! ================================
${pkg.name} v${pkg.version}
(c) 2020-present ${pkg.author}
Released under ${pkg.license} License
================================== */
`;

/**
* @type {import('rollup/dist/rollup').InputOptions}
*/
const commonConfig = {
external: [
...Object.keys(pkg.dependencies ?? {}),
...Object.keys(pkg.optionalDependencies ?? {}),
...Object.keys(pkg.peerDependencies ?? {})
],
plugins: [
// it doesn't find the config by default and doesn't emit interface files
// todo - https://github.com/rollup/plugins/pull/791/files#diff-77ceb76f06466d761730b952567396e6b5c292cc4044441cdfdf048b4614881dR83 check those tests
typescript({ tsconfig: './tsconfig.json' }),
terser({
format: {
comments: (node, comment) => {
if (comment.type === "comment2") {
return /@upfront/.test(comment.value);
}
}
}
}),
bundleSize()
]
};

/**
* @type {import('rollup/dist/rollup').RollupOptions[]}
*/
const rollupConfig = [
{
input: 'src/index.ts',
output: [
{
file: pkg.main,
format: 'cjs',
sourcemap: true,
banner
},
{
file: pkg.module,
format: 'es',
sourcemap: true,
banner
}
],
...commonConfig
},

{
input: 'src/array.ts',
output: [
{
file: 'array.min.js',
format: 'cjs',
sourcemap: true,
banner
},
{
file: 'array.es.min.js',
format: 'es',
sourcemap: true,
banner
}
],
...commonConfig
},

{
input: 'src/string.ts',
output: [
{
file: 'string.min.js',
format: 'cjs',
sourcemap: true,
banner
},
{
file: 'string.es.min.js',
format: 'es',
sourcemap: true,
banner
}
],
...commonConfig
}
];

export default rollupConfig;
10 changes: 8 additions & 2 deletions src/Calliope/AncestryCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ export default class AncestryCollection<T extends Model> extends ModelCollection
*/
protected childrenRelation: string;

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

if (!children || !children.length) {
if (!children?.length) {
leaves.push(model);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Calliope/Concerns/BuildsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ export default class BuildsQuery extends HasAttributes {
}

/**
* The static version of the when method.
* The static version of the `when` method.
*
* @param {any} value
* @param {function} closure
Expand Down
Loading

0 comments on commit 332fdb9

Please sign in to comment.