Skip to content

Commit

Permalink
Fix ids
Browse files Browse the repository at this point in the history
  • Loading branch information
phortx committed May 2, 2020
1 parent 72fa4b3 commit 50d0bb6
Show file tree
Hide file tree
Showing 28 changed files with 306 additions and 233 deletions.
2 changes: 1 addition & 1 deletion dist/actions/destroy.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class Destroy extends Action {
/**
* @param {State} state The Vuex state
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model
* @param {string} id ID of the record to delete
* @param {number} id ID of the record to delete
* @returns {Promise<any>} true
*/
static call({ state, dispatch }: ActionParams, { id, args }: ActionParams): Promise<boolean>;
Expand Down
2 changes: 1 addition & 1 deletion dist/actions/persist.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class Persist extends Action {
/**
* @param {any} state The Vuex state
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model
* @param {string} id ID of the record to persist
* @param {number|string} id ID of the record to persist
* @returns {Promise<Data>} The saved record
*/
static call({ state, dispatch }: ActionParams, { id, args }: ActionParams): Promise<Data>;
Expand Down
4 changes: 2 additions & 2 deletions dist/orm/model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ export default class Model {
isTypeFieldOfPolymorphicRelation(name: string): boolean;
/**
* Returns a record of this model with the given ID.
* @param {string} id
* @param {number|string} id
* @returns {any}
*/
getRecordWithId(id: string): import("@vuex-orm/core").Item<PatchedModel>;
getRecordWithId(id: number | string): import("@vuex-orm/core").Item<PatchedModel>;
/**
* Determines if we should eager load (means: add as a field in the graphql query) a related entity. belongsTo,
* hasOne and morphOne related entities are always eager loaded. Others can be added to the `eagerLoad` array
Expand Down
4 changes: 4 additions & 0 deletions dist/support/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ export declare function clone(input: any): any;
export declare function takeWhile(array: Array<any>, predicate: (x: any, idx: number, array: Array<any>) => any): any[];
export declare function matches(source: any): (object: any) => boolean;
export declare function removeSymbols(input: any): any;
/**
* Converts the argument into a number.
*/
export declare function toNumber(input: string | number | null): number | string;
33 changes: 22 additions & 11 deletions dist/vuex-orm-graphql.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7746,6 +7746,17 @@ function takeWhile(array, predicate) {
}
function removeSymbols(input) {
return JSON.parse(JSON.stringify(input));
}
/**
* Converts the argument into a number.
*/
function toNumber(input) {
if (input === null)
return 0;
if (typeof input === "string" && input.startsWith("$uid")) {
return input;
}
return parseInt(input.toString(), 10);
}

/**
Expand Down Expand Up @@ -7901,8 +7912,8 @@ var Model = /** @class */ (function () {
if (!field)
return false;
var context = Context.getInstance();
// Remove UID cause it must be a string
return field instanceof context.components.Number;
return field instanceof context.components.Number ||
field instanceof context.components.Uid;
};
/**
* Tells if a field is a attribute (and thus not a relation)
Expand Down Expand Up @@ -8058,14 +8069,14 @@ var Model = /** @class */ (function () {
};
/**
* Returns a record of this model with the given ID.
* @param {string} id
* @param {number|string} id
* @returns {any}
*/
Model.prototype.getRecordWithId = function (id) {
return this.baseModel
.query()
.withAllRecursive()
.where("id", id)
.where("id", toNumber(id))
.first();
};
/**
Expand Down Expand Up @@ -15338,7 +15349,7 @@ var Action = /** @class */ (function () {
if (!(name !== context.adapter.getNameForDestroy(model))) return [3 /*break*/, 4];
newData = newData[Object.keys(newData)[0]];
// IDs as String cause terrible issues, so we convert them to integers.
newData.id = parseInt(newData.id, 10);
newData.id = toNumber(newData.id);
return [4 /*yield*/, Store.insertData((_a = {}, _a[model.pluralName] = newData, _a), dispatch)];
case 3:
insertedData = _b.sent();
Expand Down Expand Up @@ -15423,7 +15434,7 @@ var Destroy = /** @class */ (function (_super) {
/**
* @param {State} state The Vuex state
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model
* @param {string} id ID of the record to delete
* @param {number} id ID of the record to delete
* @returns {Promise<any>} true
*/
Destroy.call = function (_a, _b) {
Expand Down Expand Up @@ -15575,7 +15586,7 @@ var Persist = /** @class */ (function (_super) {
/**
* @param {any} state The Vuex state
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model
* @param {string} id ID of the record to persist
* @param {number|string} id ID of the record to persist
* @returns {Promise<Data>} The saved record
*/
Persist.call = function (_a, _b) {
Expand All @@ -15591,7 +15602,7 @@ var Persist = /** @class */ (function (_super) {
mutationName = Context.getInstance().adapter.getNameForPersist(model);
oldRecord = model.getRecordWithId(id);
mockReturnValue = model.$mockHook("persist", {
id: id,
id: toNumber(id),
args: args || {}
});
if (!mockReturnValue) return [3 /*break*/, 3];
Expand Down Expand Up @@ -15922,7 +15933,7 @@ var VuexORMGraphQL = /** @class */ (function () {
return __generator(this, function (_b) {
args = args || {};
if (!args["id"])
args["id"] = this.$id;
args["id"] = toNumber(this.$id);
return [2 /*return*/, this.$dispatch("mutate", { name: name, args: args, multiple: multiple })];
});
});
Expand All @@ -15933,7 +15944,7 @@ var VuexORMGraphQL = /** @class */ (function () {
return __generator(this, function (_b) {
filter = filter || {};
if (!filter["id"])
filter["id"] = this.$id;
filter["id"] = toNumber(this.$id);
return [2 /*return*/, this.$dispatch("query", { name: name, filter: filter, multiple: multiple, bypassCache: bypassCache })];
});
});
Expand All @@ -15955,7 +15966,7 @@ var VuexORMGraphQL = /** @class */ (function () {
model.$destroy = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.$dispatch("destroy", { id: this.$id })];
return [2 /*return*/, this.$dispatch("destroy", { id: toNumber(this.$id) })];
});
});
};
Expand Down
33 changes: 22 additions & 11 deletions dist/vuex-orm-graphql.esm-bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7644,6 +7644,17 @@ function takeWhile(array, predicate) {
}
function removeSymbols(input) {
return JSON.parse(JSON.stringify(input));
}
/**
* Converts the argument into a number.
*/
function toNumber(input) {
if (input === null)
return 0;
if (typeof input === "string" && input.startsWith("$uid")) {
return input;
}
return parseInt(input.toString(), 10);
}

/**
Expand Down Expand Up @@ -7785,8 +7796,8 @@ class Model {
if (!field)
return false;
const context = Context.getInstance();
// Remove UID cause it must be a string
return field instanceof context.components.Number;
return field instanceof context.components.Number ||
field instanceof context.components.Uid;
}
/**
* Tells if a field is a attribute (and thus not a relation)
Expand Down Expand Up @@ -7940,14 +7951,14 @@ class Model {
}
/**
* Returns a record of this model with the given ID.
* @param {string} id
* @param {number|string} id
* @returns {any}
*/
getRecordWithId(id) {
return this.baseModel
.query()
.withAllRecursive()
.where("id", id)
.where("id", toNumber(id))
.first();
}
/**
Expand Down Expand Up @@ -15315,7 +15326,7 @@ class Action {
if (name !== context.adapter.getNameForDestroy(model)) {
newData = newData[Object.keys(newData)[0]];
// IDs as String cause terrible issues, so we convert them to integers.
newData.id = parseInt(newData.id, 10);
newData.id = toNumber(newData.id);
const insertedData = await Store.insertData({ [model.pluralName]: newData }, dispatch);
// Try to find the record to return
const records = insertedData[model.pluralName];
Expand Down Expand Up @@ -15392,7 +15403,7 @@ class Destroy extends Action {
/**
* @param {State} state The Vuex state
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model
* @param {string} id ID of the record to delete
* @param {number} id ID of the record to delete
* @returns {Promise<any>} true
*/
static async call({ state, dispatch }, { id, args }) {
Expand Down Expand Up @@ -15497,7 +15508,7 @@ class Persist extends Action {
/**
* @param {any} state The Vuex state
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model
* @param {string} id ID of the record to persist
* @param {number|string} id ID of the record to persist
* @returns {Promise<Data>} The saved record
*/
static async call({ state, dispatch }, { id, args }) {
Expand All @@ -15506,7 +15517,7 @@ class Persist extends Action {
const mutationName = Context.getInstance().adapter.getNameForPersist(model);
const oldRecord = model.getRecordWithId(id);
const mockReturnValue = model.$mockHook("persist", {
id,
id: toNumber(id),
args: args || {}
});
if (mockReturnValue) {
Expand Down Expand Up @@ -15749,13 +15760,13 @@ class VuexORMGraphQL {
model.$mutate = async function ({ name, args, multiple }) {
args = args || {};
if (!args["id"])
args["id"] = this.$id;
args["id"] = toNumber(this.$id);
return this.$dispatch("mutate", { name, args, multiple });
};
model.$customQuery = async function ({ name, filter, multiple, bypassCache }) {
filter = filter || {};
if (!filter["id"])
filter["id"] = this.$id;
filter["id"] = toNumber(this.$id);
return this.$dispatch("query", { name, filter, multiple, bypassCache });
};
model.$persist = async function (args) {
Expand All @@ -15765,7 +15776,7 @@ class VuexORMGraphQL {
return this.$dispatch("push", { data: this, args });
};
model.$destroy = async function () {
return this.$dispatch("destroy", { id: this.$id });
return this.$dispatch("destroy", { id: toNumber(this.$id) });
};
model.$deleteAndDestroy = async function () {
await this.$delete();
Expand Down
33 changes: 22 additions & 11 deletions dist/vuex-orm-graphql.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7644,6 +7644,17 @@ function takeWhile(array, predicate) {
}
function removeSymbols(input) {
return JSON.parse(JSON.stringify(input));
}
/**
* Converts the argument into a number.
*/
function toNumber(input) {
if (input === null)
return 0;
if (typeof input === "string" && input.startsWith("$uid")) {
return input;
}
return parseInt(input.toString(), 10);
}

/**
Expand Down Expand Up @@ -7785,8 +7796,8 @@ class Model {
if (!field)
return false;
const context = Context.getInstance();
// Remove UID cause it must be a string
return field instanceof context.components.Number;
return field instanceof context.components.Number ||
field instanceof context.components.Uid;
}
/**
* Tells if a field is a attribute (and thus not a relation)
Expand Down Expand Up @@ -7940,14 +7951,14 @@ class Model {
}
/**
* Returns a record of this model with the given ID.
* @param {string} id
* @param {number|string} id
* @returns {any}
*/
getRecordWithId(id) {
return this.baseModel
.query()
.withAllRecursive()
.where("id", id)
.where("id", toNumber(id))
.first();
}
/**
Expand Down Expand Up @@ -15315,7 +15326,7 @@ class Action {
if (name !== context.adapter.getNameForDestroy(model)) {
newData = newData[Object.keys(newData)[0]];
// IDs as String cause terrible issues, so we convert them to integers.
newData.id = parseInt(newData.id, 10);
newData.id = toNumber(newData.id);
const insertedData = await Store.insertData({ [model.pluralName]: newData }, dispatch);
// Try to find the record to return
const records = insertedData[model.pluralName];
Expand Down Expand Up @@ -15392,7 +15403,7 @@ class Destroy extends Action {
/**
* @param {State} state The Vuex state
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model
* @param {string} id ID of the record to delete
* @param {number} id ID of the record to delete
* @returns {Promise<any>} true
*/
static async call({ state, dispatch }, { id, args }) {
Expand Down Expand Up @@ -15497,7 +15508,7 @@ class Persist extends Action {
/**
* @param {any} state The Vuex state
* @param {DispatchFunction} dispatch Vuex Dispatch method for the model
* @param {string} id ID of the record to persist
* @param {number|string} id ID of the record to persist
* @returns {Promise<Data>} The saved record
*/
static async call({ state, dispatch }, { id, args }) {
Expand All @@ -15506,7 +15517,7 @@ class Persist extends Action {
const mutationName = Context.getInstance().adapter.getNameForPersist(model);
const oldRecord = model.getRecordWithId(id);
const mockReturnValue = model.$mockHook("persist", {
id,
id: toNumber(id),
args: args || {}
});
if (mockReturnValue) {
Expand Down Expand Up @@ -15749,13 +15760,13 @@ class VuexORMGraphQL {
model.$mutate = async function ({ name, args, multiple }) {
args = args || {};
if (!args["id"])
args["id"] = this.$id;
args["id"] = toNumber(this.$id);
return this.$dispatch("mutate", { name, args, multiple });
};
model.$customQuery = async function ({ name, filter, multiple, bypassCache }) {
filter = filter || {};
if (!filter["id"])
filter["id"] = this.$id;
filter["id"] = toNumber(this.$id);
return this.$dispatch("query", { name, filter, multiple, bypassCache });
};
model.$persist = async function (args) {
Expand All @@ -15765,7 +15776,7 @@ class VuexORMGraphQL {
return this.$dispatch("push", { data: this, args });
};
model.$destroy = async function () {
return this.$dispatch("destroy", { id: this.$id });
return this.$dispatch("destroy", { id: toNumber(this.$id) });
};
model.$deleteAndDestroy = async function () {
await this.$delete();
Expand Down
Loading

0 comments on commit 50d0bb6

Please sign in to comment.