-
Notifications
You must be signed in to change notification settings - Fork 639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Model and ts-mixer not bind globaly with Model.knex #2552
Comments
This way it's a working around
|
can I suggest a different approach? add these util types to your project import { type NonFunctionPropertyNames } from 'objection'
type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>
type IfEquals<X, Y, A=X, B=never> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? A : B
type NullableOptional<T> = { [P in keyof T]?: T[P] | null }
type ReadonlyKeysOf<T> = {
[P in keyof T]-?: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, never, P>
}[keyof T]
type OmitReadonly<T> = Omit<T, ReadonlyKeysOf<T>>
// these are what you'll using
export type EntityRead<T> = Omit<NonFunctionProperties<T>, 'QueryBuilderType'>
export type EntityInsert<T> = EntityRead<OmitReadonly<T>>
export type EntityUpdate<T> = NullableOptional<EntityInsert<T>> use export class User extends Model {
static tableName: string = 'users'
declare public readonly id: string
declare public username: string
declare public password: string
declare public optional?: string
declare public readonly createdAt: string
declare public readonly updatedAt: string
public dance(): string {
return '...'
}
} // class derive the entity interfaces from the model export type UserEntityRead = EntityRead<User> // all props, no methods
export type UserEntityInsert = EntityInsert<User> // required and optional but no readonly
export type UserEntityUpdate = EntityUpdate<User> // no readonly, the rest optional and nullable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've already seen this pattern working in a project where the 'outDir' is specified in the tsconfig.json file, so I believe it's the way TypeScript compiles, but in this case i don't wanna create a outDir folder.
versions: {
"ts-mixer": "^6.0.3",
"knex": "^3.0.1",
"objection": "^3.1.2",
"pg": "^8.11.2",
"ts-node": "^10.9.1",
}
The text was updated successfully, but these errors were encountered: