Skip to content

Commit 2c79cc1

Browse files
authored
Merge pull request #23 from Couchbase-Ecosystem/5-ios---index-api
Index API Support
2 parents bda251e + 1ee508c commit 2c79cc1

File tree

19 files changed

+512
-8
lines changed

19 files changed

+512
-8
lines changed

expo-example/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ allprojects {
3939
maven { url 'https://www.jitpack.io' }
4040
}
4141
}
42-
apply from: "../../android/build.gradle"
42+
apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React, { useState } from 'react';
2+
import { Collection } from 'cbl-reactnative';
3+
import CBLCollectionActionContainer from '@/components/CBLCollectionActionContainer';
4+
import HeaderView from '@/components/HeaderView';
5+
import { Divider } from '@gluestack-ui/themed';
6+
import { StyledTextInput } from '@/components/StyledTextInput';
7+
import create from '@/service/indexes/create';
8+
9+
export default function IndexCreateScreen() {
10+
const [indexName, setIndexName] = useState<string>('');
11+
const [indexProperties, setIndexProperties] = useState<string>('');
12+
13+
function reset() {
14+
setIndexName('');
15+
setIndexProperties('');
16+
}
17+
18+
async function update(collection: Collection): Promise<string[]> {
19+
try {
20+
await create(collection, indexName, indexProperties);
21+
return [`Index ${indexName} was created successfully`];
22+
} catch (error) {
23+
// @ts-ignore
24+
return [error.message];
25+
}
26+
}
27+
28+
return (
29+
<CBLCollectionActionContainer
30+
handleUpdatePressed={update}
31+
handleResetPressed={reset}
32+
screenTitle="Create Index"
33+
>
34+
<HeaderView name="Index" iconName="magnify" />
35+
<StyledTextInput
36+
autoCapitalize="none"
37+
placeholder="IndexName"
38+
onChangeText={(newText) => setIndexName(newText)}
39+
defaultValue={indexName}
40+
/>
41+
<Divider style={{ marginLeft: 8, marginTop: 10, marginBottom: 10 }} />
42+
<StyledTextInput
43+
style={{
44+
height: 120,
45+
minHeight: 20,
46+
marginBottom: 10,
47+
}}
48+
autoCapitalize="none"
49+
placeholder="Index Properties (comma separated)"
50+
onChangeText={(newText) => setIndexProperties(newText)}
51+
defaultValue={indexProperties}
52+
multiline={true}
53+
/>
54+
</CBLCollectionActionContainer>
55+
);
56+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import React, { useState } from 'react';
2+
import { Collection } from 'cbl-reactnative';
3+
import CBLCollectionActionContainer from '@/components/CBLCollectionActionContainer';
4+
import HeaderView from '@/components/HeaderView';
5+
import { Divider, Switch } from '@gluestack-ui/themed';
6+
import { StyledTextInput } from '@/components/StyledTextInput';
7+
import createFts from '@/service/indexes/createFts';
8+
import { View } from 'react-native';
9+
import { Text } from '@/components/Themed';
10+
11+
export default function IndexFtsCreateScreen() {
12+
const [indexName, setIndexName] = useState<string>('');
13+
const [ignoreAccents, setIgnoreAccents] = useState<boolean>(false);
14+
const [indexProperties, setIndexProperties] = useState<string>('');
15+
16+
function reset() {
17+
setIgnoreAccents(false);
18+
setIndexName('');
19+
setIndexProperties('');
20+
}
21+
22+
async function update(collection: Collection): Promise<string[]> {
23+
try {
24+
await createFts(collection, indexName, indexProperties, ignoreAccents);
25+
return [`Index ${indexName} was created successfully`];
26+
} catch (error) {
27+
// @ts-ignore
28+
return [error.message];
29+
}
30+
}
31+
32+
return (
33+
<CBLCollectionActionContainer
34+
handleUpdatePressed={update}
35+
handleResetPressed={reset}
36+
screenTitle="Create FTS Index"
37+
>
38+
<HeaderView name="Index" iconName="magnify" />
39+
<StyledTextInput
40+
autoCapitalize="none"
41+
placeholder="IndexName"
42+
onChangeText={(newText) => setIndexName(newText)}
43+
defaultValue={indexName}
44+
/>
45+
<Divider style={{ marginLeft: 8, marginTop: 10, marginBottom: 10 }} />
46+
<StyledTextInput
47+
style={{
48+
height: 120,
49+
minHeight: 20,
50+
marginBottom: 10,
51+
}}
52+
autoCapitalize="none"
53+
placeholder="Index Properties (comma separated)"
54+
onChangeText={(newText) => setIndexProperties(newText)}
55+
defaultValue={indexProperties}
56+
multiline={true}
57+
/>
58+
<Divider style={{ marginTop: 10, marginBottom: 10 }} />
59+
<View
60+
style={{
61+
flexDirection: 'row',
62+
justifyContent: 'space-between',
63+
alignItems: 'center',
64+
marginBottom: 16,
65+
}}
66+
>
67+
<Text style={{ paddingLeft: 6, fontSize: 16 }}>Ignore Accents</Text>
68+
<Switch
69+
style={{ paddingRight: 16 }}
70+
value={ignoreAccents}
71+
onValueChange={setIgnoreAccents}
72+
/>
73+
</View>
74+
</CBLCollectionActionContainer>
75+
);
76+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React, { useState } from 'react';
2+
import { Collection } from 'cbl-reactnative';
3+
import CBLCollectionActionContainer from '@/components/CBLCollectionActionContainer';
4+
import HeaderView from '@/components/HeaderView';
5+
import { Divider } from '@gluestack-ui/themed';
6+
import { StyledTextInput } from '@/components/StyledTextInput';
7+
import deleteIndex from '@/service/indexes/delete';
8+
9+
export default function IndexCreateScreen() {
10+
const [indexName, setIndexName] = useState<string>('');
11+
12+
function reset() {
13+
setIndexName('');
14+
}
15+
16+
async function update(collection: Collection): Promise<string[]> {
17+
try {
18+
await deleteIndex(collection, indexName);
19+
return [`Index ${indexName} was deleted successfully`];
20+
} catch (error) {
21+
// @ts-ignore
22+
return [error.message];
23+
}
24+
}
25+
26+
return (
27+
<CBLCollectionActionContainer
28+
handleUpdatePressed={update}
29+
handleResetPressed={reset}
30+
screenTitle="Delete Index"
31+
>
32+
<HeaderView name="Index" iconName="magnify" />
33+
<StyledTextInput
34+
autoCapitalize="none"
35+
placeholder="IndexName"
36+
onChangeText={(newText) => setIndexName(newText)}
37+
defaultValue={indexName}
38+
/>
39+
</CBLCollectionActionContainer>
40+
);
41+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from 'react';
2+
import { Collection } from 'cbl-reactnative';
3+
import CBLCollectionActionContainer from '@/components/CBLCollectionActionContainer';
4+
import listIndexes from '@/service/indexes/list';
5+
6+
export default function IndexesListScreen() {
7+
function reset() {}
8+
9+
async function update(collection: Collection): Promise<string[]> {
10+
try {
11+
const indexes = await listIndexes(collection);
12+
if (indexes.length > 0) {
13+
return indexes;
14+
} else {
15+
return ['No indexes found.'];
16+
}
17+
} catch (error) {
18+
// @ts-ignore
19+
return [error.message];
20+
}
21+
}
22+
23+
return (
24+
<CBLCollectionActionContainer
25+
handleUpdatePressed={update}
26+
handleResetPressed={reset}
27+
screenTitle="List Indexes"
28+
/>
29+
);
30+
}

expo-example/ios/expoexample.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@
357357
);
358358
OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_DEBUG";
359359
PRODUCT_BUNDLE_IDENTIFIER = "com.couchbase.rn.expo-example";
360-
PRODUCT_NAME = expoexample;
360+
PRODUCT_NAME = "expoexample";
361361
SWIFT_OBJC_BRIDGING_HEADER = "expoexample/expoexample-Bridging-Header.h";
362362
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
363363
SWIFT_VERSION = 5.0;
@@ -385,7 +385,7 @@
385385
);
386386
OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_RELEASE";
387387
PRODUCT_BUNDLE_IDENTIFIER = "com.couchbase.rn.expo-example";
388-
PRODUCT_NAME = expoexample;
388+
PRODUCT_NAME = "expoexample";
389389
SWIFT_OBJC_BRIDGING_HEADER = "expoexample/expoexample-Bridging-Header.h";
390390
SWIFT_VERSION = 5.0;
391391
TARGETED_DEVICE_FAMILY = "1,2";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

expo-example/ios/expoexample/Info.plist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@
5252
<key>NSUserActivityTypes</key>
5353
<array>
5454
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
55+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
56+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
57+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
58+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
59+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
60+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
61+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
62+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
63+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
64+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
65+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
66+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
67+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
5568
</array>
5669
<key>UILaunchStoryboardName</key>
5770
<string>SplashScreen</string>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Collection, ValueIndexItem, IndexBuilder } from 'cbl-reactnative';
2+
3+
export default async function create(
4+
collection: Collection,
5+
indexName: string,
6+
indexProperties: string
7+
): Promise<void> {
8+
const rawIndexes = indexProperties
9+
.split(',')
10+
.map((property) => property.trim());
11+
if (rawIndexes.length > 0 && indexName.length > 0) {
12+
//create value index items to be added to the index using
13+
//IndexBuilder
14+
let valueIndexes: ValueIndexItem[] = [];
15+
for (let i = 0; i < rawIndexes.length; i++) {
16+
valueIndexes.push(ValueIndexItem.property(rawIndexes[i].trim()));
17+
}
18+
const index = IndexBuilder.valueIndex(...valueIndexes);
19+
20+
await collection.createIndex(indexName, index);
21+
} else {
22+
throw new Error('Index name and properties are required');
23+
}
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Collection, FullTextIndexItem, IndexBuilder } from 'cbl-reactnative';
2+
3+
export default async function createFts(
4+
collection: Collection,
5+
indexName: string,
6+
indexProperties: string,
7+
ignoreAccents: boolean
8+
): Promise<void> {
9+
const rawIndexes = indexProperties
10+
.split(',')
11+
.map((property) => property.trim());
12+
if (rawIndexes.length > 0 && indexName.length > 0) {
13+
//create value index items to be added to the index using
14+
//IndexBuilder
15+
let valueIndexes: FullTextIndexItem[] = [];
16+
for (let i = 0; i < rawIndexes.length; i++) {
17+
const item = FullTextIndexItem.property(rawIndexes[i].trim());
18+
valueIndexes.push(item);
19+
}
20+
const index = IndexBuilder.fullTextIndex(...valueIndexes);
21+
index.setIgnoreAccents(ignoreAccents);
22+
await collection.createIndex(indexName, index);
23+
} else {
24+
throw new Error('Index name and properties are required');
25+
}
26+
}

0 commit comments

Comments
 (0)