diff --git a/large-local-storage/large-local-storage-tests.ts b/large-local-storage/large-local-storage-tests.ts new file mode 100644 index 000000000..6fdf8e59c --- /dev/null +++ b/large-local-storage/large-local-storage-tests.ts @@ -0,0 +1,109 @@ +/// + +import LargeLocalStorage = require('large-local-storage'); + +var storage = new LargeLocalStorage({size: 75*1024*1024}); +storage.initialized.then(function(grantedCapacity) { + // storage ready to be used. +}); + +var desiredCapacity = 50 * 1024 * 1024; // 50MB +var storage = new LargeLocalStorage({ + // desired capacity, in bytes. + size: desiredCapacity, + + // optional name for your LLS database. Defaults to lls. + // This is the name given to the underlying + // IndexedDB or WebSQL DB or FSAPI Folder. + // LLS's with different names are independent. + name: 'myStorage' + + // the following is an optional param + // that is useful for debugging. + // force LLS to use a specific storage implementation + // forceProvider: 'IndexedDB' or 'WebSQL' or 'FilesystemAPI' +}); +storage.initialized.then(function(capacity) { + if (capacity != -1 && capacity != desiredCapacity) { + // the user didn't authorize your storage request + // so instead you have some limitation on your storage + } +}); + +storage.getAllAttachments('exampleDoc').then(function(attachEntries) { + attachEntries.map(function(entry) { + var a = entry.data; + // do something with it... + if (a.type.indexOf('image') == 0) { + // show image... + } else if (a.type.indexOf('audio') == 0) { + // play audio... + } else { + + } + }) +}); + +storage.getAllAttachmentURLs('exampleDoc').then(function(urlEntries) { + urlEntries.map(function(entry) { + var url = entry.url; + }) +}); + +storage.getAttachment('exampleDoc', 'examplePic').then(function(attachment) { + var url = URL.createObjectURL(attachment); +}); + +storage.getAttachmentURL('myDoc', 'myPic').then(function(url) { + var image = new Image(); + image.src = url; + document.body.appendChild(image); + storage.revokeAttachmentURL(url); +}); + +// the initialized property will call you back with the capacity +storage.initialized.then(function(capacity) { + console.log('Authorized to store: ' + capacity + ' bytes'); +}); +// or if you know your storage is already available +// you can call getCapacity directly +storage.getCapacity(); + +storage.getContents('exampleDoc').then(function(contents) { + alert(contents); +}); + +storage.ls().then(function(docKeys) { + console.log(docKeys); +}); + +// may or may not be true +storage.ready(); + +storage.initialized.then(function() { + // always true + storage.ready(); +}); + +storage.getAttachmentURL('doc', 'attach').then(function(url) { + // do something with the URL + storage.revokeAttachmentURL(url); +}); + +storage.rm('exampleDoc').then(function() { + alert('doc and all attachments were removed'); +}); + +storage.rmAttachment('exampleDoc', 'someAttachment').then(function() { + alert('exampleDoc/someAttachment removed'); +}).catch(function(e) { + alert('Attachment removal failed: ' + e); +}); + +storage.setAttachment('myDoc', 'myPic', [1, 2, 3]).then(function() { + alert('Attachment written'); +}); + +storage.setContents('exampleDoc', 'some data...').then(function() { + alert('doc written'); +}); \ No newline at end of file diff --git a/large-local-storage/large-local-storage.d.ts b/large-local-storage/large-local-storage.d.ts new file mode 100644 index 000000000..41215067d --- /dev/null +++ b/large-local-storage/large-local-storage.d.ts @@ -0,0 +1,126 @@ +// Type definitions for LargeLocalStorage v0.1.3 +// Project: https://github.com/tantaman/LargeLocalStorage +// Definitions by: Borislav Zhivkov +// Definitions: https://github.com/borisyankov/DefinitelyTyped +declare module LargeLocalStorageInterfaces { + interface LargeLocalStorageService { + new(options: Options): LargeLocalStorageService; + + initialized: Promise; + + /** + * Gets all of the attachments for a document. + */ + getAllAttachments(docKey?: string): Promise; + + /** + * Gets all attachments URLs for a document. + */ + getAllAttachmentURLs(docKey?: string): Promise; + + /** + * Get the attachment identified by attachKey + */ + getAttachment(attachKey: string): Promise; + + /** + * Get the attachment identified by docKey and attachKey + */ + getAttachment(docKey: string, attachKey: string): Promise; + + /** + * Get the URL for a given attachment. + */ + getAttachmentURL(attachKey: string): Promise; + + /** + * Get the URL for a given attachment. + */ + getAttachmentURL(docKey: string, attachKey: string): Promise; + + /** + * Returns the actual capacity of the storage or -1 if it is unknown. + */ + getCapacity(): number; + + /** + * Get the contents of a document identified by docKey + */ + getContents(docKey: string): Promise; + + /** + * List all attachments under a given key. List all documents if no key is provided. + */ + ls(docKey?: string): Promise; + + /** + * Whether or not LLS is ready to store data. The initialized property can be used to await initialization. + */ + ready(): boolean; + + /** + * Revoke the attachment URL as required by the underlying storage system. + */ + revokeAttachmentURL(url: string): void; + + /** + * Remove the specified document and all of its attachments. + */ + rm(docKey?: string): Promise; + + /** + * Remove an attachment from a document. + */ + rmAttachment(docKey: string, attachKey: string): Promise; + + /** + * Set an attachment for a given document. Identified by attachKey. + */ + setAttachment(attachKey: string, attachment: any): Promise; + + /** + * Set an attachment for a given document. Identified by docKey and attachKey. + */ + setAttachment(docKey: string, attachKey: string, attachment: any): Promise; + + /** + * Set the contents identified by docKey to data. The document will be created if it does not exist. + */ + setContents(docKey: string, data: any): Promise; + } + + interface Options { + /** + * Desired capacity in bytes. + */ + size: number; + + /** + * Optional name for your LLS database. Defaults to lls. This is the name given to the underlying IndexedDB or WebSQL DB or FSAPI Folder. LLS's with different names are independent. + */ + name?: string; + + /** + * Force LLS to use a specific storage implementation: 'IndexedDB' or 'WebSQL' or 'FilesystemAPI'. + */ + forceProvider?: string; + } + + interface Entry { + data: any; + docKey: string; + attachKey: string; + url: string; + } + + interface Promise { + then(onFulfilled?: (value: T) => U | Promise, onRejected?: (error: any) => U | Promise): Promise; + then(onFulfilled?: (value: T) => U | Promise, onRejected?: (error: any) => void): Promise; + catch(onRejected?: (error: any) => U | Promise): Promise; + } +} + +declare module "large-local-storage" { + var LargeLocalStorage: LargeLocalStorageInterfaces.LargeLocalStorageService; + export = LargeLocalStorage; +} \ No newline at end of file