UNPKG

static-database

Version:

For Create a Demo Database

77 lines (69 loc) 1.64 kB
/*! * primitive data javascript object */ /** * Module exports. * @public */ module.exports = dataobject; function dataobject(){} dataobject.insert = function(schema,row) { if(this.dataarr===undefined){ this.dataarr = []; } if(this.dataarr[schema]===undefined){ this.dataarr[schema] = []; } this.dataarr[schema].push(row); return {ack: 'succeed'}; } dataobject.update = function(schema,idx,row) { if(this.dataarr===undefined){ return {ack: 'data not found'}; } if(this.dataarr[schema]===undefined){ return {ack: 'schema not found'}; } this.dataarr[schema][idx] = row; return {ack: 'succeed'}; } dataobject.select = function(schema,idx,where) { if(this.dataarr===undefined){ return {ack: 'data not found'}; } if(this.dataarr[schema]===undefined){ return {ack: 'schema not found'}; } selectret = []; if(idx===undefined){ this.dataarr[schema].forEach(function(row, idx){ selectret.push({idx:idx,row:row}); }); } else { if(this.dataarr[schema][idx]!==undefined){ selectret.push({idx:idx,row:this.dataarr[schema][idx]}); } else { return {ack: 'row not found'}; } } return selectret; } dataobject.delete = function(schema,idx) { //return JSON.stringify(this.dataarr); if(this.dataarr===undefined){ return {ack: 'data not found'}; } if(this.dataarr[schema]===undefined){ return {ack: 'schema not found'}; } if(idx===undefined){ return {ack: 'idx is required'}; } else { if(this.dataarr[schema][idx]!==undefined){ this.dataarr[schema].splice(idx,1); return {ack: 'succeed',debug:this.dataarr[schema]}; } else { return {ack: 'row not found'}; } } }