Skip to content

Commit

Permalink
fix(common): fix datetime column error on postgres databases
Browse files Browse the repository at this point in the history
  • Loading branch information
Frantz Kati committed Dec 5, 2020
1 parent bf71625 commit 8420da4
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 51 deletions.
5 changes: 2 additions & 3 deletions packages/common/src/fields/Date.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Field from './Field'
import format from 'date-fns/format'

export class DateField extends Field {
/**
Expand All @@ -17,9 +16,9 @@ export class DateField extends Field {
*
* https://date-fns.org/v2.14.0/docs/format
*/
protected dateFormat: string = 'MM/dd/yyyy'
protected dateFormat: string = 'YYYY-MM-DD'

protected pickerFormat: string = 'MM/dd/yyyy'
protected pickerFormat: string = 'YYYY-MM-DD'

/**
*
Expand Down
6 changes: 6 additions & 0 deletions packages/common/src/fields/DateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ export class DateTime extends DateField {
this.property.columnTypes = ['datetime']
}

public afterConfigSet() {
if (this.tenseiConfig?.databaseConfig.type === 'postgresql') {
this.property.columnTypes = ['timestamp without time zone']
}
}

/**
*
* This is a short name for the frontend component that
Expand Down
6 changes: 6 additions & 0 deletions packages/common/src/fields/Double.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export class Double extends Integer {
this.property.type = 'double'
this.property.columnTypes = ['double']
}

public afterConfigSet() {
if (this.tenseiConfig?.databaseConfig.type === 'postgresql') {
this.property.columnTypes = ['double precision']
}
}
}

export const double = (name: string, databaseField?: string) =>
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/fields/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
FieldProperty,
SerializedField,
FieldHookFunction,
AuthorizeFunction,
AuthorizeFunction
} from '@tensei/common'

export class Field implements FieldContract {
Expand Down
17 changes: 12 additions & 5 deletions packages/common/typings/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ declare module '@tensei/common/config' {
import { EntityManager } from '@mikro-orm/core'
import { IMiddleware } from 'graphql-middleware'
import { sanitizer, validator } from 'indicative'
import { EventArgs, FlushEventArgs } from '@mikro-orm/core'
import { ResourceContract } from '@tensei/common/resources'
import { ExecutionParams } from 'subscriptions-transport-ws'
import { DashboardContract } from '@tensei/common/dashboards'
import { MikroORM, ConnectionOptions } from '@mikro-orm/core'
import { IResolvers, ITypedef, PubSub } from 'apollo-server-express'
import { DocumentNode, GraphQLSchema, GraphQLResolveInfo } from 'graphql'
import { PluginContract, PluginSetupConfig } from '@tensei/common/plugins'
import {
MikroORM,
ConnectionOptions,
EventArgs,
FlushEventArgs,
MikroORMOptions
} from '@mikro-orm/core'
import {
Storage,
StorageManager,
Expand Down Expand Up @@ -250,9 +255,11 @@ declare module '@tensei/common/config' {
type: InBuiltEndpoints
handler: ExpressMiddleware
}
type DatabaseConfiguration = ConnectionOptions & {
type: SupportedDatabases
}
type DatabaseConfiguration = Partial<
MikroORMOptions & {
type: SupportedDatabases
}
>
type AdditionalEntities = {
entities?: any[]
}
Expand Down
4 changes: 2 additions & 2 deletions packages/common/typings/tensei.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ declare module '@tensei/core' {
callPluginHook(hook: SetupFunctions): Promise<this>
dashboardPath(dashboardPath: string): this
registerDatabase(): Promise<this>
databaseConfig(databaseConfig: DatabaseConfiguration): this
db(databaseConfig: DatabaseConfiguration): this
apiPath(apiPath: string): this
serverUrl(url: string): this
clientUrl(url: string): this
Expand Down Expand Up @@ -99,7 +99,7 @@ declare module '@tensei/core' {
sessionSecret(secret: string): this
registerDatabase(): Promise<this>
apiPath(apiPath: string): this
databaseConfig(databaseConfig: DatabaseConfiguration): this
db(databaseConfig: DatabaseConfiguration): this
dashboards(dashboards: DashboardContract[]): this
registerMiddleware(): void
serverUrl(url: string): this
Expand Down
39 changes: 2 additions & 37 deletions packages/core/Tensei.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ export class Tensei implements TenseiContract {
return this
}

this.setConfigOnResourceFields()

this.forceMiddleware()

await this.callPluginHook('register')

this.setConfigOnResourceFields()

await this.registerDatabase()

this.registerAssetsRoutes()
Expand Down Expand Up @@ -340,41 +340,6 @@ export class Tensei implements TenseiContract {
return this
}

public getSessionPackage() {
if (['mongodb'].includes(this.ctx.databaseConfig.type)) {
return 'connect-mongo'
}

return 'connect-session-knex'
}

getSessionPackageConfig(Store: any) {
let storeArguments: any = {}

if (
['mysql', 'pg', 'sqlite3', 'sqlite'].includes(
this.ctx.databaseConfig.type
)
) {
storeArguments = {
knex: this.ctx.databaseClient
}
}

if (['mongodb'].includes(this.ctx.databaseConfig.type)) {
storeArguments = {
mongooseConnection: require('mongoose').connection
}
}

return {
secret: process.env.SESSION_SECRET || 'tensei-session-secret',
store: new Store(storeArguments),
resave: false,
saveUninitialized: false
}
}

public registerCoreRoutes() {}

public registerMiddleware() {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Database {
return [this.orm!, this.schemas]
}

this.orm = await MikroORM.init(this.getMikroORMOptions())
this.orm = await MikroORM.init(this.getMikroORMOptions() as any)

await new Migrator(this.orm!, this.schemas).init()

Expand All @@ -42,7 +42,7 @@ class Database {
return {
entities: [
...this.generateEntitySchemas(),
...(entities || []).map(_ => new EntitySchema(_))
...(entities || []).map((_: any) => new EntitySchema(_))
],
...rest
}
Expand Down
3 changes: 2 additions & 1 deletion packages/tests/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export const getDatabaseCredentials = () => {
const databaseType = (process.env.DATABASE_TYPE || 'mysql') as any

const config: DatabaseConfiguration = {
type: databaseType
type: databaseType,
forceUtcTimezone: true
}

if (databaseType === 'postgresql') {
Expand Down

0 comments on commit 8420da4

Please sign in to comment.