Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOCSP 40401 Node.js counter page #3313

Merged
merged 19 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions examples/node/v12/__tests__/data-types.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import Realm, { BSON, Counter, ObjectSchema, UpdateMode } from "realm";

// :snippet-start:declare-counter-schema
class myClass extends Realm.Object {
_id!;
myCounter!;
nullableCounter;

static schema = {
name: "myClass",
primaryKey: "_id",
properties: {
_id: { type: "objectId", default: () => new BSON.ObjectId() },
myCounter: { type: "int", presentation: "counter" },
// or myCounter: "counter"
nullableCounter: { type: "int", presentation: "counter", optional: true },
// or nullableCounter: "counter?"
},
}
};
// :snippet-end:

const realm = await Realm.open({
schema: [myClass],
});

describe("Counter Updates", () => {
test("testing normal methods", async () => {

// :snippet-start:initialize-counter
const myObject = realm.write(() => {
return realm.create( myClass, { myCounter: 0 } );
});
// :snippet-end:

realm.write(() => {
// :snippet-start:update-counter
myObject.myCounter.increment();
myObject.myCounter.value; // 1
expect(myObject.myCounter.value).toBe(1) // :remove:
myObject.myCounter.increment(2);
myObject.myCounter.value; // 3
expect(myObject.myCounter.value).toBe(3) // :remove:
myObject.myCounter.decrement(2);
myObject.myCounter.value; // 1
expect(myObject.myCounter.value).toBe(1) // :remove:
myObject.myCounter.increment(-2);
myObject.myCounter.value; // -1
expect(myObject.myCounter.value).toBe(-1) // :remove:
myObject.myCounter.set(0); // reset counter value to 0
// :snippet-end:
expect(myObject.myCounter.value).toBe(0)
});

realm.close()
});

test("testing nullability switching", async () => {

// :snippet-start:null-updating
const myObject = realm.write(() => {
return realm.create( myClass, { nullableCounter: 0 } );
});

const myID = myObject._id
expect(myObject.nullableCounter?.value).toBe(0) // :remove:

realm.write(() => {
realm.create( myClass, { _id: myID, nullableCounter: null }, UpdateMode.Modified );
});
expect(myObject.nullableCounter).toBe(null) // :remove:

realm.write(() => {
realm.create( myClass, { _id: myID, nullableCounter: 0 }, UpdateMode.Modified );
});
// :snippet-end:

expect(myObject.nullableCounter?.value).toBe(0)

});

test("testing filtering", async () => {

// :snippet-start:filtering-with-counter
const belowThreshold = realm.write(() => {
return realm.create( myClass, { myCounter: 0 } );
});

const atThreshold = realm.write(() => {
return realm.create( myClass, { myCounter: 1 } );
});

const aboveThreshold = realm.write(() => {
return realm.create( myClass, { myCounter: 2 } );
});

const allObjects = realm.objects('myClass');

let filteredObjects = allObjects.filtered('counter >= $0', atThreshold.myCounter.value);
// :snippet-end:

let unfilteredExpected = [belowThreshold, atThreshold, aboveThreshold]
let filteredExpected = [atThreshold, aboveThreshold]

expect(JSON.stringify(allObjects)).toMatch(JSON.stringify(unfilteredExpected));
expect(JSON.stringify(filteredObjects)).toMatch(JSON.stringify(filteredExpected));

});
});
109 changes: 109 additions & 0 deletions examples/node/v12/__tests__/data-types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import Realm, { BSON, Counter, ObjectSchema, UpdateMode } from "realm";

// :snippet-start:declare-counter-schema
class myClass extends Realm.Object<myClass> {
_id!: BSON.ObjectId;
myCounter!: Counter;
nullableCounter?: Counter | null;

static schema: ObjectSchema = {
name: "myClass",
primaryKey: "_id",
properties: {
_id: { type: "objectId", default: () => new BSON.ObjectId() },
myCounter: { type: "int", presentation: "counter" },
// or myCounter: "counter"
nullableCounter: { type: "int", presentation: "counter", optional: true },
// or nullableCounter: "counter?"
},
}
};
// :snippet-end:

const realm = await Realm.open({
schema: [myClass],
});

describe("Counter Updates", () => {
osharaf-mdb marked this conversation as resolved.
Show resolved Hide resolved
test("testing normal methods", async () => {

// :snippet-start:initialize-counter
const myObject = realm.write(() => {
return realm.create( myClass, { myCounter: 0 } );
});
// :snippet-end:

realm.write(() => {
// :snippet-start:update-counter
myObject.myCounter.increment();
myObject.myCounter.value; // 1
expect(myObject.myCounter.value).toBe(1) // :remove:
myObject.myCounter.increment(2);
myObject.myCounter.value; // 3
expect(myObject.myCounter.value).toBe(3) // :remove:
myObject.myCounter.decrement(2);
myObject.myCounter.value; // 1
expect(myObject.myCounter.value).toBe(1) // :remove:
myObject.myCounter.increment(-2);
myObject.myCounter.value; // -1
expect(myObject.myCounter.value).toBe(-1) // :remove:
myObject.myCounter.set(0); // reset counter value to 0
// :snippet-end:
expect(myObject.myCounter.value).toBe(0)
});

realm.close()
});

test("testing nullability switching", async () => {

// :snippet-start:null-updating
const myObject = realm.write(() => {
return realm.create( myClass, { nullableCounter: 0 } );
});

const myID = myObject._id
expect(myObject.nullableCounter?.value).toBe(0) // :remove:

realm.write(() => {
realm.create( myClass, { _id: myID, nullableCounter: null }, UpdateMode.Modified );
});
expect(myObject.nullableCounter).toBe(null) // :remove:

realm.write(() => {
realm.create( myClass, { _id: myID, nullableCounter: 0 }, UpdateMode.Modified );
});
// :snippet-end:

expect(myObject.nullableCounter?.value).toBe(0)

});

test("testing filtering", async () => {

// :snippet-start:filtering-with-counter
cbullinger marked this conversation as resolved.
Show resolved Hide resolved
const belowThreshold = realm.write(() => {
return realm.create( myClass, { myCounter: 0 } );
});

const atThreshold = realm.write(() => {
return realm.create( myClass, { myCounter: 1 } );
});

const aboveThreshold = realm.write(() => {
return realm.create( myClass, { myCounter: 2 } );
});

const allObjects = realm.objects('myClass');

let filteredObjects = allObjects.filtered('counter >= $0', atThreshold.myCounter.value);
// :snippet-end:

let unfilteredExpected = [belowThreshold, atThreshold, aboveThreshold]
let filteredExpected = [atThreshold, aboveThreshold]

expect(JSON.stringify(allObjects)).toMatch(JSON.stringify(unfilteredExpected));
expect(JSON.stringify(filteredObjects)).toMatch(JSON.stringify(filteredExpected));

});
});
15 changes: 8 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.

2 changes: 1 addition & 1 deletion examples/node/v12/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"license": "ISC",
"dependencies": {
"fs-extra": "^11.1.1",
"realm": "^12.8.0"
"realm": "^12.11.0"
},
"devDependencies": {
"@babel/core": "^7.21.8",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class myClass extends Realm.Object {
_id!;
myCounter!;
nullableCounter;

static schema = {
name: "myClass",
primaryKey: "_id",
properties: {
_id: { type: "objectId", default: () => new BSON.ObjectId() },
myCounter: { type: "int", presentation: "counter" },
// or myCounter: "counter"
nullableCounter: { type: "int", presentation: "counter", optional: true },
// or nullableCounter: "counter?"
},
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class myClass extends Realm.Object<myClass> {
_id!: BSON.ObjectId;
myCounter!: Counter;
nullableCounter?: Counter | null;

static schema: ObjectSchema = {
name: "myClass",
primaryKey: "_id",
properties: {
_id: { type: "objectId", default: () => new BSON.ObjectId() },
myCounter: { type: "int", presentation: "counter" },
// or myCounter: "counter"
nullableCounter: { type: "int", presentation: "counter", optional: true },
// or nullableCounter: "counter?"
},
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const belowThreshold = realm.write(() => {
return realm.create( myClass, { myCounter: 0 } );
});

const atThreshold = realm.write(() => {
return realm.create( myClass, { myCounter: 1 } );
});

const aboveThreshold = realm.write(() => {
return realm.create( myClass, { myCounter: 2 } );
});

const allObjects = realm.objects('myClass');

let filteredObjects = allObjects.filtered('counter >= $0', atThreshold.myCounter.value);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const belowThreshold = realm.write(() => {
return realm.create( myClass, { myCounter: 0 } );
});

const atThreshold = realm.write(() => {
return realm.create( myClass, { myCounter: 1 } );
});

const aboveThreshold = realm.write(() => {
return realm.create( myClass, { myCounter: 2 } );
});

const allObjects = realm.objects('myClass');

let filteredObjects = allObjects.filtered('counter >= $0', atThreshold.myCounter.value);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const myObject = realm.write(() => {
return realm.create( myClass, { myCounter: 0 } );
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const myObject = realm.write(() => {
return realm.create( myClass, { myCounter: 0 } );
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const myObject = realm.write(() => {
return realm.create( myClass, { nullableCounter: 0 } );
});

const myID = myObject._id

realm.write(() => {
realm.create( myClass, { _id: myID, nullableCounter: null }, UpdateMode.Modified );
});

realm.write(() => {
realm.create( myClass, { _id: myID, nullableCounter: 0 }, UpdateMode.Modified );
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const myObject = realm.write(() => {
return realm.create( myClass, { nullableCounter: 0 } );
});

const myID = myObject._id

realm.write(() => {
realm.create( myClass, { _id: myID, nullableCounter: null }, UpdateMode.Modified );
});

realm.write(() => {
realm.create( myClass, { _id: myID, nullableCounter: 0 }, UpdateMode.Modified );
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
myObject.myCounter.increment();
myObject.myCounter.value; // 1
myObject.myCounter.increment(2);
myObject.myCounter.value; // 3
myObject.myCounter.decrement(2);
myObject.myCounter.value; // 1
myObject.myCounter.increment(-2);
myObject.myCounter.value; // -1
myObject.myCounter.set(0); // reset counter value to 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
myObject.myCounter.increment();
myObject.myCounter.value; // 1
myObject.myCounter.increment(2);
myObject.myCounter.value; // 3
myObject.myCounter.decrement(2);
myObject.myCounter.value; // 1
myObject.myCounter.increment(-2);
myObject.myCounter.value; // -1
myObject.myCounter.set(0); // reset counter value to 0
Loading
Loading