|
| 1 | +import Realm from "realm"; |
| 2 | + |
| 3 | +describe("Node.js Data Types", () => { |
| 4 | + test("should work with Mixed Type", async () => { |
| 5 | + // :code-block-start: define-mixed-in-schema |
| 6 | + const DogSchema = { |
| 7 | + name: "Dog", |
| 8 | + properties: { |
| 9 | + name: "string", |
| 10 | + birthDate: "mixed", |
| 11 | + }, |
| 12 | + }; |
| 13 | + // :code-block-end: |
| 14 | + |
| 15 | + const realm = await Realm.open({ |
| 16 | + schema: [DogSchema], |
| 17 | + }); |
| 18 | + |
| 19 | + // :code-block-start: create-objects-with-mixed-values |
| 20 | + realm.write(() => { |
| 21 | + // create a Dog with a birthDate value of type string |
| 22 | + realm.create("Dog", { name: "Euler", birthDate: "December 25th, 2017" }); |
| 23 | + |
| 24 | + // create a Dog with a birthDate value of type date |
| 25 | + realm.create("Dog", { |
| 26 | + name: "Blaise", |
| 27 | + birthDate: new Date("August 17, 2020"), |
| 28 | + }); |
| 29 | + // create a Dog with a birthDate value of type int |
| 30 | + realm.create("Dog", { |
| 31 | + name: "Euclid", |
| 32 | + birthDate: 10152021, |
| 33 | + }); |
| 34 | + // create a Dog with a birthDate value of type null |
| 35 | + realm.create("Dog", { |
| 36 | + name: "Pythagoras", |
| 37 | + birthDate: null, |
| 38 | + }); |
| 39 | + }); |
| 40 | + // :code-block-end: |
| 41 | + |
| 42 | + // :code-block-start: query-objects-with-mixed-values |
| 43 | + // To query for Blaise's birthDate, filter for his name to retrieve the realm object. |
| 44 | + // Use dot notation to access the birthDate property. |
| 45 | + let blaiseBirthDate = realm.objects("Dog").filtered(`name = 'Blaise'`)[0] |
| 46 | + .birthDate; |
| 47 | + console.log(`Blaise's birth date is ${blaiseBirthDate}`); |
| 48 | + // :code-block-end: |
| 49 | + expect(blaiseBirthDate).toEqual(new Date("August 17, 2020")); |
| 50 | + |
| 51 | + // delete the objects specifically created in this test to keep tests idempotent |
| 52 | + const Euler = realm.objects("Dog").filtered(`name = 'Euler'`)[0]; |
| 53 | + const Blaise = realm.objects("Dog").filtered(`name = 'Blaise'`)[0]; |
| 54 | + const Euclid = realm.objects("Dog").filtered(`name = 'Euclid'`)[0]; |
| 55 | + const Pythagoras = realm.objects("Dog").filtered(`name = 'Pythagoras'`)[0]; |
| 56 | + realm.write(() => { |
| 57 | + realm.delete(Euler); |
| 58 | + realm.delete(Blaise); |
| 59 | + realm.delete(Euclid); |
| 60 | + realm.delete(Pythagoras); |
| 61 | + }); |
| 62 | + }); |
| 63 | +}); |
0 commit comments