@deterministic-code/datasources-postgres 0.1.1 → 0.1.2
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/PostgresAlterTableStatementRenderer.d.ts +6 -0
- package/dist/PostgresAlterTableStatementRenderer.js +20 -0
- package/dist/PostgresBulkUpdateStatementTextRenderer.d.ts +1 -1
- package/dist/PostgresBulkUpdateStatementTextRenderer.js +24 -0
- package/dist/PostgresCreateRoutineStatementRenderer.d.ts +4 -0
- package/dist/PostgresCreateTableStatementRenderer.d.ts +1 -1
- package/dist/PostgresCreateTableStatementRenderer.js +26 -0
- package/dist/PostgresCreateTriggerStatementRenderer.d.ts +4 -0
- package/dist/PostgresCreateTriggerStatementRenderer.js +13 -0
- package/dist/PostgresDriver.d.ts +5 -1
- package/dist/PostgresDriver.js +75 -0
- package/dist/PostgresDropRoutineStatementRenderer.d.ts +4 -0
- package/dist/PostgresDropRoutineStatementRenderer.js +6 -0
- package/dist/PostgresDropTriggerStatementRenderer.d.ts +4 -0
- package/dist/PostgresDropTriggerStatementRenderer.js +6 -0
- package/dist/PostgresIdentityInsertStatementRenderer.d.ts +4 -0
- package/dist/PostgresIdentityInsertStatementRenderer.js +11 -0
- package/dist/PostgresStatementRenderer.d.ts +13 -1
- package/dist/PostgresStatementRenderer.js +64 -0
- package/dist/PostgresStoredProcedureStatementRenderer.d.ts +10 -0
- package/dist/PostgresStoredProcedureStatementRenderer.js +111 -0
- package/dist/bind.d.ts +1 -1
- package/dist/bind.js +35 -0
- package/dist/columnTypes.js +10 -0
- package/dist/command.d.ts +1 -1
- package/dist/command.js +92 -0
- package/dist/connection-options.js +1 -0
- package/dist/connection.d.ts +1 -1
- package/dist/connection.js +72 -0
- package/dist/converters/BinaryConverter.d.ts +1 -1
- package/dist/converters/BinaryConverter.js +14 -0
- package/dist/converters/BoolConverter.d.ts +1 -1
- package/dist/converters/BoolConverter.js +5 -0
- package/dist/converters/DateTimeConverter.d.ts +1 -1
- package/dist/converters/DateTimeConverter.js +6 -0
- package/dist/converters/DecimalConverter.d.ts +1 -1
- package/dist/converters/DecimalConverter.js +5 -0
- package/dist/converters/PostgresRowTypeConverters.d.ts +1 -1
- package/dist/converters/PostgresRowTypeConverters.js +19 -0
- package/dist/converters/UuidConverter.d.ts +1 -1
- package/dist/converters/UuidConverter.js +5 -0
- package/dist/i-postgres-driver.d.ts +1 -1
- package/dist/i-postgres-driver.js +22 -0
- package/dist/index.js +1 -0
- package/dist/scalar.js +1 -0
- package/dist/transaction.d.ts +1 -1
- package/dist/transaction.js +15 -0
- package/package.json +9 -10
- package/src/PostgresBulkUpdateStatementTextRenderer.ts +0 -27
- package/src/PostgresCreateTableStatementRenderer.ts +0 -33
- package/src/PostgresDriver.ts +0 -55
- package/src/PostgresStatementRenderer.test.ts +0 -105
- package/src/PostgresStatementRenderer.ts +0 -34
- package/src/bind.ts +0 -50
- package/src/columnTypes.ts +0 -10
- package/src/command.ts +0 -129
- package/src/connection-options.ts +0 -7
- package/src/connection.ts +0 -95
- package/src/converters/BinaryConverter.test.ts +0 -14
- package/src/converters/BinaryConverter.ts +0 -20
- package/src/converters/BoolConverter.test.ts +0 -14
- package/src/converters/BoolConverter.ts +0 -6
- package/src/converters/DateTimeConverter.test.ts +0 -13
- package/src/converters/DateTimeConverter.ts +0 -11
- package/src/converters/DecimalConverter.test.ts +0 -11
- package/src/converters/DecimalConverter.ts +0 -7
- package/src/converters/PostgresRowTypeConverters.test.ts +0 -18
- package/src/converters/PostgresRowTypeConverters.ts +0 -24
- package/src/converters/UuidConverter.test.ts +0 -11
- package/src/converters/UuidConverter.ts +0 -6
- package/src/i-postgres-driver.ts +0 -61
- package/src/index.ts +0 -1
- package/src/postgres.test.ts +0 -345
- package/src/scalar.ts +0 -3
- package/src/transaction.ts +0 -21
package/src/postgres.test.ts
DELETED
|
@@ -1,345 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
|
|
3
|
-
vi.mock("pg", () => {
|
|
4
|
-
class Client {
|
|
5
|
-
async connect(): Promise<void> {}
|
|
6
|
-
async query() {
|
|
7
|
-
return { rowCount: 0, rows: [] };
|
|
8
|
-
}
|
|
9
|
-
async end(): Promise<void> {}
|
|
10
|
-
}
|
|
11
|
-
return { Client, default: { Client } };
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
import { Datasource, DatasourceFactory, DatasourceError, IsolationLevel, CRUDRepository, ReadonlyRepository, untyped, Native, ParameterDirection, columnType, RowTypeConverter, CreateTableStatement, ExecuteStoredProcedureStatement, ReturnStatement, SelectStatement, type IDriver } from "@datasources/core";
|
|
15
|
-
import { bindParameters, toPostgres } from "./bind.ts";
|
|
16
|
-
import { PostgresCommand } from "./command.ts";
|
|
17
|
-
import { PostgresConnection } from "./connection.ts";
|
|
18
|
-
import { PostgresRowTypeConverters } from "./converters/PostgresRowTypeConverters.ts";
|
|
19
|
-
import { pgClient, type IPgClient, type PgModule, type PgQueryResult } from "./i-postgres-driver.ts";
|
|
20
|
-
import { PostgresDriver } from "./PostgresDriver.ts";
|
|
21
|
-
import { PostgresTransaction } from "./transaction.ts";
|
|
22
|
-
import type { Scalar } from "./scalar.ts";
|
|
23
|
-
|
|
24
|
-
class FakeClient implements IPgClient {
|
|
25
|
-
static latest: FakeClient;
|
|
26
|
-
queries: Array<{ text: string; values?: Scalar[] }> = [];
|
|
27
|
-
connected = false;
|
|
28
|
-
ended = false;
|
|
29
|
-
result: PgQueryResult = { rowCount: 2, rows: [{ id: 5 }] };
|
|
30
|
-
|
|
31
|
-
constructor(_options: { connectionString: string }) {
|
|
32
|
-
FakeClient.latest = this;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async connect(): Promise<void> {
|
|
36
|
-
this.connected = true;
|
|
37
|
-
}
|
|
38
|
-
async query(text: string, values?: Scalar[]): Promise<PgQueryResult> {
|
|
39
|
-
this.queries.push({ text, values });
|
|
40
|
-
return this.result;
|
|
41
|
-
}
|
|
42
|
-
async *rows(text: string, values?: Scalar[]): AsyncGenerator<PgQueryResult["rows"][number]> {
|
|
43
|
-
this.queries.push({ text, values });
|
|
44
|
-
for (const row of this.result.rows) yield row;
|
|
45
|
-
}
|
|
46
|
-
async end(): Promise<void> {
|
|
47
|
-
this.ended = true;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
afterEach(() => {
|
|
52
|
-
DatasourceFactory.reset();
|
|
53
|
-
DatasourceFactory.register("postgres", new PostgresDriver());
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
describe("bind", () => {
|
|
57
|
-
it("rewrites named parameters and inserts", () => {
|
|
58
|
-
expect(bindParameters()).toEqual({});
|
|
59
|
-
expect(bindParameters([{ name: "id", value: 1 }])).toEqual({ id: 1 });
|
|
60
|
-
expect(toPostgres("SELECT * FROM t WHERE id = $id AND n = $id", { id: 1 }))
|
|
61
|
-
.toEqual({ text: "SELECT * FROM t WHERE id = $1 AND n = $1", values: [1] });
|
|
62
|
-
expect(toPostgres("SELECT 1")).toEqual({ text: "SELECT 1", values: [] });
|
|
63
|
-
expect(toPostgres("SELECT * FROM t WHERE id = $id")).toEqual({
|
|
64
|
-
text: "SELECT * FROM t WHERE id = $1",
|
|
65
|
-
values: [null],
|
|
66
|
-
});
|
|
67
|
-
expect(toPostgres(`INSERT INTO "contact_sources" ("name") VALUES ('Manual')`).text)
|
|
68
|
-
.toBe(`INSERT INTO "contact_sources" ("name") VALUES ('Manual')`);
|
|
69
|
-
expect(toPostgres("SELECT * FROM OldContactsTbl").text).toBe(`SELECT * FROM "OldContactsTbl"`);
|
|
70
|
-
expect(bindParameters([{ name: "at", value: new Date("2020-01-02T03:04:05.000Z") }]))
|
|
71
|
-
.toEqual({ at: "2020-01-02T03:04:05.000Z" });
|
|
72
|
-
expect(toPostgres("SELECT $at", { at: "2020-01-02T03:04:05.000Z" })).toEqual({
|
|
73
|
-
text: "SELECT $1",
|
|
74
|
-
values: [new Date("2020-01-02T03:04:05.000Z")],
|
|
75
|
-
});
|
|
76
|
-
expect(toPostgres("SELECT $v", { v: "AA==" })).toEqual({
|
|
77
|
-
text: "SELECT $1",
|
|
78
|
-
values: [Uint8Array.from([0])],
|
|
79
|
-
});
|
|
80
|
-
expect(toPostgres("SELECT $n", { n: "ada" }).values).toEqual(["ada"]);
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
describe("PostgresConnection", () => {
|
|
85
|
-
it("opens, runs SQL, transacts, and closes", async () => {
|
|
86
|
-
const connection = new PostgresConnection({
|
|
87
|
-
url: "postgres://db/app",
|
|
88
|
-
schema: "iso",
|
|
89
|
-
Client: FakeClient,
|
|
90
|
-
});
|
|
91
|
-
await connection.open();
|
|
92
|
-
await expect(connection.open()).rejects.toThrow("Connection is already open");
|
|
93
|
-
const client = FakeClient.latest;
|
|
94
|
-
expect(client.connected).toBe(true);
|
|
95
|
-
const command = new PostgresCommand(connection);
|
|
96
|
-
expect(await command.execute("INSERT INTO items (id) VALUES ($id)", [{ name: "id", value: 1 }]))
|
|
97
|
-
.toMatchObject({ rowsAffected: 2, result: null });
|
|
98
|
-
client.result = { rowCount: 0, rows: [] };
|
|
99
|
-
expect(await command.executeNative(new Native("INSERT INTO items (id) VALUES (1)"))).toEqual({
|
|
100
|
-
rowsAffected: 0,
|
|
101
|
-
result: null,
|
|
102
|
-
parameters: undefined,
|
|
103
|
-
});
|
|
104
|
-
client.result = { rowCount: null, rows: [{ id: 9 }] };
|
|
105
|
-
expect(await command.executeNative(new Native("UPDATE items SET n = 1"))).toMatchObject({
|
|
106
|
-
rowsAffected: 1,
|
|
107
|
-
result: null,
|
|
108
|
-
});
|
|
109
|
-
expect(await command.executeNative(new Native(" "))).toEqual({ rowsAffected: 0, parameters: undefined });
|
|
110
|
-
client.result = { rowCount: null, rows: [{ id: 1 }] };
|
|
111
|
-
expect(await command.executeReader("SELECT * FROM items")).toBeInstanceOf(Object);
|
|
112
|
-
const reader = await command.executeReader("SELECT * FROM items");
|
|
113
|
-
expect(await reader.next()).toBe(true);
|
|
114
|
-
expect(await command.executeStatement(new CreateTableStatement("users", [{ name: "id", type: "INTEGER" }])))
|
|
115
|
-
.toMatchObject({ rowsAffected: 1 });
|
|
116
|
-
expect(await (await command.executeReaderNative(new Native("SELECT 1"))).next()).toBe(true);
|
|
117
|
-
await connection.startTransaction();
|
|
118
|
-
await connection.startTransaction(IsolationLevel.ReadUncommitted);
|
|
119
|
-
await connection.startTransaction(IsolationLevel.ReadCommitted);
|
|
120
|
-
await connection.startTransaction(IsolationLevel.RepeatableRead);
|
|
121
|
-
await connection.startTransaction(IsolationLevel.Serializable);
|
|
122
|
-
await new PostgresTransaction(connection).commit();
|
|
123
|
-
await new PostgresTransaction(connection).abort();
|
|
124
|
-
expect(client.queries.map((q) => q.text)).toEqual([
|
|
125
|
-
"SET search_path TO iso",
|
|
126
|
-
"INSERT INTO items (id) VALUES ($1)",
|
|
127
|
-
"INSERT INTO items (id) VALUES (1)",
|
|
128
|
-
"UPDATE items SET n = 1",
|
|
129
|
-
"SELECT * FROM items",
|
|
130
|
-
"SELECT * FROM items",
|
|
131
|
-
"CREATE TABLE users (id INTEGER)",
|
|
132
|
-
"SELECT 1",
|
|
133
|
-
"BEGIN ISOLATION LEVEL READ COMMITTED",
|
|
134
|
-
"BEGIN ISOLATION LEVEL READ UNCOMMITTED",
|
|
135
|
-
"BEGIN ISOLATION LEVEL READ COMMITTED",
|
|
136
|
-
"BEGIN ISOLATION LEVEL REPEATABLE READ",
|
|
137
|
-
"BEGIN ISOLATION LEVEL SERIALIZABLE",
|
|
138
|
-
"COMMIT",
|
|
139
|
-
"ROLLBACK",
|
|
140
|
-
]);
|
|
141
|
-
await connection.close();
|
|
142
|
-
expect(client.ended).toBe(true);
|
|
143
|
-
await expect(connection.exec("SELECT 1")).rejects.toThrow("not open");
|
|
144
|
-
await expect(command.executeNative(new Native("x"))).rejects.toThrow("not open");
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it("loads the pg client from a module", async () => {
|
|
148
|
-
const Client = class implements IPgClient {
|
|
149
|
-
async connect(): Promise<void> {}
|
|
150
|
-
async query() {
|
|
151
|
-
return { rowCount: 0, rows: [] };
|
|
152
|
-
}
|
|
153
|
-
async *rows() {}
|
|
154
|
-
async end(): Promise<void> {}
|
|
155
|
-
};
|
|
156
|
-
const named = new PostgresConnection({
|
|
157
|
-
url: "postgres://db/app",
|
|
158
|
-
Client: pgClient({ Client } as PgModule),
|
|
159
|
-
});
|
|
160
|
-
await named.open();
|
|
161
|
-
await named.close();
|
|
162
|
-
const viaDefault = new PostgresConnection({
|
|
163
|
-
url: "postgres://db/app",
|
|
164
|
-
Client: pgClient({ Client, default: { Client } } as PgModule),
|
|
165
|
-
});
|
|
166
|
-
await viaDefault.open();
|
|
167
|
-
await viaDefault.close();
|
|
168
|
-
expect(PostgresConnection.from({ DATASOURCE_URL: "postgres://db/app" }).url).toBe("postgres://db/app");
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
it("opens, reopens, transacts, and closes through the connection", async () => {
|
|
172
|
-
const connection = new PostgresConnection({
|
|
173
|
-
url: "postgres://db/app",
|
|
174
|
-
schema: "s",
|
|
175
|
-
Client: FakeClient,
|
|
176
|
-
});
|
|
177
|
-
await connection.open();
|
|
178
|
-
expect(connection.createCommand()).toBeInstanceOf(PostgresCommand);
|
|
179
|
-
expect(connection.isOpen).toBe(true);
|
|
180
|
-
expect(connection.schema).toBe("s");
|
|
181
|
-
const txn = await connection.startTransaction();
|
|
182
|
-
await txn.commit();
|
|
183
|
-
const txn2 = await connection.startTransaction(IsolationLevel.ReadUncommitted);
|
|
184
|
-
expect(txn2.isolation).toBe(IsolationLevel.ReadUncommitted);
|
|
185
|
-
await txn2.abort();
|
|
186
|
-
expect(txn.isolation).toBe("readCommitted");
|
|
187
|
-
await connection.close();
|
|
188
|
-
expect(FakeClient.latest.ended).toBe(true);
|
|
189
|
-
expect(() => connection.ensureOpen()).toThrow(DatasourceError);
|
|
190
|
-
await expect(connection.startTransaction()).rejects.toThrow(DatasourceError);
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
it("defaults url and requires a client to open", async () => {
|
|
194
|
-
const connection = new PostgresConnection();
|
|
195
|
-
expect(connection.url).toBe("");
|
|
196
|
-
await expect(connection.open()).rejects.toThrow(DatasourceError);
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
describe("PostgresCommand", () => {
|
|
201
|
-
async function setup() {
|
|
202
|
-
const connection = new PostgresConnection({ Client: FakeClient });
|
|
203
|
-
await connection.open();
|
|
204
|
-
return { connection, command: new PostgresCommand(connection), client: FakeClient.latest };
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
it("executes SQL through the client", async () => {
|
|
208
|
-
const { command, client } = await setup();
|
|
209
|
-
expect(await command.execute("DELETE FROM items", [{ name: "id", value: 1 }]))
|
|
210
|
-
.toEqual({ result: null, rowsAffected: 2, parameters: [{ name: "id", value: 1 }] });
|
|
211
|
-
expect(await command.executeNative(new Native(""))).toEqual({ result: undefined, rowsAffected: 0, parameters: undefined });
|
|
212
|
-
const reader = await command.executeReader("SELECT * FROM items");
|
|
213
|
-
expect(await reader.next()).toBe(true);
|
|
214
|
-
expect(reader.current).toEqual({ id: 5 });
|
|
215
|
-
const empty = await command.executeReaderNative(new Native(""));
|
|
216
|
-
expect(await empty.next()).toBe(false);
|
|
217
|
-
expect(await (await command.executeReaderNative(new Native(";"))).next()).toBe(false);
|
|
218
|
-
client.result = { rowCount: 1, rows: [{ add_one: 5 }] };
|
|
219
|
-
const n = { name: "n", value: 4 };
|
|
220
|
-
expect(await command.executeStatement(new ReturnStatement("add_one", [n]), [n]))
|
|
221
|
-
.toEqual({ result: 5, rowsAffected: 1, parameters: [n] });
|
|
222
|
-
client.result = { rowCount: 0, rows: [] };
|
|
223
|
-
expect(await command.executeStatement(new SelectStatement({ table: "dual", fields: ["1"] }))).toMatchObject({
|
|
224
|
-
result: null,
|
|
225
|
-
rowsAffected: 0,
|
|
226
|
-
});
|
|
227
|
-
client.result = { rowCount: 1, rows: [{}] };
|
|
228
|
-
expect(await command.executeStatement(new SelectStatement({ table: "dual", fields: ["1"] }))).toMatchObject({ result: null });
|
|
229
|
-
client.result = { rowCount: 0, rows: [] };
|
|
230
|
-
expect(await command.executeNative(new Native("SELECT 1"))).toEqual({
|
|
231
|
-
result: null,
|
|
232
|
-
rowsAffected: 0,
|
|
233
|
-
parameters: undefined,
|
|
234
|
-
});
|
|
235
|
-
client.result = { rowCount: 1, rows: [{ id: null }] };
|
|
236
|
-
expect(await command.executeNative(new Native("SELECT flag($b)"), [{ name: "b", value: true }])).toMatchObject({ result: null });
|
|
237
|
-
expect(await command.executeNative(new Native("SELECT when($d)"), [{ name: "d", value: new Date("2026-09-22T00:00:00.000Z") }])).toMatchObject({ result: null });
|
|
238
|
-
expect(await command.executeNative(new Native("SELECT blob($b)"), [{ name: "b", value: new Uint8Array([1, 2, 3]) }])).toMatchObject({ result: null });
|
|
239
|
-
const inout = { name: "result", value: 4, direction: ParameterDirection.InputOutput };
|
|
240
|
-
client.result = { rowCount: 1, rows: [{ result: { n: 1 } }] };
|
|
241
|
-
expect(await command.executeNative(new Native("CALL bump($result)"), [inout])).toMatchObject({
|
|
242
|
-
parameters: [{ name: "result", value: "{\"n\":1}", direction: ParameterDirection.InputOutput }],
|
|
243
|
-
});
|
|
244
|
-
client.result = { rowCount: 1, rows: [{ result: 5 }] };
|
|
245
|
-
expect(await command.executeStatement(new ExecuteStoredProcedureStatement("bump", [inout]), [inout])).toMatchObject({
|
|
246
|
-
result: null,
|
|
247
|
-
parameters: [{ name: "result", value: 5, direction: ParameterDirection.InputOutput }],
|
|
248
|
-
});
|
|
249
|
-
client.result = { rowCount: 1, rows: [{ id: 1 }] };
|
|
250
|
-
expect(
|
|
251
|
-
await command.executeNative(new Native("CALL noop($n)"), [
|
|
252
|
-
{ name: "n", value: 1, direction: ParameterDirection.Input },
|
|
253
|
-
]),
|
|
254
|
-
).toMatchObject({ rowsAffected: 1 });
|
|
255
|
-
expect(await command.executeNative(new Native("CALL noop()"))).toMatchObject({ rowsAffected: 1 });
|
|
256
|
-
client.result = { rowCount: 1, rows: [{ other: 9 }] };
|
|
257
|
-
expect(
|
|
258
|
-
await command.executeNative(new Native("CALL bump($missing)"), [
|
|
259
|
-
{ name: "missing", value: 1, direction: ParameterDirection.Output },
|
|
260
|
-
]),
|
|
261
|
-
).toMatchObject({
|
|
262
|
-
parameters: [{ name: "missing", value: 1, direction: ParameterDirection.Output }],
|
|
263
|
-
});
|
|
264
|
-
});
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
describe("Postgres datasource", () => {
|
|
268
|
-
it("registers and constructs children", async () => {
|
|
269
|
-
expect(
|
|
270
|
-
await new Datasource("postgres", new PostgresDriver()).createConnection(),
|
|
271
|
-
).toBeInstanceOf(PostgresConnection);
|
|
272
|
-
const of = (): IDriver => ({
|
|
273
|
-
connection: () => new PostgresConnection({ Client: FakeClient }),
|
|
274
|
-
command: (c) => c.createCommand(),
|
|
275
|
-
transaction: (c, isolation = "readCommitted") =>
|
|
276
|
-
new PostgresTransaction(c as PostgresConnection, isolation),
|
|
277
|
-
rowTypeConverters: PostgresRowTypeConverters,
|
|
278
|
-
columnType,
|
|
279
|
-
});
|
|
280
|
-
const ds = new Datasource("postgres", of());
|
|
281
|
-
expect(ds.provider).toBe("postgres");
|
|
282
|
-
const connection = await ds.createConnection();
|
|
283
|
-
await (connection as PostgresConnection).open();
|
|
284
|
-
const command = await ds.createCommand(connection);
|
|
285
|
-
expect(command).toBeInstanceOf(PostgresCommand);
|
|
286
|
-
expect(command.connection).toBe(connection);
|
|
287
|
-
await command.execute("DELETE FROM items");
|
|
288
|
-
await command.executeReaderNative(new Native("SELECT 1"));
|
|
289
|
-
await expect(ds.createCommand()).rejects.toThrow("Connection is already open");
|
|
290
|
-
const repo = ds.createRepository(untyped(), connection);
|
|
291
|
-
expect(ds.createReadonlyRepository(untyped(), connection).constructor).toBe(ReadonlyRepository);
|
|
292
|
-
expect(repo).toBeInstanceOf(CRUDRepository);
|
|
293
|
-
expect(new Datasource("postgres", of()).createRepository(untyped())).toBeInstanceOf(CRUDRepository);
|
|
294
|
-
expect(Datasource.create({ PROVIDER: "postgres" }).provider).toBe("postgres");
|
|
295
|
-
const provided = PostgresConnection.from({
|
|
296
|
-
DATASOURCE_URL: "postgres://db/app?schema=s",
|
|
297
|
-
});
|
|
298
|
-
expect(provided.url).toBe("postgres://db/app?schema=s");
|
|
299
|
-
expect(provided.schema).toBe("s");
|
|
300
|
-
expect(PostgresConnection.from({ DATASOURCE_URL: "postgres://db/app?schema=app" }).schema).toBe("app");
|
|
301
|
-
expect(PostgresConnection.from().url).toBe("");
|
|
302
|
-
expect(PostgresConnection.from().schema).toBeUndefined();
|
|
303
|
-
const provider = new PostgresDriver();
|
|
304
|
-
expect(provider.columnType("int")).toBe("INTEGER");
|
|
305
|
-
expect(provider.columnType("uuid")).toBe("UUID");
|
|
306
|
-
expect(provider.columnType("string")).toBe("TEXT");
|
|
307
|
-
expect(provider.columnType("bool")).toBe("BOOLEAN");
|
|
308
|
-
expect(provider.columnType("decimal")).toBe("NUMERIC");
|
|
309
|
-
expect(provider.columnType("float")).toBe("DOUBLE PRECISION");
|
|
310
|
-
expect(provider.columnType("datetime")).toBe("TIMESTAMPTZ");
|
|
311
|
-
expect(provider.columnType("binary")).toBe("BYTEA");
|
|
312
|
-
expect(() => provider.columnType("xml")).toThrow('Unknown postgres type "xml"');
|
|
313
|
-
expect(provider.command(connection)).toBeInstanceOf(PostgresCommand);
|
|
314
|
-
expect(provider.transaction(connection as PostgresConnection).isolation).toBe("readCommitted");
|
|
315
|
-
});
|
|
316
|
-
|
|
317
|
-
it("maps property targets", () => {
|
|
318
|
-
const provider = new PostgresDriver();
|
|
319
|
-
const created = "2020-01-02T03:04:05.000Z";
|
|
320
|
-
const converter = RowTypeConverter.from(provider.rowTypeConverters, {
|
|
321
|
-
Key: "uuid",
|
|
322
|
-
Active: "bool",
|
|
323
|
-
Login: "string",
|
|
324
|
-
Created: "datetime",
|
|
325
|
-
});
|
|
326
|
-
const row = converter.convert({
|
|
327
|
-
Key: "11111111-1111-1111-1111-111111111111",
|
|
328
|
-
Active: 1,
|
|
329
|
-
Login: "ada",
|
|
330
|
-
Created: created,
|
|
331
|
-
});
|
|
332
|
-
expect(row).toEqual({
|
|
333
|
-
Key: "11111111-1111-1111-1111-111111111111",
|
|
334
|
-
Active: true,
|
|
335
|
-
Login: "ada",
|
|
336
|
-
Created: new Date(created),
|
|
337
|
-
});
|
|
338
|
-
expect(converter.reverseConvert(row)).toEqual({
|
|
339
|
-
Key: "11111111-1111-1111-1111-111111111111",
|
|
340
|
-
Active: true,
|
|
341
|
-
Login: "ada",
|
|
342
|
-
Created: created,
|
|
343
|
-
});
|
|
344
|
-
});
|
|
345
|
-
});
|
package/src/scalar.ts
DELETED
package/src/transaction.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
IsolationLevel,
|
|
3
|
-
type ITransaction,
|
|
4
|
-
type IsolationLevelName,
|
|
5
|
-
} from "@datasources/core";
|
|
6
|
-
import type { PostgresConnection } from "./connection.ts";
|
|
7
|
-
|
|
8
|
-
export class PostgresTransaction implements ITransaction {
|
|
9
|
-
constructor(
|
|
10
|
-
readonly connection: PostgresConnection,
|
|
11
|
-
readonly isolation: IsolationLevelName = IsolationLevel.ReadCommitted,
|
|
12
|
-
) {}
|
|
13
|
-
|
|
14
|
-
async commit(): Promise<void> {
|
|
15
|
-
await this.connection.exec("COMMIT");
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async abort(): Promise<void> {
|
|
19
|
-
await this.connection.exec("ROLLBACK");
|
|
20
|
-
}
|
|
21
|
-
}
|