From 7918b3d9f99ee304f81d35a511bfa0a78809b34c Mon Sep 17 00:00:00 2001 From: moist-webDev Date: Wed, 21 Jun 2023 21:52:13 -0400 Subject: [PATCH] ability to assign custom id to data entry --- lib/nasa.ts | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/nasa.ts b/lib/nasa.ts index 45752fc..81f0598 100644 --- a/lib/nasa.ts +++ b/lib/nasa.ts @@ -55,13 +55,23 @@ export default class nasa{ let table:any = await AsyncStorage.getItem(this.#tableName); if(table){ table = JSON.parse(table); - let maxId = 0; - table.forEach((s:any)=>{ - if(s.id > maxId){ - maxId = s.id; - } - }); - newData.id = maxId + 1; + console.log(newData) + if ('id' in newData){ + table.forEach((s:any)=>{ + if(s.id === newData.id){ + this.error = 'Id already exists'; + return this + } + }); + }else{ + let maxId = 0; + table.forEach((s:any)=>{ + if(s.id > maxId){ + maxId = s.id; + } + }); + newData.id = maxId + 1; + } const {data, error} = await this.#tableSchemas[this.#tableName].safeParse(newData); if(error){ this.error = error.message; @@ -72,7 +82,9 @@ export default class nasa{ this.data = data return this; }else{ - newData.id = 1; + if (!('id' in newData)){ + newData.id = 1; + } const {data, error} = await this.#tableSchemas[this.#tableName].safeParse(newData); if(error){ this.error = error.message;