Skip to content

Commit ef75816

Browse files
author
Dawson Toth
committed
mock
0 parents  commit ef75816

12 files changed

+339
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
logs
3+
coverage

.npmignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
logs

Gruntfile.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module.exports = function(grunt) {
2+
3+
// Project configuration.
4+
grunt.initConfig({
5+
mochaTest: {
6+
options: {
7+
timeout: 30000,
8+
reporter: 'spec',
9+
ignoreLeaks: false
10+
},
11+
src: ['test/**/*.js']
12+
},
13+
jshint: {
14+
options: {
15+
jshintrc: true
16+
},
17+
src: ['lib/**/*.js', 'test/**/*.js']
18+
},
19+
kahvesi: {
20+
src: ['test/**/*.js']
21+
},
22+
clean: ['tmp']
23+
});
24+
25+
// Load grunt plugins for modules
26+
grunt.loadNpmTasks('grunt-mocha-test');
27+
grunt.loadNpmTasks('grunt-contrib-jshint');
28+
grunt.loadNpmTasks('grunt-contrib-clean');
29+
grunt.loadNpmTasks('grunt-kahvesi');
30+
31+
// register tasks
32+
grunt.registerTask('cover', ['kahvesi', 'clean']);
33+
grunt.registerTask('default', ['jshint', 'mochaTest', 'clean']);
34+
};

LICENSE

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Copyright 2014 by Appcelerator, Inc.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
15+
16+
This source code may contain patents and patents pending by Appcelerator, Inc.

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Mobware Composite Connector
2+
3+
This is a composite APIBuilder connector.
4+
5+
> This software is pre-release and not yet ready for usage. Please don't use this just yet while we're working through testing and finishing it up. Once it's ready, we'll make an announcement about it.
6+
7+
To install:
8+
9+
```bash
10+
$ appc install appc.composite --save
11+
```
12+
13+
# License
14+
15+
This source code is licensed as part of the Appcelerator Enterprise Platform and subject to the End User License Agreement and Enterprise License and Ordering Agreement. Copyright (c) 2014 by Appcelerator, Inc. All Rights Reserved. This source code is Proprietary and Confidential to Appcelerator, Inc.

app.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var APIBuilder = require('apibuilder'),
2+
server = new APIBuilder(),
3+
ConnectorFactory = require('./lib'),
4+
Connector = ConnectorFactory.create(APIBuilder, server),
5+
connector = new Connector();
6+
7+
server.addModel(APIBuilder.createModel('article', {
8+
fields: {
9+
title: { type: String, source: 'mongo' },
10+
content: { type: String, source: 'mongo' },
11+
author_id: { type: String, source: 'mongo' },
12+
author_first_name: { type: String, source: 'mysql', name: 'first_name', required: false },
13+
author_last_name: { type: String, source: 'mysql', name: 'last_name', required: false }
14+
},
15+
connector: connector, // aka 'appc.composite'
16+
17+
metadata: {
18+
composite: {
19+
sources: [
20+
{
21+
id: 'mongo',
22+
connector: 'appc.mongo',
23+
metadata: {
24+
mongo: {
25+
table: 'post'
26+
}
27+
}
28+
},
29+
{
30+
id: 'mysql',
31+
connector: 'appc.mysql',
32+
metadata: {
33+
mysql: {
34+
table: 'user'
35+
}
36+
},
37+
left_join: {
38+
mongo: {
39+
'post.author_id': 'user.id'
40+
}
41+
}
42+
}
43+
]
44+
}
45+
}
46+
47+
/*
48+
This connector handles CRUD in this way:
49+
50+
- findAll:
51+
- - with the first source, we do a findAll
52+
- - for subsequent sources, we do a query, utilizing the join, and mixing in the results
53+
54+
- findOne:
55+
- - with the first source, we do a findOne
56+
- - for subsequent sources, we do a query, utilizing the join, and mixing in the results
57+
58+
- create / save:
59+
- - none of the joined fields are required?
60+
61+
- delete:
62+
- - we don't cascade
63+
64+
*/
65+
66+
}));
67+
68+
server.start(function() {
69+
server.logger.info('server started on port', server.port);
70+
});

conf/default.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
logs: './logs',
3+
quiet: false,
4+
logLevel: 'debug',
5+
apikey: 'bYVrelF3EQ8qGaJj/SoSlTyP6IhtA+1Y',
6+
admin: {
7+
enabled: true,
8+
prefix: '/mobware'
9+
}
10+
};

index.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
exports = module.exports = require('./lib');
2+
3+
// if this is being run directly, then attempt to load it as
4+
// server instead of module
5+
if (module.id === '.') {
6+
var fs = require('fs'),
7+
path = require('path'),
8+
appjs = path.join(__dirname, 'app.js');
9+
if (fs.existsSync(appjs)){
10+
try {
11+
require(appjs);
12+
}
13+
catch (E) {
14+
console.error(E)
15+
}
16+
}
17+
}

lib/index.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
var appc = require('appcelerator'),
2+
_ = appc.lodash,
3+
pkginfo = require('pkginfo')(module) && module.exports;
4+
5+
// --------- Composite Connector -------
6+
7+
exports.create = function(APIBuilder, server) {
8+
9+
var Connector = APIBuilder.Connector,
10+
Collection = APIBuilder.Collection;
11+
12+
return Connector.extend({
13+
config: APIBuilder.Loader(),
14+
name: 'composite',
15+
pkginfo: _.pick(pkginfo, 'name', 'version', 'description', 'author', 'license', 'keywords', 'repository'),
16+
logger: APIBuilder.createLogger({}, { name: 'api-connector-composite', useConsole: true, level: 'debug' }),
17+
18+
/*
19+
Lifecycle.
20+
*/
21+
constructor: function() {},
22+
connect: doNext,
23+
disconnect: doNext,
24+
25+
/*
26+
Introspection.
27+
*/
28+
fetchConfig: doNext,
29+
fetchMetadata: doNext,
30+
fetchSchema: doNext,
31+
32+
/*
33+
CRUD.
34+
*/
35+
create: function(Model, values, next) {
36+
var instance = Model.instance(values, false);
37+
next(null, instance);
38+
},
39+
findAll: fakeFindCollection,
40+
findOne: fakeFindModel,
41+
query: fakeFindCollection,
42+
save: function(Model, instance, next) {
43+
next(null, instance);
44+
},
45+
'delete': function(Model, instance, next) {
46+
next(null, instance);
47+
}
48+
});
49+
50+
function doNext(next) {
51+
next();
52+
}
53+
54+
function fakeFindModel(Model, value, next) {
55+
var instance = Model.instance({
56+
title: 'My Title',
57+
content: 'My Content',
58+
author_id: '6e75cad0-7b24-11e4-8cce-b54dac11af63',
59+
author_first_name: 'Dawson',
60+
author_last_name: 'Toth'
61+
}, true);
62+
instance.setPrimaryKey('e7f12590-7b23-11e4-8cce-b54dac11af63');
63+
next(null, instance);
64+
}
65+
66+
function fakeFindCollection(Model, next) {
67+
var array = [];
68+
for (var c = 0; c < 4; c++) {
69+
var instance = Model.instance({
70+
title: 'My Title',
71+
content: 'My Content',
72+
author_id: '6e75cad0-7b24-11e4-8cce-b54dac11af63',
73+
author_first_name: 'Dawson',
74+
author_last_name: 'Toth'
75+
}, true);
76+
instance.setPrimaryKey('e7f12590-7b23-11e4-8cce-b54dac11af63');
77+
array.push(instance);
78+
}
79+
next(null, new Collection(Model, array));
80+
}
81+
82+
};

package.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "appc.composite",
3+
"description": "Composite connector",
4+
"version": "1.0.0",
5+
"author": "Dawson Toth",
6+
"maintainers": [
7+
"Dawson Toth <[email protected]>"
8+
],
9+
"license": "Apache2",
10+
"framework": "none",
11+
"keywords": [
12+
"apibuilder",
13+
"apibuilder-connector"
14+
],
15+
"repository": {},
16+
"scripts": {
17+
"test": "grunt",
18+
"start": "node ./app.js"
19+
},
20+
"private": true,
21+
"dependencies": {
22+
"appcelerator": "*",
23+
"highlight.js": "^8.2.0",
24+
"pkginfo": "^0.3.0"
25+
},
26+
"devDependencies": {
27+
"apibuilder": "*",
28+
"async": "^0.9.0",
29+
"grunt": "^0.4.5",
30+
"grunt-contrib-clean": "^0.6.0",
31+
"grunt-contrib-jshint": "^0.10.0",
32+
"grunt-kahvesi": "^0.1.0",
33+
"grunt-mocha-test": "^0.12.0",
34+
"should": "^4.0.4"
35+
},
36+
"appcelerator": {}
37+
}

test/.jshintrc

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"browser": false,
3+
"camelcase": false,
4+
"curly": true,
5+
"eqeqeq": true,
6+
"expr": true,
7+
"immed": true,
8+
"indent": 4,
9+
"latedef": false,
10+
"newcap": true,
11+
"noarg": true,
12+
"nonew": true,
13+
"undef": true,
14+
"unused": false,
15+
"trailing": true,
16+
"loopfunc": true,
17+
"proto": true,
18+
"node": true,
19+
"-W104": true,
20+
"-W068": true,
21+
"globals": {
22+
"describe": false,
23+
"it": false,
24+
"before": false,
25+
"beforeEach": false,
26+
"after": false,
27+
"afterEach": false
28+
}
29+
}

test/connector.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var should = require('should'),
2+
async = require('async'),
3+
url = require('url'),
4+
APIBuilder = require('apibuilder'),
5+
Connector = require('../').create(APIBuilder),
6+
log = APIBuilder.createLogger({}, { name: 'api-connector-composite TEST', useConsole: true, level: 'info' }),
7+
connector = new Connector(),
8+
Model;
9+
10+
describe("Connector", function() {
11+
12+
before(function(next) {
13+
connector.connect(next);
14+
});
15+
16+
it('should have tests', function(next) {
17+
should(false).be.true;
18+
});
19+
20+
after(function(next) {
21+
connector.disconnect(next);
22+
});
23+
24+
});

0 commit comments

Comments
 (0)