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
27 changes: 27 additions & 0 deletions docs/content/2.api/3.query/or-where-in.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: 'orWhereIn()'
description: 'Add a "or where in" clause to the query.'
---

# `orWhereIn()`

## Usage

````ts
import { useRepo } from 'pinia-orm'
import User from './models/User'

const userRepo = useRepo(User)

// Filter by array
console.log(userRepo.orWhereIn('commentIds', [1,2,5]).get())
// Filter by Set
console.log(userRepo.orWhereIn('commentIds', new Set([1,2,5])).get())

````

## Typescript Declarations

````ts
function orWhereIn(field: string, values: any[]|Set): Query
````
27 changes: 27 additions & 0 deletions docs/content/2.api/3.query/or-where-not-in.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: 'orWhereNotIn()'
description: 'Add a "or where not in" clause to the query.'
---

# `orWhereNotIn()`

## Usage

````ts
import { useRepo } from 'pinia-orm'
import User from './models/User'

const userRepo = useRepo(User)

// Filter by array
console.log(userRepo.orWhereNotIn('commentIds', [1,2,5]).get())
// Filter by Set
console.log(userRepo.orWhereNotIn('commentIds', new Set([1,2,5])).get())

````

## Typescript Declarations

````ts
function orWhereNotIn(field: string, values: any[]|Set): Query
````
27 changes: 27 additions & 0 deletions docs/content/2.api/3.query/where-not-in.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: 'whereNotIn()'
description: 'Add a "where not in" clause to the query.'
---

# `whereNotIn()`

## Usage

````ts
import { useRepo } from 'pinia-orm'
import User from './models/User'

const userRepo = useRepo(User)

// Filter by array
console.log(userRepo.whereNotIn('commentIds', [1,2,5]).get())
// Filter by Set
console.log(userRepo.whereNotIn('commentIds', new Set([1,2,5])).get())

````

## Typescript Declarations

````ts
function whereNotIn(field: string, values: any[]|Set): Query
````
26 changes: 26 additions & 0 deletions docs/content/2.api/3.query/where-not-null.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: 'whereNotNull()'
description: 'Add a where clause to get all results where `field` is not null'
---

# `where()`

## Usage

````ts
import { useRepo } from 'pinia-orm'
import User from './models/User'

const userRepo = useRepo(User)

console.log(userRepo.whereNotNull('age').get()) // User[] - where age property is not null

````

## Typescript Declarations

````ts
function whereNotNull(
field: string,
): Query
````
26 changes: 26 additions & 0 deletions docs/content/2.api/3.query/where-null.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: 'whereNull()'
description: 'Add a where clause to get all results where `field` is null'
---

# `where()`

## Usage

````ts
import { useRepo } from 'pinia-orm'
import User from './models/User'

const userRepo = useRepo(User)

console.log(userRepo.whereNull('age').get()) // User[] - with age set to null

````

## Typescript Declarations

````ts
function whereNull(
field: string,
): Query
````
2 changes: 1 addition & 1 deletion packages/pinia-orm/.eslintcache

Large diffs are not rendered by default.

43 changes: 39 additions & 4 deletions packages/pinia-orm/src/query/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,31 @@ export class Query<M extends Model = Model> {
*/
whereIn (field: string, values: any[] | Set<any>): this {
if (values instanceof Set) { values = Array.from(values) }
return this.where(field, values)
}

this.wheres.push({ field, value: values, boolean: 'and' })
/**
* Add a "where not in" clause to the query.
*/
whereNotIn (field: string, values: any[] | Set<any>): this {
if (values instanceof Set) { values = Array.from(values) }
return this.where(query => !values.includes(query[field]))
}

return this
/**
* Add a "where not in" clause to the query.
*/
orWhereIn (field: string, values: any[] | Set<any>): this {
if (values instanceof Set) { values = Array.from(values) }
return this.orWhere(field, values)
}

/**
* Add a "where not in" clause to the query.
*/
orWhereNotIn (field: string, values: any[] | Set<any>): this {
if (values instanceof Set) { values = Array.from(values) }
return this.orWhere(query => !values.includes(query[field]))
}

/**
Expand All @@ -246,6 +267,20 @@ export class Query<M extends Model = Model> {
return this
}

/**
* Add a "whereNULL" clause to the query.
*/
whereNull (field: string): this {
return this.where(field, null)
}

/**
* Add a "whereNotNULL" clause to the query.
*/
whereNotNull (field: string): this {
return this.where(query => query[field] != null)
}

/**
* Add a "where has" clause to the query.
*/
Expand Down Expand Up @@ -465,13 +500,13 @@ export class Query<M extends Model = Model> {

let models = this.select()

if (!this.orders) {
if (this.orders.length === 0) {
models = this.filterLimit(models)
}

if (!isEmpty(models)) { this.eagerLoadRelations(models) }

if (this.orders) {
if (this.orders.length > 0) {
models = this.filterOrder(models)
models = this.filterLimit(models)
}
Expand Down
22 changes: 13 additions & 9 deletions packages/pinia-orm/src/repository/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ export class Repository<M extends Model = Model> {
this.database = database
this.pinia = pinia
this.hydratedDataCache = hydratedDataCache as Map<string, M>
return new Proxy(this, {
get (repository, field) {
if (typeof field === 'symbol') { return }
if (field in repository) { return repository[field] } // normal case
if (field === 'use' || field === 'model' || field === 'queryCache') { return }

return function (...args: any) {
// This function will be executed when property is accessed as a function
// @ts-expect-error Query has index signature
return repository.query()[field](...args)
}
},
})
}

/**
Expand Down Expand Up @@ -337,15 +350,6 @@ export class Repository<M extends Model = Model> {
return this.query().get()
}

/**
* Find the model with the given id.
*/
find (id: string | number): Item<M>
find (ids: (string | number)[]): Collection<M>
find (ids: any): Item<any> {
return this.query().find(ids)
}

/**
* Retrieves the models from the store by following the given
* normalized schema.
Expand Down
36 changes: 36 additions & 0 deletions packages/pinia-orm/src/types/repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { Collection, Item, Model, Query } from '../'

declare module '../repository/Repository' {
interface Repository<M extends Model = Model> {
/**
* Add a where clause where `field` value is in values.
*/
whereIn (field: string, values: any[] | Set<any>): Query<M>
/**
* Add a where clause where `field` value is in values or ...
*/
orWhereIn (field: string, values: any[] | Set<any>): Query<M>
/**
* Add a where clause where `field` value is not in values or ...
*/
orWhereNotIn (field: string, values: any[] | Set<any>): Query<M>
/**
* Add a where clause where `field` has not defined values
*/
whereNotIn (field: string, values: any[] | Set<any>): Query<M>
/**
* Add a where clause to get all results where `field` is null
*/
whereNull (field: string): Query<M>
/**
* Add a where clause to get all results where `field` is not null
*/
whereNotNull (field: string): Query<M>
/**
* Find the model with the given id.
*/
find (id: string | number): Item<M>
find (ids: (string | number)[]): Collection<M>
find (ids: any): Item<any>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('feature/repository/retrieves_where', () => {
assertModels(users, expected)
})

it('can filter records by Set', () => {
it('can filter records by Set with whereIn', () => {
const userRepo = useRepo(User)

const ages = new Set([40, 30])
Expand All @@ -115,7 +115,7 @@ describe('feature/repository/retrieves_where', () => {
},
})

const users = userRepo.query().whereIn('age', ages).get()
const users = userRepo.whereIn('age', ages).get()

const expected = [
{ id: 1, name: 'John Doe', age: 40 },
Expand All @@ -125,5 +125,59 @@ describe('feature/repository/retrieves_where', () => {
expect(users).toHaveLength(2)
assertInstanceOf(users, User)
assertModels(users, expected)

const users2 = userRepo.whereNotIn('age', ages).get()

const expected2 = [
{ id: 3, name: 'Johnny Doe', age: 20 },
]

expect(users2).toHaveLength(1)
assertInstanceOf(users2, User)
assertModels(users2, expected2)

const users3 = userRepo.orWhereIn('name', new Set(['John Doe'])).orWhereNotIn('age', ages).get()

const expected3 = [
{ id: 1, name: 'John Doe', age: 40 },
{ id: 3, name: 'Johnny Doe', age: 20 },
]

expect(users3).toHaveLength(2)
assertInstanceOf(users3, User)
assertModels(users3, expected3)
})

it('can filter records with whereNULL and whereNotNull', () => {
const userRepo = useRepo(User)

fillState({
users: {
1: { id: 1, name: 'John Doe', age: null },
2: { id: 2, name: 'Jane Doe', age: 30 },
3: { id: 3, name: 'Johnny Doe', age: 20 },
},
})

const users = userRepo.whereNull('age').get()

const expected = [
{ id: 1, name: 'John Doe', age: null },
]

expect(users).toHaveLength(1)
assertInstanceOf(users, User)
assertModels(users, expected)

const users2 = userRepo.whereNotNull('age').get()

const expected2 = [
{ id: 2, name: 'Jane Doe', age: 30 },
{ id: 3, name: 'Johnny Doe', age: 20 },
]

expect(users2).toHaveLength(2)
assertInstanceOf(users2, User)
assertModels(users2, expected2)
})
})
4 changes: 4 additions & 0 deletions packages/pinia-orm/tests/unit/repository/Repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ describe('unit/repository/Repository', () => {

const postRepo = userRepo.repo(PostRepository.setModel(Post))

const symbol = Symbol('test')

expect(userRepo[symbol]).toBe(undefined)

expect(postRepo.getModel()).toBeInstanceOf(Post)
})

Expand Down