Skip to content

Commit bf88cab

Browse files
authored
feat: 0.16.0 (#343)
## Refactor: * refactor(query-builder): move query attributes into its own object * This slightly reduces size and looks better when logging a model out * refactor(model)!: remove non-static `find` method * If the model is instantiated it is likely to have been hydrated with data. If it has then, nothing to find. If it hasn't then the static method is enough. * refactor(internal): moved existence check up the class chain * refactor(model)!: remove `findMany` non-static method * refactor(model)!: removed `when` and `unless` non-static methods * refactor(model): move 'when` and `unless` methods to the model * refactor(internal): rename withouts query parameter to without ## Feature: * feat(helpers): added `value` function * feat(api-calls): add `setModelEndpoint` helper method * feat(model): added `tap` method * feat(model): added function resolving to `when` and `unless` methods ## Fix: * fix(relations): added existence check for `load` method * fix(api-calls): improved `get` method's type argument/return * fix(relations): fix `morphTo` relation * Endpoint was not set correctly for the next request * fix(model): add missing lastSyncedAt copy in the `clone` method ## Chore: * chore: increment version * chore: add optional peer dependencies * chore(deps-dev): updated dependencies ## Documentation * docs(model): fix heading for `make` section * docs(helpers): documented `value` function * docs(timestamps): removed invalid todo comment ## Testing * test(model): improved `tap`'s test * test(relations): fix morphTo test
1 parent 332fdb9 commit bf88cab

21 files changed

Lines changed: 1372 additions & 1269 deletions

docs/calliope/api-calls.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ user.appendToEndpoint('/something').getEndpoint(); // 'users/something'
152152
#### setLastSyncedAt
153153
<Badge text="advanced" type="warning"/>
154154

155-
The `setLastSyncedAt` method sets the [_lastSyncedAt](#_lastsyncedat) attribute to the current [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date). It optionally also accepts an argument for the value to be set as. The set value is subject to the [date-time casting](./attributes.md#datetime).
155+
The `setLastSyncedAt` method sets the [_lastSyncedAt](#_lastsyncedat) attribute to the current [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date). It optionally also accepts an argument for the value to be set as. The set value is subject to the [date-time casting](./attributes.md#datetime). Furthermore, it takes timestamps into consideration when enabled.
156156

157157
::: warning
158158
This method should only really be used when mocking the model to look like it exists. Some possible use-cases are:
@@ -167,3 +167,15 @@ const user = User.make({ id: 1 });
167167
user.exists; // false
168168
user.setLastSyncedAt().exists; // true
169169
```
170+
171+
#### setModelEndpoint
172+
173+
The `setModelEndpoint` sets the endpoint on the model to the resource endpoint by appending the primary key.
174+
175+
```js
176+
import User from '@Models/User';
177+
178+
const user = User.make({ id: 1 });
179+
user.getEndpoint(); // '/users'
180+
user.setModelEndpoint().getEndpoint(); // '/users/1'
181+
```

docs/calliope/query-building.md

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -241,26 +241,6 @@ import User from '@Models/User';
241241
User.limit(5);
242242
```
243243

244-
#### when
245-
246-
The `when` method calls the given closure when the first argument evaluates to a truthy value, allowing for adding constraints conditionally without breaking the method chaining.
247-
248-
```js
249-
import User from '@Models/User';
250-
251-
User.when(() => true, model => model.whereKey(1));
252-
```
253-
254-
#### unless
255-
256-
The `unless` method calls the given closure when the first argument evaluates to a falsy value, allowing for adding constraints conditionally without breaking the method chaining.
257-
258-
```js
259-
import User from '@Models/User';
260-
261-
User.unless(() => false, model => model.whereKey(1));
262-
```
263-
264244
#### distinct
265245

266246
The `distinct` method adds a distinct parameter for the request where the returned rows are expected to be distinct by the given columns.

docs/calliope/readme.md

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ Bundlers/minifier options examples:
167167
- [babel-minify: keepClassName](https://babeljs.io/docs/en/babel-minify#node-api)
168168
:::
169169

170-
#### create
170+
#### make
171171
<Badge text="static" type="warning"/>
172172

173-
The `create` method instantiates your model while setting up attributes and relations. This will mass assign attributes to the model while respecting the [guarding](./attributes#guarding) settings.
173+
The `make` method instantiates your model while setting up attributes and relations. This will mass assign attributes to the model while respecting the [guarding](./attributes#guarding) settings.
174174

175175
```ts
176176
import User from '@Models/User';
@@ -218,7 +218,7 @@ The `clone` method clones the instance in its current state. Meaning all changes
218218
```js
219219
import User from '@Models/User';
220220

221-
const user = User.factory().create({ myKey: 1 });
221+
const user = User.factory().createOne({ myKey: 1 });
222222
const userClone = user.clone();
223223
user.is(userClone); // true
224224

@@ -227,6 +227,46 @@ userClone.myKey === 1; // true
227227

228228
```
229229

230+
#### tap
231+
The `tap` method allows to use the model without affecting the model it is called on.
232+
233+
```js
234+
import User from '@Models/User';
235+
236+
const user = User.factory().createOne();
237+
user.with('relation')
238+
.select(['attribute1', 'attribute2'])
239+
.tap(console.log) // the model logged out to the console
240+
.tap(model => model.with('another-relation')) // will NOT add `another-relation` to the next query
241+
.get();
242+
```
243+
244+
#### when
245+
246+
The `when` method calls the given closure when the first argument evaluates to a truthy value, allowing for changing the model conditionally without breaking the method chaining.
247+
248+
```js
249+
import User from '@Models/User';
250+
251+
User.make()
252+
.when(true, user => user.setAttribute('test', 1))
253+
.when(() => false, user.setAttribute('test', 2))
254+
.getAttribute('test'); // 1
255+
```
256+
257+
#### unless
258+
259+
The `unless` method calls the given closure when the first argument evaluates to a falsy value, allowing for changing the model conditionally without breaking the method chaining.
260+
261+
```js
262+
import User from '@Models/User';
263+
264+
User.make()
265+
.unless(false, user => user.setAttribute('test', 1))
266+
.unless(() => true, user => user.setAttribute('test', 2))
267+
.getAttribute('test'); // 1
268+
```
269+
230270
#### factory
231271
<Badge text="static" type="warning"/>
232272

docs/calliope/relationships.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ import Contract from '@models/Contract'
232232

233233
const contract = await Contract.find(1);
234234
// same contract as above fetched from the API, with the relation set
235-
const contractedEntity = await contract.$contractable().get().then(contract => contract.contractable);
235+
const contractedEntity = await contract.$contractable().get<Contract>().then(contract => contract.contractable);
236236
```
237237

238238
## Manage Relations

docs/helpers/readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,3 +418,20 @@ const complexStructure = Team.factory().with(
418418

419419
dataGet(complexStructure, '0.users.0.shifts.0.id') === 1; // true
420420
```
421+
422+
#### value
423+
424+
The `value` is a function that simply resolves the given argument. If function given it will call the function with the passed in parameters. If not function given, it will return the value.
425+
426+
```ts
427+
import { value } from '@upfrontjs/framework';
428+
429+
value({}); // {};
430+
value(true); // true;
431+
value(() => []); // []
432+
value(
433+
(firstNum: number, secondNum: number) => firstNum + secondNum,
434+
1,
435+
1
436+
); // 2
437+
```

0 commit comments

Comments
 (0)