UNPKG

@aequum/typeorm

Version:

aequum TypeORM tools for repository, pagination, CRUD/CRUDL, configs and utils

59 lines (58 loc) 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SchemedDataSources = void 0; exports.URIToDataSourceOptions = URIToDataSourceOptions; /** * SchemedDataSources are the data sources that support schemas * and will be parsed as `database/schema` in `URIToDataSourceOptions` * if there are a custom DataSource driver and schema need to be parsed * in the URI, add the DataSource name to this array. * * @see {@link URIToDataSourceOptions} */ exports.SchemedDataSources = ['postgres', 'mssql', 'cockroachdb']; /** * Translate a database URI to TypeORM DataSourceOptions, protocol must be * the TypeORM DataSource type (e.g. 'mysql', 'postgres', 'sqlite', etc.), * the host, port, username, password, database and schema (If applies) * will be extracted from the URI and all the query string parameters will * be added to the options. * * @see [TypeORM Docs: DataSource API](https://orkhan.gitbook.io/typeorm/docs/data-source-api) * @see [TypeORM Docs: Data source options](https://orkhan.gitbook.io/typeorm/docs/data-source-options) * @see {@link SchemedDataSources} * @param uri DataSource URI * @returns TypeORM DataSourceOptions */ function URIToDataSourceOptions(uri) { let parsedURI; try { parsedURI = new URL(uri); } catch (err) { // @ts-ignore if (err instanceof TypeError && err.code === 'ERR_INVALID_URL') { err.message = `Invalid database URI '${uri}'`; // @ts-ignore err.code = 'ERR_INVALID_DATABASE_URI'; } throw err; } const dataSourceType = parsedURI.protocol.substring(0, parsedURI.protocol.length - 1); const [database, schema] = ((exports.SchemedDataSources.includes(dataSourceType)) ? parsedURI.pathname.split('/').slice(1) : [parsedURI.pathname.slice(1), undefined]); const additionalConfig = {}; if (parsedURI.search) Object.assign(additionalConfig, Object.fromEntries(new URLSearchParams(parsedURI.search))); return { type: dataSourceType, host: parsedURI.hostname, username: parsedURI.username, password: parsedURI.password, database, schema, port: (+parsedURI.port) || undefined, ...additionalConfig }; }