Skip to content

Commit 4cbaaea

Browse files
committed
[Flight] Add rudimentary PG binding
1 parent 41c5d00 commit 4cbaaea

File tree

11 files changed

+206
-0
lines changed

11 files changed

+206
-0
lines changed

packages/react-pg/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# react-pg
2+
3+
This package is meant to be used alongside yet-to-be-released, experimental React features. It's unlikely to be useful in any other context.
4+
5+
**Do not use in a real application.** We're publishing this early for
6+
demonstration purposes.
7+
8+
**Use it at your own risk.**
9+
10+
# No, Really, It Is Unstable
11+
12+
The API ~~may~~ will change wildly between versions.

packages/react-pg/index.browser.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
throw new Error(
11+
'This entry point is not yet supported in the browser environment',
12+
);

packages/react-pg/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
'use strict';
11+
12+
export * from './index.node';

packages/react-pg/index.node.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
'use strict';
11+
12+
export * from './src/ReactPostgres';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
if (process.env.NODE_ENV === 'production') {
4+
module.exports = require('./cjs/react-pg.browser.production.min.js');
5+
} else {
6+
module.exports = require('./cjs/react-pg.browser.development.js');
7+
}

packages/react-pg/npm/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('./index.node');

packages/react-pg/npm/index.node.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
if (process.env.NODE_ENV === 'production') {
4+
module.exports = require('./cjs/react-pg.node.production.min.js');
5+
} else {
6+
module.exports = require('./cjs/react-pg.node.development.js');
7+
}

packages/react-pg/package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"private": true,
3+
"name": "react-pg",
4+
"description": "React bindings for PostgreSQL",
5+
"version": "0.0.0",
6+
"repository": {
7+
"type" : "git",
8+
"url" : "https://github.com/facebook/react.git",
9+
"directory": "packages/react-pg"
10+
},
11+
"files": [
12+
"LICENSE",
13+
"README.md",
14+
"build-info.json",
15+
"index.js",
16+
"index.node.js",
17+
"index.browser.js",
18+
"cjs/"
19+
],
20+
"peerDependencies": {
21+
"react": "^17.0.0",
22+
"pg": "*"
23+
},
24+
"browser": {
25+
"./index.js": "./index.browser.js"
26+
}
27+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import type {Wakeable} from 'shared/ReactTypes';
11+
12+
import {unstable_getCacheForType} from 'react';
13+
import {Pool as PostgresPool} from 'pg';
14+
15+
const Pending = 0;
16+
const Resolved = 1;
17+
const Rejected = 2;
18+
19+
type PendingResult = {|
20+
status: 0,
21+
value: Wakeable,
22+
|};
23+
24+
type ResolvedResult = {|
25+
status: 1,
26+
value: mixed,
27+
|};
28+
29+
type RejectedResult = {|
30+
status: 2,
31+
value: mixed,
32+
|};
33+
34+
type Result = PendingResult | ResolvedResult | RejectedResult;
35+
36+
function toResult(thenable): Result {
37+
const result: Result = {
38+
status: Pending,
39+
value: thenable,
40+
};
41+
thenable.then(
42+
value => {
43+
if (result.status === Pending) {
44+
const resolvedResult = ((result: any): ResolvedResult);
45+
resolvedResult.status = Resolved;
46+
resolvedResult.value = value;
47+
}
48+
},
49+
err => {
50+
if (result.status === Pending) {
51+
const rejectedResult = ((result: any): RejectedResult);
52+
rejectedResult.status = Rejected;
53+
rejectedResult.value = err;
54+
}
55+
},
56+
);
57+
return result;
58+
}
59+
60+
function readResult(result: Result) {
61+
if (result.status === Resolved) {
62+
return result.value;
63+
} else {
64+
throw result.value;
65+
}
66+
}
67+
68+
export function Pool(options: mixed) {
69+
this.pool = new PostgresPool(options);
70+
// Unique function per instance because it's used for cache identity.
71+
this.createResultMap = function() {
72+
return new Map();
73+
};
74+
}
75+
76+
Pool.prototype.query = function(query: string, values?: Array<mixed>) {
77+
const pool = this.pool;
78+
const map = unstable_getCacheForType(this.createResultMap);
79+
// TODO: Is this sufficient? What about more complex types?
80+
const key = JSON.stringify({query, values});
81+
let entry = map.get(key);
82+
if (!entry) {
83+
const thenable = pool.query(query, values);
84+
entry = toResult(thenable);
85+
map.set(key, entry);
86+
}
87+
return readResult(entry);
88+
};

scripts/flow/environment.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,11 @@ declare module 'EventListener' {
6969

7070
declare function __webpack_chunk_load__(id: string): Promise<mixed>;
7171
declare function __webpack_require__(id: string): any;
72+
73+
declare module 'pg' {
74+
declare var Pool: (
75+
options: mixed,
76+
) => {
77+
query: (query: string, values?: Array<mixed>) => void,
78+
};
79+
}

scripts/rollup/bundles.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,24 @@ const bundles = [
153153
externals: ['react', 'http', 'https'],
154154
},
155155

156+
/******* React PG Browser (experimental, new) *******/
157+
{
158+
bundleTypes: [NODE_DEV, NODE_PROD],
159+
moduleType: ISOMORPHIC,
160+
entry: 'react-pg/index.browser',
161+
global: 'ReactPostgres',
162+
externals: [],
163+
},
164+
165+
/******* React PG Node (experimental, new) *******/
166+
{
167+
bundleTypes: [NODE_DEV, NODE_PROD],
168+
moduleType: ISOMORPHIC,
169+
entry: 'react-pg/index.node',
170+
global: 'ReactPostgres',
171+
externals: ['react', 'pg'],
172+
},
173+
156174
/******* React DOM *******/
157175
{
158176
bundleTypes: [

0 commit comments

Comments
 (0)