initial code

This commit is contained in:
cupcakearmy 2020-08-28 22:09:55 +02:00
parent fc3b9a25ea
commit 89fe5a4072
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
4 changed files with 124 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
node_modules
yarn.lock
dist
tsconfig.tsbuildinfo

2
.npmignore Normal file
View File

@ -0,0 +1,2 @@
*
!dist

63
src/index.ts Normal file
View File

@ -0,0 +1,63 @@
import { MikroORM, IDatabaseDriver, Connection, EntityRepository } from '@mikro-orm/core'
export type ObjectType<T> = { new (): T } | Function
let orm: MikroORM<IDatabaseDriver<Connection>>
export abstract class BaseEntity {
static getRepo<T extends BaseEntity>(this: ObjectType<T>) {
if (!orm) throw new Error('Orm not initialized')
return orm.em.getRepository<T>(this)
}
static count<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['count']>): ReturnType<EntityRepository<T>['count']> {
return (this as any).getRepo().count(...args)
}
static create<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['create']>): ReturnType<EntityRepository<T>['create']> {
return (this as any).getRepo().create(...args)
}
static find<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['find']>): ReturnType<EntityRepository<T>['find']> {
return (this as any).getRepo().find(...args)
}
static findAll<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['findAll']>): ReturnType<EntityRepository<T>['findAll']> {
return (this as any).getRepo().findAll(...args)
}
static findAndCount<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['findAndCount']>): ReturnType<EntityRepository<T>['findAndCount']> {
return (this as any).getRepo().findAndCount(...args)
}
static findOne<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['findOne']>): ReturnType<EntityRepository<T>['findOne']> {
return (this as any).getRepo().findOne(...args)
}
static findOneOrFail<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['findOneOrFail']>): ReturnType<EntityRepository<T>['findOneOrFail']> {
return (this as any).getRepo().findOneOrFail(...args)
}
static flush<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['flush']>): ReturnType<EntityRepository<T>['flush']> {
return (this as any).getRepo().flush(...args)
}
static getReference<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['getReference']>): ReturnType<EntityRepository<T>['getReference']> {
return (this as any).getRepo().getReference(...args)
}
static map<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['map']>): ReturnType<EntityRepository<T>['findOneOrFail']> {
return (this as any).getRepo().map(...args)
}
static nativeDelete<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['nativeDelete']>): ReturnType<EntityRepository<T>['nativeDelete']> {
return (this as any).getRepo().nativeDelete(...args)
}
static nativeInsert<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['nativeInsert']>): ReturnType<EntityRepository<T>['nativeInsert']> {
return (this as any).getRepo().nativeInsert(...args)
}
static nativeUpdate<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['nativeUpdate']>): ReturnType<EntityRepository<T>['nativeUpdate']> {
return (this as any).getRepo().nativeUpdate(...args)
}
static persist<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['persist']>): ReturnType<EntityRepository<T>['persist']> {
return (this as any).getRepo().persist(...args)
}
static populate<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['populate']>): ReturnType<EntityRepository<T>['populate']> {
return (this as any).getRepo().populate(...args)
}
static remove<T extends BaseEntity>(this: ObjectType<T>, ...args: Parameters<EntityRepository<T>['remove']>): ReturnType<EntityRepository<T>['remove']> {
return (this as any).getRepo().remove(...args)
}
}
export const register = <D extends IDatabaseDriver = IDatabaseDriver>(db: MikroORM<D>) => (orm = db)

55
tsconfig.json Normal file
View File

@ -0,0 +1,55 @@
{
"compilerOptions": {
"incremental": true /* Enable incremental compilation */,
"target": "ES2018" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"declaration": true /* Generates corresponding '.d.ts' file. */,
"declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,
"sourceMap": true /* Generates corresponding '.map' file. */,
"outDir": "./dist" /* Redirect output structure to the directory. */,
"rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
"strictNullChecks": true /* Enable strict null checks. */,
"strictFunctionTypes": true /* Enable strict checking of function types. */,
"strictBindCallApply": true /* Enable strict 'bind', 'call', and 'apply' methods on functions. */,
"strictPropertyInitialization": true /* Enable strict checking of property initialization in classes. */,
"noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */,
"alwaysStrict": true /* Parse in strict mode and emit "use strict" for each source file. */,
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
"experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,
"emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */,
/* Advanced Options */
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": ["./src"]
}