Skip to content

Commit

Permalink
Upgrade to 12.9.0 for collections in mixed support
Browse files Browse the repository at this point in the history
  • Loading branch information
cbullinger committed May 23, 2024
1 parent 78d4535 commit 8180c37
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 126 deletions.
2 changes: 2 additions & 0 deletions examples/node/v12/__tests__/models/rql-data-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class Project extends Realm.Object<Project> {
quota?: number;
comments?: Realm.Dictionary<string>;
projectLocation?: Office;
additionalInfo!: Realm.Mixed;

static schema: ObjectSchema = {
name: "Project",
Expand All @@ -74,6 +75,7 @@ export class Project extends Realm.Object<Project> {
quota: "int?",
comments: "string?{}",
projectLocation: "Office?",
additionalInfo: "mixed",
},
primaryKey: "_id",
};
Expand Down
20 changes: 17 additions & 3 deletions examples/node/v12/__tests__/realm-query-language.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ describe("Realm Query Language Reference", () => {
},
],
quota: 1, // doesn't meet quota
comments: { status: "Behind schedule", projectNumber: "70150" },
comments: {
status: "Behind schedule",
projectNumber: 70150,
budget: 5000.0,
customerContact: ["Mina", "Devon"],
},
projectLocation: mainBranch,
});
const project = realm.create(Project, {
Expand Down Expand Up @@ -100,7 +105,11 @@ describe("Realm Query Language Reference", () => {
},
],
quota: 1, // meets quota
comments: { status: "Ahead of schedule", projectNumber: "70187" },
comments: {
status: "Ahead of schedule",
projectNumber: 70187,
startDate: new Date("2021-01-01"),
},
projectLocation: mainBranch,
});

Expand All @@ -120,7 +129,12 @@ describe("Realm Query Language Reference", () => {
},
],
quota: 11, // doesn't meet quota
comments: { status: "On track", projectNumber: "N/A" },
comments: {
status: "On track",
projectNumber: 4444,
startDate: new Date("2021-02-01"),
budget: 10000.0,
},
projectLocation: austinBranch,
});
});
Expand Down
138 changes: 76 additions & 62 deletions examples/node/v12/__tests__/realm-query-language.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ describe("Realm Query Language Reference", () => {
quota: 1, // doesn't meet quota
comments: { status: "Behind schedule", projectNumber: "70150" },
projectLocation: mainBranch,
// Mixed property is of type dictionary of mixed values
// (date, list of strings, bool, and int)
additionalInfo: {
startDate: new Date("2021-01-01"),
customerContacts: ["Alice", "Bob"],
recurringCustomer: true,
budget: 10000,
},
});
const project = realm.create("Project", {
_id: new BSON.ObjectId(),
Expand Down Expand Up @@ -101,6 +109,8 @@ describe("Realm Query Language Reference", () => {
quota: 1, // meets quota
comments: { status: "Ahead of schedule", projectNumber: "70187" },
projectLocation: mainBranch,
// Mixed property is of type string
additionalInfo: "Customer is a VIP.",
});

realm.create("Project", {
Expand All @@ -121,6 +131,16 @@ describe("Realm Query Language Reference", () => {
quota: 11, // doesn't meet quota
comments: { status: "On track", projectNumber: "N/A" },
projectLocation: austinBranch,
// Mixed property is of type list, containing string and nested
// dictionary of mixed values (date, boolean, and int)
additionalInfo: [
"Customer is difficult to work with.",
{
startDate: new Date("2021-03-01"),
recurringCustomer: false,
budget: 10000,
},
],
});
});
});
Expand Down Expand Up @@ -470,31 +490,49 @@ describe("Realm Query Language Reference", () => {
const items = realm.objects(Item);
const projects = realm.objects(Project);

const sortedUniqueAliItems = items.filtered(
const sortedItems = items.filtered(
// :snippet-start: sort-distinct-limit
"assignee == 'Ali' SORT(priority DESC) DISTINCT(name) LIMIT(5)"
// :snippet-end:
// Find incomplete items, sort by `priority`
// in descending order, then sort equal `priority`
// values by `progressMinutes` in ascending order.
"isComplete == false SORT(priority DESC, progressMinutes ASC)"
// :remove-start:
);
expect(sortedItems[0].name).toBe("Demo template app");
const distinctItems = items.filtered(
// :remove-end:

expect(sortedUniqueAliItems.length).toBe(1);
const query =
// Find high priority items, then remove from the results
// any items with duplicate `name` AND `assignee` values.
"priority >= 5 DISTINCT(name, assignee)"
// :remove-start:
);
expect(distinctItems.length).toBe(6);
const limitItems = items.filtered(
// :remove-end:
// Find in-progress items, then return the first 10 results.
"progressMinutes > 0 && isComplete != true LIMIT(10)"
// :snippet-end:
);
expect(limitItems[0].name).toBe("Write tests");
const sortFirst = items.filtered(
// :snippet-start: sort-distinct-limit-order-matters
"SORT(priority DESC) LIMIT(2) DISTINCT(name)";
// Returns 2 items with the highest priority
// :remove-start:
const orderMatters = items.filtered(
"isComplete != true SORT(priority DESC) LIMIT(2) DISTINCT(name)"
// 1. Sorts by highest priority.
// 2. Returns the first item.
// 3. Remove duplicate names (N/A because a single
// item is always considered distinct).
"assignee == null SORT(priority ASC) LIMIT(1) DISTINCT(name)"
// :remove-start:
);
expect(orderMatters.length).toBe(2);
const query2 =
const limitLast = items.filtered(
// :remove-end:
"isComplete != true DISTINCT(name) SORT(priority DESC) LIMIT(2)";
// Returns the first 2 uniquely named items with the highest priority
// :snippet-end:
const limitFirst = items.filtered(
"isComplete != true DISTINCT(name) SORT(priority DESC) LIMIT(2)"

// 1. Removes any duplicates by name.
// 2. Sorts by highest priority.
// 3. Returns the first item.
"assignee == null DISTINCT(name) SORT(priority ASC) LIMIT(1)"
// :snippet-end:
);
console.log(limitFirst.toJSON());
});

test("Subquery queries", () => {
Expand Down Expand Up @@ -672,58 +710,34 @@ describe("Realm Query Language Reference", () => {
});
});

describe("Type operator", () => {
// Uses a test-specific schema with mixed type
// TODO: Update main schema with mixed type property once collections-in-mixed is supported
const Mixed = {
name: "Mixed",
properties: { name: "string", mixedType: "mixed" },
};
let realm: Realm;
const path = "mixed.realm";

// Add, then delete objects for this test
beforeEach(async () => {
realm = await Realm.open({
schema: [Mixed],
path,
});
realm.write(() => {
realm.create("Mixed", {
name: "Marge",
mixedType: true,
});
realm.create("Mixed", {
name: "Lisa",
mixedType: 22,
});
realm.create("Mixed", {
name: "Bart",
mixedType: "carrumba",
});
});
});

afterEach(() => {
realm.close();
Realm.deleteFile({ path });
});

describe("Type operators", () => {
test("Type operator", () => {
const mixed = realm.objects("Mixed");
const mixedString = mixed.filtered(
const projects = realm.objects(Project);
const mixedString = projects.filtered(
// :snippet-start: type-operator
"mixedType.@type == 'string'"
// Find projects with an `additionalInfo` property of
// string type.
"additionalInfo.@type == 'string'"
// :remove-start:
);
const mixedCollection = projects.filtered(
// :remove-end:
// Find projects with an `additionalInfo` property of
// `collection` type, which matches list or dictionary types.
"additionalInfo.@type == 'collection'"
// :remove-start:
);
const mixedBool = mixed.filtered(
const mixedBool = projects.filtered(
// :remove-end:

"mixedType.@type == 'bool'"
// Find projects with an `additionalInfo` property of
// list type, where any list element is of type 'bool'.
"additionalInfo[*].@type == 'bool'"
// :snippet-end:
);
expect(mixedString.length).toBe(1);
expect(mixedBool.length).toBe(1);
console.debug(mixedString[1].name + " and " + mixedBool[0].name);
// expect(mixedString.length).toBe(1);
// expect(mixedBool.length).toBe(1);
});
});

Expand Down
14 changes: 7 additions & 7 deletions examples/node/v12/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/node/v12/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"type": "module",
"scripts": {
"test": "jest --runInBand --detectOpenHandles --forceExit",
"test": "jest --runInBand --detectOpenHandles --silent=false --forceExit",
"posttest": "npm run delete-realm-files",
"test:js": "jest --selectProjects JavaScript --runInBand --detectOpenHandles --forceExit; npm run delete-realm-files",
"test:ts": "NODE_OPTIONS=--experimental-vm-modules jest --selectProjects TypeScript --runInBand --detectOpenHandles --forceExit; npm run delete-realm-files",
Expand All @@ -15,7 +15,7 @@
"license": "ISC",
"dependencies": {
"fs-extra": "^11.1.1",
"realm": "^12.8.0"
"realm": "^12.9.0"
},
"devDependencies": {
"@babel/core": "^7.21.8",
Expand Down
71 changes: 71 additions & 0 deletions source/includes/sdk-examples/query/rql-example-data-model.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
.. tabs-drivers::

tabs:
- id: cpp
content: |

.. literalinclude:: /examples/generated/cpp/asymmetric-sync.snippet.create-asymmetric-object.cpp
:language: cpp

- id: csharp
content: |

.. literalinclude:: /examples/generated/dotnet/RqlSchemaExamples.snippet.rql-schema-examples.cs
:language: csharp

- id: dart
content: |

.. literalinclude:: /examples/generated/flutter/task_project_models_test.snippet.task-project-models.dart
:language: dart

- id: java
content: |

.. code-block:: java
public class Item extends RealmObject {
ObjectId id = new ObjectId();
String name;
Boolean isComplete = false;
String assignee;
Integer priority = 0;
Integer progressMinutes = 0;
@LinkingObjects("items")
final RealmResults<Project> projects = null;
}
public class Project extends RealmObject {
ObjectId id = new ObjectId();
String name;
RealmList<Item> items;
Integer quota = null;
}
- id: javascript
content: |

.. literalinclude:: /examples/generated/node/rql-data-models.snippet.rql-data-models.js
:language: javascript

- id: kotlin
content: |

.. literalinclude:: /examples/generated/kotlin/RQLTest.snippet.rql-schema-example.kt
:language: kotlin

- id: objectivec
content: |

.. literalinclude:: /examples/MissingPlaceholders/api.m
:language: objectivec

- id: swift
content: |

.. literalinclude:: /examples/MissingPlaceholders/api.swift
:language: swift

- id: typescript
content: |

.. include:: /examples/generated/node/v12/formatted/rql-data-models.snippet.rql-data-models.ts.rst
Loading

0 comments on commit 8180c37

Please sign in to comment.