Skip to content

Commit

Permalink
fix(sequelize): no need to parse db url, sequelize accepts it
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Korzunin committed Aug 21, 2021
1 parent efe8b97 commit 7ed9a04
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 123 deletions.
13 changes: 6 additions & 7 deletions .plop/index.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ModelCtor, Sequelize } from 'sequelize';
import { tryLoadEnvs } from '@prisma/sdk';
import path from 'path';
import { findSync, parseDatabaseUrl } from './utils';
import { findSync } from './utils';

const dirname = findSync(process.cwd(), ['{{relativeOutputDir}}', '{{slsRelativeOutputDir}}'], ['d'], ['d'], 1)[0] || __dirname;

Expand All @@ -15,18 +15,17 @@ const loadedEnv = tryLoadEnvs({
rootEnvPath: config.relativeEnvPaths.rootEnvPath && path.resolve(dirname, config.relativeEnvPaths.rootEnvPath),
schemaEnvPath: config.relativeEnvPaths.schemaEnvPath && path.resolve(dirname, config.relativeEnvPaths.schemaEnvPath),
});
const env = loadedEnv ? loadedEnv.parsed : {};
const env = { ...(loadedEnv ? loadedEnv.parsed : {}), ...process.env };
const databaseUrl = config.datasource.url.fromEnvVar
? env[config.datasource.url.fromEnvVar]
: config.datasource.url.value;
const { driver, user, password, host, port, database } = parseDatabaseUrl(databaseUrl);

export const createInstance = async () => {
const sequelize = new Sequelize(database, user, password, {
host,
port,
const sequelize = new Sequelize(databaseUrl, {
ssl: true,
dialect: driver,
define: {
freezeTableName: true,
},
});

const models = {
Expand Down
5 changes: 0 additions & 5 deletions .plop/plopfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ module.exports = function (plop) {
path: 'utils/find.ts',
templateFile: path.join(__dirname, './utils/find.ts.hbs'),
},
{
type: 'add',
path: 'utils/parseDatabaseUrl.ts',
templateFile: path.join(__dirname, './utils/parseDatabaseUrl.ts.hbs'),
},
{
type: 'add',
path: 'utils/index.ts',
Expand Down
1 change: 0 additions & 1 deletion .plop/utils/index.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './find';
export * from './parseDatabaseUrl';
51 changes: 0 additions & 51 deletions .plop/utils/parseDatabaseUrl.ts.hbs

This file was deleted.

13 changes: 6 additions & 7 deletions prisma/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ModelCtor, Sequelize } from 'sequelize';
import { tryLoadEnvs } from '@prisma/sdk';
import path from 'path';
import { findSync, parseDatabaseUrl } from './utils';
import { findSync } from './utils';

const dirname = findSync(process.cwd(), ['prisma/models', 'models'], ['d'], ['d'], 1)[0] || __dirname;

Expand Down Expand Up @@ -42,18 +42,17 @@ const loadedEnv = tryLoadEnvs({
rootEnvPath: config.relativeEnvPaths.rootEnvPath && path.resolve(dirname, config.relativeEnvPaths.rootEnvPath),
schemaEnvPath: config.relativeEnvPaths.schemaEnvPath && path.resolve(dirname, config.relativeEnvPaths.schemaEnvPath),
});
const env = loadedEnv ? loadedEnv.parsed : {};
const env = { ...(loadedEnv ? loadedEnv.parsed : {}), ...process.env };
const databaseUrl = config.datasource.url.fromEnvVar
? env[config.datasource.url.fromEnvVar]
: config.datasource.url.value;
const { driver, user, password, host, port, database } = parseDatabaseUrl(databaseUrl);

export const createInstance = async () => {
const sequelize = new Sequelize(database, user, password, {
host,
port,
const sequelize = new Sequelize(databaseUrl, {
ssl: true,
dialect: driver,
define: {
freezeTableName: true,
},
});

const models = {
Expand Down
1 change: 0 additions & 1 deletion prisma/models/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './find';
export * from './parseDatabaseUrl';
51 changes: 0 additions & 51 deletions prisma/models/utils/parseDatabaseUrl.ts

This file was deleted.

0 comments on commit 7ed9a04

Please sign in to comment.