Skip to content

Commit

Permalink
BSON exported (#3363)
Browse files Browse the repository at this point in the history
* Exporting BSON

* Adding some jsdoc

* Trying to fix the types

* Updated the bson package

* Fixing the tests

* Restoring https and sha512 in the package-lock.json

* Creating a BSON unit test

* Update playlist-with-songs-with-ids.ts

The project uses the esModuleInterop option

* Update playlist-with-songs.ts

The project uses the esModuleInterop option

* Adopting code to "esModuleInterop"
  • Loading branch information
kraenhansen authored Jan 11, 2021
1 parent e5549d3 commit 724388b
Show file tree
Hide file tree
Showing 34 changed files with 221 additions and 182 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ NOTE: This version uses the Realm file format to version 20. It is not possible
* None

### Enhancements
* None
* Added an export of the `bson` module on the package, making it possible to access the BSON types via `import Realm from "realm";` followed by `Realm.BSON.ObjectId`, `Realm.BSON.Decimal128`, `Realm.BSON.Binary` etc. ([#3363](https://github.com/realm/realm-js/pull/3363))

### Fixed
* Fixed RN Android error: couldn't find DSO to load: librealmreact.so caused by: dlopen failed: cannot locate symbol. ([#3347](https://github.com/realm/realm-js/issues/3347), since v10.0.0)
Expand Down
25 changes: 25 additions & 0 deletions docs/bson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

/**
* A re-export of the "bson" package, enabling access to the BSON types without requiring an explict dependency on the "bson" package.
*
* @see {@link https://www.npmjs.com/package/bson#documentation|the BSON documentation} for more information.
* @memberof Realm
*/
const BSON = {};
99 changes: 51 additions & 48 deletions integration-tests/tests/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 integration-tests/tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"realm": "*"
},
"devDependencies": {
"@types/bson": "^4.0.2",
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.6",
"fs-extra": "^7.0.1",
Expand All @@ -26,6 +25,7 @@
"typescript": "^3.8.2"
},
"dependencies": {
"bson": "^4.2.0",
"chai": "^4.2.0"
},
"files": [
Expand Down
11 changes: 11 additions & 0 deletions integration-tests/tests/src/bson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect } from "chai";

describe("BSON", () => {
it("gets exported", () => {
expect(typeof Realm.BSON).equals("object");
expect(typeof Realm.BSON.ObjectId).equals("function");
expect(typeof Realm.BSON.Decimal128).equals("function");
expect(typeof Realm.BSON.Binary).equals("function");
expect(typeof Realm.BSON.EJSON).equals("object");
});
});
1 change: 1 addition & 0 deletions integration-tests/tests/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe(global.title, () => {
require("./objects");
require("./iterators");
require("./dynamic-schema-updates");
require("./bson");
});

beforeEach(() => {
Expand Down
9 changes: 4 additions & 5 deletions integration-tests/tests/src/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Person as PersonWithId,
PersonSchema as PersonSchemaWithId,
} from "./schemas/person-and-dog-with-object-ids";
import { ObjectId } from "bson";

describe("Realm objects", () => {
describe("Interface & object literal", () => {
Expand Down Expand Up @@ -45,7 +44,7 @@ describe("Realm objects", () => {

it("can be fetched with objectForPrimaryKey", () => {
const realm = new Realm({ schema: [PersonSchemaWithId] });
const _id = new ObjectId();
const _id = new Realm.BSON.ObjectId();

realm.write(() => {
realm.create<PersonWithId>(PersonSchemaWithId.name, {
Expand All @@ -69,7 +68,7 @@ describe("Realm objects", () => {
it("can be updated", () => {
const realm = new Realm({ schema: [PersonSchemaWithId] });
let john: IPersonWithId;
const _id = new ObjectId();
const _id = new Realm.BSON.ObjectId();

realm.write(() => {
john = realm.create<IPersonWithId>(PersonSchemaWithId.name, {
Expand Down Expand Up @@ -164,7 +163,7 @@ describe("Realm objects", () => {

it("can be fetched with objectForPrimaryKey", () => {
const realm = new Realm({ schema: [PersonWithId] });
const _id = new ObjectId();
const _id = new Realm.BSON.ObjectId();

realm.write(() => {
realm.create(PersonWithId, {
Expand All @@ -185,7 +184,7 @@ describe("Realm objects", () => {
it("can be updated", () => {
const realm = new Realm({ schema: [PersonWithId] });
let john: PersonWithId;
const _id = new ObjectId();
const _id = new Realm.BSON.ObjectId();

realm.write(() => {
john = realm.create(PersonWithId, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/* tslint:disable max-classes-per-file */

import * as Realm from "realm";
import Realm from "realm";
import { ObjectId } from "bson";

export interface IPerson {
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/schemas/person-and-dogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/* tslint:disable max-classes-per-file */

import * as Realm from "realm";
import Realm from "realm";

export interface IPerson {
name: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////

import * as Realm from "realm";
import Realm from "realm";

/* tslint:disable max-classes-per-file */

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/src/schemas/playlist-with-songs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////

import * as Realm from "realm";
import Realm from "realm";

/* tslint:disable max-classes-per-file */

Expand Down
7 changes: 3 additions & 4 deletions integration-tests/tests/src/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";
import { ObjectId } from "bson";
import {
IPlaylist as IPlaylistNoId,
ISong as ISongNoId,
Expand All @@ -34,8 +33,8 @@ import {
Playlist as PlaylistWithId,
Song as SongWithId,
} from "./schemas/playlist-with-songs-with-ids";
import * as circularCollectionResult from "./structures/circular-collection-result.json";
import * as circularCollectionResultWithIds from "./structures/circular-collection-result-with-primary-ids.json";
import circularCollectionResult from "./structures/circular-collection-result.json";
import circularCollectionResultWithIds from "./structures/circular-collection-result-with-primary-ids.json";

describe("JSON serialization (exposed properties)", () => {
it("JsonSerializationReplacer is exposed on the Realm constructor", () => {
Expand Down Expand Up @@ -439,7 +438,7 @@ const cacheIdTestSetups: ICacheIdTestSetup[] = [
{
type: "objectId",
schemaName: "ObjectIdTest",
testId: new ObjectId("5f99418846da9c45005f50bf"),
testId: new Realm.BSON.ObjectId("5f99418846da9c45005f50bf"),
expectedResult: "ObjectIdTest#5f99418846da9c45005f50bf",
},
];
Expand Down
8 changes: 3 additions & 5 deletions integration-tests/tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
"target": "es2018",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"resolveJsonModule": true,
"outDir": "dist",
"types": [
"node",
"realm",
"bson",
"buffer",
"mocha",
"chai"
],
"typeRoots": [
"types",
"node_modules/@types"
],
"noImplicitAny": true
},
"include": [
Expand Down
5 changes: 0 additions & 5 deletions integration-tests/tests/types/node/index.d.ts

This file was deleted.

7 changes: 4 additions & 3 deletions lib/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ module.exports = function(realmConstructor, context) {
setConstructorOnPrototype(realmConstructor.Results);
setConstructorOnPrototype(realmConstructor.Object);

realmConstructor._bson = require('bson');
realmConstructor._Decimal128 = realmConstructor._bson.Decimal128;
realmConstructor._ObjectId = realmConstructor._bson.ObjectId;
realmConstructor.BSON = require('bson');
realmConstructor._Decimal128 = realmConstructor.BSON.Decimal128;
realmConstructor._ObjectId = realmConstructor.BSON.ObjectId;

const { DefaultNetworkTransport } = require('realm-network-transport');
realmConstructor._networkTransport = new DefaultNetworkTransport();

Expand Down
Loading

0 comments on commit 724388b

Please sign in to comment.