typeorm#DataSourceOptions TypeScript Examples

The following examples show how to use typeorm#DataSourceOptions. 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: module.ts    From typeorm-extension with MIT License 7 votes vote down vote up
export async function runSeeder(
    dataSource: DataSource,
    seeder: SeederConstructor,
    options?: SeederOptions,
) : Promise<void> {
    options = options || {};
    options.seeds = [seeder];
    options.factoriesLoad = options.factoriesLoad ?? true;

    if (
        options.factoriesLoad &&
        !options.factories
    ) {
        const { factories: dataSourceFactories } = dataSource.options as DataSourceOptions & SeederOptions;

        if (typeof dataSourceFactories !== 'undefined') {
            options.factories = dataSourceFactories;
        }
    }

    await prepareSeeder(options);

    setDataSource(dataSource);

    // eslint-disable-next-line new-cap
    const clazz = new seeder();

    const factoryManager = useSeederFactoryManager();
    await clazz.run(dataSource, factoryManager);
}
Example #2
Source File: character-set.ts    From typeorm-extension with MIT License 6 votes vote down vote up
export function getCharacterSetFromDataSourceOptions(options: DataSourceOptions) : string | undefined {
    if (
        hasOwnProperty(options, 'characterSet') &&
        typeof options.characterSet === 'string'
    ) {
        return options.characterSet;
    }

    if (typeof options?.extra?.characterSet === 'string') {
        return options.extra.characterSet;
    }

    return undefined;
}
Example #3
Source File: data-source.ts    From typeorm-extension with MIT License 6 votes vote down vote up
options : DataSourceOptions & SeederOptions = {
    type: 'better-sqlite3',
    entities: [User],
    database: path.join(__dirname, 'db.sqlite'),
    factories: ['test/data/factory/**/*{.ts,.js}'],
    seeds: ['test/data/seed/**/*{.ts,.js}'],
    extra: {
        charset: "UTF8_GENERAL_CI"
    }
}
Example #4
Source File: module.ts    From typeorm-extension with MIT License 6 votes vote down vote up
export async function runSeeders(
    dataSource: DataSource,
    options?: SeederOptions,
) : Promise<void> {
    options = options || {};

    const { seeds, factories } = dataSource.options as DataSourceOptions & SeederOptions;

    if (
        typeof options.seeds === 'undefined' &&
        typeof seeds !== 'undefined'
    ) {
        options.seeds = seeds;
    }

    if (
        typeof options.factories === 'undefined' &&
        typeof factories !== 'undefined'
    ) {
        options.factories = factories;
    }

    const items = await prepareSeeder(options);

    for (let i = 0; i < items.length; i++) {
        await runSeeder(dataSource, items[i], {
            factoriesLoad: false,
        });
    }
}
Example #5
Source File: synchronize.ts    From typeorm-extension with MIT License 6 votes vote down vote up
export async function synchronizeDatabase(options: DataSourceOptions) {
    const dataSource = new DataSource(options);
    await dataSource.initialize();

    let migrationsAmount = 0;
    if (options.migrations) {
        migrationsAmount = Array.isArray(options.migrations) ?
            options.migrations.length :
            Object.keys(options.migrations).length;
    }

    if (migrationsAmount > 0) {
        await dataSource.runMigrations();
    } else {
        await dataSource.synchronize(false);
    }

    await dataSource.destroy();
}
Example #6
Source File: context.ts    From typeorm-extension with MIT License 6 votes vote down vote up
async function setDatabaseContextOptions<T extends DatabaseBaseContext>(context: T) : Promise<T> {
    if (!context.options) {
        const dataSource = await findDataSource(context.findOptions);
        if (dataSource) {
            context.options = dataSource.options;
        }

        if (!context.options) {
            context.options = await buildDataSourceOptions();
        }
    }

    Object.assign(context.options, {
        subscribers: [],
        synchronize: false,
        migrationsRun: false,
        dropSchema: false,
        logging: [
            ...(process.env.NODE_ENV !== 'test' ? ['query', 'error', 'schema'] : []),
        ],
    } as DataSourceOptions);

    return context;
}
Example #7
Source File: create.ts    From typeorm-extension with MIT License 6 votes vote down vote up
export function createDriver(connectionOptions: DataSourceOptions) {
    const fakeConnection: DataSource = {
        options: {
            type: connectionOptions.type,
            ...(driversRequireDatabaseOption.indexOf(connectionOptions.type) !== -1 ? {
                database: connectionOptions.database,
            } : {}),
        },
    } as DataSource;

    const driverFactory = new DriverFactory();
    return driverFactory.create(fakeConnection);
}
Example #8
Source File: charset.ts    From typeorm-extension with MIT License 6 votes vote down vote up
export function getCharsetFromDataSourceOptions(options: DataSourceOptions) : string | undefined {
    if (
        hasOwnProperty(options, 'charset') &&
        typeof options.charset === 'string'
    ) {
        return options.charset;
    }

    if (typeof options?.extra?.charset === 'string') {
        return options.extra.charset;
    }

    return undefined;
}
Example #9
Source File: module.ts    From typeorm-extension with MIT License 6 votes vote down vote up
export async function extendDataSourceOptions(
    options: DataSourceOptions,
    tsConfigDirectory?: string,
) : Promise<DataSourceOptions> {
    options = setDefaultSeederOptions(options);

    let { compilerOptions } = await readTsConfig(tsConfigDirectory || process.cwd());
    compilerOptions = compilerOptions || {};

    modifyDataSourceOptionsForRuntimeEnvironment(options, {
        dist: compilerOptions.outDir,
    });

    return options;
}
Example #10
Source File: build.ts    From typeorm-extension with MIT License 6 votes vote down vote up
export function buildDriverOptions(options: DataSourceOptions): DriverOptions {
    let driverOptions: Record<string, any>;

    switch (options.type) {
        case 'mysql':
        case 'mariadb':
        case 'postgres':
        case 'cockroachdb':
        case 'mssql':
        case 'oracle':
            driverOptions = DriverUtils.buildDriverOptions(options.replication ? options.replication.master : options);
            break;
        case 'mongodb':
            driverOptions = DriverUtils.buildMongoDBDriverOptions(options);
            break;
        default:
            driverOptions = DriverUtils.buildDriverOptions(options);
    }

    const charset = getCharsetFromDataSourceOptions(options);
    const characterSet = getCharacterSetFromDataSourceOptions(options);

    return {
        host: driverOptions.host,
        user: driverOptions.user || driverOptions.username,
        password: driverOptions.password,
        database: driverOptions.database,
        port: driverOptions.port,
        ...(charset ? { charset } : {}),
        ...(characterSet ? { characterSet } : {}),
        ...(driverOptions.ssl ? { ssl: driverOptions.ssl } : {}),
        ...(driverOptions.url ? { url: driverOptions.url } : {}),
        ...(driverOptions.connectString ? { connectString: driverOptions.connectString } : {}),
        ...(driverOptions.sid ? { sid: driverOptions.sid } : {}),
        ...(driverOptions.serviceName ? { serviceName: driverOptions.serviceName } : {}),
        ...(options.extra ? { extra: options.extra } : {}),
        ...(driverOptions.domain ? { domain: driverOptions.domain } : {}),
    };
}
Example #11
Source File: singleton.ts    From typeorm-extension with MIT License 6 votes vote down vote up
export async function useDataSourceOptions(alias?: string) : Promise<DataSourceOptions> {
    alias = alias || 'default';

    if (Object.prototype.hasOwnProperty.call(instances, alias)) {
        return instances[alias];
    }

    /* istanbul ignore next */
    if (!Object.prototype.hasOwnProperty.call(instancePromises, alias)) {
        instancePromises[alias] = buildDataSourceOptions()
            .catch((e) => {
                delete instancePromises[alias];

                throw e;
            });
    }

    instances[alias] = await instancePromises[alias];

    return instances[alias];
}
Example #12
Source File: module.ts    From typeorm-extension with MIT License 6 votes vote down vote up
/**
 * Build DataSourceOptions from DataSource or from configuration.
 *
 * @param context
 */
export async function buildDataSourceOptions(
    context?: DataSourceOptionsBuildContext,
) : Promise<DataSourceOptions> {
    context = context ?? {};

    const directory : string = context.directory || process.cwd();
    const tsconfigDirectory : string = context.tsconfigDirectory || process.cwd();

    const dataSource = await findDataSource({
        directory,
        fileName: context.dataSourceName,
    });

    if (dataSource) {
        return extendDataSourceOptions(
            dataSource.options,
            tsconfigDirectory,
        );
    }

    return buildLegacyDataSourceOptions(context);
}
Example #13
Source File: module.ts    From typeorm-extension with MIT License 6 votes vote down vote up
/**
 * Build DataSourceOptions from configuration.
 *
 * @deprecated
 * @param context
 */
export async function buildLegacyDataSourceOptions(
    context: DataSourceOptionsBuildContext,
) : Promise<DataSourceOptions> {
    const directory : string = context.directory || process.cwd();
    const tsconfigDirectory : string = context.tsconfigDirectory || process.cwd();

    const connectionOptionsReader = new ConnectionOptionsReader({
        root: directory,
        configName: context.configName,
    });

    const dataSourceOptions = await connectionOptionsReader.get(context.name || 'default');

    return extendDataSourceOptions(dataSourceOptions, tsconfigDirectory);
}
Example #14
Source File: singleton.ts    From typeorm-extension with MIT License 5 votes vote down vote up
optionsPromises: Record<string, Promise<DataSourceOptions>> = {}
Example #15
Source File: singleton.ts    From typeorm-extension with MIT License 5 votes vote down vote up
instancePromises : Record<string, Promise<DataSourceOptions>> = {}
Example #16
Source File: create.ts    From typeorm-extension with MIT License 5 votes vote down vote up
driversRequireDatabaseOption: DataSourceOptions['type'][] = [
    'sqlite',
    'better-sqlite3',
]
Example #17
Source File: singleton.ts    From typeorm-extension with MIT License 5 votes vote down vote up
instances : Record<string, DataSourceOptions> = {}
Example #18
Source File: singleton.ts    From typeorm-extension with MIT License 5 votes vote down vote up
export function setDataSourceOptions(
    options: DataSourceOptions,
    alias?: string,
) {
    alias = alias || 'default';
    instances[alias] = options;
}