acebase-core
Version:
Shared AceBase core components, no need to install manually
51 lines • 1.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectCollection = void 0;
const id_1 = require("./id");
/**
* Convenience interface for defining an object collection
* @example
* type ChatMessage = {
* text: string, uid: string, sent: Date
* }
* type Chat = {
* title: text
* messages: ObjectCollection<ChatMessage>
* }
*/
class ObjectCollection {
/**
* Converts and array of values into an object collection, generating a unique key for each item in the array
* @param array
* @example
* const array = [
* { title: "Don't make me think!", author: "Steve Krug" },
* { title: "The tipping point", author: "Malcolm Gladwell" }
* ];
*
* // Convert:
* const collection = ObjectCollection.from(array);
* // --> {
* // kh1x3ygb000120r7ipw6biln: {
* // title: "Don't make me think!",
* // author: "Steve Krug"
* // },
* // kh1x3ygb000220r757ybpyec: {
* // title: "The tipping point",
* // author: "Malcolm Gladwell"
* // }
* // }
*
* // Now it's easy to add them to the db:
* db.ref('books').update(collection);
*/
static from(array) {
const collection = {};
array.forEach(child => {
collection[id_1.ID.generate()] = child;
});
return collection;
}
}
exports.ObjectCollection = ObjectCollection;
//# sourceMappingURL=object-collection.js.map