supabase-test 0.0.1 → 0.0.4

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/test-client.js DELETED
@@ -1,154 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PgTestClient = void 0;
4
- const pg_1 = require("pg");
5
- const roles_1 = require("./roles");
6
- class PgTestClient {
7
- config;
8
- client;
9
- opts;
10
- ctxStmts = '';
11
- contextSettings = {};
12
- _ended = false;
13
- connectPromise = null;
14
- constructor(config, opts = {}) {
15
- this.opts = opts;
16
- this.config = config;
17
- this.client = new pg_1.Client({
18
- host: this.config.host,
19
- port: this.config.port,
20
- database: this.config.database,
21
- user: this.config.user,
22
- password: this.config.password
23
- });
24
- if (!opts.deferConnect) {
25
- this.connectPromise = this.client.connect();
26
- if (opts.trackConnect)
27
- opts.trackConnect(this.connectPromise);
28
- }
29
- }
30
- async ensureConnected() {
31
- if (this.connectPromise) {
32
- try {
33
- await this.connectPromise;
34
- }
35
- catch { }
36
- }
37
- }
38
- async close() {
39
- if (!this._ended) {
40
- this._ended = true;
41
- await this.ensureConnected();
42
- this.client.end();
43
- }
44
- }
45
- async begin() {
46
- await this.client.query('BEGIN;');
47
- }
48
- async savepoint(name = 'lqlsavepoint') {
49
- await this.client.query(`SAVEPOINT "${name}";`);
50
- }
51
- async rollback(name = 'lqlsavepoint') {
52
- await this.client.query(`ROLLBACK TO SAVEPOINT "${name}";`);
53
- }
54
- async commit() {
55
- await this.client.query('COMMIT;');
56
- }
57
- async beforeEach() {
58
- await this.begin();
59
- await this.savepoint();
60
- }
61
- async afterEach() {
62
- await this.rollback();
63
- await this.commit();
64
- }
65
- setContext(ctx) {
66
- Object.assign(this.contextSettings, ctx);
67
- this.ctxStmts = Object.entries(this.contextSettings)
68
- .map(([key, val]) => val === null
69
- ? `SELECT set_config('${key}', NULL, true);`
70
- : `SELECT set_config('${key}', '${val}', true);`)
71
- .join('\n');
72
- }
73
- /**
74
- * Set authentication context for the current session.
75
- * Configures role and user ID using cascading defaults from options → opts.auth → RoleMapping.
76
- */
77
- auth(options = {}) {
78
- const role = options.role ?? this.opts.auth?.role ?? (0, roles_1.getRoleName)('authenticated', this.opts);
79
- const userIdKey = options.userIdKey ?? this.opts.auth?.userIdKey ?? 'jwt.claims.user_id';
80
- const userId = options.userId ?? this.opts.auth?.userId ?? null;
81
- this.setContext({
82
- role,
83
- [userIdKey]: userId !== null ? String(userId) : null
84
- });
85
- }
86
- /**
87
- * Commit current transaction to make data visible to other connections, then start fresh transaction.
88
- * Maintains test isolation by creating a savepoint and reapplying session context.
89
- */
90
- async publish() {
91
- await this.commit(); // make data visible to other sessions
92
- await this.begin(); // fresh tx
93
- await this.savepoint(); // keep rollback harness
94
- await this.ctxQuery(); // reapply all setContext()
95
- }
96
- /**
97
- * Clear all session context variables and reset to default anonymous role.
98
- */
99
- clearContext() {
100
- const defaultRole = (0, roles_1.getRoleName)('anonymous', this.opts);
101
- const nulledSettings = {};
102
- Object.keys(this.contextSettings).forEach(key => {
103
- nulledSettings[key] = null;
104
- });
105
- nulledSettings.role = defaultRole;
106
- this.ctxStmts = Object.entries(nulledSettings)
107
- .map(([key, val]) => val === null
108
- ? `SELECT set_config('${key}', NULL, true);`
109
- : `SELECT set_config('${key}', '${val}', true);`)
110
- .join('\n');
111
- this.contextSettings = { role: defaultRole };
112
- }
113
- async any(query, values) {
114
- const result = await this.query(query, values);
115
- return result.rows;
116
- }
117
- async one(query, values) {
118
- const rows = await this.any(query, values);
119
- if (rows.length !== 1) {
120
- throw new Error('Expected exactly one result');
121
- }
122
- return rows[0];
123
- }
124
- async oneOrNone(query, values) {
125
- const rows = await this.any(query, values);
126
- return rows[0] || null;
127
- }
128
- async many(query, values) {
129
- const rows = await this.any(query, values);
130
- if (rows.length === 0)
131
- throw new Error('Expected many rows, got none');
132
- return rows;
133
- }
134
- async manyOrNone(query, values) {
135
- return this.any(query, values);
136
- }
137
- async none(query, values) {
138
- await this.query(query, values);
139
- }
140
- async result(query, values) {
141
- return this.query(query, values);
142
- }
143
- async query(query, values) {
144
- await this.ctxQuery();
145
- const result = await this.client.query(query, values);
146
- return result;
147
- }
148
- async ctxQuery() {
149
- if (this.ctxStmts) {
150
- await this.client.query(this.ctxStmts);
151
- }
152
- }
153
- }
154
- exports.PgTestClient = PgTestClient;