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

feat(@aws-amplify/datastore): Support SSR #5450

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b6e2d72
Add jest config to aws-amplify
ericclemmons Apr 15, 2020
1158231
Add exports-test.ts
ericclemmons Apr 15, 2020
a07e0ac
aws-amplify has named export DataStore
ericclemmons Apr 15, 2020
7dd9dde
Move __tests__ so build script ignores them
ericclemmons Apr 15, 2020
aaa31f5
Add Predicates from DataStore
ericclemmons Apr 15, 2020
9ce2624
Mark datastore as having sideEffects (initSchema)
ericclemmons Apr 16, 2020
a733a4e
Node is always "online"
ericclemmons Apr 16, 2020
b1e41c8
getDefaultAdapter uses fake-indexeddb for Node
ericclemmons Apr 16, 2020
fea8126
Node support for WebSocket (via isomorphic-ws)
ericclemmons Apr 17, 2020
8dd5fe2
Warn when schema has been initialized rather than throw
ericclemmons Apr 17, 2020
0baf796
Add model.fromJSON([] | {}) for creating existing instances from JSON
ericclemmons Apr 17, 2020
10a8141
Revert "Mark datastore as having sideEffects (initSchema)"
ericclemmons Apr 17, 2020
4733b5c
Remove `isomorphic-ws` from Predictions until formally supported & te…
ericclemmons Apr 17, 2020
644a5f1
Use modelInstanceCreator for fromJSON
ericclemmons Apr 17, 2020
a267d05
Add DataStore.toJSON()
ericclemmons Apr 17, 2020
280e685
Merge branch 'master' of github.com:aws-amplify/amplify-js into next-…
ericclemmons Apr 20, 2020
6f92a34
Simplify with Observerable.from
ericclemmons May 20, 2020
b476969
Merge branch 'master' of github.com:aws-amplify/amplify-js into next-…
ericclemmons Jun 16, 2020
fb25ade
Fix TypeScript error with Observable.from({ online: true })
ericclemmons Jun 16, 2020
bf5d5d5
DataStore awaits SYNC_ENGINE_SYNC_QUERIES_READY when in Node
ericclemmons Jun 16, 2020
a34bd42
Correct `Observable.from` usage in 6f92a34
ericclemmons Jun 16, 2020
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
96 changes: 96 additions & 0 deletions packages/aws-amplify/__tests__/exports-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import * as exported from '../src';

describe('aws-amplify', () => {
describe('import * keys', () => {
it('should match snapshot', () => {
expect(Object.keys(exported)).toMatchInlineSnapshot(`
Array [
"Amplify",
"Analytics",
"AWSPinpointProvider",
"AWSKinesisProvider",
"AWSKinesisFirehoseProvider",
"AmazonPersonalizeProvider",
"Auth",
"Storage",
"StorageClass",
"API",
"APIClass",
"graphqlOperation",
"DataStore",
"Predicates",
"PubSub",
"Cache",
"Interactions",
"a",
"button",
"signInButton",
"googleSignInButton",
"signInButtonIcon",
"auth0SignInButton",
"facebookSignInButton",
"amazonSignInButton",
"oAuthSignInButton",
"signInButtonContent",
"formContainer",
"formSection",
"formField",
"formRow",
"hint",
"input",
"inputLabel",
"label",
"radio",
"navBar",
"navRight",
"nav",
"navItem",
"photoPickerButton",
"photoPlaceholder",
"photoPlaceholderIcon",
"container",
"actionRow",
"sectionHeader",
"sectionHeaderHint",
"sectionBody",
"sectionHeaderContent",
"sectionFooter",
"sectionFooterPrimaryContent",
"sectionFooterSecondaryContent",
"selectInput",
"strike",
"strikeContent",
"toast",
"toastClose",
"totpQrcode",
"sumerianSceneContainer",
"sumerianScene",
"loadingOverlay",
"loadingContainer",
"loadingLogo",
"loadingSceneName",
"loadingBar",
"loadingBarFill",
"sceneErrorText",
"sceneBar",
"sceneName",
"sceneActions",
"actionButton",
"tooltip",
"actionIcon",
"autoShowTooltip",
"XR",
"Predictions",
"Logger",
"Hub",
"JS",
"ClientDevice",
"Signer",
"I18n",
"ServiceWorker",
"default",
]
`);
});
});
});
42 changes: 42 additions & 0 deletions packages/aws-amplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,47 @@
"@aws-amplify/storage": "^3.1.6",
"@aws-amplify/ui": "^2.0.2",
"@aws-amplify/xr": "^2.1.6"
},
"jest": {
"globals": {
"ts-jest": {
"diagnostics": false,
"tsConfig": {
"lib": [
"es5",
"es2015",
"dom",
"esnext.asynciterable",
"es2017.object"
],
"allowJs": true
}
}
},
"transform": {
"^.+\\.(js|jsx|ts|tsx)$": "ts-jest"
},
"preset": "ts-jest",
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json",
"jsx"
],
"testEnvironment": "jsdom",
"testURL": "http://localhost/",
"coverageThreshold": {
"global": {
"branches": 0,
"functions": 0,
"lines": 0,
"statements": 0
}
},
"coveragePathIgnorePatterns": [
"/node_modules/"
]
}
}
1 change: 1 addition & 0 deletions packages/aws-amplify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export {
export { Auth } from '@aws-amplify/auth';
export { Storage, StorageClass } from '@aws-amplify/storage';
export { API, APIClass, graphqlOperation } from '@aws-amplify/api';
export { DataStore, Predicates } from '@aws-amplify/datastore';
export { PubSub } from '@aws-amplify/pubsub';
export { default as Cache } from '@aws-amplify/cache';
export { Interactions } from '@aws-amplify/interactions';
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/Util/Reachability.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { JS } from '@aws-amplify/core';
import Observable from 'zen-observable-ts';

type NetworkStatus = {
Expand All @@ -6,6 +7,14 @@ type NetworkStatus = {

export default class ReachabilityNavigator implements Reachability {
networkMonitor(): Observable<NetworkStatus> {
if (JS.browserOrNode().isNode) {
return new Observable(observer => {
observer.next({ online: true });

return () => {};
});
ericclemmons marked this conversation as resolved.
Show resolved Hide resolved
}

return new Observable(observer => {
observer.next({ online: window.navigator.onLine });

Expand Down
8 changes: 5 additions & 3 deletions packages/datastore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"publishConfig": {
"access": "public"
},
"sideEffects": false,
"sideEffects": true,
"scripts": {
"test": "tslint 'src/**/*.ts' && jest -w 1 --coverage",
"build-with-test": "npm test && npm run build",
Expand All @@ -35,16 +35,18 @@
},
"homepage": "https://aws-amplify.github.io/",
"devDependencies": {
"@types/uuid": "3.4.5",
"fake-indexeddb": "3.0.0"
"@types/uuid": "3.4.5"
},
"dependencies": {
"@aws-amplify/api": "^3.1.6",
"@aws-amplify/core": "^3.2.3",
"@aws-amplify/pubsub": "^3.0.7",
"fake-indexeddb": "3.0.0",
"idb": "4.0.4",
"immer": "6.0.1",
"isomorphic-ws": "^4.0.1",
"uuid": "3.3.2",
"ws": "^7.2.3",
"zen-observable-ts": "0.8.19",
"zen-push": "0.2.1"
},
Expand Down
22 changes: 21 additions & 1 deletion packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ let storageClasses: TypeConstructorMap;

const initSchema = (userSchema: Schema) => {
if (schema !== undefined) {
throw new Error('The schema has already been initialized');
console.warn('The schema has already been initialized');

return userClasses;
manueliglesias marked this conversation as resolved.
Show resolved Hide resolved
}

logger.log('validating schema', { schema: userSchema });
Expand Down Expand Up @@ -321,6 +323,24 @@ const createModelClass = <T extends PersistentModel>(
draft.id = source.id;
});
}

static fromJSON(json: T | T[]) {
if (Array.isArray(json)) {
return json.map(init => this.fromJSON(init));
}

const { id, ...init } = json;

// ! This is a double-init because `id` can't be provided via `new`.
return produce(
new clazz(init as ModelInit<T>),
(draft: Draft<T & ModelInstanceMetadata>) => {
initializeInstance(init, modelDefinition, draft);

draft.id = id;
}
);
ericclemmons marked this conversation as resolved.
Show resolved Hide resolved
}
});
ericclemmons marked this conversation as resolved.
Show resolved Hide resolved

clazz[immerable] = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { JS } from '@aws-amplify/core';
import { Adapter } from '..';

const getDefaultAdapter: () => Adapter = () => {
if (window.indexedDB) {
const { isBrowser, isNode } = JS.browserOrNode();
if (isNode) {
require('fake-indexeddb/auto');
return require('../indexeddb').default;
}
if (process && process.env) {
throw new Error('Node is not supported');
ericclemmons marked this conversation as resolved.
Show resolved Hide resolved

if (isBrowser && window.indexedDB) {
return require('../indexeddb').default;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
MessageHeaderValue,
} from '@aws-sdk/eventstream-marshaller';
import { fromUtf8, toUtf8 } from '@aws-sdk/util-utf8-node';
import WebSocket from 'isomorphic-ws';

const logger = new Logger('AmazonAIConvertPredictionsProvider');
const eventBuilder = new EventStreamMarshaller(toUtf8, fromUtf8);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from '@aws-amplify/core';
import Cache from '@aws-amplify/cache';
import Auth from '@aws-amplify/auth';
import WebSocket from 'isomorphic-ws';
ericclemmons marked this conversation as resolved.
Show resolved Hide resolved
import { AbstractPubSubProvider } from './PubSubProvider';
import { CONTROL_MSG } from '../index';

Expand Down