2021-07-29 16:02:39 +01:00
|
|
|
///<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');
|
2021-07-30 08:31:58 +01:00
|
|
|
var notificationData
|
|
|
|
|
WL.JSONStore.get(collectionName).findAll(allOptions).then((value) => {
|
2021-07-29 16:02:39 +01:00
|
|
|
console.log('Find all colletion data', value);
|
2021-07-30 08:31:58 +01:00
|
|
|
notificationData = JSON.parse(value);
|
2021-07-29 16:02:39 +01:00
|
|
|
});
|
2021-07-30 08:31:58 +01:00
|
|
|
console.log('Notification Data', notificationData);
|
|
|
|
|
return notificationData;
|
2021-07-29 16:02:39 +01:00
|
|
|
}).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)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|