typeorm#InstanceChecker TypeScript Examples

The following examples show how to use typeorm#InstanceChecker. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: entity.ts    From typeorm-extension with MIT License 6 votes vote down vote up
export function getEntityName<O>(entity: ObjectType<O> | EntitySchema<O>) : string {
    if (typeof entity === 'function') {
        return entity.name;
    }

    if (InstanceChecker.isEntitySchema(entity)) {
        return entity.options.name;
    }

    return new (entity as any)().constructor.name;
}
Example #2
Source File: module.ts    From typeorm-extension with MIT License 4 votes vote down vote up
export async function findDataSource(
    context?: DataSourceFindOptions,
) : Promise<DataSource | undefined> {
    const fileNames : string[] = [
        'data-source',
    ];

    context = context || {};

    if (context.fileName) {
        fileNames.unshift(context.fileName);
    }

    const basePaths = [
        process.cwd(),
    ];

    if (
        context.directory &&
        context.directory !== process.cwd()
    ) {
        context.directory = path.isAbsolute(context.directory) ?
            context.directory :
            path.join(process.cwd(), context.directory);

        basePaths.unshift(context.directory);
    }

    const directories = [
        path.join('src', 'db'),
        path.join('src', 'database'),
        path.join('src'),
    ];

    let paths : string[] = [];
    for (let i = 0; i < basePaths.length; i++) {
        paths.push(basePaths[i]);

        if (basePaths[i] === process.cwd()) {
            for (let j = 0; j < directories.length; j++) {
                paths.push(path.join(basePaths[i], directories[j]));
            }
        }
    }

    if (!isTsNodeRuntimeEnvironment()) {
        let tsConfigFound = false;

        for (let i = 0; i < basePaths.length; i++) {
            const { compilerOptions } = await readTsConfig(basePaths[i]);
            if (compilerOptions) {
                paths = paths.map((item) => changeTSToJSPath(item, { dist: compilerOptions.outDir }));
                tsConfigFound = true;
                break;
            }
        }

        if (!tsConfigFound) {
            paths = paths.map((item) => changeTSToJSPath(item));
        }
    }

    for (let i = 0; i < fileNames.length; i++) {
        const info = await locateFile(`${fileNames[i]}.{js,ts}`, {
            path: paths,
        });

        if (info) {
            const fileExports = await loadScriptFile(info);
            if (InstanceChecker.isDataSource(fileExports)) {
                return fileExports;
            }

            if (typeof fileExports === 'object') {
                const keys = Object.keys(fileExports);
                for (let j = 0; j < keys.length; j++) {
                    const value = (fileExports as Record<string, any>)[keys[i]];

                    if (InstanceChecker.isDataSource(value)) {
                        return value;
                    }
                }
            }
        }
    }

    return undefined;
}