Skip to content

Commit d67ff61

Browse files
BlairJBlairJ
authored andcommitted
Prepare to create bowerpackage
1 parent 5353625 commit d67ff61

File tree

2 files changed

+32
-232
lines changed

2 files changed

+32
-232
lines changed

bower.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{
22
"name": "js-data-jsonapi",
33
"description": "JsonApi adapter for js-data.",
4+
"main": "./dist/js-data-jsonapi.js",
45
"homepage": "https://github.com/blairallegrotech/js-data-jsonapi",
56
"repository": {
67
"type": "git",
78
"url": "https://github.com/blairallegrotech/js-data-jsonapi.git"
89
},
910
"author": {
10-
"name": "Blair Jacobs"
11+
"name": "Blair Jacobs"
1112
},
12-
"main": "./dist/js-data-jsonapi.js",
13+
"license": "MIT",
14+
1315
"ignore": [
1416
".idea/",
1517
".*",
@@ -33,7 +35,7 @@
3335
"karma.start.js"
3436
],
3537
"dependencies": {
36-
"js-data": ">=2.0.0",
37-
"js-data-http": ">=2.0.0"
38+
"js-data": "^2.8.2 <3",
39+
"js-data-http": "^2.1.2 <3"
3840
}
3941
}

src/JsonApiSerializer.ts

Lines changed: 26 additions & 228 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ export class JsonApiHelper {
706706
for (var dataId in data[dataType]) {
707707

708708
if (data[dataType][dataId]) {
709-
jsDataArray.push( data[dataType][dataId] );
709+
jsDataArray.push(data[dataType][dataId]);
710710
}
711711
}
712712
}
@@ -788,209 +788,6 @@ export class JsonApiHelper {
788788
}
789789
}
790790

791-
// Obsolete to be removed
792-
/*
793-
private static ReplaceModelPlaceHolderRelations(included: any, options: SerializationOptions) {
794-
//Store data included as type,id key pairs
795-
if (included) {
796-
// Replace data references with included data where available
797-
for (var dataType in included) {
798-
799-
if (included[dataType]) {
800-
for (var dataId in included[dataType]) {
801-
802-
if (included[dataType][dataId]) {
803-
var includedObject = included[dataType][dataId];
804-
805-
for (var prop in includedObject) {
806-
807-
if (DSUTILS.isArray(includedObject[prop])) {
808-
DSUTILS.forEach(<Array<Object>>(includedObject[prop]), (item: Object, index: number, source: Array<Object>) => {
809-
if (item.constructor === ModelPlaceHolder) {
810-
let placeholder = <ModelPlaceHolder>item;
811-
812-
var itemOptions = options.getResource(placeholder.type);
813-
814-
// Convert any remaining model place holders to objects
815-
let metaData = new MetaData(placeholder.type);
816-
metaData.isJsonApiReference = true;
817-
818-
let newItem = <any>{};
819-
newItem[JSONAPI_META] = metaData;
820-
newItem[itemOptions.idAttribute] = placeholder.id;
821-
822-
// Apply foreign key to js-data object
823-
if (placeholder.foreignKeyName) {
824-
newItem[placeholder.foreignKeyName] = placeholder.foreignKeyValue;
825-
}
826-
827-
// Replace item in array with data from included or data, data
828-
source[index] = newItem;
829-
830-
// Attach liftime events
831-
JsonApiHelper.AssignLifeTimeEvents(itemOptions.def());
832-
}
833-
});
834-
} else {
835-
let item = includedObject[prop];
836-
if (item.constructor === ModelPlaceHolder) {
837-
let placeholder = <ModelPlaceHolder>item;
838-
839-
var itemOptions = options.getResource(placeholder.type);
840-
841-
// Convert any remaining model place holders to objects
842-
let metaData = new MetaData(placeholder.type);
843-
metaData.isJsonApiReference = true;
844-
845-
let newItem = <any>{};
846-
newItem[JSONAPI_META] = metaData;
847-
newItem[itemOptions.idAttribute] = placeholder.id;
848-
849-
// Apply foreign key to js-data object
850-
if (placeholder.foreignKeyName) {
851-
newItem[placeholder.foreignKeyName] = placeholder.foreignKeyValue;
852-
}
853-
854-
// Replace item in array with data from included or data, data
855-
includedObject[prop] = newItem;
856-
857-
// Attach liftime events
858-
JsonApiHelper.AssignLifeTimeEvents(itemOptions.def());
859-
}
860-
}
861-
}
862-
}
863-
}
864-
}
865-
}
866-
}
867-
}
868-
*/
869-
870-
/**
871-
* @Obsolete
872-
* @name NormaliseDataObjectGraph
873-
* @desc Convert all data relationshipts to real objects taken from 1 Included data, then data or joiningdata
874-
* @param {any} data Json data
875-
* @param {JSData.DSResourceDefinition} Resourceconfigurations
876-
* @param {Object} id Resource ID, primary key Number or query parameters
877-
* @param {JSData.DSConfiguration} options override default configuration options
878-
* @returns {string} JsonApi rest service Url
879-
* @memberOf JsonApiAdapter
880-
*/
881-
/*
882-
private static NormaliseDataObjectGraph(data: any, included: any, jsDataJoiningTables: any, options: SerializationOptions) : Array<any> {
883-
884-
// This is an array of top level objects with child, object references and included objects
885-
var jsDataArray = [];
886-
887-
if (data) {
888-
// Replace data references with included data where available
889-
for (var dataType in data) {
890-
891-
if (data[dataType]) {
892-
for (var dataId in data[dataType]) {
893-
894-
if (data[dataType][dataId]) {
895-
var dataObject = data[dataType][dataId];
896-
897-
for (var prop in dataObject) {
898-
899-
if (DSUTILS.isArray(dataObject[prop])) {
900-
901-
// hasMany Relationship
902-
DSUTILS.forEach(dataObject[prop], (item: ModelPlaceHolder, index: number, source: Array<ModelPlaceHolder>) => {
903-
//If included or data or joining data contains the reference we are looking for then use it
904-
let newItem = included[item.type] ? included[item.type][item.id] :
905-
(data[item.type] ? data[item.type][item.id] :
906-
(jsDataJoiningTables[item.type] ? jsDataJoiningTables[item.type][item.id] : null));
907-
908-
if (newItem) {
909-
//Included item found!!
910-
// Apply foreign key to js-data object
911-
if (item.foreignKeyName) {
912-
newItem[item.foreignKeyName] = item.foreignKeyValue;
913-
}
914-
915-
// Replace item in array with data from included or data, data
916-
source[index] = newItem;
917-
} else {
918-
// Is is possible to store arrays in js-data that are not related items?
919-
if (item.constructor === ModelPlaceHolder) {
920-
921-
// Replace item in array with plain object, but with Primary key or any foreign keys set
922-
var itemOptions = options.getResource(item.type);
923-
924-
let metaData = new MetaData(item.type);
925-
metaData.isJsonApiReference = true;
926-
927-
let newItem = <any>{};
928-
newItem[JSONAPI_META] = metaData;
929-
newItem[itemOptions.idAttribute] = item.id;
930-
931-
// Apply foreign key to js-data object
932-
if (item.foreignKeyName) {
933-
newItem[item.foreignKeyName] = item.foreignKeyValue;
934-
}
935-
936-
// Replace item in array with data from included or data, data
937-
source[index] = newItem;
938-
939-
// Attach liftime events
940-
JsonApiHelper.AssignLifeTimeEvents(itemOptions.def());
941-
}
942-
}
943-
});
944-
} else {
945-
// hasOne or parent Relationship
946-
if (dataObject[prop].constructor === ModelPlaceHolder) {
947-
var item: ModelPlaceHolder = dataObject[prop];
948-
let newItem = included[item.type] ? included[item.type][item.id] : (data[item.type] ? data[item.type][item.id] : null);
949-
if (newItem) {
950-
//Included item found!!
951-
// Apply foreign key to js-data object
952-
if (item.foreignKeyName) {
953-
newItem[item.foreignKeyName] = item.foreignKeyValue;
954-
}
955-
956-
// Replace item in array with data from included or data, data
957-
dataObject[prop] = newItem;
958-
} else {
959-
// Replace item in array with plain object, but with Primary key or any foreign keys set
960-
var itemOptions = options.getResource(item.type);
961-
962-
let metaData = new MetaData(item.type);
963-
metaData.isJsonApiReference = true;
964-
965-
let newItem = <any>{};
966-
newItem[JSONAPI_META] = metaData;
967-
newItem[itemOptions.idAttribute] = item.id;
968-
969-
// Apply foreign key to js-data object
970-
if (item.foreignKeyName) {
971-
newItem[item.foreignKeyName] = item.foreignKeyValue;
972-
}
973-
974-
// Replace item in array with data from included or data, data
975-
dataObject[prop] = newItem;
976-
977-
// Attach liftime events
978-
JsonApiHelper.AssignLifeTimeEvents(itemOptions.def());
979-
}
980-
}
981-
}
982-
}
983-
984-
jsDataArray.push(dataObject);
985-
}
986-
}
987-
}
988-
}
989-
}
990-
return jsDataArray;
991-
}
992-
*/
993-
994791
private static CreateErrorResponse(title: string, detail: string): JsonApi.JsonApiRequest {
995792
var response = new JsonApi.JsonApiRequest();
996793
var e = new JsonApi.JsonApiError();
@@ -1101,7 +898,7 @@ export class JsonApiHelper {
1101898

1102899
// Convert a JsonApi response into an object graph that can be used by js-data
1103900
// One of the main conversions reqired here is to add in ParentIds to child objects so that js-data toMany and toOne relationships work correctly
1104-
private static DeserializeJsonApiData(options: SerializationOptions, data: JsonApi.JsonApiData, joinData : any): Object {
901+
private static DeserializeJsonApiData(options: SerializationOptions, data: JsonApi.JsonApiData, joinData: any): Object {
1105902
if (!options) {
1106903
throw new Error('Missing Serialization Options, indicates possible missing jsData resource: ' + data.type);
1107904
}
@@ -1188,7 +985,7 @@ export class JsonApiHelper {
1188985
// So we will need to attach joining data to the field before returning it
1189986
// We can calculate id as the combination of both foreign keys
1190987
//Create an instance of the joining table.
1191-
joinTableFactory = function(foreignRecord: JsonApi.JsonApiData, relationshipLink: string) {
988+
joinTableFactory = function (foreignRecord: JsonApi.JsonApiData, relationshipLink: string) {
1192989

1193990
// Compare typenames in order to determine order to combine pk vales.
1194991
// Any algorithum that results in the same value regardless of the order will do here
@@ -1299,32 +1096,32 @@ export class JsonApiHelper {
12991096
if (DSUTILS.isArray(relationship.data)) {
13001097

13011098
var relatedItems = new Array<Object>();
1302-
DSUTILS.forEach(relationship.data, (item: JsonApi.JsonApiData) => {
1099+
DSUTILS.forEach(relationship.data, (item: JsonApi.JsonApiData) => {
13031100

1304-
if (joinTableFactory == null) {
1101+
if (joinTableFactory == null) {
13051102

1306-
var id = item.id;
1307-
var type = item.type;
1103+
var id = item.id;
1104+
var type = item.type;
13081105

1309-
localKeysList.push(id);
1106+
localKeysList.push(id);
13101107

1311-
let relatedItem = new ModelPlaceHolder(type, id);
1312-
if (relationshipDef.foreignKey) {
1313-
relatedItem.WithForeignKey(foreignKey, data.id, data.type);
1314-
}
1108+
let relatedItem = new ModelPlaceHolder(type, id);
1109+
if (relationshipDef.foreignKey) {
1110+
relatedItem.WithForeignKey(foreignKey, data.id, data.type);
1111+
}
13151112

1316-
relatedItems.push(relatedItem);
1317-
} else {
1318-
// This is part of a manyToMany relationship
1319-
// This new joinItem needs to be added to the main collection of objets so that in the next pass where we resolve
1320-
// ModelPlaceHolders it will be picked up
1321-
let relatedItem = joinTableFactory(item, relationship.FindLinkType('self'));
1113+
relatedItems.push(relatedItem);
1114+
} else {
1115+
// This is part of a manyToMany relationship
1116+
// This new joinItem needs to be added to the main collection of objets so that in the next pass where we resolve
1117+
// ModelPlaceHolders it will be picked up
1118+
let relatedItem = joinTableFactory(item, relationship.FindLinkType('self'));
13221119

1323-
localKeysList.push(id);
1120+
localKeysList.push(id);
13241121

1325-
relatedItems.push(relatedItem);
1326-
}
1327-
});
1122+
relatedItems.push(relatedItem);
1123+
}
1124+
});
13281125

13291126
// Store related local items for toMany relationship
13301127
fields[localField] = relatedItems;
@@ -1518,7 +1315,7 @@ export class JsonApiHelper {
15181315
};
15191316

15201317

1521-
private static beforeUpdateJsonApiData(resource: JSData.DSResourceDefinition<any>, items: any, cb: (err: Error, data: any) => void): void {
1318+
private static beforeUpdateJsonApiData(resource: JSData.DSResourceDefinition<any>, items: any, cb: (err: Error, data: any) => void): void {
15221319
var dataList = DSUTILS.isArray(items) ? items : [items];
15231320
DSUTILS.forEach<any>(dataList, (data: any) => {
15241321
// Merge a json api reference with a fully populated resource that has been previously retrieved
@@ -1537,6 +1334,8 @@ export class JsonApiHelper {
15371334
resource.afterInject = JsonApiHelper.beforeInjectJsonApiData;
15381335
resource.beforeUpdate = JsonApiHelper.beforeUpdateJsonApiData;
15391336
}
1337+
}
1338+
15401339

15411340
/* Expreimental !!
15421341
// This is where the magic of relations happens.
@@ -1664,5 +1463,4 @@ export class JsonApiHelper {
16641463
}
16651464
});
16661465
}
1667-
*/
1668-
}
1466+
*/

0 commit comments

Comments
 (0)