Skip to content
Open
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
117 changes: 117 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Type definitions for @hapipal/schwifty 6
// Project: https://github.com/hapipal/schwifty
// Definitions by: ozum <https://github.com/ozum>
// timcosta <https://github.com/timcosta>
// damusix <https://github.com/damusix>
// TypeScript Version: 4

import * as Joi from 'joi';
import { Plugin } from '@hapi/hapi';
import { Knex } from 'knex';
import { Model as ObjectionModel } from 'objection';

export type ModelClass = typeof Model | typeof ObjectionModel;

export class Model extends ObjectionModel {
static getJoiSchema(patch?: boolean): Joi.Schema;
static field(name: string): Joi.Schema;
static joiSchema: Joi.Schema;
static joiSchemaPatch: Joi.Schema;
}

export interface RegistrationOptions {
knex?: Knex | Knex.Config | undefined;
models?: ModelClass[] | string | undefined;
migrationsDir?: string | undefined;
teardownOnStop?: boolean | undefined;
migrateOnStart?: boolean | 'latest' | 'rollback' | undefined;
}

export function assertCompatible(
ModelA: typeof Model,
ModelB: typeof Model,
message?: string
): void | Error;
export const plugin: Plugin<RegistrationOptions>;

export const migrationsStubPath: string;

export const sandbox: Symbol;
export const bindKnex: Symbol;
export interface RegisteredModels {
[key: string]: ModelClass;
}

/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Hapi Decorations +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */

/**
* Server decorator for getting models scoped to the
* current plugin realm using `server.models()`,
* or models registered on the server using `server.models(true)`,
* or models scoped to plugin namespace using `server.models('namespace')`.
*
*
*
* This interface can be overwritten to modify what you want your namespace
* to actually return. For example:
*
* @example
*
* declare module '@hapipal/schwifty' {
* type AuthModels = {
* Members: Schwifty.Model
* Admin: Schwifty.Model
* Mananger: Schwifty.Model
* }
*
* type OathModels = {
* Witness: Schwifty.Model
* Promissory: Schwifty.Model
* CrownCourt: Schwifty.Model
* }
*
* interface SchwiftyDecorator {
* (namespace: 'auth'): AuthModels
* (namespace: 'oath'): OathModels
* }
* }
*
*/
export interface SchwiftyDecorator {

(all?: boolean): RegisteredModels
(namespace?: string): RegisteredModels
}



declare module '@hapi/hapi' {

interface Server {
schwifty: (
config:
| ModelClass
| ModelClass[]
| {
knex: Knex | Knex.Config;
models: ModelClass[];
migrationsDir: string;
}
) => void;
knex: () => Knex;
models: SchwiftyDecorator;

}

interface Request {
knex: () => Knex;
models: SchwiftyDecorator;
}

interface ResponseToolkit {
knex: () => Knex;
models: SchwiftyDecorator;
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "6.2.0",
"description": "A model layer for hapi integrating Objection ORM",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"test": "lab -a @hapi/code -t 100 -L",
"coveralls": "lab -r lcov | coveralls"
Expand Down