Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add support for MongoDB #6102

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions .github/workflows/tests.yml
Expand Up @@ -108,11 +108,18 @@ jobs:
POSTGRES_DB: test_db
ports:
- 5432:5432
mongodb:
image: mongo
env:
MONGO_INITDB_ROOT_USERNAME: keystone5
MONGO_INITDB_ROOT_PASSWORD: k3yst0n3
ports:
- 27017:27017
strategy:
fail-fast: false
matrix:
index: [0, 1, 2, 3, 4, 5, 6, 7, 8]
adapter: ['postgresql', 'sqlite']
adapter: ['postgresql', 'sqlite', 'mongodb']
steps:
- name: Checkout Repo
uses: actions/checkout@v2
Expand Down Expand Up @@ -153,7 +160,7 @@ jobs:
CI_NODE_TOTAL: 9
CI_NODE_INDEX: ${{ matrix.index }}
TEST_ADAPTER: ${{ matrix.adapter }}
DATABASE_URL: ${{ matrix.adapter == 'sqlite' && 'file:./dev.db' || 'postgres://keystone5:k3yst0n3@localhost:5432/test_db' }}
DATABASE_URL: ${{ matrix.adapter == 'sqlite' && 'file:./dev.db' || matrix.adapter == 'postgresql' && 'postgres://keystone5:k3yst0n3@localhost:5432/test_db' || 'mongodb://keystone5:k3yst0n3@localhost:27017/test_db' }}

non-api-tests:
name: Package Unit Tests
Expand Down
4 changes: 2 additions & 2 deletions packages/keystone/src/lib/config/initConfig.ts
Expand Up @@ -8,9 +8,9 @@ import { applyIdFieldDefaults } from './applyIdFieldDefaults';
*/

export function initConfig(config: KeystoneConfig) {
if (!['postgresql', 'sqlite'].includes(config.db.provider)) {
if (!['postgresql', 'sqlite', 'mongodb'].includes(config.db.provider)) {
throw new Error(
'Invalid db configuration. Please specify db.provider as either "sqlite" or "postgresql"'
'Invalid db configuration. Please specify db.provider as either "sqlite", "postgresql", or "mongodb"'
);
}

Expand Down
17 changes: 15 additions & 2 deletions packages/keystone/src/lib/core/prisma-schema.ts
Expand Up @@ -207,14 +207,24 @@ export function printPrismaSchema(
datasource ${provider} {
url = env("DATABASE_URL")
provider = "${provider}"
}
}\n\n`;

generator client {
if (provider === 'mongodb') {
prismaSchema += `generator client {
provider = "prisma-client-js"
previewFeatures = ["mongoDb"]
output = "${clientDir}"${prismaFlags}
engineType = "binary"
}
\n`;
} else {
prismaSchema += `generator client {
provider = "prisma-client-js"
output = "${clientDir}"${prismaFlags}
engineType = "binary"
}
\n`;
}
for (const [listKey, { resolvedDbFields }] of Object.entries(lists)) {
prismaSchema += `model ${listKey} {`;
for (const [fieldPath, field] of Object.entries(resolvedDbFields)) {
Expand All @@ -224,6 +234,9 @@ generator client {
if (fieldPath === 'id') {
assertDbFieldIsValidForIdField(listKey, field);
prismaSchema += ' @id';
if (provider === 'mongodb') {
prismaSchema += ' @map("_id")';
}
}
}
prismaSchema += `\n}\n`;
Expand Down
2 changes: 1 addition & 1 deletion packages/keystone/src/types/config/index.ts
Expand Up @@ -57,7 +57,7 @@ export type DatabaseConfig = {
useMigrations?: boolean;
enableLogging?: boolean;
idField?: IdFieldConfig;
provider: 'postgresql' | 'sqlite';
provider: 'postgresql' | 'sqlite' | 'mongodb';
prismaPreviewFeatures?: string[]; // https://www.prisma.io/docs/concepts/components/preview-features
};

Expand Down
2 changes: 1 addition & 1 deletion packages/keystone/src/types/core.ts
Expand Up @@ -3,7 +3,7 @@ import type { GraphQLResolveInfo } from 'graphql';
import type { GqlNames } from './utils';
import type { KeystoneContext, SessionContext } from './context';

export type DatabaseProvider = 'sqlite' | 'postgresql';
export type DatabaseProvider = 'sqlite' | 'postgresql' | 'mongodb';

export type CreateRequestContext = (
req: IncomingMessage,
Expand Down
1 change: 1 addition & 0 deletions packages/keystone/src/types/filters/index.ts
@@ -1,5 +1,6 @@
export * as postgresql from './providers/postgresql';
export * as sqlite from './providers/sqlite';
export * as mongodb from './providers/mongodb';

type EntriesAssumingNoExtraProps<T> = {
[Key in keyof T]-?: [Key, T[Key]];
Expand Down