@ankhorage/db-supabase 0.0.0-stage → 2.0.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/CHANGELOG.md +346 -0
- package/LICENSE +21 -0
- package/README.md +23 -2
- package/dist/adapter.d.ts +3 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +170 -0
- package/dist/adapter.js.map +1 -0
- package/dist/admin.d.ts +4 -0
- package/dist/admin.d.ts.map +1 -0
- package/dist/admin.js +145 -0
- package/dist/admin.js.map +1 -0
- package/dist/errors.d.ts +5 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +37 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/query.d.ts +4 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +94 -0
- package/dist/query.js.map +1 -0
- package/dist/realtime.d.ts +10 -0
- package/dist/realtime.d.ts.map +1 -0
- package/dist/realtime.js +74 -0
- package/dist/realtime.js.map +1 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/validation.d.ts +7 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +43 -0
- package/dist/validation.js.map +1 -0
- package/package.json +79 -4
- package/src/adapter.test.ts +171 -0
- package/src/adapter.ts +237 -0
- package/src/admin.test.ts +106 -0
- package/src/admin.ts +204 -0
- package/src/errors.ts +66 -0
- package/src/index.ts +27 -0
- package/src/metadata/repository.json +42 -0
- package/src/query.ts +122 -0
- package/src/realtime.test.ts +174 -0
- package/src/realtime.ts +127 -0
- package/src/types.ts +69 -0
- package/src/validation.ts +57 -0
package/src/realtime.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DbChangeEvent,
|
|
3
|
+
DbChangeKind,
|
|
4
|
+
DbChangeListener,
|
|
5
|
+
DbCollectionSubscriptionInput,
|
|
6
|
+
DbRealtimeAdapter,
|
|
7
|
+
DbRecord,
|
|
8
|
+
DbRecordSubscriptionInput,
|
|
9
|
+
DbSubscription,
|
|
10
|
+
} from '@ankhorage/contracts/db';
|
|
11
|
+
|
|
12
|
+
import type { SupabaseRealtimeClient, SupabaseRealtimePayload } from './types.js';
|
|
13
|
+
import { validateIdentifier } from './validation.js';
|
|
14
|
+
|
|
15
|
+
interface RealtimeApiConfig {
|
|
16
|
+
readonly client: SupabaseRealtimeClient;
|
|
17
|
+
readonly defaultSchema: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function createRealtimeApi(config: RealtimeApiConfig): DbRealtimeAdapter['realtime'] {
|
|
21
|
+
return {
|
|
22
|
+
subscribeToCollection<TRecord extends object = DbRecord>(
|
|
23
|
+
input: DbCollectionSubscriptionInput,
|
|
24
|
+
listener: DbChangeListener<TRecord>,
|
|
25
|
+
): DbSubscription {
|
|
26
|
+
return createCollectionSubscription(config, input, listener);
|
|
27
|
+
},
|
|
28
|
+
subscribeToRecord<TRecord extends object = DbRecord>(
|
|
29
|
+
input: DbRecordSubscriptionInput,
|
|
30
|
+
listener: DbChangeListener<TRecord>,
|
|
31
|
+
): DbSubscription {
|
|
32
|
+
return createRecordSubscription(config, input, listener);
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function normalizeRealtimeEvent<TRecord extends object = DbRecord>(
|
|
38
|
+
payload: SupabaseRealtimePayload,
|
|
39
|
+
fallbackTable: string,
|
|
40
|
+
fallbackSchema: string,
|
|
41
|
+
): DbChangeEvent<TRecord> | null {
|
|
42
|
+
const kind = normalizeKind(payload.eventType);
|
|
43
|
+
if (kind === null) return null;
|
|
44
|
+
const record = normalizeRecord<TRecord>(payload.new);
|
|
45
|
+
const previousRecord = normalizeRecord<TRecord>(payload.old);
|
|
46
|
+
const base = {
|
|
47
|
+
table: payload.table ?? fallbackTable,
|
|
48
|
+
schema: payload.schema ?? fallbackSchema,
|
|
49
|
+
kind,
|
|
50
|
+
record: kind === 'delete' ? null : record,
|
|
51
|
+
committedAt: payload.commit_timestamp,
|
|
52
|
+
};
|
|
53
|
+
if (previousRecord === null) return base;
|
|
54
|
+
return { ...base, previousRecord };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function createCollectionSubscription<TRecord extends object>(
|
|
58
|
+
config: RealtimeApiConfig,
|
|
59
|
+
input: DbCollectionSubscriptionInput,
|
|
60
|
+
listener: DbChangeListener<TRecord>,
|
|
61
|
+
): DbSubscription {
|
|
62
|
+
const schema = input.schema ?? config.defaultSchema;
|
|
63
|
+
const table = validateIdentifier(input.table, 'Realtime table');
|
|
64
|
+
return createSubscription(config, `ankhorage-db:${schema}:${table}`, schema, table, listener);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function createRecordSubscription<TRecord extends object>(
|
|
68
|
+
config: RealtimeApiConfig,
|
|
69
|
+
input: DbRecordSubscriptionInput,
|
|
70
|
+
listener: DbChangeListener<TRecord>,
|
|
71
|
+
): DbSubscription {
|
|
72
|
+
const schema = input.schema ?? config.defaultSchema;
|
|
73
|
+
const table = validateIdentifier(input.table, 'Realtime table');
|
|
74
|
+
const idField = validateIdentifier(input.idField ?? 'id', 'Realtime record id field');
|
|
75
|
+
const id = String(input.id);
|
|
76
|
+
return createSubscription(
|
|
77
|
+
config,
|
|
78
|
+
`ankhorage-db:${schema}:${table}:${idField}:${id}`,
|
|
79
|
+
schema,
|
|
80
|
+
table,
|
|
81
|
+
listener,
|
|
82
|
+
`${idField}=eq.${id}`,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function createSubscription<TRecord extends object>(
|
|
87
|
+
config: RealtimeApiConfig,
|
|
88
|
+
channelName: string,
|
|
89
|
+
schema: string,
|
|
90
|
+
table: string,
|
|
91
|
+
listener: DbChangeListener<TRecord>,
|
|
92
|
+
filter?: string,
|
|
93
|
+
): DbSubscription {
|
|
94
|
+
const channel = config.client
|
|
95
|
+
.channel(channelName)
|
|
96
|
+
.on('postgres_changes', { event: '*', schema, table, filter }, (payload) => {
|
|
97
|
+
const event = normalizeRealtimeEvent<TRecord>(payload, table, schema);
|
|
98
|
+
if (event !== null) listener(event);
|
|
99
|
+
})
|
|
100
|
+
.subscribe();
|
|
101
|
+
return {
|
|
102
|
+
async unsubscribe(): Promise<void> {
|
|
103
|
+
await config.client.removeChannel(channel);
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function isRecord<TRecord extends object>(value: unknown): value is TRecord {
|
|
109
|
+
return typeof value === 'object' && value !== null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function normalizeKind(value: string | undefined): DbChangeKind | null {
|
|
113
|
+
switch (value) {
|
|
114
|
+
case 'INSERT':
|
|
115
|
+
return 'insert';
|
|
116
|
+
case 'UPDATE':
|
|
117
|
+
return 'update';
|
|
118
|
+
case 'DELETE':
|
|
119
|
+
return 'delete';
|
|
120
|
+
default:
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function normalizeRecord<TRecord extends object>(value: unknown): TRecord | null {
|
|
126
|
+
return isRecord<TRecord>(value) ? value : null;
|
|
127
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DbAdapter,
|
|
3
|
+
DbAdminAdapter,
|
|
4
|
+
DbAdminResult,
|
|
5
|
+
DbRealtimeAdapter,
|
|
6
|
+
} from '@ankhorage/contracts/db';
|
|
7
|
+
|
|
8
|
+
interface SupabaseRealtimePostgresChangesFilter {
|
|
9
|
+
readonly event: 'INSERT' | 'UPDATE' | 'DELETE' | '*';
|
|
10
|
+
readonly schema: string;
|
|
11
|
+
readonly table: string;
|
|
12
|
+
readonly filter?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface SupabaseRealtimeChannel {
|
|
16
|
+
on(
|
|
17
|
+
type: 'postgres_changes',
|
|
18
|
+
filter: SupabaseRealtimePostgresChangesFilter,
|
|
19
|
+
callback: (payload: SupabaseRealtimePayload) => void,
|
|
20
|
+
): SupabaseRealtimeChannel;
|
|
21
|
+
subscribe(callback?: (status: string) => void): SupabaseRealtimeChannel;
|
|
22
|
+
unsubscribe(): Promise<'ok' | 'timed out' | 'error'>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface SupabaseRealtimeClient {
|
|
26
|
+
channel(topic: string): SupabaseRealtimeChannel;
|
|
27
|
+
removeChannel(channel: SupabaseRealtimeChannel): Promise<'ok' | 'timed out' | 'error'>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface SupabaseRealtimePayload {
|
|
31
|
+
eventType?: string;
|
|
32
|
+
schema?: string;
|
|
33
|
+
table?: string;
|
|
34
|
+
commit_timestamp?: string;
|
|
35
|
+
new?: unknown;
|
|
36
|
+
old?: unknown;
|
|
37
|
+
errors?: string[] | null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface SupabaseDbAdapterOptions {
|
|
41
|
+
readonly url: string;
|
|
42
|
+
readonly anonKey: string;
|
|
43
|
+
readonly schema?: string;
|
|
44
|
+
readonly fetch?: typeof fetch;
|
|
45
|
+
readonly realtime?: boolean;
|
|
46
|
+
readonly realtimeClient?: SupabaseRealtimeClient;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface SupabaseDbAdminAdapterOptions {
|
|
50
|
+
readonly url: string;
|
|
51
|
+
readonly serviceRoleKey?: string;
|
|
52
|
+
readonly schema?: string;
|
|
53
|
+
readonly execute?: boolean;
|
|
54
|
+
readonly executeSql?: (sql: string) => Promise<DbAdminSqlExecutionResult>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type DbAdminSqlExecutionResult =
|
|
58
|
+
| {
|
|
59
|
+
readonly ok: true;
|
|
60
|
+
}
|
|
61
|
+
| {
|
|
62
|
+
readonly ok: false;
|
|
63
|
+
readonly error: DbAdminResult extends { readonly ok: false; readonly error: infer TError }
|
|
64
|
+
? TError
|
|
65
|
+
: never;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export type SupabaseDbAdapter = DbAdapter & Partial<DbRealtimeAdapter>;
|
|
69
|
+
export type SupabaseDbAdminAdapter = DbAdminAdapter;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { DbFilter } from '@ankhorage/contracts/db';
|
|
2
|
+
|
|
3
|
+
export function validateUrl(url: string): string {
|
|
4
|
+
const trimmed = url.trim();
|
|
5
|
+
|
|
6
|
+
if (trimmed.length === 0) {
|
|
7
|
+
throw new TypeError('Supabase URL is required.');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
new URL(trimmed);
|
|
12
|
+
} catch {
|
|
13
|
+
throw new TypeError('Supabase URL must be a valid URL.');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return trimmed.replace(/\/+$/, '');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function validateKey(value: string, label: string): string {
|
|
20
|
+
const trimmed = value.trim();
|
|
21
|
+
|
|
22
|
+
if (trimmed.length === 0) {
|
|
23
|
+
throw new TypeError(`${label} is required.`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return trimmed;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function validateIdentifier(value: string, label: string): string {
|
|
30
|
+
const trimmed = value.trim();
|
|
31
|
+
|
|
32
|
+
if (trimmed.length === 0) {
|
|
33
|
+
throw new TypeError(`${label} is required.`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(trimmed)) {
|
|
37
|
+
throw new TypeError(`${label} must be a valid SQL identifier.`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return trimmed;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function validateFilters(filters: readonly DbFilter[], label: string): void {
|
|
44
|
+
if (filters.length === 0) {
|
|
45
|
+
throw new TypeError(`${label} requires at least one filter.`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
for (const filter of filters) {
|
|
49
|
+
validateIdentifier(filter.field, 'Filter field');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function quoteIdentifier(value: string): string {
|
|
54
|
+
const identifier = validateIdentifier(value, 'SQL identifier');
|
|
55
|
+
|
|
56
|
+
return `"${identifier.replaceAll('"', '""')}"`;
|
|
57
|
+
}
|