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
47 changes: 27 additions & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4

- name: Install pnpm
uses: pnpm/action-setup@v2
- run: corepack enable

- name: Set node version to 18
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 18
cache: "pnpm"

- name: Install pnpm
uses: pnpm/action-setup@v2

- run: pnpm i

- run: pnpm run build:ci
- run: pnpm run build:stub
- run: pnpm run build:prepare
- run: pnpm run build

- name: Run unit tests
run: pnpm run test
Expand All @@ -41,18 +45,19 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4

- name: Install pnpm
uses: pnpm/action-setup@v2
- run: corepack enable

- name: Set node version to 18
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 18
cache: "pnpm"

- run: pnpm i

- run: pnpm run build:ci
- run: pnpm run build:stub
- run: pnpm run build:prepare
- run: pnpm run build

- name: Run test coverage
run: pnpm run test:coverage
Expand All @@ -67,18 +72,19 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4

- name: Install pnpm
uses: pnpm/action-setup@v2
- run: corepack enable

- name: Set node version to 18
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 18
cache: "pnpm"

- run: pnpm i

- run: pnpm run build:ci
- run: pnpm run build:stub
- run: pnpm run build:prepare
- run: pnpm run build

- name: Run eslint
run: pnpm run lint
Expand All @@ -89,15 +95,16 @@ jobs:
CI_JOB_NUMBER: 1
steps:
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4

- name: Install pnpm
uses: pnpm/action-setup@v2
- run: corepack enable

- name: Set node version to 18
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 18
cache: "pnpm"

- run: pnpm i
- run: pnpm run build:ci
- run: pnpm run build:stub
- run: pnpm run build:prepare
- run: pnpm run build
- run: pnpm run size
30 changes: 30 additions & 0 deletions docs/content/2.api/2.model/1.options/3.namespace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: 'entity'
description: 'Defines the entity name in the store'
---

# `namespace`

Define a namespace if you have multiple equal entity names.
Resulting in "{namespace}/{entity}"

## Usage

````js
class User extends Model {
static entity = 'users'

static namespace = 'orm'

static fields () {
return {
userId: this.attr(null)
}
}
}
````

## Typescript Declarations
````ts
const namespace: string = ''
````
12 changes: 7 additions & 5 deletions docs/content/2.api/5.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ icon: heroicons-outline:adjustments

## `model`

| Option | Default | Description |
|------------|:-------:|:--------------------------------------------------------|
| `withMeta` | `false` | Activates the `_meta` field to be saved for every model |
| `visible` | `[*]` | Sets default visible fields for every model |
| `hidden` | `[]` | Sets default hidden fields for every model |
| Option | Default | Description |
|-------------|:-------:|:--------------------------------------------------------|
| `withMeta` | `false` | Activates the `_meta` field to be saved for every model |
| `visible` | `[*]` | Sets default visible fields for every model |
| `hidden` | `[]` | Sets default hidden fields for every model |
| `namespace` | `''` | Define a namespace for the store as prefix |

## `cache`

Expand All @@ -27,6 +28,7 @@ icon: heroicons-outline:adjustments
export interface ModelConfigOptions {
withMeta?: boolean
hidden?: string[]
namespace?: string
visible?: string[]
}

Expand Down
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
"packageManager": "pnpm@8.7.1",
"scripts": {
"release": "node scripts/release.mjs",
"size": "lerna run size --scope pinia-orm",
"build": "lerna run build --ignore @pina-orm/playground-*",
"cleanup": "rimraf 'packages/**/node_modules' 'playground/node_modules' 'node_modules'",
"size": "pnpm --filter './packages/**' size",
"build": "pnpm --filter './packages/**' build",
"build:stub": "pnpm --filter './packages/**' build --stub",
"build:prepare": "pnpm --filter './packages/**' dev:prepare",
"sponsor": "sponsorkit",
"build:ci": "lerna run build --ignore @pina-orm/playground-* --ignore @pinia-orm/nuxt",
"build:pinia-orm": "lerna run build --scope pinia-orm",
"play": "lerna run play",
"build:dts": "lerna run build:dts --parallel",
"format": "prettier -c --parser typescript \"packages/*/{src,__tests__,e2e}/**/*.[jt]s?(x)\"",
"format:fix": "pnpm run format --write",
"lint": "lerna run lint --ignore @pina-orm/playground-* --ignore @pina-orm/playground-nuxt3",
"lint:fix": "lerna run lint:fix --ignore @pina-orm/playground-* --ignore @pina-orm/playground-nuxt3",
"test": "lerna run test --scope pinia-orm",
"test:coverage": "lerna run coverage --scope pinia-orm",
"lint": "pnpm --filter './packages/**' lint",
"lint:fix": "pnpm --filter './packages/**' lint:fix",
"test": "pnpm --filter './packages/**' test",
"test:coverage": "pnpm --filter './packages/**' coverage",
"test:types": "tsc --build ./tsconfig.json",
"test:dts": "lerna run test:dts",
"docs:api": "lerna run docs:api --scope @pinia/docs"
Expand Down
1 change: 0 additions & 1 deletion packages/nuxt/src/runtime/plugin.vue2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ import { createORM } from 'pinia-orm'
import { ormOptions } from '#build/orm-options'

export default function (ctx: any) {
// eslint-disable-next-line import/no-named-as-default-member
ctx.$pinia.use(createORM(ormOptions))
}
1 change: 0 additions & 1 deletion packages/nuxt/src/runtime/plugin.vue3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { defineNuxtPlugin } from '#app'
import { ormOptions } from '#build/orm-options'

export default defineNuxtPlugin((nuxtApp) => {
// eslint-disable-next-line import/no-named-as-default-member
nuxtApp.$pinia.use(createORM(ormOptions))
setActivePinia(nuxtApp.$pinia)
})
4 changes: 2 additions & 2 deletions packages/pinia-orm/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// build.config.ts
import fs from 'node:fs'
// import fs from 'node:fs'
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
Expand All @@ -21,7 +21,7 @@ export default defineBuildConfig({
externals: ['@/composables', 'nanoid', 'uuid', 'nanoid/async', 'nanoid/non-secure', 'pinia'],
rollup: {
emitCJS: true
},
}
// hooks: {
// 'build:done': (ctx) => {
// ctx.buildEntries.filter(entry => entry.path.includes('.cjs') && !entry.path.includes('shared')).forEach((entry) => {
Expand Down
20 changes: 20 additions & 0 deletions packages/pinia-orm/src/model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export class Model {
*/
static baseEntity: string

/**
* Define a namespace if you have multiple equal entity names.
* Resulting in "{namespace}/{entity}"
*/
static namespace: string

/**
* The primary key for the model.
*/
Expand Down Expand Up @@ -601,6 +607,20 @@ export class Model {
return this.$self().config
}

/**
* Get the namespace.
*/
$namespace (): String {
return this.$self().namespace ?? config.model.namespace
}

/**
* Get the store name.
*/
$storeName (): string {
return (this.$namespace() ? this.$namespace() + '/' : '') + this.$baseEntity()
}

/**
* Get the base entity for this model.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/pinia-orm/src/query/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class Query<M extends Model = Model> {
* Commit a store action and get the data
*/
protected commit (name: string, payload?: any) {
const store = useDataStore(this.model.$baseEntity(), this.model.$piniaOptions(), this)(this.pinia)
const store = useDataStore(this.model.$storeName(), this.model.$piniaOptions(), this)(this.pinia)
if (name && typeof store[name] === 'function') { store[name](payload, false) }

if (this.cache && ['get', 'all', 'insert', 'flush', 'delete', 'update', 'destroy'].includes(name)) { this.cache.clear() }
Expand Down
2 changes: 1 addition & 1 deletion packages/pinia-orm/src/repository/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class Repository<M extends Model = Model> {
* Returns the pinia store used with this model
*/
piniaStore<S extends DataStoreState = DataStoreState> () {
return useDataStore<S>(this.model.$entity(), this.model.$piniaOptions(), this.query())(this.pinia)
return useDataStore<S>(this.model.$storeName(), this.model.$piniaOptions(), this.query())(this.pinia)
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/pinia-orm/src/store/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import type { FilledInstallOptions } from './Store'

export const CONFIG_DEFAULTS = {
model: {
namespace: '',
withMeta: false,
hidden: ['_meta'],
visible: ['*'],
visible: ['*']
},
cache: {
shared: true,
provider: WeakCache
},
}
}

export const config: FilledInstallOptions = { ...CONFIG_DEFAULTS }
1 change: 1 addition & 0 deletions packages/pinia-orm/src/store/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CONFIG_DEFAULTS, config } from './Config'
export interface ModelConfigOptions {
withMeta?: boolean
hidden?: string[]
namespace?: string
visible?: string[]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,14 @@ describe('feature/repository/retrieves_has', () => {
query.where('title', 'Title 03')
}).get()

const users3 = userRepo.whereHas('post').get()

const expected = [
{ id: 2, name: 'Jane Doe' }
]

expect(users).toHaveLength(1)
expect(users3).toHaveLength(2)
assertInstanceOf(users, User)
assertModels(users, expected)
assertModels(users2, [{ id: 1, name: 'John Doe' }, { id: 2, name: 'Jane Doe' }])
Expand All @@ -202,12 +205,15 @@ describe('feature/repository/retrieves_has', () => {
query.where('title', 'Title 03')
}).get()

const users2 = userRepo.whereDoesntHave('posts').get()

const expected = [
{ id: 2, name: 'Jane Doe' },
{ id: 3, name: 'Johnny Doe' }
]

expect(users).toHaveLength(2)
expect(users2).toHaveLength(1)
assertInstanceOf(users, User)
assertModels(users, expected)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,18 @@ describe('feature/repository/retrieves_has_or', () => {
.where('name', 'Johnny Doe')
.get()

const users2 = userRepo
.orWhereHas('posts')
.where('name', 'Johnny Doe')
.get()

const expected = [
{ id: 2, name: 'Jane Doe' },
{ id: 3, name: 'Johnny Doe' }
]

expect(users).toHaveLength(2)
expect(users2).toHaveLength(3)
assertInstanceOf(users, User)
assertModels(users, expected)
})
Expand Down Expand Up @@ -135,12 +141,18 @@ describe('feature/repository/retrieves_has_or', () => {
.where('name', 'Johnny Doe')
.get()

const users2 = userRepo
.orWhereDoesntHave('posts')
.where('name', 'Johnny Doe')
.get()

const expected = [
{ id: 2, name: 'Jane Doe' },
{ id: 3, name: 'Johnny Doe' }
]

expect(users).toHaveLength(2)
expect(users2).toHaveLength(1)
assertInstanceOf(users, User)
assertModels(users, expected)
})
Expand Down
Loading