Skip to content

Commit

Permalink
Generate updated snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
krollins-mdb committed Oct 30, 2023
1 parent 15e06d8 commit 2cd921b
Show file tree
Hide file tree
Showing 38 changed files with 254 additions and 139 deletions.
4 changes: 2 additions & 2 deletions examples/react-native/legacy/bluehawk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ JS_GENERATED_EXAMPLES=$PROJECT/source/examples/generated/react-native/js
TS_GENERATED_EXAMPLES=$PROJECT/source/examples/generated/react-native/ts
echo $JS_GENERATED_EXAMPLES

bluehawk snip $JS_RN_EXAMPLES -o $JS_GENERATED_EXAMPLES
npx bluehawk snip $JS_RN_EXAMPLES -o $JS_GENERATED_EXAMPLES

bluehawk snip $TS_RN_EXAMPLES -o $TS_GENERATED_EXAMPLES
npx bluehawk snip $TS_RN_EXAMPLES -o $TS_GENERATED_EXAMPLES
2 changes: 1 addition & 1 deletion examples/react-native/v12/scripts/bluehawk-one.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ then
# Bluehawk a single file
echo "${GREEN_BG_BOLD} Bluehawk: ${CLEAR} ${GREEN}Generate samples from '$FILE_NAME' ${CLEAR}"

bluehawk snip $INPUT_FILE -o $OUTPUT_DIRECTORY --format=rst
npx bluehawk snip $INPUT_FILE -o $OUTPUT_DIRECTORY --format=rst

# TODO: There's probably a more idiomatic way to do this results filtering.
GENERATED_FILES=$(find $OUTPUT_DIRECTORY -type f | grep -i $BASE_FILE_NAME)
Expand Down
2 changes: 1 addition & 1 deletion examples/react-native/v12/scripts/bluehawk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ OUTPUT_DIRECTORY=$PROJECT/source/examples/generated/react-native/v12

# standard bluehawking
echo "${GREEN_BG_BOLD}Bluehawk: ${CLEAR} ${GREEN} Generate React Native v12 examples ${CLEAR}"
bluehawk snip $INPUT_DIRECTORY -o $OUTPUT_DIRECTORY --format=rst
npx bluehawk snip $INPUT_DIRECTORY -o $OUTPUT_DIRECTORY --format=rst

FILES_TO_REMOVE=$(find $OUTPUT_DIRECTORY -type f -not -name "*.rst")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const DogList = () => {
const myDogs = useQuery(Dog);

const deleteAllYoungDogObjects = () => {
const youngDogs = myDogs.filtered('age < 3');
const youngDogs = useQuery(Dog, dogs => {
return dogs.filtered('age < 3');
});
realm.write(() => {
realm.delete(youngDogs);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const HomeInfo = ({homeOwnerName}) => {
const realm = useRealm();
const homeOwner = useQuery(HomeOwner).filtered(
`name == '${homeOwnerName}'`,
const homeOwner = useQuery(
HomeOwner,
homeOwners => {
return homeOwners.filtered(`name == '${homeOwnerName}'`);
},
[homeOwnerName],
)[0];

const deleteExtraHomeInfo = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ const HomeList = () => {

// run the `.filtered()` method on all the returned homeOwners to
// find all homeOwners that have a house with a listed price
const listedPriceHomes = homeOwners.filtered('home.@keys = "price"');
const listedPriceHomes = useQuer(HomeOwner, homeOwners => {
return homeOwners.filtered('home.@keys = "price"');
});

// run the `.filtered()` method on all the returned homeOwners to
// find the house with the address "Summerhill St."
const summerHillHouse = homeOwners.filtered(
'home["address"] = "Summerhill St."',
)[0].home;
const summerHillHouse = useQuery(HomeOwner, homeOwners => {
return homeOwner.filtered('home["address"] = "Summerhill St."');
})[0].home;

// run the `.filtered()` method on all the returned homeOwners to
// find the first house that has any field with a value of 'red'
const redHouse = homeOwners.filtered('home.@values = "red"')[0].home;
const redHouse = useQuery(HomeOwner, homeOwners => {
return homeOwners.filtered('home.@values = "red"');
})[0].home;

return (
<View>
<Text>All homes:</Text>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const UpdateHome = ({homeOwnerName}) => {
const [address, setAddress] = useState('');
const realm = useRealm();
const homeOwner = useQuery(HomeOwner).filtered(
`name == '${homeOwnerName}'`,
const homeOwner = useQuery(
HomeOwner,
homeOwners => {
return homeOwners.filtered(`name == '${homeOwnerName}'`);
},
[homeOwnerName],
)[0];

const updateAddress = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
const ContactInfo = ({contactCity, postalCode}) => {
const contacts = useQuery(Contact);
const parentsToDelete = contacts.filtered(
`address.city == '${contactCity}'`,
const realm = useRealm();
const parentsToDelete = useQuery(
Contact,
contacts => {
return contacts.filtered(`address.city == '${contactCity}'`);
},
[contactCity],
);
const embeddedToDelete = contacts.filtered(
`address.postalCode == '${postalCode}'`,
const embeddedToDelete = useQuery(
Contact,
contacts => {
return contacts.filtered(`address.postalCode == '${postalCode}'`);
},
[postalCode],
);
const realm = useRealm();

const deleteParentObject = () => {
realm.write(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const ContactList = ({postalCode}) => {
// Query for all Contact objects
const contacts = useQuery(Contact);

// Run the `.filtered()` method on all the returned Contacts to get
// contacts with a specific postal code.
const contactsInArea = contacts.filtered(
`address.postalCode == '${postalCode}'`,
const contactsInArea = useQuery(
Contact,
contacts => {
return contacts.filtered(`address.postalCode == '${postalCode}'`);
},
[postalCode],
);

if (contactsInArea.length) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const CatInfoCard = ({catName}) => {
// To query for the cat's birthDate, filter for their name to retrieve the realm object.
// Use dot notation to access the birthDate property.
const cat = useQuery(Cat).filtered(`name = '${catName}'`)[0];
const cat = useQuery(
Cat,
cats => {
return cats.filtered(`name = '${catName}'`);
},
[catName],
)[0];
const catBirthDate = cat.birthDate;

if (cat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ const FindSortFilterComponent = ({objectPrimaryKey}) => {

const filterProfiles = (filter, letter) => {
// Use [c] for case-insensitivity.
const filtered = profiles.filtered(`name ${filter}[c] "${letter}"`);
const filtered = useQuery(
Profile,
profiles => {
return profiles.filtered(`name ${filter}[c] "${letter}"`);
},
[filter, letter],
);

setAllProfiles(filtered);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const TaskList = () => {
// retrieve the set of Task objects
const tasks = useQuery(Task);

// filter for tasks with a high priority
const highPriorityTasks = tasks.filtered('priority >= $0', 4);
const highPriorityTasks = useQuery(Task, tasks => {
return tasks.filtered('priority >= $0', 4);
});

// filter for tasks that have just-started or short-running progress
const lowProgressTasks = tasks.filtered(
'$0 <= progressMinutes && progressMinutes < $1',
1,
10,
);
const lowProgressTasks = useQuery(Task, tasks => {
return tasks.filtered(
'$0 <= progressMinutes && progressMinutes < $1',
1,
10,
);
});

return (
<>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const PostsByYoungUsers = () => {
const posts = useQuery(Post);
const postsByYoungUsers = useMemo(() => {
const postsByYoungUsers = useQuery(Post, posts => {
return posts.filtered(
'@links.User.posts.birthdate >= 2000-01-01@00:00:00:0',
);
}, [posts]);
});

if (!posts) return <Text>The post was not found.</Text>;
if (!postsByYoungUsers) return <Text>The post was not found.</Text>;
return (
<View>
<Text>Posts By Young Users</Text>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const AddInventoryToCharacter = ({characterName}) => {
const realm = useRealm();
const [inventoryItem, setInventoryItem] = useState('');
const character = useQuery(Character).filtered(
`name = '${characterName}'`,
const character = useQuery(
Character,
characters => {
return characters.filtered(`name = '${characterName}'`);
},
[characterName],
)[0];

const addInventoryItem = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const QueryCharacterInventory = ({characterName}) => {
const [inventoryItem, setInventoryItem] = useState('');
const character = useQuery(Character).filtered(
`name = '${characterName}'`,
const character = useQuery(
Character,
characters => {
return characters.filtered(`name = '${characterName}'`);
},
[characterName],
)[0];

const queryCharacterInventory = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const RemoveInventoryFromCharacter = ({characterName}) => {
const realm = useRealm();
const [inventoryItem, setInventoryItem] = useState('');
const character = useQuery(Character).filtered(
`name = '${characterName}'`,
const character = useQuery(
Character,
characters => {
return characters.filtered(`name = '${characterName}'`);
},
[characterName],
)[0];

const removeInventoryItem = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ const TraverseCharacterInventory = ({characterName}) => {
const [inventoryItem, setInventoryItem] = useState('');
const [inventory, setInventory] = useState([]);

const character = useQuery(Character).filtered(
`name = '${characterName}'`,
const character = useQuery(
Character,
characters => {
return characters.filtered(`name = '${characterName}'`);
},
[characterName],
)[0];

const addInventoryItem = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class Profile extends Realm.Object<Profile> {
_id!: Realm.BSON.UUID;
_id!: Realm.BSON.ObjectId;
name!: string;

static schema: ObjectSchema = {
name: 'Profile',
primaryKey: '_id',
properties: {
_id: 'uuid',
_id: 'objectId',
name: 'string',
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
import React, {useEffect} from 'react';
// get realm context from createRealmContext()
import {RealmContext} from '../RealmConfig';
import {Text, FlatList} from 'react-native';

const {useRealm} = RealmContext;
import {useRealm, useQuery} from '@realm/react';

function SubscriptionManager() {
const realm = useRealm();
const seenBirds = realm.objects('Bird').filtered('haveSeen == true');
const seenBirds = useQuery(Bird, birds => {
return birds.filtered('haveSeen == true');
});

useEffect(() => {
realm.subscriptions.update((mutableSubs, realm) => {
// Create subscription query
const seenBirdsSubQuery = realm
.objects('Bird')
.filtered('haveSeen == true');
realm.subscriptions.update(
(mutableSubs: Realm.App.Sync.MutableSubscriptionSet) => {

// Create subscription for filtered results.
mutableSubs.add(seenBirdsSubQuery, {name: 'seenBirds'});
});
// Create subscription for filtered collection.
mutableSubs.add(seenBirds, {name: 'seenBirds'});
},
);
});

return (
<FlatList
data={seenBirds}
keyExtractor={bird => bird._id.toString()}
renderItem={({item}) => <Text>{item._id}</Text>}
keyExtractor={item => item._id.toString()}
renderItem={({item}) => <Text>{item._id.toString()}</Text>}
/>
);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import React, {useEffect} from 'react';
import {Text, View} from 'react-native';
// get realm context from createRealmContext()
import {RealmContext} from '../RealmConfig';

const {useRealm, useQuery} = RealmContext;
import {useRealm, useQuery} from '@realm/react';
import {Bird} from '../Models/Bird';

function SubscriptionManager() {
const realm = useRealm();
const seenBirds = useQuery(Bird, birds => {
return birds.filtered('haveSeen == true');
});

useEffect(() => {
realm.subscriptions.update((mutableSubs, realm) => {
// Create subscription for filtered results.
mutableSubs.add(realm.objects('Bird').filtered('haveSeen == true'));
});
realm.subscriptions.update(
(mutableSubs: Realm.App.Sync.MutableSubscriptionSet) => {

// Create subscription for filtered collection.
mutableSubs.add(seenBirds, {name: 'seenBirds'});
},
);
});

// Returns state of all subscriptions, not individual subscriptions.
Expand All @@ -22,7 +26,9 @@ function SubscriptionManager() {

return (
<View>
<Text>Status of all subscriptions: {allSubscriptionState}</Text>
<Text >
Status of all subscriptions: {allSubscriptionState}
</Text>
</View>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const DogList = () => {
const myDogs = useQuery(Dog);

const deleteAllYoungDogObjects = () => {
const youngDogs = myDogs.filtered('age < 3');
const youngDogs = useQuery(Dog, dogs => {
return dogs.filtered('age < 3');
});
realm.write(() => {
realm.delete(youngDogs);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@

const HomeInfo = ({homeOwnerName}: {homeOwnerName: string}) => {
const realm = useRealm();
const homeOwner = useQuery(HomeOwner).filtered(
`name == '${homeOwnerName}'`,
const homeOwner = useQuery(
HomeOwner,
homeOwners => {
return homeOwners.filtered(`name == '${homeOwnerName}'`);
},
[homeOwnerName],
)[0];

const deleteExtraHomeInfo = () => {
Expand Down
Loading

0 comments on commit 2cd921b

Please sign in to comment.