Skip to content

fix: Resolve issues with updates in mongo and @updatedAt fields #34

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MONGO_DATABASE_URL="mongodb://root:example@localhost:27017/example?authSource=admin"
POSTGRESQL_DATABASE_URL="postgresql://root:example@localhost:5438/example?schema=public"
52 changes: 45 additions & 7 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ jobs:
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: yarn install

test:
name: Test
test-postgres:
name: Tests with postgres
runs-on: ubuntu-latest
needs: setup
env:
NODE_ENV: test
DATABASE_URL: "postgresql://postgres:postgres@localhost:5432/prisma_example_test?schema=public"
POSTGRESQL_DATABASE_URL: "postgresql://postgres:postgres@localhost:5432/prisma_example_test?schema=public"
services:
postgres:
image: postgres:10.8
Expand Down Expand Up @@ -59,14 +59,52 @@ jobs:
run: yarn install
- name: Lint
run: yarn lint
- name: Migrate
run: npx prisma migrate dev
- name: Setup prisma for tests
run: yarn test:setup:postgresql
- name: Test
run: yarn test


test-mongo:
name: Tests with mongo
runs-on: ubuntu-latest
needs: setup
env:
NODE_ENV: test
MONGO_DATABASE_URL: "mongodb://root:example@localhost:27017/example?authSource=admin"
services:
mongo:
image: prismagraphql/mongo-single-replica:4.4.3-bionic
env:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
MONGO_INITDB_DATABASE: example
ports:
- 27017:27017
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup
uses: actions/setup-node@v2
with:
node-version: '14'
- uses: actions/cache@v1
id: yarn-cache
with:
path: node_modules
key: ${{ runner.os }}-node_modules-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node_modules-
- name: Install
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: yarn install
- name: Setup prisma for tests
run: yarn test:setup:mongo
- name: Test
run: yarn test

publish:
name: Publish
needs: test
needs: [test-mongo, test-postgres]
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,4 @@ types
build
lib
.adminjs
/spec/prisma/migrations
/spec/prisma
24 changes: 24 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: '3.8'

name: adminjs-prisma

services:
postgres:
image: postgres:latest
restart: always
environment:
POSTGRES_DB: example
POSTGRES_USER: root
POSTGRES_PASSWORD: example
ports:
- '5438:5432'

mongo: # This image automatically creates a replica set required for transactions
image: prismagraphql/mongo-single-replica:4.4.3-bionic
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
MONGO_INITDB_DATABASE: example
ports:
- '27017:27017'
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
"ts-node": "ts-node",
"lint": "eslint './src/**/*.{ts,js}' './spec/**/*.{ts,js}' './example-app/**/*.{ts,js}' --ignore-pattern 'build' --ignore-pattern 'yarn.lock'",
"check:all": "yarn lint && yarn test && yarn build",
"release": "semantic-release"
"release": "semantic-release",
"db:setup": "docker-compose up -d",
"db:migrate": "prisma migrate dev --name init",
"prisma:reset": "rm -rf spec/prisma",
"test:setup:mongo": "yarn prisma:reset && cp -r spec/.mongo.prisma spec/prisma && prisma generate",
"test:setup:postgresql": "yarn prisma:reset && cp -r spec/.postgres.prisma spec/prisma && yarn db:migrate && prisma generate"
},
"repository": {
"type": "git",
Expand Down
47 changes: 47 additions & 0 deletions spec/.mongo.prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
datasource db {
provider = "mongodb"
url = env("MONGO_DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
}

enum Status {
ACTIVE
REMOVED
}

model Post {
id String @id @default(uuid()) @map("_id")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String
content String?
someJson Json? @db.Json
status Status @default(ACTIVE)
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId String
}

model Profile {
id String @id @default(uuid()) @map("_id")
bio String?
user User @relation(fields: [userId], references: [id])
userId String @unique
}

model User {
id String @id @default(uuid()) @map("_id")
email String @unique
name String?
posts Post[]
profile Profile?
updatedAt DateTime @default(now()) @updatedAt
}

model UuidExample {
id String @id @default(uuid()) @map("_id")
label String
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
url = env("POSTGRESQL_DATABASE_URL")
}

generator client {
Expand Down Expand Up @@ -33,14 +33,15 @@ model Profile {
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
profile Profile?
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
profile Profile?
updatedAt DateTime @default(now()) @updatedAt
}

model UuidExample {
id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
label String
id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
label String
}
17 changes: 12 additions & 5 deletions spec/Resource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('Resource', () => {
let resource: Resource;
let prisma: PrismaClient;
let dmmf: DMMFClass;
let provider: string;

const data = {
name: 'Someone',
Expand All @@ -17,6 +18,7 @@ describe('Resource', () => {
beforeAll(async () => {
prisma = new PrismaClient();
dmmf = ((prisma as any)._baseDmmf as DMMFClass);
provider = ((prisma as any)._engineConfig.activeProvider as string);
});

beforeEach(async () => {
Expand All @@ -43,7 +45,7 @@ describe('Resource', () => {

describe('#databaseType', () => {
it('returns database dialect', () => {
expect(resource.databaseType()).toEqual('postgresql');
expect(resource.databaseType()).toEqual(provider);
});
});

Expand All @@ -55,13 +57,13 @@ describe('Resource', () => {

describe('#properties', () => {
it('returns all the properties', () => {
expect(resource.properties()).toHaveLength(3);
expect(resource.properties()).toHaveLength(4);
});

it('returns all properties with the correct position', () => {
expect(
resource.properties().map((property) => property.position()),
).toEqual([0, 1, 2]);
).toEqual([0, 1, 2, 3]);
});
});

Expand Down Expand Up @@ -95,14 +97,19 @@ describe('Resource', () => {
record = await resource.findOne(params.id);
const name = 'Michael';

jest.useFakeTimers().advanceTimersByTime(10000);
await resource.update((record && record.id()) as string, {
...record?.params,
name,
});
jest.useRealTimers()

const recordInDb = await resource.findOne(
(record && record.id()) as string,
);

expect(recordInDb && recordInDb.get('name')).toEqual(name);
expect(record?.get('updatedAt')).not.toEqual(recordInDb?.get('updatedAt'));
});
});

Expand All @@ -119,7 +126,7 @@ describe('Resource', () => {
const filter = new Filter(undefined, resource);
filter.filters = {
name: { path: 'name', value: params.name, property: resource.property('name') as BaseProperty },
}
};
record = await resource.find(filter);

expect(record[0] && record[0].get('name')).toEqual(data.name);
Expand All @@ -135,7 +142,7 @@ describe('Resource', () => {
const filter = new Filter(undefined, uuidResource);
filter.filters = {
id: { path: 'id', value: params.id, property: uuidResource.property('id') as BaseProperty },
}
};
record = await uuidResource.find(filter);

expect(record[0] && record[0].get('id')).toEqual(params.id);
Expand Down
2 changes: 1 addition & 1 deletion src/Property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Property extends BaseProperty {
}

public isEditable(): boolean {
return !this.isId() && this.column.name !== 'createdAt' && this.column.name !== 'updatedAt';
return !this.isId() && this.column.name !== 'createdAt' && !this.column.isUpdatedAt;
}

public isId(): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class Resource extends BaseResource {
const key = property.path();

// eslint-disable-next-line no-continue
if (param === undefined) continue;
if (param === undefined || !property.isEditable()) continue;

const type = property.type();
const foreignColumnName = property.foreignColumnName();
Expand Down