-
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.
- Loading branch information
Showing
65 changed files
with
6,423 additions
and
1,231 deletions.
There are no files selected for viewing
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
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,79 @@ | ||
import Debug from 'debug' | ||
import { DataSource } from 'typeorm' | ||
import { BaseDataSourceOptions } from 'typeorm/data-source/BaseDataSourceOptions' | ||
import { DataSourceOptions } from 'typeorm/data-source/DataSourceOptions' | ||
|
||
const debug = Debug(`demo:databaseService`) | ||
|
||
export class DataSources { | ||
private dataSources = new Map<string, DataSource>() | ||
private configs | ||
|
||
private static singleton: DataSources | ||
|
||
public static singleInstance() { | ||
if (!DataSources.singleton) { | ||
DataSources.singleton = new DataSources() | ||
} | ||
return DataSources.singleton | ||
} | ||
|
||
public static newInstance(configs?: Map<string, DataSourceOptions>) { | ||
return new DataSources(configs) | ||
} | ||
|
||
private constructor(configs?: Map<string, DataSourceOptions>) { | ||
this.configs = configs ?? new Map<string, BaseDataSourceOptions>() | ||
} | ||
|
||
addConfig(dbName: string, config: DataSourceOptions): this { | ||
this.configs.set(dbName, config) | ||
return this | ||
} | ||
|
||
deleteConfig(dbName: string): this { | ||
this.configs.delete(dbName) | ||
return this | ||
} | ||
|
||
getConfig(dbName: string): BaseDataSourceOptions { | ||
const config = this.configs.get(dbName) | ||
if (!config) { | ||
throw Error(`No DB config found for ${dbName}`) | ||
} | ||
return config | ||
} | ||
|
||
public getDbNames(): string[] { | ||
return [...this.configs.keys()] | ||
} | ||
|
||
async getDbConnection(dbName: string): Promise<DataSource> { | ||
const config = this.getConfig(dbName) | ||
/*if (config.synchronize) { | ||
return Promise.reject( | ||
`WARNING: Automatic migrations need to be disabled in this app! Adjust the database configuration and set synchronize to false` | ||
) | ||
}*/ | ||
|
||
let dataSource = this.dataSources.get(dbName) | ||
if (dataSource) { | ||
return dataSource | ||
} | ||
|
||
dataSource = await new DataSource({ ...(config as DataSourceOptions), name: dbName }).initialize() | ||
this.dataSources.set(dbName, dataSource) | ||
if (config.synchronize) { | ||
debug(`WARNING: Automatic migrations need to be disabled in this app! Adjust the database configuration and set synchronize to false`) | ||
} else if (config.migrationsRun) { | ||
debug( | ||
`Migrations are currently managed from config. Please set migrationsRun and synchronize to false to get consistent behaviour. We run migrations from code explicitly` | ||
) | ||
} else { | ||
debug(`Running ${dataSource.migrations.length} migration(s) from code if needed...`) | ||
await dataSource.runMigrations() | ||
debug(`${dataSource.migrations.length} migration(s) from code were inspected and applied`) | ||
} | ||
return dataSource | ||
} | ||
} |
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,6 +1,7 @@ | ||
/** | ||
* @public | ||
*/ | ||
export * from './dataSources' | ||
export * from './agentCreator' | ||
export * from './objectCreator' | ||
export * from './generic' |
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
95 changes: 95 additions & 0 deletions
95
packages/data-store/src/entities/statusList2021/StatusList2021Entity.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,95 @@ | ||
import { | ||
IIssuer, | ||
OriginalVerifiableCredential, | ||
StatusListCredentialIdMode, | ||
StatusListDriverType, | ||
StatusListIndexingDirection, | ||
StatusListType, | ||
StatusPurpose2021, | ||
} from '@sphereon/ssi-types' | ||
import { ProofFormat } from '@veramo/core' | ||
import { BaseEntity, Column, Entity, OneToMany, PrimaryColumn, Unique } from 'typeorm' | ||
import { StatusListEntryEntity } from './StatusList2021EntryEntity' | ||
|
||
@Entity('StatusList') | ||
@Unique('UQ_correlationId', ['correlationId']) | ||
export class StatusListEntity extends BaseEntity { | ||
@PrimaryColumn({ name: 'id', type: 'varchar' }) | ||
id!: string | ||
|
||
@Column({ name: 'correlationId', type: 'varchar', nullable: false }) | ||
correlationId!: string | ||
|
||
@Column({ name: 'length', nullable: false, unique: false }) | ||
length!: number | ||
|
||
@Column({ | ||
name: 'issuer', | ||
type: 'text', | ||
nullable: false, | ||
unique: false, | ||
transformer: { | ||
from(value: string): string | IIssuer { | ||
if (value?.trim()?.startsWith('{')) { | ||
return JSON.parse(value) | ||
} | ||
return value | ||
}, | ||
to(value: string | IIssuer): string { | ||
if (typeof value === 'string') { | ||
return value | ||
} | ||
return JSON.stringify(value) | ||
}, | ||
}, | ||
}) | ||
issuer!: string | IIssuer | ||
|
||
@Column('simple-enum', { name: 'type', enum: StatusListType, nullable: false, default: StatusListType.StatusList2021 }) | ||
type!: StatusListType | ||
|
||
@Column('simple-enum', { name: 'driverType', enum: StatusListDriverType, nullable: false, default: StatusListDriverType.AGENT_TYPEORM }) | ||
driverType!: StatusListDriverType | ||
|
||
@Column('simple-enum', { | ||
name: 'credentialIdMode', | ||
enum: StatusListCredentialIdMode, | ||
nullable: false, | ||
default: StatusListCredentialIdMode.ISSUANCE, | ||
}) | ||
credentialIdMode!: StatusListCredentialIdMode | ||
|
||
@Column({ type: 'varchar', name: 'proofFormat', enum: ['lds', 'jwt'], nullable: false, default: 'lds' }) | ||
proofFormat!: ProofFormat | ||
|
||
@Column({ type: 'varchar', name: 'indexingDirection', enum: ['rightToLeft'], nullable: false, default: 'rightToLeft' }) | ||
indexingDirection!: StatusListIndexingDirection | ||
|
||
@Column({ type: 'varchar', name: 'statusPurpose', nullable: false, default: 'revocation' }) | ||
statusPurpose!: StatusPurpose2021 | ||
|
||
@Column({ | ||
name: 'statusListCredential', | ||
type: 'text', | ||
nullable: true, | ||
unique: false, | ||
transformer: { | ||
from(value: string): OriginalVerifiableCredential { | ||
if (value?.startsWith('ey')) { | ||
return value | ||
} | ||
return JSON.parse(value) | ||
}, | ||
to(value: OriginalVerifiableCredential): string { | ||
if (typeof value === 'string') { | ||
return value | ||
} | ||
return JSON.stringify(value) | ||
}, | ||
}, | ||
}) | ||
statusListCredential?: OriginalVerifiableCredential | ||
|
||
@OneToMany((type) => StatusListEntryEntity, (entry) => entry.statusList) | ||
statusListEntries!: StatusListEntryEntity[] | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/data-store/src/entities/statusList2021/StatusList2021EntryEntity.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,29 @@ | ||
import { Validate } from 'class-validator' | ||
import { BaseEntity, Column, Entity, ManyToOne, PrimaryColumn } from 'typeorm' | ||
import { IsNonEmptyStringConstraint } from '../validators' | ||
import { StatusListEntity } from './StatusList2021Entity' | ||
|
||
@Entity('StatusListEntry') | ||
// @Unique('uq_credential_statuslist', ['statusList', 'credentialId']) // disabled because one prop can be null | ||
// @Unique('uq_credentialHash_statuslistId', ['statusList', 'credentialHash']) // disabled because one prop can be null | ||
export class StatusListEntryEntity extends BaseEntity { | ||
@PrimaryColumn({ name: 'statusListId', type: 'varchar' }) | ||
@ManyToOne(() => StatusListEntity, (statusList) => statusList.statusListEntries) | ||
statusList!: StatusListEntity | ||
|
||
@PrimaryColumn({ name: 'statusListIndex', nullable: false, unique: false }) | ||
@Validate(IsNonEmptyStringConstraint, { message: 'Status list index is required' }) | ||
statusListIndex!: number | ||
|
||
@Column({ name: 'credentialId', nullable: true }) | ||
credentialId?: string | ||
|
||
@Column({ name: 'credentialHash', length: 128, nullable: true, unique: false }) | ||
credentialHash?: string | ||
|
||
@Column({ name: 'correlationId', length: 255, nullable: true, unique: false }) | ||
correlationId?: string | ||
|
||
@Column({ name: 'value', length: 50, nullable: true, unique: false }) | ||
value?: string | ||
} |
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
File renamed without changes.
54 changes: 54 additions & 0 deletions
54
packages/data-store/src/migrations/generic/3-CreateStatusList.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,54 @@ | ||
import Debug from 'debug' | ||
import { MigrationInterface, QueryRunner } from 'typeorm' | ||
import { CreateStatusList1693866470001 } from '../postgres/CreateStatusList1693866470001-CreateStatusList' | ||
import { CreateStatusList1693866470002 } from '../sqlite/1693866470000-CreateStatusList' | ||
|
||
const debug = Debug('sphereon:ssi-sdk:migrations') | ||
|
||
export class CreateStatusList1693866470000 implements MigrationInterface { | ||
name = 'CreateStatusList1693866470000' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
debug('migration: creating issuance branding tables') | ||
const dbType = queryRunner.connection.driver.options.type | ||
if (dbType === 'postgres') { | ||
debug('using postgres migration file') | ||
const mig = new CreateStatusList1693866470001() | ||
const up = await mig.up(queryRunner) | ||
debug('Migration statements executed') | ||
return up | ||
} else if (dbType === 'sqlite' || 'react-native') { | ||
debug('using sqlite/react-native migration file') | ||
const mig = new CreateStatusList1693866470002() | ||
const up = await mig.up(queryRunner) | ||
debug('Migration statements executed') | ||
return up | ||
} else { | ||
return Promise.reject( | ||
"Migrations are currently only supported for sqlite, react-native and postgres. 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 issuance branding tables') | ||
const dbType = queryRunner.connection.driver.options.type | ||
if (dbType === 'postgres') { | ||
debug('using postgres migration file') | ||
const mig = new CreateStatusList1693866470002() | ||
const down = await mig.down(queryRunner) | ||
debug('Migration statements executed') | ||
return down | ||
} else if (dbType === 'sqlite' || 'react-native') { | ||
debug('using sqlite/react-native migration file') | ||
const mig = new CreateStatusList1693866470002() | ||
const down = await mig.down(queryRunner) | ||
debug('Migration statements executed') | ||
return down | ||
} else { | ||
return Promise.reject( | ||
"Migrations are currently only supported for sqlite, react-native and postgres. 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
Oops, something went wrong.