Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/axios/.eslintcache

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/axios/src/composables/useAxiosApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request } from '../api/Request'
import type { AxiosRepository } from '../repository/AxiosRepository'

export function useAxiosApi (repository: AxiosRepository) {
export function useAxiosApi (repository: AxiosRepository<any>) {
return new Request(repository)
}
4 changes: 2 additions & 2 deletions packages/axios/src/composables/useAxiosRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { AxiosRepository } from '../repository/AxiosRepository'

export function useAxiosRepo<M extends Model> (model: M) {
const pinia = getActivePinia()
AxiosRepository.useModel = model
return useRepo(AxiosRepository, pinia)
AxiosRepository.useModel = model as unknown as typeof Model
return useRepo(AxiosRepository<Model>, pinia)
}
2 changes: 1 addition & 1 deletion packages/nuxt/.eslintcache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/build.config.ts":"1","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/eslint.config.mjs":"2","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/app.vue":"3","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/nuxt.config.ts":"4","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/src/module.ts":"5","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/models/ToDo.ts":"6","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/models/User.ts":"7","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/src/runtime/plugin.vue2.ts":"8","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/src/runtime/plugin.vue3.ts":"9"},{"size":235,"mtime":1714559937615,"results":"10","hashOfConfig":"11"},{"size":1823,"mtime":1714560279404,"results":"12","hashOfConfig":"13"},{"size":529,"mtime":1714559937615,"results":"14","hashOfConfig":"15"},{"size":314,"mtime":1714559937615,"results":"16","hashOfConfig":"11"},{"size":2171,"mtime":1714560751373,"results":"17","hashOfConfig":"11"},{"size":276,"mtime":1714559937615,"results":"18","hashOfConfig":"11"},{"size":435,"mtime":1714559937615,"results":"19","hashOfConfig":"11"},{"size":172,"mtime":1694953847623,"results":"20","hashOfConfig":"11"},{"size":297,"mtime":1714556854692,"results":"21","hashOfConfig":"11"},{"filePath":"22","messages":"23","suppressedMessages":"24","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"g061w5",{"filePath":"25","messages":"26","suppressedMessages":"27","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"1kc458",{"filePath":"28","messages":"29","suppressedMessages":"30","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"1iiqhc3",{"filePath":"31","messages":"32","suppressedMessages":"33","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"34","messages":"35","suppressedMessages":"36","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"37","messages":"38","suppressedMessages":"39","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"40","messages":"41","suppressedMessages":"42","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"43","messages":"44","suppressedMessages":"45","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"46","messages":"47","suppressedMessages":"48","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/build.config.ts",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/eslint.config.mjs",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/app.vue",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/nuxt.config.ts",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/src/module.ts",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/models/ToDo.ts",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/models/User.ts",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/src/runtime/plugin.vue2.ts",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/src/runtime/plugin.vue3.ts",[],[]]
[{"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/build.config.ts":"1","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/eslint.config.mjs":"2","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/app.vue":"3","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/nuxt.config.ts":"4","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/src/module.ts":"5","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/models/ToDo.ts":"6","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/models/User.ts":"7","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/src/runtime/plugin.vue2.ts":"8","/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/src/runtime/plugin.vue3.ts":"9","/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/build.config.ts":"10","/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/eslint.config.mjs":"11","/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/playground/app.vue":"12","/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/playground/nuxt.config.ts":"13","/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/src/module.ts":"14","/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/playground/models/ToDo.ts":"15","/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/playground/models/User.ts":"16","/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/src/runtime/plugin.vue2.ts":"17","/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/src/runtime/plugin.vue3.ts":"18"},{"size":235,"mtime":1714559937615,"results":"19","hashOfConfig":"20"},{"size":1823,"mtime":1714560279404,"results":"21","hashOfConfig":"22"},{"size":529,"mtime":1714559937615,"results":"23","hashOfConfig":"24"},{"size":314,"mtime":1714559937615,"results":"25","hashOfConfig":"20"},{"size":2171,"mtime":1714560751373,"results":"26","hashOfConfig":"20"},{"size":276,"mtime":1714559937615,"results":"27","hashOfConfig":"20"},{"size":435,"mtime":1714559937615,"results":"28","hashOfConfig":"20"},{"size":172,"mtime":1694953847623,"results":"29","hashOfConfig":"20"},{"size":297,"mtime":1714556854692,"results":"30","hashOfConfig":"20"},{"size":223,"mtime":1714939657239,"results":"31","hashOfConfig":"32"},{"size":1823,"mtime":1714939657249,"results":"33","hashOfConfig":"34"},{"size":512,"mtime":1714939657249,"results":"35","hashOfConfig":"36"},{"size":314,"mtime":1714939657249,"results":"37","hashOfConfig":"32"},{"size":2171,"mtime":1714939657249,"results":"38","hashOfConfig":"32"},{"size":276,"mtime":1714939657249,"results":"39","hashOfConfig":"32"},{"size":435,"mtime":1714939657249,"results":"40","hashOfConfig":"32"},{"size":166,"mtime":1714939657329,"results":"41","hashOfConfig":"32"},{"size":297,"mtime":1714939657329,"results":"42","hashOfConfig":"32"},{"filePath":"43","messages":"44","suppressedMessages":"45","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"g061w5",{"filePath":"46","messages":"47","suppressedMessages":"48","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"1kc458",{"filePath":"49","messages":"50","suppressedMessages":"51","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"1iiqhc3",{"filePath":"52","messages":"53","suppressedMessages":"54","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"55","messages":"56","suppressedMessages":"57","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"58","messages":"59","suppressedMessages":"60","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"61","messages":"62","suppressedMessages":"63","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"64","messages":"65","suppressedMessages":"66","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"67","messages":"68","suppressedMessages":"69","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"70","messages":"71","suppressedMessages":"72","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"qqa32v",{"filePath":"73","messages":"74","suppressedMessages":"75","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"t9eayu",{"filePath":"76","messages":"77","suppressedMessages":"78","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"1n9n2ww",{"filePath":"79","messages":"80","suppressedMessages":"81","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"82","messages":"83","suppressedMessages":"84","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"85","messages":"86","suppressedMessages":"87","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"88","messages":"89","suppressedMessages":"90","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"91","messages":"92","suppressedMessages":"93","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"94","messages":"95","suppressedMessages":"96","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/build.config.ts",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/eslint.config.mjs",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/app.vue",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/nuxt.config.ts",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/src/module.ts",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/models/ToDo.ts",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/playground/models/User.ts",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/src/runtime/plugin.vue2.ts",[],[],"/home/dredd/PhpstormProjects/storm-tail/pinia-orm/packages/nuxt/src/runtime/plugin.vue3.ts",[],[],"/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/build.config.ts",[],[],"/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/eslint.config.mjs",[],[],"/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/playground/app.vue",[],[],"/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/playground/nuxt.config.ts",[],[],"/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/src/module.ts",[],[],"/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/playground/models/ToDo.ts",[],[],"/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/playground/models/User.ts",[],[],"/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/src/runtime/plugin.vue2.ts",[],[],"/home/dredd/PhpstormProjects/codedredd/pinia-orm-fix/pinia-orm-auto-types/packages/nuxt/src/runtime/plugin.vue3.ts",[],[]]
2 changes: 1 addition & 1 deletion packages/pinia-orm/.eslintcache

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/pinia-orm/src/composables/collection/useKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import type { Collection } from '../../../src'
/**
* The keys method returns all of the collection's primary keys
*/
export function useKeys (models: Collection): string[] {
export function useKeys (models: Collection<any>): string[] {
return models.map(model => model[model.$getLocalKey()])
}
2 changes: 1 addition & 1 deletion packages/pinia-orm/src/composables/collection/useSum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Collection } from '../../../src'
/**
* Get the sum value of the specified filed.
*/
export function useSum (models: Collection, field: string): number {
export function useSum (models: Collection<any>, field: string): number {
return models.reduce<number>((sum, item) => {
if (typeof item[field] === 'number') { sum += item[field] }

Expand Down
4 changes: 2 additions & 2 deletions packages/pinia-orm/src/composables/useDataStore.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { DefineStoreOptionsBase } from 'pinia'
import { defineStore } from 'pinia'
import type { Query } from '../query/Query'
import type { Query } from '../'
import { useStoreActions } from './useStoreActions'

export function useDataStore<S extends DataStoreState, T extends DataStore = DataStore> (
id: string,
options: DefineStoreOptionsBase<S, T>,
query?: Query,
query?: Query<any>,
) {
return defineStore(id, {
state: () => ({ data: {} } as S),
Expand Down
1 change: 1 addition & 0 deletions packages/pinia-orm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export * from './repository/Repository'
export * from './interpreter/Interpreter'
export * from './query/Query'
export * from './query/Options'
export * from './types'
52 changes: 29 additions & 23 deletions packages/pinia-orm/src/model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ export interface InheritanceTypes {
[key: string]: typeof Model
}

export type WithKeys<T> = { [P in keyof T]: T[P] extends (Model | null) | Model[] ? P & string : never }[keyof T]
// export type WithKeys<T> = { [P in keyof T]: T[P] extends Model[] ? P : never }[keyof T];

export class Model {
[s: keyof ModelFields]: any
// [s: keyof ModelFields]: any
pivot?: any

declare _meta: undefined | MetaValues
/**
Expand Down Expand Up @@ -523,47 +527,47 @@ export class Model {
/**
* Lifecycle hook for before saving
*/
static saving: BeforeHook = () => {}
static saving: BeforeHook = () => { }

/**
* Lifecycle hook for before updating
*/
static updating: BeforeHook = () => {}
static updating: BeforeHook = () => { }

/**
* Lifecycle hook for before creating
*/
static creating: BeforeHook = () => {}
static creating: BeforeHook = () => { }

/**
* Lifecycle hook for before deleting
*/
static deleting: BeforeHook = () => {}
static deleting: BeforeHook = () => { }

/**
* Lifecycle hook for after getting data
*/
static retrieved: AfterHook = () => {}
static retrieved: AfterHook = () => { }

/**
* Lifecycle hook for after saved
*/
static saved: AfterHook = () => {}
static saved: AfterHook = () => { }

/**
* Lifecycle hook for after updated
*/
static updated: AfterHook = () => {}
static updated: AfterHook = () => { }

/**
* Lifecycle hook for after created
*/
static created: AfterHook = () => {}
static created: AfterHook = () => { }

/**
* Lifecycle hook for after deleted
*/
static deleted: AfterHook = () => {}
static deleted: AfterHook = () => { }

/**
* Mutators to mutate matching fields when instantiating the model.
Expand Down Expand Up @@ -757,7 +761,7 @@ export class Model {

if (cast && operation === 'set') { keyValue = cast.set(keyValue) }

this[key] = this[key] ?? keyValue
this[key as keyof this] = this[key as keyof this] ?? keyValue
}

operation === 'set' && (this.$self().original[this.$getKey(this, true) as string] = this.$getAttributes())
Expand All @@ -770,12 +774,14 @@ export class Model {
protected $fillMeta (action = 'save') {
const timestamp = Math.floor(Date.now() / 1000)
if (action === 'save') {
this[this.$self().metaKey] = {
// @ts-expect-error Setting an object
this[this.$self().metaKey as keyof this] = {
createdAt: timestamp,
updatedAt: timestamp,
}
}
if (action === 'update') { this[this.$self().metaKey].updatedAt = timestamp }
// @ts-expect-error Setting an object
if (action === 'update') { this[this.$self().metaKey as keyof this].updatedAt = timestamp }
}

/**
Expand All @@ -784,11 +790,11 @@ export class Model {
protected $fillField (key: string, attr: Attribute, value: any): any {
if (value !== undefined) {
return attr instanceof MorphTo
? attr.setKey(key).make(value, this[attr.getType()])
? attr.setKey(key).make(value, this[attr.getType() as keyof this] as string)
: attr.setKey(key).make(value)
}

if (this[key] === undefined) { return attr.setKey(key).make() }
if (this[key as keyof this] === undefined) { return attr.setKey(key).make() }
}

protected isFieldVisible (key: string, modelHidden: string[], modelVisible: string[], options: ModelOptions): boolean {
Expand Down Expand Up @@ -836,9 +842,8 @@ export class Model {
* Get the composite key values for the given model as an array of ids.
*/
protected $getCompositeKey (record: Element): (string | number)[] | null {
let ids = [] as (string | number)[] | null

;(this.$getKeyName() as string[]).every((key) => {
let ids = [] as (string | number)[] | null;
(this.$getKeyName() as string[]).every((key) => {
const id = record[key]

if (isNullish(id)) {
Expand Down Expand Up @@ -917,7 +922,8 @@ export class Model {
this.pivot = model
return this
}
if (this.$fields()[relation]) { this[relation] = model }
// @ts-expect-error Setting model as field
if (this.$fields()[relation]) { this[relation as keyof this] = model }

return this
}
Expand Down Expand Up @@ -949,7 +955,7 @@ export class Model {
$refresh (): this {
if (this.$isDirty()) {
Object.entries(this.$getOriginal()).forEach((entry) => {
this[entry[0]] = entry[1]
this[entry[0] as keyof this] = entry[1]
})
}
return this
Expand All @@ -962,7 +968,7 @@ export class Model {
const original = this.$getOriginal()
if ($attribute) {
if (!Object.keys(original).includes($attribute)) { throwError(['The property"', $attribute, '"does not exit in the model "', this.$entity(), '"']) }
return !equals(this[$attribute], original[$attribute])
return !equals(this[$attribute as keyof this], original[$attribute])
}

return !equals(original, this.$getAttributes())
Expand All @@ -987,14 +993,14 @@ export class Model {

for (const key in fields) {
const attr = fields[key]
const value = model[key]
const value = model[key as keyof Model]

if (!(attr instanceof Relation)) {
record[key] = this.serializeValue(value)
continue
}

if (withRelation) { record[key] = this.serializeRelation(value) }
if (withRelation) { record[key] = this.serializeRelation(value as any) }
}

return record
Expand Down
Loading