openbase-js 0.1.7 → 0.1.9
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/README.md +182 -0
- package/index.cjs +143 -106
- package/index.d.ts +31 -1
- package/index.js +144 -106
- package/openbase_readme.md +115 -0
- package/package.json +1 -1
- package/test.js +109 -39
package/test.js
CHANGED
|
@@ -1,97 +1,167 @@
|
|
|
1
1
|
const { createClient } = require('./index');
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Openbase Dual-Key Test
|
|
5
|
+
*
|
|
6
|
+
* 1. Authorization Key (JWT or Service Key): Pass as 2nd argument.
|
|
7
|
+
* 2. Identity Key (Anon Key): Pass in the options object.
|
|
8
|
+
*/
|
|
3
9
|
const client = createClient(
|
|
4
|
-
'http://localhost:
|
|
5
|
-
|
|
6
|
-
'
|
|
10
|
+
'http://localhost:3003',
|
|
11
|
+
// In production, this would be your secret JWT
|
|
12
|
+
'umNrqtUU87S-e94PqVZm39Gdyu6FWOul1sPIw0DYKVE',
|
|
13
|
+
'mydb',
|
|
14
|
+
{
|
|
15
|
+
// This identifies your project to the backend
|
|
16
|
+
anonKey: 'umNrqtUU87S-e94PqVZm39Gdyu6FWOul1sPIw0DYKVE'
|
|
17
|
+
}
|
|
7
18
|
);
|
|
8
19
|
|
|
20
|
+
// Existing employees: IDs 101–104, salaries 55000–80000
|
|
21
|
+
|
|
9
22
|
async function test() {
|
|
10
23
|
console.log('\n── SELECT all ──────────────────────');
|
|
11
|
-
const { data, error } = await client.from('
|
|
24
|
+
const { data, error } = await client.from('employees').select('*');
|
|
12
25
|
console.log('data:', data);
|
|
13
26
|
console.log('error:', error);
|
|
14
27
|
|
|
15
28
|
console.log('\n── SELECT with .eq ─────────────────');
|
|
16
29
|
const { data: single } = await client
|
|
17
|
-
.from('
|
|
18
|
-
.select('
|
|
19
|
-
.eq('
|
|
30
|
+
.from('employees')
|
|
31
|
+
.select('firstname, lastname')
|
|
32
|
+
.eq('employeeid', 101)
|
|
20
33
|
.single();
|
|
21
34
|
console.log('single:', single);
|
|
22
35
|
|
|
23
36
|
console.log('\n── INSERT ──────────────────────────');
|
|
24
|
-
const { data: inserted } = await client.from('
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
37
|
+
const { data: inserted, error: insertErr } = await client.from('employees').insert({
|
|
38
|
+
employeeid: 999,
|
|
39
|
+
firstname: 'Test',
|
|
40
|
+
lastname: 'User',
|
|
41
|
+
hiredate: '2024-01-01',
|
|
42
|
+
salary: 50000,
|
|
28
43
|
});
|
|
29
44
|
console.log('inserted:', inserted);
|
|
45
|
+
console.log('insertErr:', insertErr);
|
|
30
46
|
|
|
31
47
|
console.log('\n── UPDATE ──────────────────────────');
|
|
32
48
|
const { data: updated } = await client
|
|
33
|
-
.from('
|
|
34
|
-
.update({
|
|
35
|
-
.eq('
|
|
49
|
+
.from('employees')
|
|
50
|
+
.update({ salary: 60000 })
|
|
51
|
+
.eq('employeeid', 999);
|
|
36
52
|
console.log('updated:', updated);
|
|
37
53
|
|
|
38
54
|
console.log('\n── DELETE ──────────────────────────');
|
|
39
55
|
const { data: deleted } = await client
|
|
40
|
-
.from('
|
|
56
|
+
.from('employees')
|
|
41
57
|
.delete()
|
|
42
|
-
.eq('
|
|
58
|
+
.eq('employeeid', 999);
|
|
43
59
|
console.log('deleted:', deleted);
|
|
44
60
|
}
|
|
45
61
|
|
|
46
62
|
async function testOperators() {
|
|
47
63
|
console.log('\n── GT / LT ─────────────────────────');
|
|
48
|
-
const { data: gt } = await client.from('
|
|
49
|
-
console.log('gt
|
|
64
|
+
const { data: gt } = await client.from('employees').select('*').gt('salary', 60000);
|
|
65
|
+
console.log('gt salary > 60000:', gt?.map(r => `${r.firstname} (${r.salary})`));
|
|
50
66
|
|
|
51
67
|
console.log('\n── GTE / LTE ───────────────────────');
|
|
52
|
-
const { data: lte } = await client.from('
|
|
53
|
-
console.log('lte
|
|
68
|
+
const { data: lte } = await client.from('employees').select('*').lte('salary', 65000);
|
|
69
|
+
console.log('lte salary <= 65000:', lte?.map(r => `${r.firstname} (${r.salary})`));
|
|
54
70
|
|
|
55
71
|
console.log('\n── LIKE ────────────────────────────');
|
|
56
|
-
const { data: like } = await client.from('
|
|
57
|
-
console.log('like
|
|
72
|
+
const { data: like } = await client.from('employees').select('*').like('firstname', '%a%');
|
|
73
|
+
console.log('like firstname %a%:', like?.map(r => r.firstname));
|
|
58
74
|
|
|
59
75
|
console.log('\n── ILIKE (case insensitive) ────────');
|
|
60
|
-
const { data: ilike } = await client.from('
|
|
61
|
-
console.log('ilike
|
|
76
|
+
const { data: ilike } = await client.from('employees').select('*').ilike('lastname', '%s%');
|
|
77
|
+
console.log('ilike lastname %s%:', ilike?.map(r => r.lastname));
|
|
62
78
|
|
|
63
79
|
console.log('\n── IN ──────────────────────────────');
|
|
64
|
-
const { data: inResult } = await client.from('
|
|
65
|
-
console.log('in
|
|
80
|
+
const { data: inResult } = await client.from('employees').select('*').in('employeeid', [101, 102]);
|
|
81
|
+
console.log('in employeeid [101, 102]:', inResult?.map(r => r.firstname));
|
|
66
82
|
|
|
67
83
|
console.log('\n── IS NULL ─────────────────────────');
|
|
68
|
-
const { data: isNull } = await client.from('
|
|
69
|
-
console.log('is
|
|
84
|
+
const { data: isNull } = await client.from('employees').select('*').is('hiredate', null);
|
|
85
|
+
console.log('is hiredate null:', isNull);
|
|
70
86
|
|
|
71
87
|
console.log('\n── ORDER ASC ───────────────────────');
|
|
72
|
-
const { data: asc } = await client.from('
|
|
73
|
-
console.log('order
|
|
88
|
+
const { data: asc } = await client.from('employees').select('*').order('salary', { ascending: true });
|
|
89
|
+
console.log('order salary asc:', asc?.map(r => r.salary));
|
|
74
90
|
|
|
75
91
|
console.log('\n── ORDER DESC ──────────────────────');
|
|
76
|
-
const { data: desc } = await client.from('
|
|
77
|
-
console.log('order
|
|
92
|
+
const { data: desc } = await client.from('employees').select('*').order('salary', { ascending: false });
|
|
93
|
+
console.log('order salary desc:', desc?.map(r => r.salary));
|
|
78
94
|
|
|
79
95
|
console.log('\n── LIMIT ───────────────────────────');
|
|
80
|
-
const { data: limited } = await client.from('
|
|
96
|
+
const { data: limited } = await client.from('employees').select('*').limit(2);
|
|
81
97
|
console.log('limit 2:', limited?.length, 'rows');
|
|
82
98
|
|
|
83
99
|
console.log('\n── RANGE (pagination) ──────────────');
|
|
84
|
-
const { data: page } = await client.from('
|
|
100
|
+
const { data: page } = await client.from('employees').select('*').range(0, 1);
|
|
85
101
|
console.log('range 0-1:', page?.length, 'rows');
|
|
86
102
|
|
|
87
103
|
console.log('\n── CHAINED (gt + order + limit) ────');
|
|
88
104
|
const { data: chained } = await client
|
|
89
|
-
.from('
|
|
105
|
+
.from('employees')
|
|
90
106
|
.select('*')
|
|
91
|
-
.gt('
|
|
92
|
-
.order('
|
|
107
|
+
.gt('salary', 55000)
|
|
108
|
+
.order('salary', { ascending: false })
|
|
93
109
|
.limit(3);
|
|
94
|
-
console.log('chained:', chained);
|
|
110
|
+
console.log('chained:', chained?.map(r => `${r.firstname} (${r.salary})`));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function testUpsert() {
|
|
114
|
+
console.log('\n── UPSERT ──────────────────────────');
|
|
115
|
+
// First, ensure record 999 is gone
|
|
116
|
+
await client.from('employees').delete().eq('employeeid', 999);
|
|
117
|
+
|
|
118
|
+
console.log('1. Upserting new record...');
|
|
119
|
+
const { data: upserted1 } = await client.from('employees').upsert({
|
|
120
|
+
employeeid: 999,
|
|
121
|
+
firstname: 'Upsert',
|
|
122
|
+
lastname: 'Test',
|
|
123
|
+
salary: 77777,
|
|
124
|
+
}, 'employeeid');
|
|
125
|
+
console.log('upserted1:', upserted1);
|
|
126
|
+
|
|
127
|
+
console.log('2. Upserting same record with new salary...');
|
|
128
|
+
const { data: upserted2 } = await client.from('employees').upsert({
|
|
129
|
+
employeeid: 999,
|
|
130
|
+
firstname: 'Upsert',
|
|
131
|
+
lastname: 'Test',
|
|
132
|
+
salary: 88888,
|
|
133
|
+
}, 'employeeid');
|
|
134
|
+
console.log('upserted2 (should be 88888):', upserted2);
|
|
135
|
+
|
|
136
|
+
// Clean up
|
|
137
|
+
await client.from('employees').delete().eq('employeeid', 999);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function testAuth() {
|
|
141
|
+
console.log('\n── AUTH ────────────────────────────');
|
|
142
|
+
const email = `test_${Math.random().toString(36).substring(7)}@example.com`;
|
|
143
|
+
const password = 'password123';
|
|
144
|
+
|
|
145
|
+
console.log('1. Signing up...');
|
|
146
|
+
const { data: signup, error: err1 } = await client.auth.signUp(email, password);
|
|
147
|
+
console.log('signup:', signup?.user?.email, err1);
|
|
148
|
+
|
|
149
|
+
console.log('2. Signing in...');
|
|
150
|
+
const { data: signin, error: err2 } = await client.auth.signIn(email, password);
|
|
151
|
+
console.log('signin:', signin?.session ? 'SUCCESS' : 'FAILED', err2);
|
|
152
|
+
|
|
153
|
+
console.log('3. Getting session...');
|
|
154
|
+
const { data: session, error: err3 } = await client.auth.getSession();
|
|
155
|
+
console.log('session user:', session?.user?.email, err3);
|
|
156
|
+
|
|
157
|
+
console.log('4. Signing out...');
|
|
158
|
+
client.auth.signOut();
|
|
159
|
+
const { data: sessionAfter } = await client.auth.getSession();
|
|
160
|
+
console.log('session after signout:', sessionAfter);
|
|
95
161
|
}
|
|
96
162
|
|
|
97
|
-
|
|
163
|
+
test()
|
|
164
|
+
.then(() => testOperators())
|
|
165
|
+
.then(() => testUpsert())
|
|
166
|
+
.then(() => testAuth())
|
|
167
|
+
.catch(console.error);
|