Skip to content

Commit 5afb486

Browse files
committed
feat: added code for replication config
1 parent 548d3e7 commit 5afb486

File tree

7 files changed

+141
-38
lines changed

7 files changed

+141
-38
lines changed

expo-example/app/replication/config.tsx

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
import React, { useState } from 'react';
1+
import React, { useContext, useState } from 'react';
22
import { Database } from 'cbl-reactnative';
33
import { SafeAreaView, ScrollView, StyleSheet } from 'react-native';
44
import { useStyleScheme } from '@/components/Themed/Themed';
55
import ReplicatorConfigGeneralForm from '@/components/ReplicationConfigGeneralForm/ReplicatorConfigGeneralForm';
66
import ReplicatorAuthenticationForm from '@/components/ReplicatorAuthenticationForm/ReplicatorAuthenticationForm';
77
import ReplicatorConfigCollectionForm from '@/components/ReplicationConfigCollectionForm/ReplicatorConfigCollectionForm';
88
import ResultListView from '@/components/ResultsListView/ResultsListView';
9+
import ReplicatorContext from '@/providers/ReplicatorContext';
910
import { useNavigation } from '@react-navigation/native';
1011
import useNavigationBarTitleOption from '@/hooks/useNativgationBarTitle';
12+
import createReplicatorFromConfig from '@/service/replicator/createReplicatorFromConfig';
1113

1214
export default function ReplicationConfigCreateScreen() {
15+
const { setReplicatorIds } = useContext(ReplicatorContext)!;
1316
const styles = useStyleScheme();
1417
const navigation = useNavigation();
1518
useNavigationBarTitleOption('Add Replicator Config', navigation);
19+
1620
const [replicatorType, setReplicatorType] = useState<string>('');
1721
const [connectionString, setConnectionString] = useState<string>('');
1822
const [heartbeat, setHeartbeat] = useState<string>('300');
@@ -58,6 +62,7 @@ export default function ReplicationConfigCreateScreen() {
5862
collections: string[]
5963
): Promise<void> {
6064
try {
65+
const connStringLower = connectionString.toLowerCase();
6166
if (replicatorType === '') {
6267
setResultMessages(['Replicator Type is required']);
6368
return;
@@ -74,6 +79,39 @@ export default function ReplicationConfigCreateScreen() {
7479
setResultMessages(['At least one collection is required']);
7580
return;
7681
}
82+
if (
83+
connStringLower === '' ||
84+
!connStringLower.startsWith('ws://') ||
85+
!connStringLower.startsWith('wss://')
86+
) {
87+
setResultMessages(['Connection String is required']);
88+
return;
89+
}
90+
const replicatorId = await createReplicatorFromConfig(
91+
setReplicatorIds,
92+
database,
93+
scopeName,
94+
collections,
95+
replicatorType,
96+
connectionString,
97+
heartbeat,
98+
maxAttempts,
99+
maxWaitTime,
100+
continuous,
101+
autoPurgeEnabled,
102+
acceptParentDomainCookies,
103+
acceptOnlySelfSignedCerts,
104+
selectedAuthenticationType,
105+
username,
106+
password,
107+
sessionId,
108+
cookieName
109+
);
110+
const date = new Date();
111+
setResultMessages((prev) => [
112+
...prev,
113+
`${date.toISOString()}:: Replicator created with id: ${replicatorId}`,
114+
]);
77115
} catch (error) {
78116
// @ts-ignore
79117
return [error.message];

expo-example/components/ReplicatorAuthenticationForm/ReplicatorAuthenticationForm.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ export default function ReplicatorAuthenticationForm({
9090
onChangeText={(newText) => setCookieName(newText)}
9191
defaultValue={cookieName}
9292
/>
93-
<Divider style={localStyles.divider} />
9493
</>
9594
) : null}
9695
</View>

expo-example/providers/DatabaseProvider.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState, ReactNode, useMemo } from 'react';
2-
import { Database, CblReactNativeEngine } from 'cbl-reactnative';
2+
import { Database, CblReactNativeEngine, Replicator } from 'cbl-reactnative';
33
import DatabaseContext from '@/providers/DatabaseContext';
44
import ReplicatorContext from '@/providers/ReplicatorContext';
55

@@ -9,9 +9,9 @@ type DatabaseProviderProps = {
99

1010
const DatabaseProvider: React.FC<DatabaseProviderProps> = ({ children }) => {
1111
const [databases, setDatabases] = useState<Record<string, Database>>({});
12-
const [replicatorIds, setReplicatorIds] = useState<Record<string, string>>(
13-
{}
14-
);
12+
const [replicatorIds, setReplicatorIds] = useState<
13+
Record<string, Replicator>
14+
>({});
1515

1616
const databasesValue = useMemo(
1717
() => ({ databases, setDatabases }),

expo-example/service/replicator/createReplicationConfig.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import {
2+
Database,
3+
Replicator,
4+
ReplicatorConfiguration,
5+
BasicAuthenticator,
6+
SessionAuthenticator,
7+
URLEndpoint,
8+
Collection,
9+
ReplicatorType,
10+
} from 'cbl-reactnative';
11+
import React from 'react';
12+
13+
export default async function createReplicatorFromConfig(
14+
setReplicatorIds: React.Dispatch<
15+
React.SetStateAction<Record<string, Replicator>>
16+
>,
17+
database: Database,
18+
scopeName: string,
19+
collections: string[],
20+
replicatorType: string,
21+
connectionString: string,
22+
heartbeat: string,
23+
maxAttempts: string,
24+
maxWaitTime: string,
25+
continuous: boolean,
26+
autoPurgeEnabled: boolean,
27+
acceptParentDomainCookies: boolean,
28+
acceptOnlySelfSignedCerts: boolean,
29+
selectedAuthenticationType: string,
30+
username: string | undefined,
31+
password: string | undefined,
32+
sessionId: string | undefined,
33+
cookieName: string | undefined
34+
): Promise<string> {
35+
const target = new URLEndpoint(connectionString);
36+
const config = new ReplicatorConfiguration(target);
37+
const cols = await getCollections(collections, scopeName, database);
38+
39+
if (selectedAuthenticationType.toLowerCase() === 'basic') {
40+
const uname = username || '';
41+
const pwd = password || '';
42+
config.setAuthenticator(new BasicAuthenticator(uname, pwd));
43+
} else {
44+
const session = sessionId || '';
45+
const cookie = cookieName || '';
46+
config.setAuthenticator(new SessionAuthenticator(session, cookie));
47+
}
48+
49+
config.addCollections(cols);
50+
config.setReplicatorType(getReplicationType(replicatorType));
51+
config.setHeartbeat(parseInt(heartbeat, 10));
52+
config.setMaxAttempts(parseInt(maxAttempts, 10));
53+
config.setMaxAttemptWaitTime(parseInt(maxWaitTime, 10));
54+
config.setContinuous(continuous);
55+
config.setAutoPurgeEnabled(autoPurgeEnabled);
56+
config.setAcceptParentDomainCookies(acceptParentDomainCookies);
57+
config.setAcceptOnlySelfSignedCerts(acceptOnlySelfSignedCerts);
58+
const replicator = await Replicator.create(config);
59+
const uuid = replicator.getId();
60+
if (uuid !== undefined && uuid !== '') {
61+
setReplicatorIds((prev) => {
62+
return { ...prev, [uuid]: replicator };
63+
});
64+
return uuid;
65+
} else {
66+
throw new Error("Can't create replicator - id is undefined");
67+
}
68+
}
69+
70+
async function getCollections(
71+
collections: string[],
72+
scopeName: string,
73+
database: Database
74+
): Promise<Collection[]> {
75+
const resultsCollections: Collection[] = [];
76+
collections.map(async (collectionName) => {
77+
const collection = await database.collection(scopeName, collectionName);
78+
resultsCollections.push(collection);
79+
});
80+
return resultsCollections;
81+
}
82+
83+
function getReplicationType(replicatorType: string): ReplicatorType {
84+
switch (replicatorType) {
85+
case 'PUSH':
86+
return ReplicatorType.PUSH;
87+
case 'PULL':
88+
return ReplicatorType.PULL;
89+
case 'PUSH AND PULL':
90+
return ReplicatorType.PUSH_AND_PULL;
91+
default:
92+
return ReplicatorType.PUSH_AND_PULL;
93+
}
94+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React from 'react';
2+
import { Replicator } from 'cbl-reactnative';
23
export type ReplicatorContextType = {
3-
replicatorIds: Record<string, string>;
4+
replicatorIds: Record<string, Replicator>;
45
setReplicatorIds: React.Dispatch<
5-
React.SetStateAction<Record<string, string>>
6+
React.SetStateAction<Record<string, Replicator>>
67
>;
78
};

src/cblite-js

0 commit comments

Comments
 (0)