-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat(@aws-amplify/datastore): Support SSR #5450
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
Closed
Closed
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
b6e2d72
Add jest config to aws-amplify
ericclemmons 1158231
Add exports-test.ts
ericclemmons a07e0ac
aws-amplify has named export DataStore
ericclemmons 7dd9dde
Move __tests__ so build script ignores them
ericclemmons aaa31f5
Add Predicates from DataStore
ericclemmons 9ce2624
Mark datastore as having sideEffects (initSchema)
ericclemmons a733a4e
Node is always "online"
ericclemmons b1e41c8
getDefaultAdapter uses fake-indexeddb for Node
ericclemmons fea8126
Node support for WebSocket (via isomorphic-ws)
ericclemmons 8dd5fe2
Warn when schema has been initialized rather than throw
ericclemmons 0baf796
Add model.fromJSON([] | {}) for creating existing instances from JSON
ericclemmons 10a8141
Revert "Mark datastore as having sideEffects (initSchema)"
ericclemmons 4733b5c
Remove `isomorphic-ws` from Predictions until formally supported & te…
ericclemmons 644a5f1
Use modelInstanceCreator for fromJSON
ericclemmons a267d05
Add DataStore.toJSON()
ericclemmons 280e685
Merge branch 'master' of github.com:aws-amplify/amplify-js into next-…
ericclemmons 6f92a34
Simplify with Observerable.from
ericclemmons b476969
Merge branch 'master' of github.com:aws-amplify/amplify-js into next-…
ericclemmons fb25ade
Fix TypeScript error with Observable.from({ online: true })
ericclemmons bf5d5d5
DataStore awaits SYNC_ENGINE_SYNC_QUERIES_READY when in Node
ericclemmons a34bd42
Correct `Observable.from` usage in 6f92a34
ericclemmons File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { Amplify, ConsoleLogger as Logger, Hub } from '@aws-amplify/core'; | ||
| import { Amplify, ConsoleLogger as Logger, Hub, JS } from '@aws-amplify/core'; | ||
| import { Draft, immerable, produce, setAutoFreeze } from 'immer'; | ||
| import { v4 as uuid4 } from 'uuid'; | ||
| import Observable, { ZenObservable } from 'zen-observable-ts'; | ||
|
|
@@ -52,6 +52,7 @@ setAutoFreeze(true); | |
| const logger = new Logger('DataStore'); | ||
|
|
||
| const ulid = monotonicUlidFactory(Date.now()); | ||
| const { isNode } = JS.browserOrNode(); | ||
|
|
||
| declare class Setting { | ||
| constructor(init: ModelInit<Setting>); | ||
|
|
@@ -100,7 +101,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; | ||
| } | ||
|
|
||
| logger.log('validating schema', { schema: userSchema }); | ||
|
|
@@ -325,6 +328,14 @@ 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)); | ||
| } | ||
|
|
||
| return modelInstanceCreator(clazz, json); | ||
| } | ||
| }); | ||
|
|
||
| clazz[immerable] = true; | ||
|
|
@@ -861,7 +872,13 @@ async function start(): Promise<void> { | |
| .start({ fullSyncInterval: fullSyncIntervalInMilliseconds }) | ||
| .subscribe({ | ||
| next: ({ type, data }) => { | ||
| if (type === ControlMessage.SYNC_ENGINE_STORAGE_SUBSCRIBED) { | ||
| // In Node, we need to wait for queries to be synced to prevent returning empty arrays. | ||
| // In the Browser, we can begin returning data once subscriptions are in place. | ||
| const readyType = isNode | ||
| ? ControlMessage.SYNC_ENGINE_SYNC_QUERIES_READY | ||
| : ControlMessage.SYNC_ENGINE_STORAGE_SUBSCRIBED; | ||
|
|
||
| if (type === readyType) { | ||
|
Comment on lines
+875
to
+881
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change fixes expected behavior in Node (particularly lambdas). Notice here where I'm listening to // 👇 Hub.listen('datastore', ...)
datastore {
channel: 'datastore',
payload: { event: 'storageSubscribed', data: undefined },
source: '',
patternInfo: []
}
datastore {
channel: 'datastore',
payload: { event: 'networkStatus', data: { active: true } },
source: '',
patternInfo: []
}
datastore {
channel: 'datastore',
payload: { event: 'outboxStatus', data: { isEmpty: true } },
source: '',
patternInfo: []
}
datastore {
channel: 'datastore',
payload: { event: 'subscriptionsEstablished', data: undefined },
source: '',
patternInfo: []
}
datastore {
channel: 'datastore',
payload: { event: 'syncQueriesStarted', data: { models: [Array] } },
source: '',
patternInfo: []
}
datastore {
channel: 'datastore',
payload: {
event: 'modelSynced',
data: {
model: [Function],
isFullSync: true,
isDeltaSync: false,
counts: [Object]
}
},
source: '',
patternInfo: []
}
datastore {
channel: 'datastore',
payload: {
event: 'modelSynced',
data: {
model: [Function],
isFullSync: true,
isDeltaSync: false,
counts: [Object]
}
},
source: '',
patternInfo: []
}
datastore {
channel: 'datastore',
payload: { event: 'syncQueriesReady', data: undefined },
source: '',
patternInfo: []
}
datastore {
channel: 'datastore',
payload: { event: 'ready', data: undefined },
source: '',
patternInfo: []
}// 👇 { posts }
{
posts: [
Post {
id: 'd4ad30d3-bb95-4cb2-9f55-b5917605e797',
title: 'New Post (4/16/2020, 4:58:42 PM)',
rating: 1,
status: 'ACTIVE',
_version: 2,
_lastChangedAt: 1587081528053,
_deleted: null
},
Post {
id: '7b14789a-40a9-4b3a-8a23-849e3866f77a',
title: 'New Post (4/17/2020, 3:17:13 PM)',
rating: 4,
status: 'INACTIVE',
_version: 1,
_lastChangedAt: 1587161833864,
_deleted: null
},
Post {
id: '2e5a6422-47e8-4693-b141-a126e8aaacfc',
title: 'New Post (4/16/2020, 4:28:16 PM)',
rating: 3,
status: 'ACTIVE',
_version: 2,
_lastChangedAt: 1587081526956,
_deleted: null
},
Post {
id: 'f05918fc-2209-4013-a185-d6f9f8d9c468',
title: 'New Post (4/20/2020, 2:04:21 PM)',
rating: 2,
status: 'INACTIVE',
_version: 1,
_lastChangedAt: 1587416662244,
_deleted: null
},
Post {
id: 'a94a6cc5-8a0b-441a-9da8-5dfc391df18d',
title: 'New Post (4/16/2020, 4:58:43 PM)',
rating: 3,
status: 'INACTIVE',
_version: 1,
_lastChangedAt: 1587081523206,
_deleted: null
}
]
}
{
posts: [
Post {
id: 'd4ad30d3-bb95-4cb2-9f55-b5917605e797',
title: 'New Post (4/16/2020, 4:58:42 PM)',
rating: 1,
status: 'ACTIVE',
_version: 2,
_lastChangedAt: 1587081528053,
_deleted: null
},
Post {
id: '7b14789a-40a9-4b3a-8a23-849e3866f77a',
title: 'New Post (4/17/2020, 3:17:13 PM)',
rating: 4,
status: 'INACTIVE',
_version: 1,
_lastChangedAt: 1587161833864,
_deleted: null
},
Post {
id: '2e5a6422-47e8-4693-b141-a126e8aaacfc',
title: 'New Post (4/16/2020, 4:28:16 PM)',
rating: 3,
status: 'ACTIVE',
_version: 2,
_lastChangedAt: 1587081526956,
_deleted: null
},
Post {
id: 'f05918fc-2209-4013-a185-d6f9f8d9c468',
title: 'New Post (4/20/2020, 2:04:21 PM)',
rating: 2,
status: 'INACTIVE',
_version: 1,
_lastChangedAt: 1587416662244,
_deleted: null
},
Post {
id: 'a94a6cc5-8a0b-441a-9da8-5dfc391df18d',
title: 'New Post (4/16/2020, 4:58:43 PM)',
rating: 3,
status: 'INACTIVE',
_version: 1,
_lastChangedAt: 1587081523206,
_deleted: null
}
]
} |
||
| initResolve(); | ||
| } | ||
|
|
||
|
|
@@ -940,6 +957,10 @@ function getNamespace(): SchemaNamespace { | |
| return namespace; | ||
| } | ||
|
|
||
| const toJSON = <T extends PersistentModel>(model: T | T[]): JSON => { | ||
| return JSON.parse(JSON.stringify(model)); | ||
| }; | ||
|
|
||
| class DataStore { | ||
| constructor() { | ||
| Amplify.register(this); | ||
|
|
@@ -954,6 +975,7 @@ class DataStore { | |
| observe = observe; | ||
| configure = configure; | ||
| clear = clear; | ||
| toJSON = toJSON; | ||
| } | ||
|
|
||
| const instance = new DataStore(); | ||
|
|
||
11 changes: 8 additions & 3 deletions
11
packages/datastore/src/storage/adapter/getDefaultAdapter/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.