Skip to content

Commit

Permalink
Add example web apps for testing (#72)
Browse files Browse the repository at this point in the history
* parcel web app for testing

* example webpack app for testing

* eslint typings
  • Loading branch information
cvara authored Feb 27, 2023
1 parent 7561a6e commit 72a483a
Show file tree
Hide file tree
Showing 14 changed files with 5,481 additions and 994 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import("eslint").Linter.Config} */
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
*.tgz
.vscode/
.env
.parcel-cache/
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ yarn.lock
tests/
bin/
.env
.circleci/
.circleci/
.parcel-cache/
39 changes: 39 additions & 0 deletions examples/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export type Character = {
id: string;
name: string;
affiliation: string;
age: number;
};

export const characters = [
{
id: '1',
name: 'Darth Vader',
affiliation: 'Sith',
age: 30,
},
{
id: '2',
name: 'Yoda',
affiliation: 'Jedi',
age: 900,
},
{
id: '3',
name: 'Darth Sidius',
affiliation: 'Sith',
age: 65,
},
{
id: '4',
name: 'Obi-Wan Kenobi',
affiliation: 'Jedi',
age: 68,
},
{
id: '5',
name: 'Qui-Gon Jin',
affiliation: 'Jedi',
age: 38,
},
];
51 changes: 51 additions & 0 deletions examples/initialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { addRxPlugin, createRxDatabase } from 'rxdb';
import { getRxStorageDexie } from 'rxdb/plugins/storage-dexie';
import { RxDBQueryBuilderPlugin } from 'rxdb/plugins/query-builder';
import { RxDBDevModePlugin } from 'rxdb/plugins/dev-mode';

import { characters } from './data';

addRxPlugin(RxDBDevModePlugin);
addRxPlugin(RxDBQueryBuilderPlugin);

const initialize = async () => {
// create RxDB
const db = await createRxDatabase({
name: 'test_database',
storage: getRxStorageDexie(),
ignoreDuplicate: true,
});

// create a collection
const collection = await db.addCollections({
characters: {
schema: {
title: 'characters',
version: 0,
type: 'object',
primaryKey: 'id',
properties: {
id: {
type: 'string',
maxLength: 250,
},
name: {
type: 'string',
},
affiliation: {
type: 'string',
},
age: {
type: 'integer',
},
},
},
},
});

await collection.characters.bulkInsert(characters);

return db;
};

export default initialize;
31 changes: 31 additions & 0 deletions examples/parcel/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { useRxData } from '../../src';
import { Character } from '../data';

function App() {
const { result: characters, isFetching } = useRxData<Character>(
'characters',
collection =>
collection?.find({
selector: {
affiliation: 'Jedi',
},
})
);

if (isFetching) {
return <div>loading characters...</div>;
}

return (
<div>
<ul>
{characters.map((character, idx) => (
<li key={idx}>{character.name}</li>
))}
</ul>
</div>
);
}

export default App;
11 changes: 11 additions & 0 deletions examples/parcel/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>rxdb-hooks</title>
<script type="module" src="./index.tsx"></script>
</head>
<body>
<div id="root"></div>
</body>
</html>
22 changes: 22 additions & 0 deletions examples/parcel/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React, { useEffect, useState } from 'react';
import { render } from 'react-dom';
import { RxDatabase } from 'rxdb';
import { Provider } from '../../src';
import initialize from '../initialize';
import App from './App';

const Root = () => {
const [db, setDb] = useState<RxDatabase>();

useEffect(() => {
initialize().then(setDb);
}, []);

return (
<Provider db={db}>
<App />
</Provider>
);
};

render(<Root />, document.getElementById('root'));
31 changes: 31 additions & 0 deletions examples/webpack/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { useRxData } from '../../src';
import { Character } from '../data';

function App() {
const { result: characters, isFetching } = useRxData<Character>(
'characters',
collection =>
collection?.find({
selector: {
affiliation: 'Jedi',
},
})
);

if (isFetching) {
return <div>loading characters...</div>;
}

return (
<div>
<ul>
{characters.map((character, idx) => (
<li key={idx}>{character.name}</li>
))}
</ul>
</div>
);
}

export default App;
10 changes: 10 additions & 0 deletions examples/webpack/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Webpack App</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
22 changes: 22 additions & 0 deletions examples/webpack/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React, { useEffect, useState } from 'react';
import { render } from 'react-dom';
import { RxDatabase } from 'rxdb';
import { Provider } from '../../src';
import initialize from '../initialize';
import App from './App';

const Root = () => {
const [db, setDb] = useState<RxDatabase>();

useEffect(() => {
initialize().then(setDb);
}, []);

return (
<Provider db={db}>
<App />
</Provider>
);
};

render(<Root />, document.getElementById('root'));
34 changes: 34 additions & 0 deletions examples/webpack/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const config = {
mode: 'development',
entry: path.resolve(__dirname, './index.tsx'),
output: {
path: path.resolve(__dirname, 'dist'),
},
devServer: {
open: true,
host: 'localhost',
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, './index.html'),
}),
],
module: {
rules: [
{
test: /\.(ts|tsx)$/i,
loader: 'ts-loader',
exclude: ['/node_modules/'],
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '...'],
},
};

module.exports = config;
101 changes: 56 additions & 45 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,60 @@
"build": "rimraf dist && tsc --project tsconfig.json",
"prepack": "yarn build",
"typecheck": "tsc --noEmit",
"changelog": "node ./bin/changelog.js"
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@testing-library/react-hooks": "^7.0.2",
"@types/jest": "^25.2.3",
"@types/react": "^17.0.38",
"@typescript-eslint/eslint-plugin": "^5.9.0",
"@typescript-eslint/parser": "^5.9.0",
"dotenv": "^8.2.0",
"eslint": "^8.6.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-react": "^7.18.3",
"eslint-plugin-react-hooks": "^4.0.4",
"fake-indexeddb": "^4.0.1",
"husky": "^4.2.3",
"jest": "^26.0.1",
"pouchdb-adapter-memory": "^7.2.1",
"prettier": "^2.6.2",
"pretty-quick": "^3.1.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-test-renderer": "^17.0.2",
"rimraf": "^3.0.2",
"rxdb": "14.1.1",
"rxjs": "^7.5.7",
"shelljs": "^0.8.4",
"ts-jest": "^26.0.0",
"typescript": "^4.5.4"
},
"peerDependencies": {
"react": ">=16.8",
"react-dom": ">=16.8",
"rxdb": ">=14",
"rxjs": ">=7.5.4"
},
"resolutions": {
"minimist": "1.2.5"
}
"changelog": "node ./bin/changelog.js",
"start:parcel": "parcel ./examples/parcel/index.html",
"start:webpack": "webpack serve --config examples/webpack/webpack.config.js"
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@testing-library/react-hooks": "^7.0.2",
"@types/jest": "^25.2.3",
"@types/react": "^17.0.38",
"@typescript-eslint/eslint-plugin": "^5.9.0",
"@typescript-eslint/parser": "^5.9.0",
"@webpack-cli/generators": "^3.0.1",
"dotenv": "^8.2.0",
"eslint": "^8.6.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-react": "^7.18.3",
"eslint-plugin-react-hooks": "^4.0.4",
"events": "^3.1.0",
"fake-indexeddb": "^4.0.1",
"html-webpack-plugin": "^5.5.0",
"husky": "^4.2.3",
"jest": "^26.0.1",
"parcel": "^2.8.3",
"pouchdb-adapter-memory": "^7.2.1",
"prettier": "^2.6.2",
"pretty-quick": "^3.1.3",
"process": "^0.11.10",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-test-renderer": "^17.0.2",
"rimraf": "^3.0.2",
"rxdb": "14.1.1",
"rxjs": "^7.5.7",
"shelljs": "^0.8.4",
"ts-jest": "^26.0.0",
"ts-loader": "^9.4.2",
"typescript": "^4.9.5",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1",
"webpack-dev-server": "^4.11.1"
},
"peerDependencies": {
"react": ">=16.8",
"react-dom": ">=16.8",
"rxdb": ">=14",
"rxjs": ">=7.5.4"
},
"resolutions": {
"minimist": "1.2.5"
}
}
Loading

0 comments on commit 72a483a

Please sign in to comment.