-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2982ab9
commit 92468b1
Showing
15 changed files
with
686 additions
and
303 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as path from 'path' | ||
import { CreateBrowserApi, createBrowser } from 'page-with' | ||
|
||
let browser: CreateBrowserApi | ||
|
||
beforeAll(async () => { | ||
browser = await createBrowser({ | ||
serverOptions: { | ||
webpackConfig: { | ||
resolve: { | ||
alias: { | ||
'@mswjs/data': path.resolve(__dirname, '.'), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
}) | ||
|
||
afterAll(async () => { | ||
await browser.cleanup() | ||
}) |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Database } from '../db/Database' | ||
import { isBrowser, supports } from '../utils/env' | ||
|
||
const STORAGE_KEY_PREFIX = 'mswjs-data' | ||
|
||
/** | ||
* Persists database state into `sessionStorage` and | ||
* hydrates from it on the initial page load. | ||
*/ | ||
export function persist(db: Database<any>) { | ||
if (!isBrowser() || !supports.sessionStorage()) { | ||
return | ||
} | ||
|
||
const key = `${STORAGE_KEY_PREFIX}/${db.id}` | ||
|
||
function persistState() { | ||
const json = db.toJson() | ||
console.log('persists state to storage...', json) | ||
sessionStorage.setItem(key, JSON.stringify(json)) | ||
} | ||
|
||
db.events.addListener('create', persistState) | ||
db.events.addListener('update', persistState) | ||
db.events.addListener('delete', persistState) | ||
|
||
function hydrateState() { | ||
const initialState = sessionStorage.getItem(key) | ||
|
||
if (!initialState) { | ||
return | ||
} | ||
|
||
console.log('should hydrate from "%s"', key, initialState) | ||
db.hydrate(JSON.parse(initialState)) | ||
} | ||
|
||
window.addEventListener('load', hydrateState) | ||
} |
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import { ModelDictionary } from 'lib/glossary' | ||
import { parseModelDefinition } from 'src/model/parseModelDefinition' | ||
import { oneOf, manyOf, primaryKey } from '../../src' | ||
import { Database } from '../../src/db/Database' | ||
import { RelationKind } from '../../src/glossary' | ||
import { createModel } from '../../src/model/createModel' | ||
|
||
test('serialized database models into JSON', () => { | ||
const dictionary: ModelDictionary = { | ||
user: { | ||
id: primaryKey(String), | ||
firstName: 'John', | ||
role: oneOf('role'), | ||
posts: manyOf('post'), | ||
}, | ||
role: { | ||
id: primaryKey(String), | ||
name: String, | ||
}, | ||
post: { | ||
id: primaryKey(String), | ||
title: String, | ||
}, | ||
} | ||
|
||
const db = new Database({ | ||
user: dictionary.user, | ||
role: dictionary.role, | ||
post: dictionary.post, | ||
}) | ||
|
||
const role = createModel( | ||
'role', | ||
dictionary.role, | ||
parseModelDefinition(dictionary, 'role'), | ||
{ id: 'role-1', name: 'Reader' }, | ||
db, | ||
) | ||
const posts = [ | ||
createModel('post', 'id', { id: 'post-1', title: 'First' }, {}, db), | ||
createModel('post', 'id', { id: 'post-2', title: 'Second' }, {}, db), | ||
] | ||
|
||
db.create('role', role) | ||
posts.forEach((post) => { | ||
db.create('post', post) | ||
}) | ||
|
||
db.create( | ||
'user', | ||
createModel( | ||
'user', | ||
'id', | ||
{ | ||
id: 'abc-123', | ||
firstName: 'John', | ||
} as any, | ||
{ | ||
role: { | ||
kind: RelationKind.OneOf, | ||
modelName: 'role', | ||
unique: false, | ||
refs: [ | ||
{ | ||
__type: 'role', | ||
__primaryKey: 'id', | ||
__nodeId: 'role-1', | ||
}, | ||
], | ||
}, | ||
posts: { | ||
kind: RelationKind.ManyOf, | ||
modelName: 'post', | ||
unique: false, | ||
refs: [ | ||
{ | ||
__type: 'post', | ||
__primaryKey: 'id', | ||
__nodeId: 'post-1', | ||
}, | ||
{ | ||
__type: 'post', | ||
__primaryKey: 'id', | ||
__nodeId: 'post-2', | ||
}, | ||
], | ||
}, | ||
}, | ||
db, | ||
), | ||
) | ||
|
||
const json = db.toJson() | ||
console.log(JSON.stringify(json, null, 2)) | ||
|
||
expect(json).toEqual({ | ||
user: [ | ||
[ | ||
'abc-123', | ||
{ | ||
__type: 'user', | ||
__primaryKey: 'id', | ||
id: 'abc-123', | ||
firstName: 'John', | ||
role: { | ||
kind: RelationKind.OneOf, | ||
modelName: 'role', | ||
unique: false, | ||
refs: [ | ||
{ | ||
__type: 'role', | ||
__nodeId: 'role-1', | ||
__primaryKey: 'id', | ||
}, | ||
], | ||
}, | ||
posts: [], | ||
}, | ||
], | ||
], | ||
role: [ | ||
[ | ||
'role-1', | ||
{ | ||
__type: 'role', | ||
__primaryKey: 'id', | ||
id: 'role-1', | ||
name: 'Reader', | ||
}, | ||
], | ||
], | ||
post: [ | ||
[ | ||
'post-1', | ||
{ | ||
__type: 'post', | ||
__primaryKey: 'id', | ||
id: 'post-1', | ||
title: 'First', | ||
}, | ||
], | ||
[ | ||
'post-2', | ||
{ | ||
__type: 'post', | ||
__primaryKey: 'id', | ||
id: 'post-2', | ||
title: 'Second', | ||
}, | ||
], | ||
], | ||
}) | ||
}) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { factory, primaryKey, oneOf } from '@mswjs/data' | ||
|
||
const db = factory({ | ||
user: { | ||
id: primaryKey(String), | ||
firstName: String, | ||
role: oneOf('userRole'), | ||
}, | ||
role: { | ||
id: primaryKey(String), | ||
name: String, | ||
}, | ||
}) | ||
|
||
db.user.create({ | ||
id: 'abc-123', | ||
firstName: 'John', | ||
role: db.role.create({ | ||
id: 1, | ||
name: 'Reader', | ||
}), | ||
}) | ||
|
||
window.db = db |
Oops, something went wrong.