@credo-ts/drizzle-storage 0.6.0-pr-2324-20250708122805 → 0.6.0-pr-2324-20250708123651
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +15 -14
- package/src/DrizzleDatabase.ts +29 -0
- package/src/DrizzleRecord.ts +73 -0
- package/src/DrizzleStorageModule.ts +112 -0
- package/src/DrizzleStorageModuleConfig.ts +48 -0
- package/src/action-menu/action-menu/DrizzleDidcommActionMenuRecordAdapter.ts +46 -0
- package/src/action-menu/action-menu/index.ts +10 -0
- package/src/action-menu/action-menu/postgres.ts +31 -0
- package/src/action-menu/action-menu/sqlite.ts +34 -0
- package/src/action-menu/bundle.ts +18 -0
- package/src/action-menu/postgres.ts +1 -0
- package/src/action-menu/sqlite.ts +1 -0
- package/src/adapter/BaseDrizzleRecordAdapter.ts +319 -0
- package/src/adapter/__tests__/queryToDrizzlePostgres.test.ts +328 -0
- package/src/adapter/__tests__/queryToDrizzleSqlite.test.ts +324 -0
- package/src/adapter/index.ts +6 -0
- package/src/adapter/queryToDrizzlePostgres.ts +217 -0
- package/src/adapter/queryToDrizzleSqlite.ts +213 -0
- package/src/anoncreds/bundle.ts +34 -0
- package/src/anoncreds/credential/DrizzleAnonCredsCredentialRecordAdapter.ts +93 -0
- package/src/anoncreds/credential/index.ts +10 -0
- package/src/anoncreds/credential/postgres.ts +27 -0
- package/src/anoncreds/credential/sqlite.ts +27 -0
- package/src/anoncreds/credential-definition/DrizzleAnonCredsCredentialDefinitionRecordAdapter.ts +72 -0
- package/src/anoncreds/credential-definition/index.ts +10 -0
- package/src/anoncreds/credential-definition/postgres.ts +23 -0
- package/src/anoncreds/credential-definition/sqlite.ts +23 -0
- package/src/anoncreds/credential-definition-private/DrizzleAnonCredsCredentialDefinitionPrivateRecordAdapter.ts +48 -0
- package/src/anoncreds/credential-definition-private/index.ts +10 -0
- package/src/anoncreds/credential-definition-private/postgres.ts +14 -0
- package/src/anoncreds/credential-definition-private/sqlite.ts +14 -0
- package/src/anoncreds/key-correctness-proof/DrizzleAnonCredsKeyCorrectnessProofRecordAdapter.ts +46 -0
- package/src/anoncreds/key-correctness-proof/index.ts +10 -0
- package/src/anoncreds/key-correctness-proof/postgres.ts +13 -0
- package/src/anoncreds/key-correctness-proof/sqlite.ts +14 -0
- package/src/anoncreds/link-secret/DrizzleAnonCredsLinkSecretRecordAdapter.ts +45 -0
- package/src/anoncreds/link-secret/index.ts +10 -0
- package/src/anoncreds/link-secret/postgres.ts +18 -0
- package/src/anoncreds/link-secret/sqlite.ts +18 -0
- package/src/anoncreds/postgres.ts +8 -0
- package/src/anoncreds/revocation-registry-definition/DrizzleAnonCredsRevocationRegistryDefinitionRecordAdapter.ts +52 -0
- package/src/anoncreds/revocation-registry-definition/index.ts +10 -0
- package/src/anoncreds/revocation-registry-definition/postgres.ts +18 -0
- package/src/anoncreds/revocation-registry-definition/sqlite.ts +20 -0
- package/src/anoncreds/revocation-registry-definition-private/DrizzleAnonCredsRevocationRegistryDefinitionPrivateRecordAdapter.ts +53 -0
- package/src/anoncreds/revocation-registry-definition-private/index.ts +10 -0
- package/src/anoncreds/revocation-registry-definition-private/postgres.ts +22 -0
- package/src/anoncreds/revocation-registry-definition-private/sqlite.ts +18 -0
- package/src/anoncreds/schema/DrizzleAnonCredsSchemaRecordAdapter.ts +54 -0
- package/src/anoncreds/schema/__tests__/schema.test.ts +66 -0
- package/src/anoncreds/schema/index.ts +10 -0
- package/src/anoncreds/schema/postgres.ts +22 -0
- package/src/anoncreds/schema/sqlite.ts +22 -0
- package/src/anoncreds/sqlite.ts +8 -0
- package/src/applyReactNativeMigrations.ts +24 -0
- package/src/combineSchemas.ts +44 -0
- package/src/core/bundle.ts +33 -0
- package/src/core/context/index.ts +8 -0
- package/src/core/context/postgres.ts +5 -0
- package/src/core/context/sqlite.ts +5 -0
- package/src/core/did/DrizzleDidRecordAdapter.ts +71 -0
- package/src/core/did/index.ts +10 -0
- package/src/core/did/postgres.ts +28 -0
- package/src/core/did/sqlite.ts +26 -0
- package/src/core/genericRecord/DrizzleGenericRecordAdapter.ts +36 -0
- package/src/core/genericRecord/index.ts +10 -0
- package/src/core/genericRecord/postgres.ts +12 -0
- package/src/core/genericRecord/sqlite.ts +12 -0
- package/src/core/mdoc/DrizzleMdocRecordAdapter.ts +44 -0
- package/src/core/mdoc/index.ts +10 -0
- package/src/core/mdoc/postgres.ts +15 -0
- package/src/core/mdoc/sqlite.ts +15 -0
- package/src/core/postgres.ts +8 -0
- package/src/core/sdJwtVc/DrizzleSdJwtVcRecordAdapter.ts +46 -0
- package/src/core/sdJwtVc/index.ts +10 -0
- package/src/core/sdJwtVc/postgres.ts +17 -0
- package/src/core/sdJwtVc/sqlite.ts +17 -0
- package/src/core/singleContextLruCache/DrizzleSingleContextLruCacheRecordAdapter.ts +43 -0
- package/src/core/singleContextLruCache/index.ts +10 -0
- package/src/core/singleContextLruCache/postgres.ts +12 -0
- package/src/core/singleContextLruCache/sqlite.ts +13 -0
- package/src/core/sqlite.ts +8 -0
- package/src/core/storageVersion/DrizzleStorageVersionRecordAdapter.ts +36 -0
- package/src/core/storageVersion/index.ts +10 -0
- package/src/core/storageVersion/postgres.ts +13 -0
- package/src/core/storageVersion/sqlite.ts +13 -0
- package/src/core/w3cCredential/DrizzleW3cCredentialRecordAdapter.ts +95 -0
- package/src/core/w3cCredential/index.ts +10 -0
- package/src/core/w3cCredential/postgres.ts +31 -0
- package/src/core/w3cCredential/sqlite.ts +29 -0
- package/src/didcomm/basic-message/DrizzleDidcommBasicMessageRecordAdapter.ts +54 -0
- package/src/didcomm/basic-message/index.ts +10 -0
- package/src/didcomm/basic-message/postgres.ts +30 -0
- package/src/didcomm/basic-message/sqlite.ts +29 -0
- package/src/didcomm/bundle.ts +34 -0
- package/src/didcomm/connection/DrizzleDidcommConnectionRecordAdapter.ts +70 -0
- package/src/didcomm/connection/index.ts +10 -0
- package/src/didcomm/connection/postgres.ts +55 -0
- package/src/didcomm/connection/sqlite.ts +39 -0
- package/src/didcomm/credential-exchange/DrizzleDidcommCredentialExchangeRecordAdapter.ts +64 -0
- package/src/didcomm/credential-exchange/__tests__/credentialExchange.test.ts +97 -0
- package/src/didcomm/credential-exchange/index.ts +10 -0
- package/src/didcomm/credential-exchange/postgres.ts +45 -0
- package/src/didcomm/credential-exchange/sqlite.ts +39 -0
- package/src/didcomm/didcomm-message/DrizzleDidcommMessageRecordAdapter.ts +75 -0
- package/src/didcomm/didcomm-message/index.ts +10 -0
- package/src/didcomm/didcomm-message/postgres.ts +27 -0
- package/src/didcomm/didcomm-message/sqlite.ts +26 -0
- package/src/didcomm/mediation/DrizzleDidcommMediationRecordAdapter.ts +51 -0
- package/src/didcomm/mediation/index.ts +10 -0
- package/src/didcomm/mediation/postgres.ts +33 -0
- package/src/didcomm/mediation/sqlite.ts +31 -0
- package/src/didcomm/mediator-routing/DrizzleDidcommMediatorRoutingRecordAdapter.ts +49 -0
- package/src/didcomm/mediator-routing/index.ts +10 -0
- package/src/didcomm/mediator-routing/postgres.ts +14 -0
- package/src/didcomm/mediator-routing/sqlite.ts +17 -0
- package/src/didcomm/out-of-band/DrizzleDidcommOutOfBandRecordAdapter.ts +85 -0
- package/src/didcomm/out-of-band/__tests__/outOfBand.test.ts +84 -0
- package/src/didcomm/out-of-band/index.ts +10 -0
- package/src/didcomm/out-of-band/postgres.ts +46 -0
- package/src/didcomm/out-of-band/sqlite.ts +46 -0
- package/src/didcomm/postgres.ts +8 -0
- package/src/didcomm/proof-exchange/DrizzleDidcommProofExchangeRecordAdapter.ts +51 -0
- package/src/didcomm/proof-exchange/index.ts +10 -0
- package/src/didcomm/proof-exchange/postgres.ts +33 -0
- package/src/didcomm/proof-exchange/sqlite.ts +29 -0
- package/src/didcomm/sqlite.ts +8 -0
- package/src/drpc/bundle.ts +18 -0
- package/src/drpc/drpc/DrizzleDidcommDrpcRecordAdapter.ts +44 -0
- package/src/drpc/drpc/index.ts +10 -0
- package/src/drpc/drpc/postgres.ts +31 -0
- package/src/drpc/drpc/sqlite.ts +29 -0
- package/src/drpc/postgres.ts +1 -0
- package/src/drpc/sqlite.ts +1 -0
- package/src/error/CredoDrizzleColumnDoesNotExistError.ts +21 -0
- package/src/error/CredoDrizzleStorageError.ts +7 -0
- package/src/error/index.ts +2 -0
- package/src/index.ts +29 -0
- package/src/openid4vc/bundle.ts +26 -0
- package/src/openid4vc/openid4vc-issuance-session/DrizzleOpenId4VcIssuanceSessionRecordAdapter.ts +90 -0
- package/src/openid4vc/openid4vc-issuance-session/index.ts +10 -0
- package/src/openid4vc/openid4vc-issuance-session/postgres.ts +54 -0
- package/src/openid4vc/openid4vc-issuance-session/sqlite.ts +54 -0
- package/src/openid4vc/openid4vc-issuer/DrizzleOpenid4vcIssuerRecordAdapter.ts +50 -0
- package/src/openid4vc/openid4vc-issuer/index.ts +10 -0
- package/src/openid4vc/openid4vc-issuer/postgres.ts +31 -0
- package/src/openid4vc/openid4vc-issuer/sqlite.ts +40 -0
- package/src/openid4vc/openid4vc-verification-session/DrizzleOpenId4VcVerificationSessionRecordAdapter.ts +62 -0
- package/src/openid4vc/openid4vc-verification-session/index.ts +10 -0
- package/src/openid4vc/openid4vc-verification-session/postgres.ts +46 -0
- package/src/openid4vc/openid4vc-verification-session/sqlite.ts +40 -0
- package/src/openid4vc/openid4vc-verifier/DrizzleOpenid4vcVerifierRecordAdapter.ts +43 -0
- package/src/openid4vc/openid4vc-verifier/index.ts +10 -0
- package/src/openid4vc/openid4vc-verifier/postgres.ts +14 -0
- package/src/openid4vc/openid4vc-verifier/sqlite.ts +19 -0
- package/src/openid4vc/postgres.ts +4 -0
- package/src/openid4vc/sqlite.ts +4 -0
- package/src/postgres/baseRecord.ts +44 -0
- package/src/postgres/index.ts +1 -0
- package/src/question-answer/bundle.ts +18 -0
- package/src/question-answer/postgres.ts +1 -0
- package/src/question-answer/question-answer/DrizzleDidcommQuestionAnswerRecordAdapter.ts +52 -0
- package/src/question-answer/question-answer/index.ts +10 -0
- package/src/question-answer/question-answer/postgres.ts +34 -0
- package/src/question-answer/question-answer/sqlite.ts +32 -0
- package/src/question-answer/sqlite.ts +1 -0
- package/src/sqlite/baseRecord.ts +38 -0
- package/src/sqlite/index.ts +1 -0
- package/src/storage/DrizzleStorageService.ts +87 -0
- package/src/storage/index.ts +1 -0
- package/src/tenants/bundle.ts +19 -0
- package/src/tenants/postgres.ts +2 -0
- package/src/tenants/sqlite.ts +2 -0
- package/src/tenants/tenant/DrizzleTenantRecordAdapter.ts +45 -0
- package/src/tenants/tenant/index.ts +10 -0
- package/src/tenants/tenant/postgres.ts +16 -0
- package/src/tenants/tenant/sqlite.ts +16 -0
- package/src/tenants/tenant-routing/DrizzleTenantRoutingRecordAdapter.ts +44 -0
- package/src/tenants/tenant-routing/index.ts +10 -0
- package/src/tenants/tenant-routing/postgres.ts +20 -0
- package/src/tenants/tenant-routing/sqlite.ts +20 -0
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@credo-ts/drizzle-storage",
|
|
3
|
-
"version": "0.6.0-pr-2324-
|
|
3
|
+
"version": "0.6.0-pr-2324-20250708123651",
|
|
4
4
|
"files": [
|
|
5
5
|
"build",
|
|
6
6
|
"cli-build",
|
|
7
7
|
"migrations",
|
|
8
8
|
"tsconfig.json",
|
|
9
9
|
"types",
|
|
10
|
-
"cli/drizzle.config.ts"
|
|
10
|
+
"cli/drizzle.config.ts",
|
|
11
|
+
"src"
|
|
11
12
|
],
|
|
12
13
|
"license": "Apache-2.0",
|
|
13
14
|
"description": "Credo storage service implementation based on Drizzle ORM, supporting PostgreSQL and SQLite",
|
|
@@ -21,12 +22,12 @@
|
|
|
21
22
|
"default": "./build/index.js"
|
|
22
23
|
},
|
|
23
24
|
"./*/sqlite": {
|
|
24
|
-
"
|
|
25
|
-
"
|
|
25
|
+
"default": "./build/*/sqlite.js",
|
|
26
|
+
"types": "./build/*/sqlite.d.ts"
|
|
26
27
|
},
|
|
27
28
|
"./*/postgres": {
|
|
28
|
-
"
|
|
29
|
-
"
|
|
29
|
+
"default": "./build/*/postgres.js",
|
|
30
|
+
"types": "./build/*/postgres.d.ts"
|
|
30
31
|
},
|
|
31
32
|
"./*": {
|
|
32
33
|
"default": "./build/*/bundle.js",
|
|
@@ -51,14 +52,14 @@
|
|
|
51
52
|
"ts-node": "^10.9.2",
|
|
52
53
|
"tsyringe": "^4.8.0",
|
|
53
54
|
"zod": "^3.24.2",
|
|
54
|
-
"@credo-ts/action-menu": "0.6.0-pr-2324-
|
|
55
|
-
"@credo-ts/anoncreds": "0.6.0-pr-2324-
|
|
56
|
-
"@credo-ts/core": "0.6.0-pr-2324-
|
|
57
|
-
"@credo-ts/didcomm": "0.6.0-pr-2324-
|
|
58
|
-
"@credo-ts/drpc": "0.6.0-pr-2324-
|
|
59
|
-
"@credo-ts/openid4vc": "0.6.0-pr-2324-
|
|
60
|
-
"@credo-ts/question-answer": "0.6.0-pr-2324-
|
|
61
|
-
"@credo-ts/tenants": "0.6.0-pr-2324-
|
|
55
|
+
"@credo-ts/action-menu": "0.6.0-pr-2324-20250708123651",
|
|
56
|
+
"@credo-ts/anoncreds": "0.6.0-pr-2324-20250708123651",
|
|
57
|
+
"@credo-ts/core": "0.6.0-pr-2324-20250708123651",
|
|
58
|
+
"@credo-ts/didcomm": "0.6.0-pr-2324-20250708123651",
|
|
59
|
+
"@credo-ts/drpc": "0.6.0-pr-2324-20250708123651",
|
|
60
|
+
"@credo-ts/openid4vc": "0.6.0-pr-2324-20250708123651",
|
|
61
|
+
"@credo-ts/question-answer": "0.6.0-pr-2324-20250708123651",
|
|
62
|
+
"@credo-ts/tenants": "0.6.0-pr-2324-20250708123651"
|
|
62
63
|
},
|
|
63
64
|
"peerDependencies": {
|
|
64
65
|
"drizzle-orm": "^0.44.2"
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { PgDatabase } from 'drizzle-orm/pg-core'
|
|
2
|
+
import { BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core'
|
|
3
|
+
|
|
4
|
+
export type DrizzleSqliteDatabase<Schema extends Record<string, unknown> = Record<string, unknown>> =
|
|
5
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
6
|
+
BaseSQLiteDatabase<'sync' | 'async', any, Schema>
|
|
7
|
+
|
|
8
|
+
export type DrizzlePostgresDatabase<Schema extends Record<string, unknown> = Record<string, unknown>> = PgDatabase<
|
|
9
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
10
|
+
any,
|
|
11
|
+
Schema
|
|
12
|
+
>
|
|
13
|
+
|
|
14
|
+
export type DrizzleDatabase<
|
|
15
|
+
PostgresSchema extends Record<string, unknown> = Record<string, unknown>,
|
|
16
|
+
SqliteSchema extends Record<string, unknown> = Record<string, unknown>,
|
|
17
|
+
> = DrizzlePostgresDatabase<PostgresSchema> | DrizzleSqliteDatabase<SqliteSchema>
|
|
18
|
+
|
|
19
|
+
export function isDrizzlePostgresDatabase(database: DrizzleDatabase): database is DrizzlePostgresDatabase {
|
|
20
|
+
return database instanceof PgDatabase
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function isDrizzleSqliteDatabase(database: DrizzleDatabase): database is DrizzleSqliteDatabase {
|
|
24
|
+
return database instanceof BaseSQLiteDatabase
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function getDrizzleDatabaseType(database: DrizzleDatabase): 'sqlite' | 'postgres' {
|
|
28
|
+
return isDrizzlePostgresDatabase(database) ? 'postgres' : 'sqlite'
|
|
29
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { AnyDrizzleAdapter } from './adapter/BaseDrizzleRecordAdapter'
|
|
2
|
+
|
|
3
|
+
export interface DrizzleRecordBundle {
|
|
4
|
+
/**
|
|
5
|
+
* The name of the drizzle record bundle. Ideally it should be simple but unique.
|
|
6
|
+
*
|
|
7
|
+
* E.g. `core` or `anoncreds` for internal modules. For custom modules we recommend
|
|
8
|
+
* prefixing a scope. E.g. `my-org-custom-protocol`
|
|
9
|
+
*/
|
|
10
|
+
name: string
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The drizzle records part of this bundle. This should stay consistent, as migrations are
|
|
14
|
+
* managed per drizzle record bundle. Removing migrations or records can result in data loss
|
|
15
|
+
* or locked databases.
|
|
16
|
+
*/
|
|
17
|
+
records: DrizzleRecord[]
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Information related to generationg and applying migrations
|
|
21
|
+
*/
|
|
22
|
+
migrations: {
|
|
23
|
+
sqlite: {
|
|
24
|
+
/**
|
|
25
|
+
* A resolvable module that contains the drizzle SQLite schema definition for the complete bundle.
|
|
26
|
+
*
|
|
27
|
+
* NOTE: this MUST resolve to the TypeScript schema file, it cannot point to the built files. This
|
|
28
|
+
* file will only be resolved during generation of migrations, and thus it's not needed to publish
|
|
29
|
+
* the source files in your NPM package, as long as the module resolves during migration generation.
|
|
30
|
+
*/
|
|
31
|
+
schemaModule: string
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The migrations path where the migrations should be generated and retrieved. Note that the contents
|
|
35
|
+
* of this directory must be published with your NPM package so it can be used to apply migrations.
|
|
36
|
+
*
|
|
37
|
+
* The path should be relative to the resolved `schemaModule`. Note that if you use `publishConfig` to
|
|
38
|
+
* have different exports during development, this should still resolve to the generated migration files
|
|
39
|
+
* in the generated packages, so it's recommended to place your migrations outside of the src folder
|
|
40
|
+
*/
|
|
41
|
+
migrationsPath: string
|
|
42
|
+
}
|
|
43
|
+
postgres: {
|
|
44
|
+
/**
|
|
45
|
+
* A resolvable module that contains the drizzle SQLite schema definition for the complete bundle.
|
|
46
|
+
*
|
|
47
|
+
* NOTE: this MUST resolve to the TypeScript schema file, it cannot point to the built files. This
|
|
48
|
+
* file will only be resolved during generation of migrations, and thus it's not needed to publish
|
|
49
|
+
* the source files in your NPM package, as long as the module resolves during migration generation.
|
|
50
|
+
*/
|
|
51
|
+
schemaModule: string
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The migrations path where the migrations should be generated and retrieved. Note that the contents
|
|
55
|
+
* of this directory must be published with your NPM package so it can be used to apply migrations.
|
|
56
|
+
*
|
|
57
|
+
* The path should be relative to the resolved `schemaModule`. Note that if you use `publishConfig` to
|
|
58
|
+
* have different exports during development, this should still resolve to the generated migration files
|
|
59
|
+
* in the generated packages, so it's recommended to place your migrations outside of the src folder
|
|
60
|
+
*/
|
|
61
|
+
migrationsPath: string
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface DrizzleRecord {
|
|
67
|
+
postgres: Record<string, unknown>
|
|
68
|
+
sqlite: Record<string, unknown>
|
|
69
|
+
adapter?: new (
|
|
70
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
71
|
+
database: any
|
|
72
|
+
) => AnyDrizzleAdapter
|
|
73
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { AgentContext, DependencyManager, Module } from '@credo-ts/core'
|
|
2
|
+
import type { DrizzleStorageModuleConfigOptions } from './DrizzleStorageModuleConfig'
|
|
3
|
+
|
|
4
|
+
import { CredoError, InjectionSymbols, StorageUpdateService } from '@credo-ts/core'
|
|
5
|
+
|
|
6
|
+
import { eq } from 'drizzle-orm'
|
|
7
|
+
import { isDrizzlePostgresDatabase, isDrizzleSqliteDatabase } from './DrizzleDatabase'
|
|
8
|
+
import { DrizzleStorageModuleConfig } from './DrizzleStorageModuleConfig'
|
|
9
|
+
import { context as postgresContext } from './core/postgres'
|
|
10
|
+
import { context as sqliteContext } from './core/sqlite'
|
|
11
|
+
import { CredoDrizzleStorageError } from './error'
|
|
12
|
+
import { DrizzleStorageService } from './storage'
|
|
13
|
+
|
|
14
|
+
export class DrizzleStorageModule implements Module {
|
|
15
|
+
public readonly config: DrizzleStorageModuleConfig
|
|
16
|
+
|
|
17
|
+
public constructor(config: DrizzleStorageModuleConfigOptions) {
|
|
18
|
+
this.config = new DrizzleStorageModuleConfig(config)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public register(dependencyManager: DependencyManager) {
|
|
22
|
+
dependencyManager.registerInstance(DrizzleStorageModuleConfig, this.config)
|
|
23
|
+
|
|
24
|
+
if (dependencyManager.isRegistered(InjectionSymbols.StorageService)) {
|
|
25
|
+
throw new CredoError(
|
|
26
|
+
'Unable to register DrizzleStorageService. There is an instance of StorageService already registered'
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
dependencyManager.registerSingleton(InjectionSymbols.StorageService, DrizzleStorageService)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public async onInitializeContext(agentContext: AgentContext): Promise<void> {
|
|
33
|
+
// For the root agent context we don't call provision, we should probably change that
|
|
34
|
+
// but this is because agent.initialize doesn't differantiate between create and open
|
|
35
|
+
// So we need to check basically when we start the agent if this is a new agent
|
|
36
|
+
// and if so, set the framework storage version. This is a bit inefficient, as we will
|
|
37
|
+
// again fetch the record after the module has been initialized, but probably something
|
|
38
|
+
// we can improve with caching.
|
|
39
|
+
if (agentContext.isRootAgentContext) {
|
|
40
|
+
const storageUpdateService = agentContext.resolve(StorageUpdateService)
|
|
41
|
+
|
|
42
|
+
// Previously if we didn't have a storage version record yet it meant we assumed you
|
|
43
|
+
// were on 0.1 before the record was added, but since this module is introduced in 0.6
|
|
44
|
+
// we can be sure that's not the case.
|
|
45
|
+
if (!(await storageUpdateService.hasStorageVersionRecord(agentContext))) {
|
|
46
|
+
await this.createNewContextInDatabase(agentContext)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public async onProvisionContext(agentContext: AgentContext): Promise<void> {
|
|
52
|
+
// This method is never called for root agent context
|
|
53
|
+
// but to be sure if that changesin the future
|
|
54
|
+
if (agentContext.isRootAgentContext) return
|
|
55
|
+
|
|
56
|
+
await this.createNewContextInDatabase(agentContext)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public async onDeleteContext(agentContext: AgentContext): Promise<void> {
|
|
60
|
+
// If it's the root agent context, we want to delete all the tenants as well
|
|
61
|
+
if (agentContext.isRootAgentContext) {
|
|
62
|
+
// Do we want to allow this? It can be quite impactfull
|
|
63
|
+
await this.deleteContextQuery()
|
|
64
|
+
} else {
|
|
65
|
+
await this.deleteContextQuery().where(eq(sqliteContext.contextCorrelationId, agentContext.contextCorrelationId))
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private deleteContextQuery() {
|
|
70
|
+
// TODO: we could probably add some extra abstraction here.
|
|
71
|
+
if (isDrizzlePostgresDatabase(this.config.database)) {
|
|
72
|
+
return this.config.database.delete(postgresContext)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (isDrizzleSqliteDatabase(this.config.database)) {
|
|
76
|
+
return this.config.database.delete(sqliteContext)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
throw new CredoDrizzleStorageError(
|
|
80
|
+
'Unable to delete context. Database must be instance of postgres or sqlite database.'
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private async createNewContextInDatabase(agentContext: AgentContext) {
|
|
85
|
+
// For new contexts, we need to set the storage version
|
|
86
|
+
const storageUpdateService = agentContext.resolve(StorageUpdateService)
|
|
87
|
+
|
|
88
|
+
if (isDrizzlePostgresDatabase(this.config.database)) {
|
|
89
|
+
// Create a new context record. This is mostly used to allow deleting a context and
|
|
90
|
+
// cascade delete all other records.
|
|
91
|
+
await this.config.database
|
|
92
|
+
.insert(postgresContext)
|
|
93
|
+
.values({
|
|
94
|
+
contextCorrelationId: agentContext.contextCorrelationId,
|
|
95
|
+
})
|
|
96
|
+
.onConflictDoNothing()
|
|
97
|
+
} else if (isDrizzleSqliteDatabase(this.config.database)) {
|
|
98
|
+
await this.config.database
|
|
99
|
+
.insert(sqliteContext)
|
|
100
|
+
.values({
|
|
101
|
+
contextCorrelationId: agentContext.contextCorrelationId,
|
|
102
|
+
})
|
|
103
|
+
.onConflictDoNothing()
|
|
104
|
+
} else {
|
|
105
|
+
throw new CredoDrizzleStorageError(
|
|
106
|
+
'Unable to provision context. Database must be instance of postgres or sqlite database.'
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
await storageUpdateService.setCurrentStorageVersion(agentContext, StorageUpdateService.frameworkStorageVersion)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { DrizzleDatabase, getDrizzleDatabaseType } from './DrizzleDatabase'
|
|
2
|
+
import { DrizzleRecord, DrizzleRecordBundle } from './DrizzleRecord'
|
|
3
|
+
import { AnyDrizzleAdapter } from './adapter/BaseDrizzleRecordAdapter'
|
|
4
|
+
import { getSchemaFromDrizzleRecords } from './combineSchemas'
|
|
5
|
+
import coreDrizzleBundle from './core/bundle'
|
|
6
|
+
|
|
7
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
8
|
+
export type AnyDrizzleDatabase = DrizzleDatabase<any, any>
|
|
9
|
+
|
|
10
|
+
export interface DrizzleStorageModuleConfigOptions<Database extends AnyDrizzleDatabase = AnyDrizzleDatabase> {
|
|
11
|
+
/**
|
|
12
|
+
* The drizzle database to use. To not depend on specific drivers and support different
|
|
13
|
+
* environments you need to configure the database yourself.
|
|
14
|
+
*
|
|
15
|
+
* See https://orm.drizzle.team/docs/get-started for available drivers.
|
|
16
|
+
* All SQLite and Postgres database are supported.
|
|
17
|
+
*/
|
|
18
|
+
database: Database
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The drizzle bundles to register. Each drizzle bundles consists of records, which need to contain both an
|
|
22
|
+
* sqlite and postgres definition, as well as an adapter.
|
|
23
|
+
*/
|
|
24
|
+
bundles: DrizzleRecordBundle[]
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export class DrizzleStorageModuleConfig {
|
|
31
|
+
public readonly database: AnyDrizzleDatabase
|
|
32
|
+
public readonly adapters: AnyDrizzleAdapter[]
|
|
33
|
+
public readonly schemas: Record<string, unknown>
|
|
34
|
+
|
|
35
|
+
public constructor(options: DrizzleStorageModuleConfigOptions) {
|
|
36
|
+
this.database = options.database
|
|
37
|
+
|
|
38
|
+
// core MUST always be registered
|
|
39
|
+
const allRecords: DrizzleRecord[] = Array.from(
|
|
40
|
+
new Set([...coreDrizzleBundle.records, ...options.bundles.flatMap((bundle) => bundle.records)])
|
|
41
|
+
)
|
|
42
|
+
this.adapters = allRecords
|
|
43
|
+
.map((record) => (record.adapter ? new record.adapter(this.database) : undefined))
|
|
44
|
+
.filter((adapter) => adapter !== undefined)
|
|
45
|
+
|
|
46
|
+
this.schemas = getSchemaFromDrizzleRecords(allRecords, getDrizzleDatabaseType(options.database))
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { JsonTransformer, TagsBase } from '@credo-ts/core'
|
|
2
|
+
|
|
3
|
+
import { BaseDrizzleRecordAdapter, DrizzleAdapterRecordValues } from '../../adapter/BaseDrizzleRecordAdapter'
|
|
4
|
+
|
|
5
|
+
import { ActionMenuOptions, ActionMenuRecord, ActionMenuSelectionOptions } from '@credo-ts/action-menu'
|
|
6
|
+
import { DrizzleDatabase } from '../../DrizzleDatabase'
|
|
7
|
+
import * as postgres from './postgres'
|
|
8
|
+
import * as sqlite from './sqlite'
|
|
9
|
+
|
|
10
|
+
type DrizzleDidcommActionMenuAdapterValues = DrizzleAdapterRecordValues<(typeof sqlite)['didcommActionMenu']>
|
|
11
|
+
export class DrizzleDidcommActionMenuRecordAdapter extends BaseDrizzleRecordAdapter<
|
|
12
|
+
ActionMenuRecord,
|
|
13
|
+
typeof postgres.didcommActionMenu,
|
|
14
|
+
typeof postgres,
|
|
15
|
+
typeof sqlite.didcommActionMenu,
|
|
16
|
+
typeof sqlite
|
|
17
|
+
> {
|
|
18
|
+
public constructor(database: DrizzleDatabase<typeof postgres, typeof sqlite>) {
|
|
19
|
+
super(database, { postgres: postgres.didcommActionMenu, sqlite: sqlite.didcommActionMenu }, 'ActionMenuRecord')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public getValues(record: ActionMenuRecord) {
|
|
23
|
+
const { role, connectionId, threadId, ...customTags } = record.getTags()
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
role,
|
|
27
|
+
threadId,
|
|
28
|
+
connectionId,
|
|
29
|
+
state: record.state,
|
|
30
|
+
menu: record.menu ? (JsonTransformer.toJSON(record.menu) as ActionMenuOptions) : null,
|
|
31
|
+
performedAction: record.performedAction
|
|
32
|
+
? (JsonTransformer.toJSON(record.performedAction) as ActionMenuSelectionOptions)
|
|
33
|
+
: null,
|
|
34
|
+
customTags,
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public toRecord(values: DrizzleDidcommActionMenuAdapterValues): ActionMenuRecord {
|
|
39
|
+
const { customTags, ...remainingValues } = values
|
|
40
|
+
|
|
41
|
+
const record = JsonTransformer.fromJSON(remainingValues, ActionMenuRecord)
|
|
42
|
+
if (customTags) record.setTags(customTags as TagsBase)
|
|
43
|
+
|
|
44
|
+
return record
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { DrizzleRecord } from '../../DrizzleRecord'
|
|
2
|
+
import { DrizzleDidcommActionMenuRecordAdapter } from './DrizzleDidcommActionMenuRecordAdapter'
|
|
3
|
+
import * as postgres from './postgres'
|
|
4
|
+
import * as sqlite from './sqlite'
|
|
5
|
+
|
|
6
|
+
export const didcommActionMenuDrizzleRecord = {
|
|
7
|
+
adapter: DrizzleDidcommActionMenuRecordAdapter,
|
|
8
|
+
postgres,
|
|
9
|
+
sqlite,
|
|
10
|
+
} satisfies DrizzleRecord
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ActionMenuOptions, ActionMenuRole, ActionMenuSelectionOptions, ActionMenuState } from '@credo-ts/action-menu'
|
|
2
|
+
import { foreignKey, jsonb, pgEnum, pgTable, text, unique } from 'drizzle-orm/pg-core'
|
|
3
|
+
import { didcommConnection } from '../../didcomm/connection/postgres'
|
|
4
|
+
import { getPostgresBaseRecordTable, postgresBaseRecordIndexes } from '../../postgres/baseRecord'
|
|
5
|
+
|
|
6
|
+
export const didcommActionMenuStateEnum = pgEnum('DidcommActionMenuState', ActionMenuState)
|
|
7
|
+
export const didcommActionMenuRoleEnum = pgEnum('DidcommActionMenuRole', ActionMenuRole)
|
|
8
|
+
|
|
9
|
+
export const didcommActionMenu = pgTable(
|
|
10
|
+
'DidcommActionMenu',
|
|
11
|
+
{
|
|
12
|
+
...getPostgresBaseRecordTable(),
|
|
13
|
+
|
|
14
|
+
state: didcommActionMenuStateEnum().notNull(),
|
|
15
|
+
role: didcommActionMenuRoleEnum().notNull(),
|
|
16
|
+
|
|
17
|
+
connectionId: text('connection_id').notNull(),
|
|
18
|
+
threadId: text('thread_id').notNull(),
|
|
19
|
+
|
|
20
|
+
menu: jsonb('menu').$type<ActionMenuOptions>(),
|
|
21
|
+
performedAction: jsonb('performed_action').$type<ActionMenuSelectionOptions>(),
|
|
22
|
+
},
|
|
23
|
+
(table) => [
|
|
24
|
+
...postgresBaseRecordIndexes(table, 'didcommActionMenu'),
|
|
25
|
+
unique().on(table.contextCorrelationId, table.threadId),
|
|
26
|
+
foreignKey({
|
|
27
|
+
columns: [table.connectionId, table.contextCorrelationId],
|
|
28
|
+
foreignColumns: [didcommConnection.id, didcommConnection.contextCorrelationId],
|
|
29
|
+
}).onDelete('cascade'),
|
|
30
|
+
]
|
|
31
|
+
)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { foreignKey, sqliteTable, text, unique } from 'drizzle-orm/sqlite-core'
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
ActionMenuOptions,
|
|
5
|
+
ActionMenuRole,
|
|
6
|
+
ActionMenuSelectionOptions,
|
|
7
|
+
ActionMenuState,
|
|
8
|
+
} from '@credo-ts/action-menu'
|
|
9
|
+
import { didcommConnection } from '../../didcomm/connection/sqlite'
|
|
10
|
+
import { getSqliteBaseRecordTable, sqliteBaseRecordIndexes } from '../../sqlite/baseRecord'
|
|
11
|
+
|
|
12
|
+
export const didcommActionMenu = sqliteTable(
|
|
13
|
+
'DidcommActionMenu',
|
|
14
|
+
{
|
|
15
|
+
...getSqliteBaseRecordTable(),
|
|
16
|
+
|
|
17
|
+
state: text('state').$type<ActionMenuState>().notNull(),
|
|
18
|
+
role: text('role').$type<ActionMenuRole>().notNull(),
|
|
19
|
+
|
|
20
|
+
connectionId: text('connection_id').notNull(),
|
|
21
|
+
threadId: text('thread_id').notNull(),
|
|
22
|
+
|
|
23
|
+
menu: text('menu', { mode: 'json' }).$type<ActionMenuOptions>(),
|
|
24
|
+
performedAction: text('performed_action', { mode: 'json' }).$type<ActionMenuSelectionOptions>(),
|
|
25
|
+
},
|
|
26
|
+
(table) => [
|
|
27
|
+
...sqliteBaseRecordIndexes(table, 'didcommActionMenu'),
|
|
28
|
+
unique().on(table.contextCorrelationId, table.threadId),
|
|
29
|
+
foreignKey({
|
|
30
|
+
columns: [table.connectionId, table.contextCorrelationId],
|
|
31
|
+
foreignColumns: [didcommConnection.id, didcommConnection.contextCorrelationId],
|
|
32
|
+
}).onDelete('cascade'),
|
|
33
|
+
]
|
|
34
|
+
)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DrizzleRecordBundle } from '../DrizzleRecord'
|
|
2
|
+
import { didcommActionMenuDrizzleRecord } from './action-menu'
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
name: 'action-menu',
|
|
6
|
+
records: [didcommActionMenuDrizzleRecord],
|
|
7
|
+
|
|
8
|
+
migrations: {
|
|
9
|
+
postgres: {
|
|
10
|
+
schemaModule: '@credo-ts/drizzle-storage/action-menu/postgres',
|
|
11
|
+
migrationsPath: '../../migrations/action-menu/postgres',
|
|
12
|
+
},
|
|
13
|
+
sqlite: {
|
|
14
|
+
schemaModule: '@credo-ts/drizzle-storage/action-menu/sqlite',
|
|
15
|
+
migrationsPath: '../../migrations/action-menu/sqlite',
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
} as const satisfies DrizzleRecordBundle
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './action-menu/postgres'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './action-menu/sqlite'
|