yayson 2.0.10-quickfix → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +28 -0
- package/.github/workflows/node.js.yml +30 -0
- package/.mocharc.js +15 -0
- package/.nvmrc +1 -0
- package/README.md +77 -78
- package/RELEASE.md +1 -7
- package/babel.config.json +4 -0
- package/legacy.js +2 -0
- package/package.json +32 -26
- package/prettier.config.js +5 -0
- package/src/legacy.js +14 -0
- package/src/yayson/adapter.js +18 -0
- package/src/yayson/adapters/index.js +1 -0
- package/src/yayson/adapters/sequelize.js +11 -0
- package/src/yayson/legacy-presenter.js +121 -0
- package/src/yayson/legacy-store.js +156 -0
- package/src/yayson/presenter.js +198 -0
- package/src/yayson/store.js +194 -0
- package/src/yayson.js +23 -0
- package/tags +59 -0
- package/webpack.browser.js +27 -0
- package/webpack.common.js +39 -0
- package/webpack.dist.js +21 -0
- package/.circleci/config.yml +0 -35
- package/bower.json +0 -31
- package/coffeelint.json +0 -120
- package/gulpfile.coffee +0 -43
- package/lib/yayson/adapter.js +0 -22
- package/lib/yayson/adapters/index.js +0 -3
- package/lib/yayson/adapters/sequelize.js +0 -14
- package/lib/yayson/presenter.js +0 -206
- package/lib/yayson/store.js +0 -169
- package/lib/yayson/utils.js +0 -47
- package/lib/yayson.js +0 -53
package/webpack.dist.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const webpack = require('webpack')
|
|
2
|
+
const { merge } = require('webpack-merge')
|
|
3
|
+
const common = require('./webpack.common.js')
|
|
4
|
+
const PACKAGE = require('./package.json')
|
|
5
|
+
const TerserPlugin = require('terser-webpack-plugin')
|
|
6
|
+
|
|
7
|
+
module.exports = merge(common, {
|
|
8
|
+
optimization: {
|
|
9
|
+
minimize: true,
|
|
10
|
+
minimizer: [
|
|
11
|
+
new TerserPlugin({
|
|
12
|
+
extractComments: false,
|
|
13
|
+
}),
|
|
14
|
+
],
|
|
15
|
+
},
|
|
16
|
+
plugins: [
|
|
17
|
+
new webpack.BannerPlugin({
|
|
18
|
+
banner: `yayson v ${PACKAGE.version} (${PACKAGE.homepage}) by ${PACKAGE.author}`,
|
|
19
|
+
}),
|
|
20
|
+
],
|
|
21
|
+
})
|
package/.circleci/config.yml
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
# Javascript Node CircleCI 2.0 configuration file
|
|
2
|
-
#
|
|
3
|
-
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
|
|
4
|
-
#
|
|
5
|
-
version: 2
|
|
6
|
-
jobs:
|
|
7
|
-
build:
|
|
8
|
-
docker:
|
|
9
|
-
# specify the version you desire here
|
|
10
|
-
- image: circleci/node:6.11
|
|
11
|
-
environment:
|
|
12
|
-
NODE_ENV: 'test'
|
|
13
|
-
|
|
14
|
-
working_directory: ~/repo
|
|
15
|
-
|
|
16
|
-
steps:
|
|
17
|
-
- checkout
|
|
18
|
-
- run:
|
|
19
|
-
name: Setup Environment Variables
|
|
20
|
-
command: |
|
|
21
|
-
echo 'export NODE_ENV="test"' >> $BASH_ENV
|
|
22
|
-
# Download and cache dependencies
|
|
23
|
-
- restore_cache:
|
|
24
|
-
keys:
|
|
25
|
-
- v1-dependencies-{{ checksum "package.json" }}
|
|
26
|
-
# fallback to using the latest cache if no exact match is found
|
|
27
|
-
- v1-dependencies-
|
|
28
|
-
- run: npm install
|
|
29
|
-
- save_cache:
|
|
30
|
-
paths:
|
|
31
|
-
- node_modules
|
|
32
|
-
key: v1-dependencies-{{ checksum "package.json" }}
|
|
33
|
-
|
|
34
|
-
# run tests!
|
|
35
|
-
- run: npm test
|
package/bower.json
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "yayson",
|
|
3
|
-
"description": "A library for serializing and reading JSON API standardized data in JavaScript.",
|
|
4
|
-
"main": "dist/yayson.js",
|
|
5
|
-
"authors": [
|
|
6
|
-
"Johannes Edelstam <johannes@edelst.am>",
|
|
7
|
-
"Jonny Strömberg <jonny.stromberg@gmail.com>"
|
|
8
|
-
],
|
|
9
|
-
"license": "MIT",
|
|
10
|
-
"keywords": [
|
|
11
|
-
"json-api",
|
|
12
|
-
"json",
|
|
13
|
-
"api",
|
|
14
|
-
"response",
|
|
15
|
-
"format",
|
|
16
|
-
"serialization",
|
|
17
|
-
"deserialization",
|
|
18
|
-
"yay",
|
|
19
|
-
"wow",
|
|
20
|
-
"omg"
|
|
21
|
-
],
|
|
22
|
-
"homepage": "https://github.com/confetti/yayson",
|
|
23
|
-
"moduleType": [],
|
|
24
|
-
"ignore": [
|
|
25
|
-
"**/.*",
|
|
26
|
-
"node_modules",
|
|
27
|
-
"bower_components",
|
|
28
|
-
"test",
|
|
29
|
-
"tests"
|
|
30
|
-
]
|
|
31
|
-
}
|
package/coffeelint.json
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"arrow_spacing": {
|
|
3
|
-
"level": "ignore"
|
|
4
|
-
},
|
|
5
|
-
"braces_spacing": {
|
|
6
|
-
"level": "ignore",
|
|
7
|
-
"spaces": 0
|
|
8
|
-
},
|
|
9
|
-
"camel_case_classes": {
|
|
10
|
-
"level": "error"
|
|
11
|
-
},
|
|
12
|
-
"coffeescript_error": {
|
|
13
|
-
"level": "error"
|
|
14
|
-
},
|
|
15
|
-
"colon_assignment_spacing": {
|
|
16
|
-
"level": "ignore",
|
|
17
|
-
"spacing": {
|
|
18
|
-
"left": 0,
|
|
19
|
-
"right": 0
|
|
20
|
-
}
|
|
21
|
-
},
|
|
22
|
-
"cyclomatic_complexity": {
|
|
23
|
-
"value": 10,
|
|
24
|
-
"level": "ignore"
|
|
25
|
-
},
|
|
26
|
-
"duplicate_key": {
|
|
27
|
-
"level": "error"
|
|
28
|
-
},
|
|
29
|
-
"empty_constructor_needs_parens": {
|
|
30
|
-
"level": "ignore"
|
|
31
|
-
},
|
|
32
|
-
"ensure_comprehensions": {
|
|
33
|
-
"level": "warn"
|
|
34
|
-
},
|
|
35
|
-
"indentation": {
|
|
36
|
-
"value": 2,
|
|
37
|
-
"level": "error"
|
|
38
|
-
},
|
|
39
|
-
"line_endings": {
|
|
40
|
-
"level": "ignore",
|
|
41
|
-
"value": "unix"
|
|
42
|
-
},
|
|
43
|
-
"max_line_length": {
|
|
44
|
-
"value": 180,
|
|
45
|
-
"level": "error",
|
|
46
|
-
"limitComments": true
|
|
47
|
-
},
|
|
48
|
-
"missing_fat_arrows": {
|
|
49
|
-
"level": "ignore"
|
|
50
|
-
},
|
|
51
|
-
"newlines_after_classes": {
|
|
52
|
-
"value": 3,
|
|
53
|
-
"level": "ignore"
|
|
54
|
-
},
|
|
55
|
-
"no_backticks": {
|
|
56
|
-
"level": "error"
|
|
57
|
-
},
|
|
58
|
-
"no_debugger": {
|
|
59
|
-
"level": "warn"
|
|
60
|
-
},
|
|
61
|
-
"no_empty_functions": {
|
|
62
|
-
"level": "ignore"
|
|
63
|
-
},
|
|
64
|
-
"no_empty_param_list": {
|
|
65
|
-
"level": "ignore"
|
|
66
|
-
},
|
|
67
|
-
"no_implicit_braces": {
|
|
68
|
-
"level": "ignore",
|
|
69
|
-
"strict": true
|
|
70
|
-
},
|
|
71
|
-
"no_implicit_parens": {
|
|
72
|
-
"strict": true,
|
|
73
|
-
"level": "ignore"
|
|
74
|
-
},
|
|
75
|
-
"no_interpolation_in_single_quotes": {
|
|
76
|
-
"level": "ignore"
|
|
77
|
-
},
|
|
78
|
-
"no_plusplus": {
|
|
79
|
-
"level": "ignore"
|
|
80
|
-
},
|
|
81
|
-
"no_stand_alone_at": {
|
|
82
|
-
"level": "ignore"
|
|
83
|
-
},
|
|
84
|
-
"no_tabs": {
|
|
85
|
-
"level": "error"
|
|
86
|
-
},
|
|
87
|
-
"no_throwing_strings": {
|
|
88
|
-
"level": "error"
|
|
89
|
-
},
|
|
90
|
-
"no_trailing_semicolons": {
|
|
91
|
-
"level": "error"
|
|
92
|
-
},
|
|
93
|
-
"no_trailing_whitespace": {
|
|
94
|
-
"level": "error",
|
|
95
|
-
"allowed_in_comments": false,
|
|
96
|
-
"allowed_in_empty_lines": true
|
|
97
|
-
},
|
|
98
|
-
"no_unnecessary_double_quotes": {
|
|
99
|
-
"level": "ignore"
|
|
100
|
-
},
|
|
101
|
-
"no_unnecessary_fat_arrows": {
|
|
102
|
-
"level": "warn"
|
|
103
|
-
},
|
|
104
|
-
"non_empty_constructor_needs_parens": {
|
|
105
|
-
"level": "ignore"
|
|
106
|
-
},
|
|
107
|
-
"prefer_english_operator": {
|
|
108
|
-
"level": "ignore",
|
|
109
|
-
"doubleNotLevel": "ignore"
|
|
110
|
-
},
|
|
111
|
-
"space_operators": {
|
|
112
|
-
"level": "ignore"
|
|
113
|
-
},
|
|
114
|
-
"spacing_after_comma": {
|
|
115
|
-
"level": "ignore"
|
|
116
|
-
},
|
|
117
|
-
"transform_messes_up_line_numbers": {
|
|
118
|
-
"level": "warn"
|
|
119
|
-
}
|
|
120
|
-
}
|
package/gulpfile.coffee
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
gulp = require 'gulp'
|
|
2
|
-
source = require 'vinyl-source-stream'
|
|
3
|
-
coffeeify = require 'coffeeify'
|
|
4
|
-
browserify = require 'browserify'
|
|
5
|
-
gutil = require 'gulp-util'
|
|
6
|
-
coffee = require 'gulp-coffee'
|
|
7
|
-
http = require 'http'
|
|
8
|
-
ecstatic = require 'ecstatic'
|
|
9
|
-
|
|
10
|
-
serverport = 5005
|
|
11
|
-
|
|
12
|
-
gulp.task 'browserify', ->
|
|
13
|
-
browserify('./src/yayson.coffee', {extensions: ['.coffee'], ignoreMissing: true, standalone: 'yayson'})
|
|
14
|
-
.transform(coffeeify)
|
|
15
|
-
.bundle()
|
|
16
|
-
.pipe(source('yayson.js'))
|
|
17
|
-
.pipe(gulp.dest('./dist/'))
|
|
18
|
-
|
|
19
|
-
gulp.task 'build', ->
|
|
20
|
-
gulp.src('./src/**/*.coffee')
|
|
21
|
-
.pipe(coffee({bare: true}).on('error', gutil.log))
|
|
22
|
-
.pipe(gulp.dest('./lib/'))
|
|
23
|
-
|
|
24
|
-
gulp.task 'default', gulp.series('browserify', 'build')
|
|
25
|
-
|
|
26
|
-
gulp.task 'test-serve', ->
|
|
27
|
-
http.createServer(ecstatic root: __dirname + '/' ).listen(serverport)
|
|
28
|
-
|
|
29
|
-
console.log "Listening on #{serverport}"
|
|
30
|
-
|
|
31
|
-
gulp.task 'test-coffee', ->
|
|
32
|
-
browserify('./test/browser.coffee', {extensions: ['.coffee']})
|
|
33
|
-
.transform(coffeeify)
|
|
34
|
-
.bundle()
|
|
35
|
-
.pipe(source('browser.js'))
|
|
36
|
-
.pipe(gulp.dest('./test/.tmp/'))
|
|
37
|
-
|
|
38
|
-
gulp.task 'test-watch', ->
|
|
39
|
-
gulp.watch('./src/**/*.coffee', ['test-coffee'])
|
|
40
|
-
gulp.watch('./test/**/*.coffee', ['test-coffee'])
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
gulp.task 'test-browser', gulp.series('test-watch', 'test-serve')
|
package/lib/yayson/adapter.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
var Adapter;
|
|
2
|
-
|
|
3
|
-
Adapter = class Adapter {
|
|
4
|
-
static get(model, key) {
|
|
5
|
-
if (key) {
|
|
6
|
-
return model[key];
|
|
7
|
-
}
|
|
8
|
-
return model;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
static id(model) {
|
|
12
|
-
var id;
|
|
13
|
-
id = this.get(model, 'id');
|
|
14
|
-
if (id === void 0) {
|
|
15
|
-
return id;
|
|
16
|
-
}
|
|
17
|
-
return `${id}`;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
module.exports = Adapter;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
var Adapter, SequelizeAdapter;
|
|
2
|
-
|
|
3
|
-
Adapter = require('../adapter');
|
|
4
|
-
|
|
5
|
-
SequelizeAdapter = class SequelizeAdapter extends Adapter {
|
|
6
|
-
static get(model, key) {
|
|
7
|
-
if (model != null) {
|
|
8
|
-
return model.get(key);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
module.exports = SequelizeAdapter;
|
package/lib/yayson/presenter.js
DELETED
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
module.exports = function(utils, adapter) {
|
|
2
|
-
var Presenter;
|
|
3
|
-
Presenter = (function() {
|
|
4
|
-
var buildLinks;
|
|
5
|
-
|
|
6
|
-
class Presenter {
|
|
7
|
-
constructor(scope = {}) {
|
|
8
|
-
this.scope = scope;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
id(instance) {
|
|
12
|
-
return this.constructor.adapter.id(instance);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
selfLinks(instance) {}
|
|
16
|
-
|
|
17
|
-
links() {}
|
|
18
|
-
|
|
19
|
-
relationships() {}
|
|
20
|
-
|
|
21
|
-
attributes(instance) {
|
|
22
|
-
var attributes, key, relationships;
|
|
23
|
-
if (instance == null) {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
attributes = utils.clone(this.constructor.adapter.get(instance));
|
|
27
|
-
delete attributes['id'];
|
|
28
|
-
relationships = this.relationships();
|
|
29
|
-
for (key in relationships) {
|
|
30
|
-
delete attributes[key];
|
|
31
|
-
}
|
|
32
|
-
return attributes;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
includeRelationships(scope, instance) {
|
|
36
|
-
var data, factory, key, presenter, relationships, results;
|
|
37
|
-
relationships = this.relationships();
|
|
38
|
-
results = [];
|
|
39
|
-
for (key in relationships) {
|
|
40
|
-
factory = relationships[key] || (function() {
|
|
41
|
-
throw new Error(`Presenter for ${key} in ${this.type} is not defined`);
|
|
42
|
-
}).call(this);
|
|
43
|
-
presenter = new factory(scope);
|
|
44
|
-
data = this.constructor.adapter.get(instance, key);
|
|
45
|
-
if (data != null) {
|
|
46
|
-
results.push(presenter.toJSON(data, {
|
|
47
|
-
include: true
|
|
48
|
-
}));
|
|
49
|
-
} else {
|
|
50
|
-
results.push(void 0);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return results;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
buildRelationships(instance) {
|
|
57
|
-
var build, buildData, data, key, links, presenter, relationships, rels;
|
|
58
|
-
if (instance == null) {
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
rels = this.relationships();
|
|
62
|
-
links = this.links(instance) || {};
|
|
63
|
-
relationships = null;
|
|
64
|
-
for (key in rels) {
|
|
65
|
-
data = this.constructor.adapter.get(instance, key);
|
|
66
|
-
presenter = rels[key];
|
|
67
|
-
buildData = (d) => {
|
|
68
|
-
return data = {
|
|
69
|
-
id: this.constructor.adapter.id(d),
|
|
70
|
-
type: presenter.prototype.type
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
build = (d) => {
|
|
74
|
-
var rel;
|
|
75
|
-
rel = {};
|
|
76
|
-
if (d != null) {
|
|
77
|
-
rel.data = buildData(d);
|
|
78
|
-
}
|
|
79
|
-
if (links[key] != null) {
|
|
80
|
-
rel.links = buildLinks(links[key]);
|
|
81
|
-
} else if (d == null) {
|
|
82
|
-
rel.data = null;
|
|
83
|
-
}
|
|
84
|
-
return rel;
|
|
85
|
-
};
|
|
86
|
-
relationships || (relationships = {});
|
|
87
|
-
relationships[key] || (relationships[key] = {});
|
|
88
|
-
if (data instanceof Array) {
|
|
89
|
-
relationships[key].data = data.map(buildData);
|
|
90
|
-
if (links[key] != null) {
|
|
91
|
-
relationships[key].links = buildLinks(links[key]);
|
|
92
|
-
}
|
|
93
|
-
} else {
|
|
94
|
-
relationships[key] = build(data);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return relationships;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
buildSelfLink(instance) {
|
|
101
|
-
return buildLinks(this.selfLinks(instance));
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
toJSON(instanceOrCollection, options = {}) {
|
|
105
|
-
var added, base, base1, base2, collection, instance, links, model, relationships;
|
|
106
|
-
if (options.meta != null) {
|
|
107
|
-
this.scope.meta = options.meta;
|
|
108
|
-
}
|
|
109
|
-
(base = this.scope).data || (base.data = null);
|
|
110
|
-
if (instanceOrCollection == null) {
|
|
111
|
-
return this.scope;
|
|
112
|
-
}
|
|
113
|
-
if (instanceOrCollection instanceof Array) {
|
|
114
|
-
collection = instanceOrCollection;
|
|
115
|
-
(base1 = this.scope).data || (base1.data = []);
|
|
116
|
-
collection.forEach((instance) => {
|
|
117
|
-
return this.toJSON(instance, options);
|
|
118
|
-
});
|
|
119
|
-
} else {
|
|
120
|
-
instance = instanceOrCollection;
|
|
121
|
-
added = true;
|
|
122
|
-
model = {
|
|
123
|
-
id: this.id(instance),
|
|
124
|
-
type: this.type,
|
|
125
|
-
attributes: this.attributes(instance)
|
|
126
|
-
};
|
|
127
|
-
if (model.id === void 0) {
|
|
128
|
-
delete model.id;
|
|
129
|
-
}
|
|
130
|
-
relationships = this.buildRelationships(instance);
|
|
131
|
-
if (relationships != null) {
|
|
132
|
-
model.relationships = relationships;
|
|
133
|
-
}
|
|
134
|
-
links = this.buildSelfLink(instance);
|
|
135
|
-
if (links != null) {
|
|
136
|
-
model.links = links;
|
|
137
|
-
}
|
|
138
|
-
if (options.include) {
|
|
139
|
-
(base2 = this.scope).included || (base2.included = []);
|
|
140
|
-
if (!utils.any(this.scope.included.concat(this.scope.data), function(i) {
|
|
141
|
-
return i.id === model.id && i.type === model.type;
|
|
142
|
-
})) {
|
|
143
|
-
this.scope.included.push(model);
|
|
144
|
-
} else {
|
|
145
|
-
added = false;
|
|
146
|
-
}
|
|
147
|
-
} else if (this.scope.data != null) {
|
|
148
|
-
if (!(this.scope.data instanceof Array && utils.any(this.scope.data, function(i) {
|
|
149
|
-
return i.id === model.id;
|
|
150
|
-
}))) {
|
|
151
|
-
this.scope.data.push(model);
|
|
152
|
-
} else {
|
|
153
|
-
added = false;
|
|
154
|
-
}
|
|
155
|
-
} else {
|
|
156
|
-
this.scope.data = model;
|
|
157
|
-
}
|
|
158
|
-
if (added) {
|
|
159
|
-
this.includeRelationships(this.scope, instance);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return this.scope;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
render(instanceOrCollection, options) {
|
|
166
|
-
if (utils.isPromise(instanceOrCollection)) {
|
|
167
|
-
return instanceOrCollection.then((data) => {
|
|
168
|
-
return this.toJSON(data, options);
|
|
169
|
-
});
|
|
170
|
-
} else {
|
|
171
|
-
return this.toJSON(instanceOrCollection, options);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
static toJSON() {
|
|
176
|
-
return (new this).toJSON(...arguments);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
static render() {
|
|
180
|
-
return (new this).render(...arguments);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
buildLinks = function(link) {
|
|
186
|
-
if (link == null) {
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
if ((link.self != null) || (link.related != null)) {
|
|
190
|
-
return link;
|
|
191
|
-
} else {
|
|
192
|
-
return {
|
|
193
|
-
self: link
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
};
|
|
197
|
-
|
|
198
|
-
Presenter.adapter = adapter;
|
|
199
|
-
|
|
200
|
-
Presenter.prototype.type = 'objects';
|
|
201
|
-
|
|
202
|
-
return Presenter;
|
|
203
|
-
|
|
204
|
-
}).call(this);
|
|
205
|
-
return module.exports = Presenter;
|
|
206
|
-
};
|
package/lib/yayson/store.js
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
module.exports = function(utils) {
|
|
2
|
-
var Record, Store;
|
|
3
|
-
Record = class Record {
|
|
4
|
-
constructor(options) {
|
|
5
|
-
({id: this.id, type: this.type, attributes: this.attributes, relationships: this.relationships, links: this.links, meta: this.meta} = options);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
};
|
|
9
|
-
return Store = class Store {
|
|
10
|
-
constructor(options) {
|
|
11
|
-
this.reset();
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
reset() {
|
|
15
|
-
this.records = [];
|
|
16
|
-
return this.relations = {};
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
toModel(rec, type, models) {
|
|
20
|
-
var base, currentModel, data, key, links, meta, model, name, ref, rel, resolve, typeAttribute;
|
|
21
|
-
model = utils.clone(rec.attributes) || {};
|
|
22
|
-
if (model.type) {
|
|
23
|
-
typeAttribute = model.type;
|
|
24
|
-
}
|
|
25
|
-
model.id = rec.id;
|
|
26
|
-
model.type = rec.type;
|
|
27
|
-
models[type] || (models[type] = {});
|
|
28
|
-
(base = models[type])[name = rec.id] || (base[name] = model);
|
|
29
|
-
if (model.hasOwnProperty('meta')) {
|
|
30
|
-
model.attributes = {
|
|
31
|
-
meta: model.meta
|
|
32
|
-
};
|
|
33
|
-
delete model.meta;
|
|
34
|
-
}
|
|
35
|
-
if (rec.meta != null) {
|
|
36
|
-
model.meta = rec.meta;
|
|
37
|
-
}
|
|
38
|
-
if (rec.links != null) {
|
|
39
|
-
model.links = rec.links;
|
|
40
|
-
}
|
|
41
|
-
if (rec.relationships != null) {
|
|
42
|
-
ref = rec.relationships;
|
|
43
|
-
for (key in ref) {
|
|
44
|
-
rel = ref[key];
|
|
45
|
-
data = rel.data;
|
|
46
|
-
links = rel.links;
|
|
47
|
-
meta = rel.meta;
|
|
48
|
-
model[key] = null;
|
|
49
|
-
if (!((data != null) || (links != null))) {
|
|
50
|
-
continue;
|
|
51
|
-
}
|
|
52
|
-
resolve = ({type, id}) => {
|
|
53
|
-
return this.find(type, id, models);
|
|
54
|
-
};
|
|
55
|
-
model[key] = data instanceof Array ? data.map(resolve) : data != null ? resolve(data) : {};
|
|
56
|
-
// Model of the relation
|
|
57
|
-
currentModel = model[key];
|
|
58
|
-
if (currentModel != null) {
|
|
59
|
-
// retain the links and meta from the relationship entry
|
|
60
|
-
// use as underscore property name because the currentModel may also have a link and meta reference
|
|
61
|
-
currentModel._links = links || {};
|
|
62
|
-
currentModel._meta = meta || {};
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
if (typeAttribute) {
|
|
67
|
-
model.type = typeAttribute;
|
|
68
|
-
}
|
|
69
|
-
return model;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
findRecord(type, id) {
|
|
73
|
-
return utils.find(this.records, function(r) {
|
|
74
|
-
return r.type === type && r.id === id;
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
findRecords(type) {
|
|
79
|
-
return utils.filter(this.records, function(r) {
|
|
80
|
-
return r.type === type;
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
find(type, id, models = {}) {
|
|
85
|
-
var rec;
|
|
86
|
-
rec = this.findRecord(type, id);
|
|
87
|
-
if (rec == null) {
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
models[type] || (models[type] = {});
|
|
91
|
-
return models[type][id] || this.toModel(rec, type, models);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
findAll(type, models = {}) {
|
|
95
|
-
var recs;
|
|
96
|
-
recs = this.findRecords(type);
|
|
97
|
-
if (recs == null) {
|
|
98
|
-
return [];
|
|
99
|
-
}
|
|
100
|
-
recs.forEach((rec) => {
|
|
101
|
-
models[type] || (models[type] = {});
|
|
102
|
-
return this.toModel(rec, type, models);
|
|
103
|
-
});
|
|
104
|
-
return utils.values(models[type]);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
remove(type, id) {
|
|
108
|
-
var records, remove;
|
|
109
|
-
remove = (record) => {
|
|
110
|
-
var index;
|
|
111
|
-
index = this.records.indexOf(record);
|
|
112
|
-
if (!(index < 0)) {
|
|
113
|
-
return this.records.splice(index, 1);
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
if (id != null) {
|
|
117
|
-
return remove(this.findRecord(type, id));
|
|
118
|
-
} else {
|
|
119
|
-
records = this.findRecords(type);
|
|
120
|
-
return records.map(remove);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
sync(body) {
|
|
125
|
-
var models, recs, result, sync;
|
|
126
|
-
sync = (data) => {
|
|
127
|
-
var add;
|
|
128
|
-
if (data == null) {
|
|
129
|
-
return null;
|
|
130
|
-
}
|
|
131
|
-
add = (obj) => {
|
|
132
|
-
var id, rec, type;
|
|
133
|
-
({type, id} = obj);
|
|
134
|
-
this.remove(type, id);
|
|
135
|
-
rec = new Record(obj);
|
|
136
|
-
this.records.push(rec);
|
|
137
|
-
return rec;
|
|
138
|
-
};
|
|
139
|
-
if (data instanceof Array) {
|
|
140
|
-
return data.map(add);
|
|
141
|
-
} else {
|
|
142
|
-
return add(data);
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
sync(body.included);
|
|
146
|
-
recs = sync(body.data);
|
|
147
|
-
if (recs == null) {
|
|
148
|
-
return null;
|
|
149
|
-
}
|
|
150
|
-
models = {};
|
|
151
|
-
result = null;
|
|
152
|
-
if (recs instanceof Array) {
|
|
153
|
-
result = recs.map((rec) => {
|
|
154
|
-
return this.toModel(rec, rec.type, models);
|
|
155
|
-
});
|
|
156
|
-
} else {
|
|
157
|
-
result = this.toModel(recs, recs.type, models);
|
|
158
|
-
}
|
|
159
|
-
if (body.hasOwnProperty('links')) {
|
|
160
|
-
result.links = body.links;
|
|
161
|
-
}
|
|
162
|
-
if (body.hasOwnProperty('meta')) {
|
|
163
|
-
result.meta = body.meta;
|
|
164
|
-
}
|
|
165
|
-
return result;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
};
|
|
169
|
-
};
|