Skip to content
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

snapshot(db): Moved db codebase #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ build/Release

# OS generated files #
.DS_Store
ehthumbs.db
Icon?
Thumbs.db

# Node Files #
node_modules
Expand Down
21 changes: 21 additions & 0 deletions modules/db/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Brandon Roberts, Mike Ryan, Rob Wormald

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions modules/db/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @ngrx/db
[![CircleCI Status for ngrx/db](https://circleci.com/gh/ngrx/db.svg?style=shield&circle-token=af75fce7603493fca82caac8f0c46cd614407e2c
)](https://circleci.com/gh/ngrx/db)

### RxJS powered IndexedDB for Angular apps

Don't use me yet.
1 change: 1 addition & 0 deletions modules/db/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src/database';
88 changes: 88 additions & 0 deletions modules/db/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
var path = require('path');
var webpack = require('webpack');

module.exports = function(karma) {
'use strict';

karma.set({
basePath: __dirname,

frameworks: ['jasmine'],

files: [
{ pattern: 'tests.bundle.ts', watched: false }
],

exclude: [],

preprocessors: {
'tests.bundle.ts': ['coverage', 'webpack', 'sourcemap']
},

reporters: ['mocha', 'coverage'],

coverageReporter: {
dir: 'coverage/',
reporters: [
{ type: 'text-summary' },
{ type: 'json' },
{ type: 'html' }
]
},

mime: {
'text/x-typescript': ['ts', 'tsx']
},

browsers: ['Chrome'],

port: 9018,
runnerPort: 9101,
colors: true,
logLevel: karma.LOG_INFO,
autoWatch: true,
singleRun: false,
webpackServer: { noInfo: true },
webpack: {
devtool: 'inline-source-map',
resolve: {
extensions: ['.ts', '.js']
},
module: {
loaders: [
{
test: /\.ts?$/,
exclude: /(node_modules)/,
loader: 'ts-loader'
},
{
enforce: 'post',
test: /\.(js|ts)$/, loader: 'istanbul-instrumenter-loader',
include: path.resolve(__dirname, 'src'),
exclude: [
/\.(e2e|spec|bundle)\.ts$/,
/node_modules/
]
}
]
},
plugins: [
new webpack.LoaderOptionsPlugin({
options: {
tslint: {
emitErrors: false,
failOnHint: false,
resourcePath: 'src'
}
}
}),
new webpack.ContextReplacementPlugin(
// The (\\|\/) piece accounts for path separators in *nix and Windows
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
path.resolve('src'),
{}
)
]
}
});
};
45 changes: 45 additions & 0 deletions modules/db/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "@ngrx/db",
"version": "2.0.1",
"description": "RxJS + IndexedDB for Angular",
"main": "bundles/db.umd.js",
"module": "index.js",
"scripts": {
"test:unit": "karma start --single-run",
"test": "npm run test:unit",
"clean:pre": "rimraf release",
"clean:post": "rimraf \"release/**/*.ngfactory.ts\" \"release/node_modules\"",
"copy": "cpy LICENSE package.json README.md release",
"build:js": "ngc -p tsconfig.dist.json",
"build:umd": "rollup -c rollup.config.js",
"build:uglify": "uglifyjs -c --screw-ie8 --comments -o ./release/bundles/db.min.umd.js ./release/bundles/db.umd.js",
"prebuild": "npm run test && npm run clean:pre",
"postbuild": "npm run clean:post && npm run copy",
"build": "npm run build:js && npm run build:umd && npm run build:uglify"
},
"author": "Rob Wormald <[email protected]>",
"license": "MIT",
"peerDependencies": {
"@angular/core": "^4.0.0",
"rxjs": "^5.0.0"
},
"devDependencies": {
"@angular/compiler-cli": "^4.0.0",
"@ngrx/core": "^1.2.0",
"cpy-cli": "^1.0.1",
"istanbul-instrumenter-loader": "^0.2.0",
"jasmine": "^2.5.3",
"karma": "^1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-coverage": "^1.1.1",
"karma-jasmine": "^1.0.2",
"karma-mocha-reporter": "^2.1.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^2.0.0",
"rimraf": "^2.5.4",
"rollup": "^0.41.4",
"ts-loader": "^2.0.0",
"webpack": "^2.2.0",
"uglifyjs": "^2.4.10"
}
}
15 changes: 15 additions & 0 deletions modules/db/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default {
entry: './release/index.js',
dest: './release/bundles/db.umd.js',
format: 'umd',
moduleName: 'ngrx.db',
globals: {
'@angular/core': 'ng.core',

'rxjs/Observable': 'Rx',
'rxjs/Subject': 'Rx',
'rxjs/operator/mergeMap': 'Rx.Observable.prototype',
'rxjs/operator/do': 'Rx.Observable.prototype',
'rxjs/observable/from': 'Rx.Observable'
}
}
191 changes: 191 additions & 0 deletions modules/db/spec/database.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import { TestBed, inject } from '@angular/core/testing';
import * as DB from '../src/database';
import 'rxjs/add/operator/toArray';


const todoAppSchema: DB.DBSchema = {
version: 1,
name: 'todo_app',
stores: {
'todos': {autoIncrement: true},
'categories': {autoIncrement: true},
'friends': {autoIncrement: true},
'users': {autoIncrement: true, primaryKey: 'userID'}
}
};


// cleanup function
const deleteDatabase = (done: any) => {

let del = indexedDB.deleteDatabase(todoAppSchema.name);

del.onerror = (err) => {
(<any>del).onblocked = undefined;
console.error(err);
throw err;
};
del.onsuccess = () => {
done();
};
};

describe('database functionality', () => {

let idb: DB.Database;
let dbBackend: any;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
DB.DBModule.provideDB(todoAppSchema)
]
})
});

beforeEach(inject([ DB.Database ], (_idb: DB.Database) => {
idb = _idb;
}))

beforeAll((done) => {
deleteDatabase(done);
});

it('should instantiate a DB', () => {
expect(idb).toBeDefined();
});

it('should open successfully', (done) => {
let openReq = idb.open(todoAppSchema.name);

openReq.subscribe(db => {
expect(db).toBeDefined();
expect(db.objectStoreNames.length).toBe(4);
done();
});
});

it('should insert some data', (done) => {
idb.insert('todos', [{name: 'todo1'}, {name: 'todo2'}])
.toArray()
.subscribe((results: any) => {
expect(results[0]).toEqual({$key: 1, name: 'todo1'});
expect(results[1]).toEqual({$key: 2, name: 'todo2'});
done();
}, (err: any) => {
console.error(err);
done.fail(err);
});
});

it('should insert some more data', (done) => {
idb.insert('todos', [{name: 'todo3'}, {name: 'todo4'}])
.toArray()
.subscribe((results: any) => {
expect(results[0]).toEqual({$key: 3, name: 'todo3'});
expect(results[1]).toEqual({$key: 4, name: 'todo4'});
done();
}, (err: any) => {
console.error(err);
done.fail(err);
});
});

it('should update existing data', (done) => {
idb.insert('todos', [{$key: 3, name: 'todo3++'}, {$key: 4, name: 'todo4++'}])
.toArray()
.subscribe((results: any) => {
expect(results[0]).toEqual({$key: 3, name: 'todo3++'});
expect(results[1]).toEqual({$key: 4, name: 'todo4++'});
done();
}, (err: any) => {
console.error(err);
done.fail(err);
});
});

it('should insert some more data with a primary key', (done) => {
idb.insert('users', [{userID: 'user1'}, {userID: 'user2'}])
.toArray()
.subscribe((results: any) => {
expect(results[0]).toEqual({userID: 'user1'});
expect(results[1]).toEqual({userID: 'user2'});
done();
}, (err: any) => {
console.error(err);
done.fail(err);
});
});

it('should broadcast notifications on insert', (done) => {

let notificationCount = 0;

idb.changes.subscribe(notif => {
notificationCount++;
});
idb.insert('todos', [{name: 'todo5'}, {name: 'todo6'}])
.toArray()
.subscribe(() => {}, () => {}, () => {
expect(notificationCount).toBe(2);
done();
});
});

it('should get a record by key', (done) => {
let found: any;
idb.get('todos', 2)
.subscribe((record: any) => {
found = record;
}, (err: any) => {
console.error(err);
done.fail(err);
}, () => {
expect(found).toEqual({name: 'todo2'});
done();
});
});

it('should get a record by primaryKey', (done) => {
let found: any;
idb.get('users', 'user1')
.subscribe((record: any) => {
found = record;
}, (err: any) => {
console.error(err);
done.fail(err);
}, () => {
expect(found).toEqual({userID: 'user1'});
done();
});
});

it('should iterate records', (done) => {
let found: any;
idb.query('todos').toArray()
.subscribe((records: any) => {
found = records;
}, (err: any) => {
console.error(err);
done.fail(err);
}, () => {
expect(found.length).toEqual(6);
done();
});
});

it('should iterate records with a predicate fn', (done) => {
let found: any;
idb.query('todos', (rec) => rec.name === 'todo5').toArray()
.subscribe((records: any) => {
found = records;
}, (err: any) => {
console.error(err);
done.fail(err);
}, () => {
expect(found.length).toEqual(1);
done();
});
});

});
Loading