ability to assign custom id to data entry
This commit is contained in:
parent
318a17b9be
commit
7918b3d9f9
12
lib/nasa.ts
12
lib/nasa.ts
|
|
@ -55,6 +55,15 @@ export default class nasa{
|
|||
let table:any = await AsyncStorage.getItem(this.#tableName);
|
||||
if(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;
|
||||
table.forEach((s:any)=>{
|
||||
if(s.id > maxId){
|
||||
|
|
@ -62,6 +71,7 @@ export default class nasa{
|
|||
}
|
||||
});
|
||||
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{
|
||||
if (!('id' in newData)){
|
||||
newData.id = 1;
|
||||
}
|
||||
const {data, error} = await this.#tableSchemas[this.#tableName].safeParse(newData);
|
||||
if(error){
|
||||
this.error = error.message;
|
||||
|
|
|
|||
Loading…
Reference in New Issue