From 89fe5a40722303c0caf3565a1e47ed1f3c0f1972 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Fri, 28 Aug 2020 22:09:55 +0200 Subject: [PATCH] initial code --- .gitignore | 4 ++++ .npmignore | 2 ++ src/index.ts | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 55 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 src/index.ts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..678035a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules +yarn.lock +dist +tsconfig.tsbuildinfo \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..284023d --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +* +!dist \ No newline at end of file diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..35e05f5 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,63 @@ +import { MikroORM, IDatabaseDriver, Connection, EntityRepository } from '@mikro-orm/core' + +export type ObjectType = { new (): T } | Function + +let orm: MikroORM> + +export abstract class BaseEntity { + static getRepo(this: ObjectType) { + if (!orm) throw new Error('Orm not initialized') + return orm.em.getRepository(this) + } + + static count(this: ObjectType, ...args: Parameters['count']>): ReturnType['count']> { + return (this as any).getRepo().count(...args) + } + static create(this: ObjectType, ...args: Parameters['create']>): ReturnType['create']> { + return (this as any).getRepo().create(...args) + } + static find(this: ObjectType, ...args: Parameters['find']>): ReturnType['find']> { + return (this as any).getRepo().find(...args) + } + static findAll(this: ObjectType, ...args: Parameters['findAll']>): ReturnType['findAll']> { + return (this as any).getRepo().findAll(...args) + } + static findAndCount(this: ObjectType, ...args: Parameters['findAndCount']>): ReturnType['findAndCount']> { + return (this as any).getRepo().findAndCount(...args) + } + static findOne(this: ObjectType, ...args: Parameters['findOne']>): ReturnType['findOne']> { + return (this as any).getRepo().findOne(...args) + } + static findOneOrFail(this: ObjectType, ...args: Parameters['findOneOrFail']>): ReturnType['findOneOrFail']> { + return (this as any).getRepo().findOneOrFail(...args) + } + static flush(this: ObjectType, ...args: Parameters['flush']>): ReturnType['flush']> { + return (this as any).getRepo().flush(...args) + } + static getReference(this: ObjectType, ...args: Parameters['getReference']>): ReturnType['getReference']> { + return (this as any).getRepo().getReference(...args) + } + static map(this: ObjectType, ...args: Parameters['map']>): ReturnType['findOneOrFail']> { + return (this as any).getRepo().map(...args) + } + static nativeDelete(this: ObjectType, ...args: Parameters['nativeDelete']>): ReturnType['nativeDelete']> { + return (this as any).getRepo().nativeDelete(...args) + } + static nativeInsert(this: ObjectType, ...args: Parameters['nativeInsert']>): ReturnType['nativeInsert']> { + return (this as any).getRepo().nativeInsert(...args) + } + static nativeUpdate(this: ObjectType, ...args: Parameters['nativeUpdate']>): ReturnType['nativeUpdate']> { + return (this as any).getRepo().nativeUpdate(...args) + } + static persist(this: ObjectType, ...args: Parameters['persist']>): ReturnType['persist']> { + return (this as any).getRepo().persist(...args) + } + static populate(this: ObjectType, ...args: Parameters['populate']>): ReturnType['populate']> { + return (this as any).getRepo().populate(...args) + } + static remove(this: ObjectType, ...args: Parameters['remove']>): ReturnType['remove']> { + return (this as any).getRepo().remove(...args) + } +} + +export const register = (db: MikroORM) => (orm = db) diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..dd0ef80 --- /dev/null +++ b/tsconfig.json @@ -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"] +}