From fad98f92a0e37d80e6d7f336e5bcfc02117a62a4 Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Tue, 12 Jul 2022 21:45:21 +0200 Subject: [PATCH 01/10] feat(pinia-orm): add mutators --- packages/pinia-orm/src/model/Model.ts | 31 +++++- packages/pinia-orm/src/query/Query.ts | 16 +-- packages/pinia-orm/src/types/index.ts | 11 ++ .../tests/unit/model/Model_Mutators.spec.ts | 104 ++++++++++++++++++ packages/pinia-orm/tsconfig.json | 5 +- 5 files changed, 154 insertions(+), 13 deletions(-) create mode 100644 packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts diff --git a/packages/pinia-orm/src/model/Model.ts b/packages/pinia-orm/src/model/Model.ts index 784c6bff9..fc60d4f43 100644 --- a/packages/pinia-orm/src/model/Model.ts +++ b/packages/pinia-orm/src/model/Model.ts @@ -1,5 +1,6 @@ import { assert, isArray, isNullish } from '../support/Utils' import type { Collection, Element, Item } from '../data/Data' +import type { Mutators } from '../types' import type { Attribute } from './attributes/Attribute' import { Attr } from './attributes/types/Attr' import { String as Str } from './attributes/types/String' @@ -24,6 +25,7 @@ export type ModelRegistry = Record Attribute> export interface ModelOptions { fill?: boolean relations?: boolean + mutator?: 'set' | 'get' | 'none' } export class Model { @@ -313,6 +315,13 @@ export class Model { return new MorphMany(model, related.newRawInstance(), id, type, localKey) } + /** + * Mutators to mutate matching fields when instantiating the model. + */ + static mutators(): Mutators { + return {} + } + /** * Get the constructor for this model. */ @@ -384,14 +393,27 @@ export class Model { $fill(attributes: Element = {}, options: ModelOptions = {}): this { const fields = this.$fields() const fillRelation = options.relations ?? true + const useMutator = options.mutator ?? 'get' for (const key in fields) { const attr = fields[key] - const value = attributes[key] + let value = attributes[key] if (attr instanceof Relation && !fillRelation) continue + if (useMutator !== 'none') { + const mutator = this.$getMutators()[key] + if (mutator && useMutator === 'get') { + value = typeof mutator === 'function' + ? mutator(value) + : typeof mutator.get === 'function' ? mutator.get(value) : value + } + + if (mutator && typeof mutator !== 'function' && useMutator === 'set' && mutator.set) + value = mutator.set(value) + } + this.$fillField(key, attr, value) } @@ -526,6 +548,13 @@ export class Model { return this } + /** + * Get the mutators of the model + */ + $getMutators(): Mutators { + return this.$self().mutators() + } + /** * Get the serialized model attributes. */ diff --git a/packages/pinia-orm/src/query/Query.ts b/packages/pinia-orm/src/query/Query.ts index 596ae093d..13f555a8a 100644 --- a/packages/pinia-orm/src/query/Query.ts +++ b/packages/pinia-orm/src/query/Query.ts @@ -10,7 +10,7 @@ import type { Collection, Element, Elements, Item } from '../data/Data' import type { Database } from '../database/Database' import { Relation } from '../model/attributes/relations/Relation' import { MorphTo } from '../model/attributes/relations/MorphTo' -import type { Model } from '../model/Model' +import type { Model, ModelOptions } from '../model/Model' import { Interpreter } from '../interpreter/Interpreter' import { Connection } from '../connection/Connection' import type { @@ -515,8 +515,8 @@ export class Query { const existing = currentData[id] newData[id] = existing - ? this.hydrate({ ...existing, ...record }).$getAttributes() - : this.hydrate(record).$getAttributes() + ? this.hydrate({ ...existing, ...record }, { mutator: 'set' }).$getAttributes() + : this.hydrate(record, { mutator: 'set' }).$getAttributes() } this.newConnection().save(newData) @@ -639,12 +639,12 @@ export class Query { /** * Instantiate new models with the given record. */ - protected hydrate(record: Element): M - protected hydrate(records: Element[]): Collection - protected hydrate(records: Element | Element[]): M | Collection { + protected hydrate(record: Element, options?: ModelOptions): M + protected hydrate(records: Element[], options?: ModelOptions): Collection + protected hydrate(records: Element | Element[], options?: ModelOptions): M | Collection { return isArray(records) - ? records.map(record => this.hydrate(record)) - : this.model.$newInstance(records, { relations: false }) + ? records.map(record => this.hydrate(record), options) + : this.model.$newInstance(records, { relations: false, ...(options || {}) }) } /** diff --git a/packages/pinia-orm/src/types/index.ts b/packages/pinia-orm/src/types/index.ts index c9826ce09..726faf254 100644 --- a/packages/pinia-orm/src/types/index.ts +++ b/packages/pinia-orm/src/types/index.ts @@ -1,3 +1,14 @@ export interface Constructor { new (...args: any[]): T } + +export type Mutator = (value: T) => T + +export interface MutatorFunctions { + get?: Mutator + set?: Mutator +} + +export interface Mutators { + [name: string]: MutatorFunctions | Mutator +} diff --git a/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts b/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts new file mode 100644 index 000000000..c2ac00b5e --- /dev/null +++ b/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts @@ -0,0 +1,104 @@ +import { describe, expect, it } from 'vitest' + +import { Attr, Model, useRepo } from '../../../src' +import {assertState} from "../../helpers"; + +describe('unit/model/Model_Sanitize', () => { + it('should mutate data if mutators are present', () => { + class User extends Model { + static entity = 'users' + + @Attr('') name!: string + + static mutators() { + return { + name(value: any) { + return value.toUpperCase() + }, + } + } + } + + expect(new User({ name: 'john doe' }).name).toBe('JOHN DOE') + }) + + it('should mutate data if mutators with getter are present', () => { + class User extends Model { + static entity = 'users' + + @Attr('') name!: string + + static mutators() { + return { + name: { + get: (value: any) => value.toUpperCase(), + }, + } + } + } + + expect(new User({ name: 'john doe' }).name).toBe('JOHN DOE') + }) + + it('should not mutate data in the store with get', () => { + class User extends Model { + static entity = 'users' + + @Attr(0) id!: number + @Attr('') name!: string + + static mutators() { + return { + name: { + get: (value: any) => value.toUpperCase(), + }, + } + } + } + + const userRepo = useRepo(User) + userRepo.save({ + id: 1, + name: 'John Doe', + }) + + assertState({ + users: { + 1: { id: 1, name: 'John Doe' }, + }, + }) + + expect(userRepo.find(1)?.name).toBe('JOHN DOE') + }) + + it('should mutate data in the store with set', () => { + class User extends Model { + static entity = 'users' + + @Attr(0) id!: number + @Attr('') name!: string + + static mutators() { + return { + name: { + set: (value: any) => value.toUpperCase(), + }, + } + } + } + + const userRepo = useRepo(User) + userRepo.save({ + id: 1, + name: 'John Doe', + }) + + assertState({ + users: { + 1: { id: 1, name: 'JOHN DOE' }, + }, + }) + + expect(userRepo.find(1)?.name).toBe('JOHN DOE') + }) +}) diff --git a/packages/pinia-orm/tsconfig.json b/packages/pinia-orm/tsconfig.json index 64712e940..a43bd05bb 100644 --- a/packages/pinia-orm/tsconfig.json +++ b/packages/pinia-orm/tsconfig.json @@ -13,10 +13,7 @@ "strict": true, "strictNullChecks": true, "target": "es2017", - "types": ["@types/uuid", "@types/node", "vitest/globals"], - "paths": { - "@/*": ["./src/*", "./dist/*"] - } + "types": ["@types/uuid", "@types/node", "vitest/globals"] }, "exclude": [ "**/dist", From c81002b78db6b7498880ac6c7ffbff17ceca53ba Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Tue, 12 Jul 2022 21:59:44 +0200 Subject: [PATCH 02/10] refactor(pinia-orm): linting --- packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts b/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts index c2ac00b5e..07b13f0f7 100644 --- a/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts +++ b/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from 'vitest' import { Attr, Model, useRepo } from '../../../src' -import {assertState} from "../../helpers"; +import { assertState } from '../../helpers' describe('unit/model/Model_Sanitize', () => { it('should mutate data if mutators are present', () => { @@ -81,7 +81,8 @@ describe('unit/model/Model_Sanitize', () => { static mutators() { return { name: { - set: (value: any) => value.toUpperCase(), + get: (value: any) => value.toUpperCase(), + set: (value: any) => value.toLowerCase(), }, } } From 7405c1abce0358fbdbbfc158ed8169b78e85ab6a Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Tue, 12 Jul 2022 22:06:54 +0200 Subject: [PATCH 03/10] test(pinia-orm): fix mutator test --- packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts b/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts index 07b13f0f7..ba7418062 100644 --- a/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts +++ b/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts @@ -81,8 +81,7 @@ describe('unit/model/Model_Sanitize', () => { static mutators() { return { name: { - get: (value: any) => value.toUpperCase(), - set: (value: any) => value.toLowerCase(), + set: (value: any) => value.toUpperCase(), }, } } From b81c271fe24c18a427f967a6bf023d993302f940 Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Tue, 12 Jul 2022 22:38:21 +0200 Subject: [PATCH 04/10] test(pinia-orm): update test name correctly --- packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts b/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts index ba7418062..dcb3f6533 100644 --- a/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts +++ b/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts @@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest' import { Attr, Model, useRepo } from '../../../src' import { assertState } from '../../helpers' -describe('unit/model/Model_Sanitize', () => { +describe('unit/model/Model_Mutators', () => { it('should mutate data if mutators are present', () => { class User extends Model { static entity = 'users' From 18e03fec73d7330e6090856bb7b80f7f6c6d19f4 Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Wed, 13 Jul 2022 09:43:24 +0200 Subject: [PATCH 05/10] feat(pinia-orm): add 'mutate' decorator --- packages/pinia-orm/src/index.ts | 1 + packages/pinia-orm/src/model/Model.ts | 33 +++++++++++++++++-- .../pinia-orm/src/model/decorators/Mutate.ts | 16 +++++++++ .../tests/unit/model/Model_Mutators.spec.ts | 14 +++++++- 4 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 packages/pinia-orm/src/model/decorators/Mutate.ts diff --git a/packages/pinia-orm/src/index.ts b/packages/pinia-orm/src/index.ts index 8e7b59c05..dab22be12 100644 --- a/packages/pinia-orm/src/index.ts +++ b/packages/pinia-orm/src/index.ts @@ -50,6 +50,7 @@ export * from './model/decorators/attributes/relations/MorphOne' export * from './model/decorators/attributes/relations/MorphTo' export * from './model/decorators/attributes/relations/MorphMany' export * from './model/decorators/Contracts' +export * from './model/decorators/Mutate' export * from './model/decorators/NonEnumerable' export * from './model/attributes/Attribute' export * from './model/attributes/types/Type' diff --git a/packages/pinia-orm/src/model/Model.ts b/packages/pinia-orm/src/model/Model.ts index fc60d4f43..d95cd79b0 100644 --- a/packages/pinia-orm/src/model/Model.ts +++ b/packages/pinia-orm/src/model/Model.ts @@ -1,6 +1,6 @@ import { assert, isArray, isNullish } from '../support/Utils' import type { Collection, Element, Item } from '../data/Data' -import type { Mutators } from '../types' +import type { MutatorFunctions, Mutators } from '../types' import type { Attribute } from './attributes/Attribute' import { Attr } from './attributes/types/Attr' import { String as Str } from './attributes/types/String' @@ -53,8 +53,18 @@ export class Model { */ protected static registries: ModelRegistries = {} + /** + * The pinia options for the model. It can contain options which will passed + * to the 'defineStore' function of pinia. + */ protected static piniaOptions: any = {} + /** + * The mutators for the model. It contains all mutators + * to the 'defineStore' function of pinia. + */ + protected static fieldMutators: Mutators = {} + /** * The array of booted models. */ @@ -113,12 +123,26 @@ export class Model { return this } + /** + * Set an mutator for a field + */ + static setMutator( + this: M, + key: string, + mutator: MutatorFunctions, + ): M { + this.fieldMutators[key] = mutator + + return this + } + /** * Clear the list of booted models so they can be re-booted. */ static clearBootedModels(): void { this.booted = {} this.schemas = {} + this.fieldMutators = {} } /** @@ -394,6 +418,11 @@ export class Model { const fields = this.$fields() const fillRelation = options.relations ?? true const useMutator = options.mutator ?? 'get' + const mutators: Mutators = { + ...this.$getMutators(), + ...this.$self().fieldMutators, + } + console.log('mutate', mutators, this.$self().fieldMutators) for (const key in fields) { const attr = fields[key] @@ -403,7 +432,7 @@ export class Model { continue if (useMutator !== 'none') { - const mutator = this.$getMutators()[key] + const mutator = mutators[key] if (mutator && useMutator === 'get') { value = typeof mutator === 'function' ? mutator(value) diff --git a/packages/pinia-orm/src/model/decorators/Mutate.ts b/packages/pinia-orm/src/model/decorators/Mutate.ts new file mode 100644 index 000000000..82733d38a --- /dev/null +++ b/packages/pinia-orm/src/model/decorators/Mutate.ts @@ -0,0 +1,16 @@ +import type { Mutator } from '../../types' +import type { PropertyDecorator } from './Contracts' + +/** + * Create an Mutate attribute property decorator. + */ +export function Mutate(get?: Mutator, set?: Mutator): PropertyDecorator { + return (target, propertyKey) => { + const self = target.$self() + console.log('mutate', propertyKey, target) + self.setMutator(propertyKey, { + get, + set, + }) + } +} diff --git a/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts b/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts index dcb3f6533..cec994023 100644 --- a/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts +++ b/packages/pinia-orm/tests/unit/model/Model_Mutators.spec.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest' -import { Attr, Model, useRepo } from '../../../src' +import { Attr, Model, Mutate, useRepo } from '../../../src' import { assertState } from '../../helpers' describe('unit/model/Model_Mutators', () => { @@ -22,6 +22,18 @@ describe('unit/model/Model_Mutators', () => { expect(new User({ name: 'john doe' }).name).toBe('JOHN DOE') }) + it('should mutate data if mutators are present with decorator', () => { + class User extends Model { + static entity = 'users' + + @Mutate((value: any) => value.toUpperCase()) + @Attr('') + name!: string + } + + expect(new User({ name: 'john doe' }).name).toBe('JOHN DOE') + }) + it('should mutate data if mutators with getter are present', () => { class User extends Model { static entity = 'users' From da5c41cdb1a8826b0cfb6c3fbd775c7f5d5db375 Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Wed, 13 Jul 2022 09:49:45 +0200 Subject: [PATCH 06/10] refactor(pinia-orm): remove console.log --- packages/pinia-orm/src/model/Model.ts | 1 - packages/pinia-orm/src/model/decorators/Mutate.ts | 6 +----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/pinia-orm/src/model/Model.ts b/packages/pinia-orm/src/model/Model.ts index d95cd79b0..fc8b754dd 100644 --- a/packages/pinia-orm/src/model/Model.ts +++ b/packages/pinia-orm/src/model/Model.ts @@ -422,7 +422,6 @@ export class Model { ...this.$getMutators(), ...this.$self().fieldMutators, } - console.log('mutate', mutators, this.$self().fieldMutators) for (const key in fields) { const attr = fields[key] diff --git a/packages/pinia-orm/src/model/decorators/Mutate.ts b/packages/pinia-orm/src/model/decorators/Mutate.ts index 82733d38a..eb8ffd14e 100644 --- a/packages/pinia-orm/src/model/decorators/Mutate.ts +++ b/packages/pinia-orm/src/model/decorators/Mutate.ts @@ -7,10 +7,6 @@ import type { PropertyDecorator } from './Contracts' export function Mutate(get?: Mutator, set?: Mutator): PropertyDecorator { return (target, propertyKey) => { const self = target.$self() - console.log('mutate', propertyKey, target) - self.setMutator(propertyKey, { - get, - set, - }) + self.setMutator(propertyKey, { get, set }) } } From 2dab3bb972f79418eaab5844eac10cd7c8b136a1 Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Wed, 13 Jul 2022 13:57:22 +0200 Subject: [PATCH 07/10] docs(pinia-orm): adding mutators --- docs/content/2.model/2.accessors.md | 75 +++++++++++++++++++++++++++- docs/content/2.model/3.decorators.md | 18 +++++++ packages/pinia-orm/src/index.cjs.ts | 2 + 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/docs/content/2.model/2.accessors.md b/docs/content/2.model/2.accessors.md index 119746c97..a0142274f 100644 --- a/docs/content/2.model/2.accessors.md +++ b/docs/content/2.model/2.accessors.md @@ -1,8 +1,10 @@ --- -title: Accessors +title: Accessors & Mutators description: '' --- +## Defining Accessors + Model accessors are computed properties which have access to the model instance and all it's properties and methods. You can define accessors on models to create virtual properties using methods and ES6 getters. Accessors are exempt from persistence and are hidden from model serialization. The following example defines an accessor on a User model as `fullName`, using ES6 getters, which combines the `firstName` and `lastName` attributes from the model schema, and also a prefix method to allow customizing the `firstName`. @@ -38,3 +40,74 @@ const user = store.$repo(User).make({ console.log(user.fullName) // 'John Doe' console.log(user.prefix('Sir.')) // 'Sir. John Doe' ``` + +## Defining Mutators + +Pina ORM lets you define mutators which are going to modify the specific field when instantiating the Model. +The difference between accessors and mutators is that mutators are going to modify the field itself. +You can define a setter, getter or both for a model. + +### directly via method + +Using directly a method is like using the `get` attribute. The getter will always be called before +displaying the data but will never change the value in the store. + +````ts{60-66} +import { Attr, Model } from 'pinia-orm' + +class User extends Model { + static entity = 'users' + + @Attr('') name!: string + + static mutators() { + return { + name(value: any) { + return value.toUpperCase() + }, + } + } +} +console.log(new User({ name: 'john doe' }).name) // 'JOHN DOE' +```` + +## via `set` and `get` + +Now with this approach you can also now apply an setter which will mutate +the value before it is saved in the store. + +````ts +import {Attr, Model, useRepo} from 'pinia-orm' +import { getActivePinia } from 'pinia'; + +class User extends Model { + static entity = 'users' + + @Attr(0) id!: number + @Attr('') name!: string + + static mutators() { + return { + name: { + get: (value: any) => value.toLowerCase(), + set: (value: any) => value.toUpperCase(), + }, + } + } +} + +const userRepo = useRepo(User) +userRepo.save({ + id: 1, + name: 'John Doe', +}) + +console.log(getActivePinia().state.value) +// users: { +// 1: {id: 1, name: 'JOHN DOE'}, +// } + +console.log(userRepo.find(1)?.name) // 'JOHN DOE' +```` + +If you like decorators you can also use the [`Mutate` decorator](3.decorators.md#mutator) to apply a mutation to an value diff --git a/docs/content/2.model/3.decorators.md b/docs/content/2.model/3.decorators.md index 33694a6a3..e803c8eb5 100644 --- a/docs/content/2.model/3.decorators.md +++ b/docs/content/2.model/3.decorators.md @@ -140,6 +140,24 @@ class User extends Model { } ``` +## Mutator + +Adds a [mutator](2.accessors.md#defining-mutators) to a property on the model. For example: + +````ts +import { Attr, Mutate, Model } from 'pinia-orm' + +class User extends Model { + static entity = 'users' + + @Mutate((value: any) => value.toUpperCase()) + @Attr('') + name!: string +} + +console.log(new User({ name: 'john doe' }).name) // 'JOHN DOE' +```` + ## Relationships Decorators on relation properties accept the same argument signature as their corresponding field attribute type with the exception that model references should be defined as a closure that return the model constructor (to avoid circular dependencies). diff --git a/packages/pinia-orm/src/index.cjs.ts b/packages/pinia-orm/src/index.cjs.ts index 4fbef0c0d..77cab9337 100644 --- a/packages/pinia-orm/src/index.cjs.ts +++ b/packages/pinia-orm/src/index.cjs.ts @@ -7,6 +7,7 @@ import { mapRepos } from './helpers/Helpers' import { Database } from './database/Database' import { Schema } from './schema/Schema' import { Model } from './model/Model' +import { Mutate } from './model/decorators/Mutate' import { Attr } from './model/decorators/attributes/types/Attr' import { Str } from './model/decorators/attributes/types/Str' import { Num } from './model/decorators/attributes/types/Num' @@ -49,6 +50,7 @@ export default { Database, Schema, Model, + Mutate, Attr, Str, Num, From 33f34f86083ed3cfbf9c60bf38828d541b33efbb Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Wed, 13 Jul 2022 14:57:30 +0200 Subject: [PATCH 08/10] docs(pinia-orm): fix broken links --- docs/content/2.model/2.accessors.md | 2 +- docs/content/2.model/3.decorators.md | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/docs/content/2.model/2.accessors.md b/docs/content/2.model/2.accessors.md index a0142274f..444105827 100644 --- a/docs/content/2.model/2.accessors.md +++ b/docs/content/2.model/2.accessors.md @@ -110,4 +110,4 @@ console.log(getActivePinia().state.value) console.log(userRepo.find(1)?.name) // 'JOHN DOE' ```` -If you like decorators you can also use the [`Mutate` decorator](3.decorators.md#mutator) to apply a mutation to an value +If you like decorators you can also use the [`Mutate` decorator](model/decorators#mutator) to apply a mutation to an value diff --git a/docs/content/2.model/3.decorators.md b/docs/content/2.model/3.decorators.md index e803c8eb5..0c21fe7b2 100644 --- a/docs/content/2.model/3.decorators.md +++ b/docs/content/2.model/3.decorators.md @@ -73,7 +73,7 @@ export class User extends Model { ### `@Str` -Marks a property on the model as a [string attribute](1.getting-started.md#string-type) type. For example: +Marks a property on the model as a [string attribute](./getting-started#string-type) type. For example: ```ts import { Model, Attr, Str } from 'pinia-orm' @@ -91,7 +91,7 @@ export class User extends Model { ### `@Num` -Marks a property on the model as a [number attribute](1.getting-started.md#string-type) type. For example: +Marks a property on the model as a [number attribute](./getting-started#string-type) type. For example: ```ts import { Model, Num } from 'pinia-orm' @@ -109,7 +109,7 @@ export class User extends Model { ### `@Bool` -Marks a property on the model as a [boolean attribute](1.getting-started.md#boolean-type) type. For example: +Marks a property on the model as a [boolean attribute](./getting-started#boolean-type) type. For example: ```ts import { Model, Bool } from 'pinia-orm' @@ -127,7 +127,7 @@ export class User extends Model { ### `@Uid` -Marks a property on the model as a [Uid attribute](1.getting-started.md#uid-type) type. For example: +Marks a property on the model as a [Uid attribute](./getting-started#uid-type) type. For example: ```ts import { Model, Uid } from 'pinia-orm' @@ -142,7 +142,7 @@ class User extends Model { ## Mutator -Adds a [mutator](2.accessors.md#defining-mutators) to a property on the model. For example: +Adds a [mutator](model/accessors#defining-mutators) to a property on the model. For example: ````ts import { Attr, Mutate, Model } from 'pinia-orm' @@ -164,7 +164,7 @@ Decorators on relation properties accept the same argument signature as their co ### `@HasOne` -Marks a property on the model as a [hasOne attribute](../relationships/1.getting-started.md) type. For example: +Marks a property on the model as a [hasOne attribute](../relationships/getting-started) type. For example: ```ts import { Model, HasOne } from 'pinia-orm' @@ -180,7 +180,7 @@ class User extends Model { ### `@BelongsTo` -Marks a property on the model as a [belongsTo attribute](../relationships/1.getting-started.md) type. For example: +Marks a property on the model as a [belongsTo attribute](../relationships/getting-started) type. For example: ```ts import { Model, Attr, BelongsTo } from 'pinia-orm' @@ -199,7 +199,7 @@ class Post extends Model { ### `@HasMany` -Marks a property on the model as a [hasMany attribute](../3.relationships/1.getting-started.md) type. For example: +Marks a property on the model as a [hasMany attribute](../relationships/getting-started) type. For example: ```ts import { Model, HasMany } from 'pinia-orm' @@ -215,7 +215,7 @@ class User extends Model { ### `@HasManyBy` -Marks a property on the model as a [hasManyBy attribute](../3.relationships/3.one-to-many) type. For example: +Marks a property on the model as a [hasManyBy attribute](../relationships/one-to-many) type. For example: ```ts import { Model, HasManyBy } from 'pinia-orm' @@ -234,7 +234,7 @@ class Cluster extends Model { ### `@BelongsToMany` -Marks a property on the model as a [belongsToMany attribute](../3.relationships/4.many-to-many) type. For example: +Marks a property on the model as a [belongsToMany attribute](../relationships/many-to-many) type. For example: ```ts import { Model, HasManyBy } from 'pinia-orm' @@ -250,7 +250,7 @@ class User extends Model { ### `@MorphOne` -Marks a property on the model as a [morphOne attribute](../3.relationships/5.polymorphic) type. For example: +Marks a property on the model as a [morphOne attribute](../relationships/polymorphic) type. For example: ```ts import { Model, MorphOne } from 'pinia-orm' @@ -266,7 +266,7 @@ class User extends Model { ### `@MorphTo` -Marks a property on the model as a [morphTo attribute](../3.relationships/5.polymorphic) type. For example: +Marks a property on the model as a [morphTo attribute](../relationships/polymorphic) type. For example: ```ts import { Model, MorphTo } from 'pinia-orm' @@ -289,7 +289,7 @@ class Image extends Model { ### `@MorphMany` -Marks a property on the model as a [morphMany attribute](../3.relationships/5.polymorphic) type. For example: +Marks a property on the model as a [morphMany attribute](../relationships/polymorphic) type. For example: ```ts import { Model, MorphMany } from 'pinia-orm' From 80f9e0ea293e624e0fd0ab9a380cc3a825a90ad1 Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Wed, 13 Jul 2022 15:01:42 +0200 Subject: [PATCH 09/10] docs(pinia-orm): fix two links --- docs/content/2.model/2.accessors.md | 2 +- docs/content/2.model/3.decorators.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/content/2.model/2.accessors.md b/docs/content/2.model/2.accessors.md index 444105827..1dc2b709e 100644 --- a/docs/content/2.model/2.accessors.md +++ b/docs/content/2.model/2.accessors.md @@ -110,4 +110,4 @@ console.log(getActivePinia().state.value) console.log(userRepo.find(1)?.name) // 'JOHN DOE' ```` -If you like decorators you can also use the [`Mutate` decorator](model/decorators#mutator) to apply a mutation to an value +If you like decorators you can also use the [`Mutate` decorator](/model/decorators#mutator) to apply a mutation to an value diff --git a/docs/content/2.model/3.decorators.md b/docs/content/2.model/3.decorators.md index 0c21fe7b2..e23e56cf8 100644 --- a/docs/content/2.model/3.decorators.md +++ b/docs/content/2.model/3.decorators.md @@ -142,7 +142,7 @@ class User extends Model { ## Mutator -Adds a [mutator](model/accessors#defining-mutators) to a property on the model. For example: +Adds a [mutator](/model/accessors#defining-mutators) to a property on the model. For example: ````ts import { Attr, Mutate, Model } from 'pinia-orm' From b4ad892b1297d16d38d6cbd240376687fd1547e8 Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Wed, 13 Jul 2022 15:15:31 +0200 Subject: [PATCH 10/10] docs(pinia-orm): missing comma --- docs/content/2.model/3.decorators.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/content/2.model/3.decorators.md b/docs/content/2.model/3.decorators.md index e23e56cf8..179126e42 100644 --- a/docs/content/2.model/3.decorators.md +++ b/docs/content/2.model/3.decorators.md @@ -37,7 +37,7 @@ class User extends Model { static fields () { return { id: this.attr(), - name: this.string('') + name: this.string(''), posts: this.hasMany(Post, 'userId') } }