-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Created migrations and refactored the database layer
- Loading branch information
Showing
11 changed files
with
183 additions
and
11 deletions.
There are no files selected for viewing
14 changes: 3 additions & 11 deletions
14
...state-state-manager/src/entities/State.ts → ...ntities/xstatePersistence/XStateEntity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,21 @@ | ||
import {BaseEntity, Column, Entity, PrimaryColumn} from "typeorm"; | ||
|
||
@Entity('state') | ||
export class State extends BaseEntity { | ||
@PrimaryColumn() | ||
// @ts-ignore | ||
@Entity('XstateEntity') | ||
export class XStateEntity extends BaseEntity { | ||
@PrimaryColumn({ name: 'id', type: 'varchar' }) | ||
id: string | ||
@Column() | ||
// @ts-ignore | ||
state: string | ||
@Column() | ||
// @ts-ignore | ||
type: string | ||
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' }) | ||
// @ts-ignore | ||
createdAt: Date | ||
@Column({ type: 'timestamp', onUpdate: 'CURRENT_TIMESTAMP', nullable: true }) | ||
// @ts-ignore | ||
updatedAt: Date | ||
@Column({ type: 'timestamp', nullable: true }) | ||
// @ts-ignore | ||
completedAt: Date | ||
@Column() | ||
// @ts-ignore | ||
tenantId?: string | ||
@Column({ default: 0 }) | ||
// @ts-ignore | ||
ttl: number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/data-store/src/migrations/generic/6-CreateXStateStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { DatabaseType, MigrationInterface, QueryRunner } from 'typeorm' | ||
import Debug from 'debug' | ||
import { CreateXStateStore1708097018115 } from '../postgres/1708097018115-CreateXStateStore' | ||
import { CreateXStateStore1708096002272 } from '../sqlite/1708096002272-CreateXStateStore' | ||
|
||
const debug: Debug.Debugger = Debug('sphereon:ssi-sdk:migrations') | ||
|
||
export class CreateXStateStore1708098041262 implements MigrationInterface { | ||
name = 'CreateXStateStore1708098041262' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
debug('migration: creating contacts tables') | ||
const dbType: DatabaseType = queryRunner.connection.driver.options.type | ||
|
||
switch (dbType) { | ||
case 'postgres': { | ||
debug('using postgres migration file') | ||
const mig: CreateXStateStore1708097018115 = new CreateXStateStore1708097018115() | ||
await mig.up(queryRunner) | ||
debug('Migration statements executed') | ||
return | ||
} | ||
case 'sqlite': | ||
case 'expo': | ||
case 'react-native': { | ||
debug('using sqlite/react-native migration file') | ||
const mig: CreateXStateStore1708096002272 = new CreateXStateStore1708096002272() | ||
await mig.up(queryRunner) | ||
debug('Migration statements executed') | ||
return | ||
} | ||
default: | ||
return Promise.reject( | ||
`Migrations are currently only supported for sqlite, react-native, expo and postgres. Was ${dbType}. Please run your database without migrations and with 'migrationsRun: false' and 'synchronize: true' for now` | ||
) | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
debug('migration: reverting contacts tables') | ||
const dbType: DatabaseType = queryRunner.connection.driver.options.type | ||
|
||
switch (dbType) { | ||
case 'postgres': { | ||
debug('using postgres migration file') | ||
const mig: CreateXStateStore1708097018115 = new CreateXStateStore1708097018115() | ||
await mig.down(queryRunner) | ||
debug('Migration statements executed') | ||
return | ||
} | ||
case 'sqlite': | ||
case 'expo': | ||
case 'react-native': { | ||
debug('using sqlite/react-native migration file') | ||
const mig: CreateXStateStore1708096002272 = new CreateXStateStore1708096002272() | ||
await mig.down(queryRunner) | ||
debug('Migration statements executed') | ||
return | ||
} | ||
default: | ||
return Promise.reject( | ||
`Migrations are currently only supported for sqlite, react-native, expo and postgres. Was ${dbType}. Please run your database without migrations and with 'migrationsRun: false' and 'synchronize: true' for now` | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/data-store/src/migrations/postgres/1708097018115-CreateXStateStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {enablePostgresUuidExtension} from "@sphereon/ssi-sdk.core"; | ||
import {MigrationInterface, QueryRunner} from "typeorm"; | ||
|
||
export class CreateXStateStore1708097018115 implements MigrationInterface { | ||
name = 'CreateXStateStore1708097018115' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<any> { | ||
await enablePostgresUuidExtension(queryRunner) | ||
await queryRunner.query( | ||
`CREATE TABLE "XStateStore" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "state" varchar(255) NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "deletedAt" TIMESTAMP, CONSTRAINT PK_XStateStore_id PRIMARY KEY ("id")` | ||
) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "XStateStore" DROP CONSTRAINT "PK_XStateStore_id"`) | ||
await queryRunner.query(`DROP TABLE "XStateStore"`) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/data-store/src/migrations/sqlite/1708096002272-CreateXStateStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {MigrationInterface, QueryRunner} from "typeorm"; | ||
|
||
export class CreateXStateStore1708096002272 implements MigrationInterface { | ||
name = 'CreateXStateStore1708096002272' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<any> { | ||
await queryRunner.query( | ||
`CREATE TABLE "XStateStore" ("id" varchar PRIMARY KEY NOT NULL, "state" varchar(255) NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "deletedAt" TIMESTAMP, CONSTRAINT "PK_XStateStore_id"` | ||
) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "XStateStore" DROP CONSTRAINT "PK_XStateStore_id"`) | ||
await queryRunner.query(`DROP TABLE "XStateStore"`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {XStateEntity} from "../../entities/xstatePersistence/XStateEntity"; | ||
|
||
export type PersistStateArgs = { | ||
state: string | ||
type: string | ||
createdAt: Date | ||
updatedAt: Date | ||
completedAt: Date | ||
tenantId?: string | ||
ttl: number | ||
} | ||
|
||
export type LoadStateArgs = Pick<PersistStateArgs, 'type'> | ||
|
||
export type DeleteStateArgs = Pick<PersistStateArgs, 'type'> | ||
|
||
export type VoidResult = void | ||
|
||
export type LoadStateResult = XStateEntity | null |
7 changes: 7 additions & 0 deletions
7
packages/data-store/src/xstatePersistence/IAbstractStateStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {LoadStateArgs, LoadStateResult, PersistStateArgs, VoidResult} from "../types"; | ||
|
||
export abstract class IAbstractStateStore { | ||
abstract persistState(state: PersistStateArgs): Promise<VoidResult> | ||
abstract loadState(args: LoadStateArgs): Promise<LoadStateResult> | ||
abstract deleteState(args: LoadStateArgs): Promise<VoidResult> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {OrPromise} from "@sphereon/ssi-types"; | ||
import Debug from 'debug' | ||
import {DataSource} from "typeorm"; | ||
|
||
import {XStateEntity} from "../entities/xstatePersistence/XStateEntity"; | ||
import {DeleteStateArgs, LoadStateArgs, LoadStateResult, PersistStateArgs, VoidResult,} from "../types"; | ||
import {IAbstractStateStore} from "./IAbstractStateStore"; | ||
|
||
const debug = Debug('sphereon:ssi-sdk:xstatePersistence') | ||
|
||
export class XStateStore extends IAbstractStateStore { | ||
private readonly dbConnection: OrPromise<DataSource> | ||
|
||
constructor(dbConnection: OrPromise<DataSource>) { | ||
super() | ||
this.dbConnection = dbConnection | ||
} | ||
|
||
async persistState(state: PersistStateArgs): Promise<VoidResult> { | ||
const connection: DataSource = await this.dbConnection | ||
debug.log(`Executing persistState with state: ${JSON.stringify(state)}`) | ||
await connection.getRepository(XStateEntity).save(state) | ||
} | ||
|
||
async loadState(args: LoadStateArgs): Promise<LoadStateResult> { | ||
const connection: DataSource = await this.dbConnection | ||
debug.log(`Executing loadState query with type: ${args.type}`) | ||
return await connection.getRepository(XStateEntity) | ||
.findOne({ | ||
where: { type: args.type } | ||
}) | ||
} | ||
|
||
async deleteState(args: DeleteStateArgs): Promise<VoidResult> { | ||
const connection: DataSource = await this.dbConnection | ||
debug.log(`Executing loadState query with type: ${args.type}`) | ||
await connection.getRepository(XStateEntity) | ||
.delete({ type: args.type }) | ||
} | ||
} |