Skip to content

Commit

Permalink
[yaml/databases] Add mongo-atlas database type
Browse files Browse the repository at this point in the history
  • Loading branch information
costinsin committed Sep 18, 2024
1 parent 181067f commit d9be585
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/models/requests.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {
AuthenticationDatabaseType,
AuthenticationEmailTemplateType,
DatabaseType,
} from "../projectConfiguration/yaml/models.js";
import { ProjectDetailsEnvElement } from "../requests/models.js";

export interface CreateDatabaseRequest {
name: string;
region: string;
type?: string;
type: DatabaseType;
}

export interface CreateDatabaseResponse {
Expand Down
1 change: 1 addition & 0 deletions src/projectConfiguration/yaml/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export enum AuthenticationDatabaseType {

export enum DatabaseType {
neon = "postgres-neon",
mongo = "mongo-atlas",
}

export enum TriggerType {
Expand Down
27 changes: 21 additions & 6 deletions src/projectConfiguration/yaml/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { YAMLContext, parse as parseYaml, stringify as stringifyYaml } from "yam
import zod from "zod";
import nativeFs from "fs";
import { IFs } from "memfs";
import { databaseRegions, legacyRegions } from "../../utils/configs.js";
import { neonDatabaseRegions, legacyRegions, mongoDatabaseRegions } from "../../utils/configs.js";
import { GENEZIO_CONFIGURATION_FILE_NOT_FOUND, UserError, zodFormatError } from "../../errors.js";
import { AuthenticationDatabaseType, DatabaseType, FunctionType, Language } from "./models.js";
import {
Expand All @@ -28,6 +28,9 @@ type YamlScripts = NonNullable<YAMLBackend["scripts"]> | NonNullable<YamlFronten
export type YamlScript = YamlScripts[keyof YamlScripts];

export type YamlProjectConfiguration = ReturnType<typeof fillDefaultGenezioConfig>;
export type YamlDatabase = NonNullable<
NonNullable<YamlProjectConfiguration["services"]>["databases"]
>[number];

function parseGenezioConfig(config: unknown) {
const languageSchema = zod.object({
Expand Down Expand Up @@ -99,11 +102,23 @@ function parseGenezioConfig(config: unknown) {
type: zod.nativeEnum(FunctionType).default(FunctionType.aws),
});

const databaseSchema = zod.object({
name: zod.string(),
type: zod.nativeEnum(DatabaseType).optional().default(DatabaseType.neon),
region: zod.enum(databaseRegions.map((r) => r.value) as [string, ...string[]]).optional(),
});
const databaseSchema = zod
.object({
name: zod.string(),
type: zod.literal(DatabaseType.neon),
region: zod
.enum(neonDatabaseRegions.map((r) => r.value) as [string, ...string[]])
.optional(),
})
.or(
zod.object({
name: zod.string(),
type: zod.literal(DatabaseType.mongo),
region: zod
.enum(mongoDatabaseRegions.map((r) => r.value) as [string, ...string[]])
.optional(),
}),
);

const redirectUrlSchema = zod.string();

Expand Down
9 changes: 7 additions & 2 deletions src/requests/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ export async function createDatabase(
envId?: string,
linkToStage: boolean = false,
): Promise<CreateDatabaseResponse> {
const { name, region, type = "postgres-neon" } = request;
const { name, type } = request;
let { region } = request;

if (type === "postgres-neon") {
region = `aws-${region}`;
}

const data: string = JSON.stringify({
name: name,
region: "aws-" + region,
region: region,
type: type,
});

Expand Down
7 changes: 6 additions & 1 deletion src/utils/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ export const regions = [
{ name: "Europe (Frankfurt)", value: "eu-central-1" },
];

export const databaseRegions = [
export const neonDatabaseRegions = [
{ name: "US East (N. Virginia)", value: "us-east-1" },
{ name: "US East (Ohio)", value: "us-east-2" },
{ name: "US West (Oregon)", value: "us-west-2" },
{ name: "Asia Pacific (Singapore)", value: "ap-southeast-1" },
{ name: "Asia Pacific (Sydney)", value: "ap-southeast-2" },
{ name: "Europe (Frankfurt)", value: "eu-central-1" },
];

export const mongoDatabaseRegions = [
{ name: "US East (N. Virginia)", value: "us-east-1" },
{ name: "Europe (Frankfurt)", value: "eu-central-1" },
];

0 comments on commit d9be585

Please sign in to comment.