deno.land / x / remult@v0.16.3-exp.3 / postgres.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { Remult, SqlDatabase, EntityMetadata, SqlCommand, SqlImplementation, SqlResult } from "https://cdn.skypack.dev/remult@0.15.4?dts";import { verifyStructureOfAllEntities } from "https://cdn.skypack.dev/remult@0.15.4/postgres/schema-builder?dts";import { ClientOptions, ConnectionString, Pool } from "https://deno.land/x/postgres@v0.16.1/mod.ts";import { QueryObjectResult } from "https://deno.land/x/postgres@v0.16.1/query/query.ts";
class PostgresBridgeToSQLCommand implements SqlCommand { constructor(private source: { queryObject(queryText: string, values?: any[]): Promise<QueryObjectResult>; }) {
} values: any[] = []; addParameterAndReturnSqlToken(val: any): string { this.values.push(val); return '$' + this.values.length; } execute(sql: string): Promise<SqlResult> { return this.source.queryObject(sql, this.values).then(r => new PostgresBridgeToSQLQueryResult(r)); }}class PostgresBridgeToSQLQueryResult implements SqlResult { getColumnKeyInResultForIndexInSelect(index: number): string {
return this.r.columns![index]; }
constructor(public r: QueryObjectResult) { this.rows = r.rows; } rows: any[];
}export class PostgresDataProvider implements SqlImplementation { async entityIsUsedForTheFirstTime(entity: EntityMetadata): Promise<void> {} getLimitSqlSyntax(limit: number, offset: number) { return ' limit ' + limit + ' offset ' + offset; }
createCommand(): SqlCommand { return new PostgresBridgeToSQLCommand({ queryObject: async (queryText, values) => { const client = await this.pool.connect(); try { return await client.queryObject(queryText, values); } finally { await client.end(); client.release(); } } }); } constructor(private pool: Pool) { }
async transaction(action: (dataProvider: SqlImplementation) => Promise<void>) { let client = await this.pool.connect(); const transaction = client.createTransaction("trans");

try { await transaction.begin(); await action({ createCommand: () => new PostgresBridgeToSQLCommand(transaction), entityIsUsedForTheFirstTime: this.entityIsUsedForTheFirstTime, transaction: () => { throw "nested transactions not allowed" }, getLimitSqlSyntax: this.getLimitSqlSyntax }); await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } finally { await client.end(); client.release(); } }}
export async function createPostgresConnection(options?: { connectionString?: string, poolSize?: number, configuration?: ClientOptions | ConnectionString | undefined, autoCreateTables?: boolean}) { if (!options) options = {}; let config: ClientOptions | ConnectionString = {}; if (options.configuration) config = options.configuration;
if (!options.configuration && options.connectionString) { config = options.connectionString; } if (!options.poolSize) options.poolSize = 4; const db = new SqlDatabase(new PostgresDataProvider(new Pool(config, options.poolSize))); let remult = new Remult(); remult.setDataProvider(db); if (options.autoCreateTables === undefined || options.autoCreateTables) await verifyStructureOfAllEntities(db, remult); return db;}
remult
A CRUD framework for full stack TypeScript
GitHub Repositoryremult/remult
GitHub Stars
1621

Version Info

Tagged at
2 months ago