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

Realm Web: App "services" and "functions" removed #3298

Merged
merged 1 commit into from
Oct 6, 2020
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
8 changes: 1 addition & 7 deletions packages/realm-web-integration-tests/src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@

import { expect } from "chai";

import {
App,
Credentials,
User,
LocalStorage,
getEnvironment,
} from "realm-web";
import { App, Credentials, User, getEnvironment } from "realm-web";

import { createApp } from "./utils";

Expand Down
4 changes: 2 additions & 2 deletions packages/realm-web-integration-tests/src/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ describe("Realm.FunctionsFactory", () => {
const app = createApp<TranslateFunctions>();
// Login a user
const credentials = Credentials.anonymous();
await app.logIn(credentials);
const user = await app.logIn(credentials);
// Call a function
const response = await app.functions.translate("hello", "en_fr");
const response = await user.functions.translate("hello", "en_fr");
expect(response).to.equal("bonjour");
});
});
4 changes: 4 additions & 0 deletions packages/realm-web-integration-tests/src/http-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
//
////////////////////////////////////////////////////////////////////////////

// TODO: Re-enable if the HTTP service gets reintroduced to the API.

/*
import { expect } from "chai";
import { Credentials } from "realm-web";

Expand Down Expand Up @@ -43,3 +46,4 @@ describe("HTTP", () => {
expect(body.name).equals("realm-js");
});
});
*/
4 changes: 2 additions & 2 deletions packages/realm-web-integration-tests/src/iife.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ describeIf(typeof window === "object", "IIFE bundle", () => {
const app = new Realm.App({ id: APP_ID, baseUrl: BASE_URL });
// Authenticate
const credentials = Realm.Credentials.anonymous();
await app.logIn(credentials);
const user = await app.logIn(credentials);
// Call a function
const response = await app.functions.translate("hello", "en_fr");
const response = await user.functions.translate("hello", "en_fr");
expect(response).to.equal("bonjour");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,22 @@ describe("Remote MongoDB", () => {
await app.logIn(credentials);
});

function getCollection() {
const mongodb = app.services.mongodb("local-mongodb");
const db = mongodb.db("test-database");
return db.collection<TestDocument>("test-collection");
function getCollection<T extends Realm.Services.MongoDB.Document>() {
if (app.currentUser) {
const mongodb = app.currentUser.mongoClient("local-mongodb");
const db = mongodb.db("test-database");
return db.collection<T>("test-collection");
} else {
throw new Error("Expected an authenticated user");
}
}

let runId: number;
beforeEach(async () => {
// Genereate a collection
runId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
// Insert a couple of documents
const collection = getCollection();
const collection = getCollection<TestDocument>();
await collection.insertMany([
{
runId,
Expand All @@ -71,7 +75,7 @@ describe("Remote MongoDB", () => {
});

it("can find documents", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
const result = await collection.find(
{ name: { $exists: true } },
{ limit: 10 },
Expand All @@ -84,15 +88,15 @@ describe("Remote MongoDB", () => {
});

it("returns null when finding no document", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
const result = await collection.findOne({
whatever: "non-existent",
});
expect(result).equals(null);
});

it("can find a single documents, with projection", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
const result = await collection.findOne(
{ runId, name: "Bob" },
{ projection: { name: 1 } },
Expand All @@ -108,7 +112,7 @@ describe("Remote MongoDB", () => {
});

it("can upsert a document if no one is found", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
const result = await collection.findOneAndUpdate(
{ runId, name: "Nobody" },
{ name: "Dennis", hiddenField: "a hidden value" },
Expand All @@ -125,7 +129,7 @@ describe("Remote MongoDB", () => {
});

it("can find and update a document, with projection", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
const result = await collection.findOneAndUpdate(
{ runId, name: "Bob" },
{ name: "Bobby" },
Expand All @@ -142,7 +146,7 @@ describe("Remote MongoDB", () => {
});

it("can find and replace a document, with projection", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
const result = await collection.findOneAndReplace(
{ runId, name: "Alice" },
{ runId, name: "Alison" },
Expand All @@ -163,7 +167,7 @@ describe("Remote MongoDB", () => {
});

it("can find and delete a document", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
const countBefore = await collection.count({ runId });
// Delete the document with the last name (Charlie)
const result = await collection.findOneAndDelete(
Expand All @@ -185,7 +189,7 @@ describe("Remote MongoDB", () => {
});

it("can aggregate", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
const result = await collection.aggregate([
{ $match: { runId } },
{ $group: { _id: null, names: { $push: "$name" } } },
Expand All @@ -195,7 +199,7 @@ describe("Remote MongoDB", () => {
});

it("can count documents, insert a document and count & retrieve it again", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
// Determine the number of documents before insertion
const countBefore = await collection.count();
// Insert a document
Expand All @@ -221,7 +225,7 @@ describe("Remote MongoDB", () => {
});

it("can insert many documents", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
// Determine the number of documents before insertion
const countBefore = await collection.count();
// Insert a document
Expand All @@ -240,7 +244,7 @@ describe("Remote MongoDB", () => {
});

it("can delete a document", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
const countBefore = await collection.count({ runId });
// Delete the document with the last name (Charlie)
const result = await collection.deleteOne({ runId });
Expand All @@ -253,7 +257,7 @@ describe("Remote MongoDB", () => {
});

it("can delete many documents", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
const countBefore = await collection.count({ runId });
expect(countBefore).equals(3);
// Delete all documents in this run
Expand All @@ -267,7 +271,7 @@ describe("Remote MongoDB", () => {
});

it("can update a document", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
// Delete the document with the last name (Charlie)
const result = await collection.updateOne(
{ runId, name: "Alice" },
Expand All @@ -281,7 +285,7 @@ describe("Remote MongoDB", () => {
});

it("upserts a document when updating and the query match nothing", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
// Delete the document with the last name (Charlie)
const result = await collection.updateOne(
{ runId, name: "Dennis" },
Expand All @@ -296,7 +300,7 @@ describe("Remote MongoDB", () => {
});

it("can update many documents", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
// Delete the document with the last name (Charlie)
const result = await collection.updateMany(
{ runId },
Expand All @@ -309,7 +313,7 @@ describe("Remote MongoDB", () => {
});

it("upserts when updating many non-existing documents", async () => {
const collection = getCollection();
const collection = getCollection<TestDocument>();
// Delete the document with the last name (Charlie)
const result = await collection.updateMany(
{ runId, name: "Dennis" },
Expand All @@ -327,10 +331,9 @@ describe("Remote MongoDB", () => {
this.timeout(10_000);
this.slow(2_000);

const mongodb = app.services.mongodb("local-mongodb");
const db = mongodb.db("test-database");
const collection = db.collection("test-collection");
const collection = getCollection<any>();

// Delete all documents in the collection
await collection.deleteMany({});

const sleep = async (time: number) =>
Expand Down
3 changes: 3 additions & 0 deletions packages/realm-web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
?.?.? Release notes (2020-??-??)
=============================================================

### Breaking Changes
* Removed the `functions` and `services` properties from `App`, use the `functions` property and `mongoClient` method on `User` instances instead. ([#3298](https://github.com/realm/realm-js/pull/3298))

### Enhancements
* Changing the behaviour when refreshing an access token fails. With this change, if the refresh token cannot be used to refresh an access token, the user is logged out. ([#3269](https://github.com/realm/realm-js/pull/3269))

Expand Down
15 changes: 0 additions & 15 deletions packages/realm-web/src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ import {
} from "realm-network-transport";
import { ObjectId } from "bson";

import { FunctionsFactory } from "./FunctionsFactory";
import { User, UserState } from "./User";
import { Credentials } from "./Credentials";
import { create as createServicesFactory } from "./services";
import { EmailPasswordAuth } from "./auth-providers";
import { Storage } from "./storage";
import { AppStorage } from "./AppStorage";
Expand Down Expand Up @@ -61,13 +59,6 @@ export class App<
FunctionsFactoryType extends object = Realm.DefaultFunctionsFactory,
CustomDataType extends object = any
> implements Realm.App<FunctionsFactoryType, CustomDataType> {
/** @inheritdoc */
public readonly functions: FunctionsFactoryType &
Realm.BaseFunctionsFactory;

/** @inheritdoc */
public readonly services: Realm.Services;

/** @inheritdoc */
public readonly id: string;

Expand Down Expand Up @@ -149,12 +140,6 @@ export class App<
locationUrlContext: this,
transport,
});
// Construct the functions factory
this.functions = FunctionsFactory.create<FunctionsFactoryType>(
this.fetcher,
);
// Construct the services factory
this.services = createServicesFactory(this.fetcher);
// Construct the auth providers
this.emailPasswordAuth = new EmailPasswordAuth(this.fetcher);
// Construct the storage
Expand Down
2 changes: 1 addition & 1 deletion packages/realm-web/src/services/HTTPService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Response = Realm.Services.HTTP.Response;
*
* @see https://docs.mongodb.com/stitch/services/http/
*/
class HTTPService implements HTTP {
export class HTTPService implements HTTP {
/**
* The functions factory interface to use when sending requests.
*/
Expand Down
6 changes: 2 additions & 4 deletions packages/realm-web/src/services/MongoDBService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ export { MongoDBCollection };
* @param collectionName A collection name.
* @returns The collection.
*/
export function createCollection<
T extends Realm.Services.MongoDB.Document = any
>(
function createCollection<T extends Realm.Services.MongoDB.Document = any>(
fetcher: Fetcher,
serviceName: string,
databaseName: string,
Expand All @@ -56,7 +54,7 @@ export function createCollection<
* @param databaseName A database name
* @returns The database.
*/
export function createDatabase(
function createDatabase(
fetcher: Fetcher,
serviceName: string,
databaseName: string,
Expand Down
21 changes: 5 additions & 16 deletions packages/realm-web/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,9 @@
//
////////////////////////////////////////////////////////////////////////////

import { Fetcher } from "../Fetcher";
export {
MongoDBCollection,
createService as createMongoDBService,
} from "./MongoDBService";

import { createService as createMongoDBRemoteService } from "./MongoDBService";
import { createService as createHttpService } from "./HTTPService";

/**
* Create all services for a particular app.
*
* @param fetcher The fetcher to use when senting requests to the services.
* @returns An object containing functions that create the individual services.
*/
export function create(fetcher: Fetcher): Realm.Services {
return {
mongodb: createMongoDBRemoteService.bind(null, fetcher),
http: createHttpService.bind(null, fetcher),
};
}
export { HTTPService, createService as createHTTPService } from "./HTTPService";
Loading