@zenith-open/zenithcms-db-postgres 0.1.0
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/dist/PostgresDrizzleAdapter.d.ts +97 -0
- package/dist/PostgresDrizzleAdapter.js +2028 -0
- package/dist/PostgresDrizzleAdapter.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/query-ast.d.ts +28 -0
- package/dist/query-ast.js +94 -0
- package/dist/query-ast.js.map +1 -0
- package/eslint.config.mjs +26 -0
- package/package.json +25 -0
- package/src/PostgresDrizzleAdapter.ts +2239 -0
- package/src/index.ts +2 -0
- package/src/query-ast.ts +117 -0
- package/tsconfig.eslint.json +8 -0
- package/tsconfig.json +11 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { CollectionConfig, DatabaseAdapter, FindOptions, BaseOptions, AuditLogData, VersionData, WebhookDeliveryData, WebhookDeliveryRecord } from '@zenith-open/zenithcms-types';
|
|
2
|
+
export interface CacheLayer {
|
|
3
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
4
|
+
set<T>(key: string, value: T, collection: string): Promise<void>;
|
|
5
|
+
invalidate(collection: string): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
export declare class LocalCacheLayer implements CacheLayer {
|
|
8
|
+
private cache;
|
|
9
|
+
constructor();
|
|
10
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
11
|
+
set<T>(key: string, value: T, collection: string): Promise<void>;
|
|
12
|
+
invalidate(collection: string): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
export declare class RedisCacheLayer implements CacheLayer {
|
|
15
|
+
private redis;
|
|
16
|
+
constructor(redisUrl: string);
|
|
17
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
18
|
+
set<T>(key: string, value: T, collection: string): Promise<void>;
|
|
19
|
+
invalidate(collection: string): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
|
22
|
+
/**
|
|
23
|
+
* PostgreSQL Database Adapter with Drizzle ORM
|
|
24
|
+
* ─────────────────────────────────────────────
|
|
25
|
+
* Phase B: "The Tightening"
|
|
26
|
+
* Features:
|
|
27
|
+
* - Dynamic Column Mapping (No more JSONB traps)
|
|
28
|
+
* - Auto-Migration Engine (safe DDL execution on boot)
|
|
29
|
+
* - Atomic Multi-Table Transactions.
|
|
30
|
+
* - Pre-compiled Zod validation caching.
|
|
31
|
+
*/
|
|
32
|
+
export declare class PostgresDrizzleAdapter implements DatabaseAdapter {
|
|
33
|
+
private connectionString;
|
|
34
|
+
name: string;
|
|
35
|
+
private pool;
|
|
36
|
+
db: NodePgDatabase;
|
|
37
|
+
private cache;
|
|
38
|
+
private tables;
|
|
39
|
+
private configs;
|
|
40
|
+
private tenantPools;
|
|
41
|
+
private systemTables;
|
|
42
|
+
constructor(connectionString: string);
|
|
43
|
+
/**
|
|
44
|
+
* Executes a database operation within a tenant-isolated RLS context.
|
|
45
|
+
* If siteId is provided, it begins a transaction, sets the local config parameter,
|
|
46
|
+
* and yields the transaction object.
|
|
47
|
+
*/
|
|
48
|
+
runWithTenantContext<T>(siteId: string | undefined, operation: (tx: any) => Promise<T>): Promise<T>;
|
|
49
|
+
registerTenant(tenantId: string, tenantConnectionString: string): Promise<void>;
|
|
50
|
+
private getDbClient;
|
|
51
|
+
connect(): Promise<void>;
|
|
52
|
+
disconnect(): Promise<void>;
|
|
53
|
+
getHealth(): 'ok' | 'connecting' | 'disconnected' | 'error';
|
|
54
|
+
private _ensureSystemTables;
|
|
55
|
+
private mapFieldToDrizzleColumn;
|
|
56
|
+
private mapFieldToSqlType;
|
|
57
|
+
registerCollection(config: CollectionConfig, db?: NodePgDatabase<any>): Promise<void>;
|
|
58
|
+
getExistingCollections(): Promise<string[]>;
|
|
59
|
+
private _runAutoMigrations;
|
|
60
|
+
private getTable;
|
|
61
|
+
private _getCacheKey;
|
|
62
|
+
private _invalidateCache;
|
|
63
|
+
private buildWhereClause;
|
|
64
|
+
/** Inject tenant scoping into the WHERE clause to prevent cross-tenant data access */
|
|
65
|
+
private tenantScope;
|
|
66
|
+
private mapAstToDrizzle;
|
|
67
|
+
find<T = unknown>(collection: string, query: Record<string, unknown>, options?: FindOptions): Promise<T[]>;
|
|
68
|
+
findOne<T = unknown>(collection: string, query: Record<string, unknown>, options?: FindOptions): Promise<T | null>;
|
|
69
|
+
/**
|
|
70
|
+
* Builds a Drizzle select query, optionally scoped to a subset of columns.
|
|
71
|
+
* When options.select is populated, only those columns are fetched (plus
|
|
72
|
+
* always-loaded metadata: id, createdAt, updatedAt, status).
|
|
73
|
+
*/
|
|
74
|
+
private _selectWithColumns;
|
|
75
|
+
/** Maximum depth for nested relation population to prevent query explosion */
|
|
76
|
+
private static readonly MAX_POPULATE_DEPTH;
|
|
77
|
+
private _populateRelations;
|
|
78
|
+
private _loadJunctionIds;
|
|
79
|
+
private _writeJunctionRelations;
|
|
80
|
+
create<T = unknown>(collection: string, data: Partial<T>, options?: BaseOptions): Promise<T>;
|
|
81
|
+
update<T = unknown>(collection: string, id: string, data: Partial<T>, options?: BaseOptions): Promise<T | null>;
|
|
82
|
+
findOneAndUpdate<T = unknown>(collection: string, query: Record<string, unknown>, update: Record<string, unknown>, options?: BaseOptions & {
|
|
83
|
+
returnDocument?: 'before' | 'after';
|
|
84
|
+
}): Promise<T | null>;
|
|
85
|
+
updateMany(collection: string, query: Record<string, unknown>, data: unknown, options?: BaseOptions): Promise<number>;
|
|
86
|
+
delete(collection: string, id: string, options?: BaseOptions): Promise<boolean>;
|
|
87
|
+
deleteMany(collection: string, query: Record<string, unknown>, options?: BaseOptions): Promise<number>;
|
|
88
|
+
count(collection: string, query: Record<string, unknown>, options?: BaseOptions): Promise<number>;
|
|
89
|
+
aggregate<T = unknown>(collection: string, pipeline: unknown[], options?: BaseOptions): Promise<T[]>;
|
|
90
|
+
transaction<T>(fn: (session: unknown) => Promise<T>): Promise<T>;
|
|
91
|
+
createAuditLog(data: AuditLogData, options?: BaseOptions): Promise<void>;
|
|
92
|
+
createVersion(data: VersionData, options?: BaseOptions): Promise<void>;
|
|
93
|
+
getVersions(collection: string, documentId: string, options?: BaseOptions): Promise<VersionData[]>;
|
|
94
|
+
createWebhookDelivery(data: WebhookDeliveryData, options?: BaseOptions): Promise<void>;
|
|
95
|
+
getWebhookDeliveries(webhookId: string, limit?: number): Promise<WebhookDeliveryRecord[]>;
|
|
96
|
+
search<T = unknown>(collection: string, query: string, fields: string[], limit?: number, options?: BaseOptions): Promise<T[]>;
|
|
97
|
+
}
|