ability to assign custom id to data entry

This commit is contained in:
moist-webDev 2023-06-21 21:52:13 -04:00
parent 318a17b9be
commit 7918b3d9f9
1 changed files with 20 additions and 8 deletions

View File

@ -55,6 +55,15 @@ export default class nasa{
let table:any = await AsyncStorage.getItem(this.#tableName); let table:any = await AsyncStorage.getItem(this.#tableName);
if(table){ if(table){
table = JSON.parse(table); table = JSON.parse(table);
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; let maxId = 0;
table.forEach((s:any)=>{ table.forEach((s:any)=>{
if(s.id > maxId){ if(s.id > maxId){
@ -62,6 +71,7 @@ export default class nasa{
} }
}); });
newData.id = maxId + 1; newData.id = maxId + 1;
}
const {data, error} = await this.#tableSchemas[this.#tableName].safeParse(newData); const {data, error} = await this.#tableSchemas[this.#tableName].safeParse(newData);
if(error){ if(error){
this.error = error.message; this.error = error.message;
@ -72,7 +82,9 @@ export default class nasa{
this.data = data this.data = data
return this; return this;
}else{ }else{
if (!('id' in newData)){
newData.id = 1; newData.id = 1;
}
const {data, error} = await this.#tableSchemas[this.#tableName].safeParse(newData); const {data, error} = await this.#tableSchemas[this.#tableName].safeParse(newData);
if(error){ if(error){
this.error = error.message; this.error = error.message;