Skip to content

Commit 6f24ba6

Browse files
committed
fix: 🏷️ better server side types
1 parent b9c0f7a commit 6f24ba6

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

src/module.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ export default defineNuxtModule<ModuleOptions>({
210210
`export const oaSchemasByName = ${JSON.stringify(schemasByName)}`,
211211
`export const oaDefsSchemas = ${JSON.stringify(defsSchemas)}`,
212212
`export type OaModelName = keyof typeof oaSchemasByName`,
213-
`export const useOaServerSchema = () => ({ schemasByName: oaSchemasByName, defsSchemas: oaDefsSchemas })`
213+
`export const useOaServerSchema = () => ({ schemasByName: oaSchemasByName, defsSchemas: oaDefsSchemas })`,
214+
`export interface OaModels {`,
215+
...Object.keys(schemasByName).map(name => ` ${name}: Oa${name}`),
216+
'}'
214217
].join('\n')
215218
})
216219
nuxt.options.nitro.imports.presets.push({
@@ -224,7 +227,7 @@ export default defineNuxtModule<ModuleOptions>({
224227
// Add types
225228
let n = -1
226229
addTypeTemplate({
227-
filename: 'types/nuxt-oa.d.ts',
230+
filename: 'shared/nuxt-oa.d.ts',
228231
getContents: () => {
229232
if (++n) { // on schema update
230233
const { schemasByName, defsSchemas } = getSchemas(options, nuxt)

src/runtime/server/helpers/model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { pluralize } from './pluralize'
1313
import { decrypt, encrypt } from './cipher'
1414
import { useOaConfig } from './config'
1515
import * as _ from './_'
16-
import { useOaServerSchema, type OaModelName } from '~/.nuxt/oa/nitro'
16+
import { useOaServerSchema, type OaModelName, type OaModels } from '~/.nuxt/oa/nitro'
1717

1818
const { cipherAlgo, cipherKey, cipherIvSize, dbClientOnRenderer } = useOaConfig()
1919
const { schemasByName, defsSchemas } = useOaServerSchema()
@@ -25,7 +25,7 @@ addFormats(ajv)
2525
type Timestamps = { createdAt?: boolean, updatedAt?: boolean }
2626
type Userstamps = { createdBy?: boolean, updatedBy?: boolean, deletedBy?: boolean }
2727

28-
type OaDbItem<T extends OaModelName> = OaModels[T] & { _id?: ObjectId, createdAt?: string | Date, updatedAt?: string | Date, createdBy?: string | ObjectId, updatedBy?: string | ObjectId, updates?: unknown[], _iv?: string }
28+
type OaDbItem<T extends OaModelName> = Omit<OaModels[T], 'id'> & { _id?: ObjectId, createdAt?: string | Date, updatedAt?: string | Date, createdBy?: string | ObjectId, updatedBy?: string | ObjectId, updates?: unknown[], _iv?: string }
2929
type OaSchema<T extends OaModelName> = (typeof schemasByName)[T] & { encryptedProperties?: string[], trackedProperties?: (keyof OaDbItem<T>)[], timestamps: Timestamps | boolean, userstamps: Userstamps | boolean }
3030
type OaTrackedProps<T extends OaModelName> = keyof OaDbItem<T> & string
3131

0 commit comments

Comments
 (0)