JsonStore added and Service created

This commit is contained in:
Eudes Inácio
2021-07-29 16:02:39 +01:00
parent 32017af732
commit 8870d2de05
6 changed files with 150 additions and 7 deletions
+121
View File
@@ -0,0 +1,121 @@
///<reference path="../../../plugins/cordova-plugin-mfp/typings/worklight.d.ts" />
///<reference path="../../../plugins/cordova-plugin-mfp-jsonstore/typings/jsonstore.d.ts" />
import { Injectable } from "@angular/core";
@Injectable({
providedIn: 'root'
})
export class JsonStore {
createCollection(name, data) {
var collectionName = name;
var addOptions = {
// Mark data as dirty (true = yes, false = no), default true.
markDirty: true
};
var JSONStoreCollections = {};
JSONStoreCollections[collectionName] = {};
JSONStoreCollections[collectionName].searchFields = { UserId: 'integer' };
WL.JSONStore.init(JSONStoreCollections)
.then(function () {
WL.Logger.debug('Init done');
console.log('Init done');
return WL.JSONStore.get(collectionName).add(data, addOptions);
}).fail(function (err) {
WL.Logger.error(err);
console.log(err)
});
}
getCollection(collectionName) {
var allOptions = {
// Returns a maximum of 10 documents, default no limit.
limit: 10,
// Skip 0 documents, default no offset.
offset: 0,
// Search fields to return, default: ['_id', 'json'].
filter: ['UserId', 'json'],
// How to sort the returned values, default no sort.}]
};
var JSONStoreCollections = {};
JSONStoreCollections[collectionName] = {};
JSONStoreCollections[collectionName].searchFields = { UserId: 'integer' };
WL.JSONStore.init(JSONStoreCollections)
.then(function () {
WL.Logger.debug('Find all colletion data');
console.log('Find all colletion data');
return WL.JSONStore.get(collectionName).findAll(allOptions).then((value) => {
console.log('Find all colletion data', value);
});
}).fail(function (err) {
WL.Logger.error(err);
console.log("JsonStore getColletion error ",err)
});
}
getColletionById(collectionName, value) {
var query = { UserId: value };
var allOptions = {
// Returns a maximum of 10 documents, default no limit.
limit: 10,
// Skip 0 documents, default no offset.
offset: 0,
// Search fields to return, default: ['_id', 'json'].
filter: ['UserId', 'json'],
// How to sort the returned values, default no sort.}]
};
var JSONStoreCollections = {};
JSONStoreCollections[collectionName] = {};
JSONStoreCollections[collectionName].searchFields = { UserId: 'integer' };
WL.JSONStore.init(JSONStoreCollections)
.then(function () {
WL.Logger.debug('Find colletion data by id');
console.log('Find colletion data by id');
return WL.JSONStore.get(collectionName).find(query, allOptions).then((value) => {
console.log('Find colletion data by id', value);
});
}).fail(function (err) {
WL.Logger.error(err);
console.log(err)
});
}
replaceDocument(collectionName: string, document: any) {
/* var document = { Example of how identify the document to replace
_id: 1, json: {name: 'chevy', age: 23}
}; */
var options = {};
WL.JSONStore.get(collectionName).replace(document, options).then(function (numberOfDocsReplaced) {
console.log("JsonStore replace document sucess: ", numberOfDocsReplaced)
}).fail(function (error) {
console.log("JsonStore replace document error: ", error)
});
}
removeDocument(collectionName: any,query: any,) {
/* var query = { _id: 1 }; Exemple of query*/
var options = { exact: true };
WL.JSONStore.get(collectionName).remove(query, options).then(function (numberOfDocsRemoved) {
console.log("JsonStore remove document sucess: ", numberOfDocsRemoved)
}).fail(function (error) {
console.log("JsonStore remove document erro: ", error)
});
}
}