Skip to content

Commit fecdbbd

Browse files
authored
Build system (#121)
Big rewrite to prepare for future changes. This now uses Typescript and Webpack to build everything. Some breaking changes to eliminate extend usage. Supersedes #116 and thus should also solve #86
1 parent 19e871a commit fecdbbd

22 files changed

+11195
-2533
lines changed

.eslintrc.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
module.exports = {
3+
root: true,
4+
parserOptions: {
5+
// ecmaVersion: 2017,
6+
sourceType: 'module',
7+
// project: "./tsconfig.json",
8+
tsconfigRootDir: __dirname,
9+
project: ['./test.tsconfig.json'],
10+
},
11+
parser: '@typescript-eslint/parser',
12+
plugins: [
13+
'@typescript-eslint',
14+
],
15+
extends: [
16+
// 'eslint:recommended',
17+
// 'plugin:@typescript-eslint/eslint-recommended',
18+
// 'plugin:@typescript-eslint/recommended',
19+
// "plugin:@typescript-eslint/recommended-requiring-type-checking",
20+
],
21+
env: {
22+
browser: true,
23+
node: true,
24+
es6: true,
25+
},
26+
// plugins: [
27+
// "promise"
28+
// ],
29+
rules: {
30+
"@typescript-eslint/no-floating-promises": "error",
31+
// "await-promise": 2,
32+
// "no-debugger": 0,
33+
// "no-console": 0,
34+
// "no-mixed-spaces-and-tabs": 0,
35+
}
36+
};

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ coverage
55
test/test-bundle.js
66
npm-debug.log
77
dist
8+
/.nyc_output
9+
/lib
10+
/dist

.jshintrc

Lines changed: 0 additions & 33 deletions
This file was deleted.

README.md

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Then include it after `pouchdb.js` in your HTML page:
4040
```html
4141
<script src="pouchdb.js"></script>
4242
<script src="pouchdb.find.js"></script>
43-
<script src="pouchdb.relational-pouch.js"></script>
43+
<script src="pouchdb.relational-pouch.browser.js"></script>
4444
```
4545

4646
### In Node.js
@@ -55,6 +55,31 @@ PouchDB.plugin(require('relational-pouch'));
5555
PouchDB.plugin(require('pouchdb-find'));
5656
```
5757

58+
#### Typescript
59+
60+
This package contains its own type definitions. Due to the nature of setSchema, which alters the database on which it is called, typescript needs to know about these changes. This is done by returning a new type. So working with this plugin should look something like this:
61+
62+
```js
63+
import Pouch from 'pouchdb-core';
64+
//import some adapter
65+
import find from 'pouchdb-find';
66+
import rel from 'relational-pouch';
67+
68+
Pouch
69+
//.plugin(someadapter)
70+
.plugin(find)
71+
.plugin(rel);
72+
73+
const baseDB = new Pouch(...);//adapter options
74+
const relDB = baseDB.setSchema(...);//schema options
75+
76+
let relDoc = await relDB.rel.find('sometype', 'someid');
77+
78+
//non relation pouch API is still available
79+
let doc = await relDB.get('someid');
80+
```
81+
82+
5883
API
5984
----------
6085

@@ -167,14 +192,8 @@ Result:
167192

168193
```js
169194
{
170-
"posts": [
171-
{
172-
"title": "Rails is Omakase",
173-
"text": "There are a lot of a-la-carte software...",
174-
"id": "14760983-285C-6D1F-9813-D82E08F1AC29",
175-
"rev": "1-84df2c73028e5b8d0ae1cbb401959370"
176-
}
177-
]
195+
"id": "14760983-285C-6D1F-9813-D82E08F1AC29",
196+
"rev": "1-84df2c73028e5b8d0ae1cbb401959370"
178197
}
179198
```
180199

@@ -192,14 +211,8 @@ Result:
192211

193212
```js
194213
{
195-
"posts": [
196-
{
197-
"title": "Rails is Unagi",
198-
"text": "Delicious unagi. Mmmmmm.",
199-
"id": 1,
200-
"rev": "1-0ae315ee597b22cc4b1acf9e0edc35ba"
201-
}
202-
]
214+
"id": 1,
215+
"rev": "1-0ae315ee597b22cc4b1acf9e0edc35ba"
203216
}
204217
```
205218

@@ -359,25 +372,10 @@ var attachment = new Blob(['Is there life on Mars?']);
359372
db.rel.putAttachment('post', {id:1, rev:"1-..."}, 'file', attachment, 'text/plain');
360373
```
361374

362-
Result:
375+
This returns the new rev:
363376

364377
```js
365-
{
366-
"posts": [
367-
{
368-
"attachments": {
369-
"file": {
370-
"content_type": "text/plain",
371-
"digest": "md5-1cz9JKh0i+1OLJonmitgiQ==",
372-
"length": 22,
373-
// ... http://pouchdb.com/guides/attachments.html
374-
}
375-
},
376-
"id": 1,
377-
"rev": "2-...."
378-
}
379-
]
380-
}
378+
"2-...."
381379
```
382380

383381
### db.rel.getAttachment(type, id, attachmentId)
@@ -409,17 +407,10 @@ Or continuing from the `putAttachment` example:
409407
db.rel.removeAttachment('post', {id: 1, rev:"2-09d5c5bd86fc170c064b296773044ea9"} , 'file');
410408
```
411409

412-
Result:
410+
This returns the new rev:
413411

414412
```js
415-
{
416-
"posts": [
417-
{
418-
"id": 1,
419-
"rev": "3-...."
420-
}
421-
]
422-
}
413+
"3-...."
423414
```
424415

425416
### db.rel.parseDocID(docID)
@@ -1118,10 +1109,14 @@ Testing
11181109
11191110
### In Node
11201111
1121-
This will run the tests in Node using LevelDB:
1112+
This will run the tests in Node using memory and http adapter:
11221113
11231114
npm test
11241115
1116+
if you don't have a admin party setup you can specify admin credentials in the RELATIONAL_POUCH_DB_AUTH environment variable like this:
1117+
1118+
RELATIONAL_POUCH_DB_AUTH=user:password@
1119+
11251120
You can also check for 100% code coverage using:
11261121
11271122
npm run coverage
@@ -1150,3 +1145,20 @@ You can run e.g.
11501145
CLIENT=selenium:phantomjs npm test
11511146
11521147
This will run the tests automatically and the process will exit with a 0 or a 1 when it's done. Firefox uses IndexedDB, and PhantomJS uses WebSQL.
1148+
1149+
## Changelog
1150+
1151+
### 4.0.0
1152+
1153+
- Breaking change: To prevent us from having to do cloning of input documents, we have changed the `save`, `putAttachment` and `removeAttachment` API. These functions no longer return the complete document. The attachment functions only return the new `rev` value, while the save will also return the `id`. So after these promises resolve you have to manually update your in app data to reflect this new revision (and possibly id) if you want to update the document later. You can use something like the following:
1154+
```js
1155+
let updatedData = await db.rel.save('post', post);
1156+
Object.assign(post, updatedData);
1157+
```
1158+
or
1159+
```js
1160+
post.rev = await db.rel.putAttachment('post', post, 'file', fileData);
1161+
```
1162+
- This library now uses Typescript, Webpack and Babel in its build setup. The build creates files in 2 output directories: lib and dist.
1163+
- The lib directory will contain the output of `tsc` in esnext mode. So this can be used by Webpack and other module aware systems. These will require Babel transformations if you want to use them, but this way you can specify your own target.
1164+
- The dist directory contains 2 files, pouchdb.relational-pouch.browser.js and pouchdb.relational-pouch.node.js. These are compiled by webpack with targets ">2%, not ie 11" and "node 10". This should be sufficient for now, but otherwise you can build your own with Webpack.

bin/dev-server.js

Lines changed: 77 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,96 @@
55
var COUCH_HOST = process.env.COUCH_HOST || 'http://127.0.0.1:5984';
66
var HTTP_PORT = 8001;
77

8+
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
9+
810
var Promise = require('bluebird');
911
var request = require('request');
1012
var http_server = require("http-server");
1113
var fs = require('fs');
1214
var indexfile = "./test/test.js";
1315
var dotfile = "./test/.test-bundle.js";
1416
var outfile = "./test/test-bundle.js";
15-
var watchify = require("watchify");
16-
var browserify = require('browserify');
17-
var b = browserify(indexfile, {
18-
cache: {},
19-
packageCache: {},
20-
plugin: [watchify]
21-
})
17+
var webpack = require('webpack');
18+
var path = require('path');
2219

23-
b.on('update', bundle);
24-
bundle();
20+
var b = webpack({
21+
target: "web",
22+
entry: "./test/test.ts",
23+
mode: 'development',
24+
devtool: 'source-map',
25+
output: {
26+
path: path.resolve(__dirname, '../test'),
27+
filename: 'test-bundle.js',
28+
libraryTarget: 'umd',
29+
},
30+
plugins: [
31+
new ForkTsCheckerWebpackPlugin({eslint: true, tsconfig: "test.tsconfig.json"}),
32+
new webpack.EnvironmentPlugin(['RELATIONAL_POUCH_DB_AUTH']),
33+
],
34+
module: {
35+
rules: [
36+
// {
37+
// enforce: 'pre',
38+
// test: /\.[tj]s$/,
39+
// exclude: /node_modules/,
40+
// loader: 'eslint-loader',
41+
// },
42+
{
43+
test: /\.[tj]s$/,
44+
exclude: /(node_modules|bower_components)/,
45+
use: [
46+
{
47+
loader: 'babel-loader',
48+
options: {
49+
presets: [
50+
['@babel/preset-env',
51+
{
52+
"targets": "last 1 Chrome versions",
53+
"modules": false,
54+
useBuiltIns: "usage",
55+
corejs: 3,
56+
}],
57+
],
58+
},
59+
},
60+
{
61+
loader: 'ts-loader',
62+
options: {
63+
transpileOnly: true,
64+
experimentalWatchApi: true,
65+
context: path.resolve(__dirname, '../'),
66+
configFile: "test.tsconfig.json",
67+
},
68+
},
69+
],
70+
}
71+
]
72+
},
73+
resolve: {
74+
extensions: ['tsx', '.ts', '.js', '.json'],
75+
},
76+
}).watch({}, (error, stats) => {
77+
if (error) {
78+
console.error(error);
79+
return;
80+
}
81+
82+
let logOptions = {all: false, colors: true, assets: true, errors: true, errorDetails: true, warnings: true, errorStack: true};
83+
if (!stats.hasErrors()) {
84+
console.log('Updated');
85+
console.log(stats.toString(logOptions));
86+
filesWritten = true;
87+
checkReady();
88+
} else {
89+
const info = stats.toJson();
90+
console.error(stats.toString(logOptions));//children: false, entrypoints: false, hash: false, modules: false, , chunks: false
91+
}
92+
});
2593

2694
var filesWritten = false;
2795
var serverStarted = false;
2896
var readyCallback;
2997

30-
function bundle() {
31-
var wb = b.bundle();
32-
wb.on('error', function (err) {
33-
console.error(String(err));
34-
});
35-
wb.on("end", end);
36-
wb.pipe(fs.createWriteStream(dotfile));
37-
38-
function end() {
39-
fs.rename(dotfile, outfile, function (err) {
40-
if (err) { return console.error(err); }
41-
console.log('Updated:', outfile);
42-
filesWritten = true;
43-
checkReady();
44-
});
45-
}
46-
}
47-
4898
function startServers(callback) {
4999
readyCallback = callback;
50100
// enable CORS globally, because it's easier this way

bower.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "relational-pouch",
3-
"version": "3.2.0",
3+
"version": "4.0.0",
44
"description": "PouchDB, relational-style",
55
"homepage": "https://github.com/pouchdb-community/relational-pouch",
66
"authors": [
77
"Nolan Lawson <[email protected]>"
88
],
9-
"main": "dist/pouchdb.relational-pouch.js",
9+
"main": "dist/pouchdb.relational-pouch.browser.js",
1010
"moduleType": [
1111
"node"
1212
],
@@ -23,6 +23,7 @@
2323
"bower_components",
2424
"test",
2525
"tests",
26-
"vendor"
26+
"vendor",
27+
"lib"
2728
]
2829
}

0 commit comments

Comments
 (0)