Created
June 12, 2019 17:31
-
-
Save sajeetharan/d7336abce15fa4d10ffd9d806b819462 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HttpClient, HttpHeaders } from "@angular/common/http"; | |
import * as Cosmos from "@azure/cosmos"; | |
import * as Model from "../models/todoItem"; | |
export class CosmosService { | |
private static instance: CosmosService; | |
private readonly cosmosHost = "<your_db>.documents.azure.com:443/"; | |
private readonly primaryKey = "<primary_key>"; | |
private readonly database = "ToDoList"; | |
private readonly collection = "Items"; | |
private client: Cosmos.CosmosClient; | |
db: Cosmos.Database; | |
container: Cosmos.Container; | |
public items: Model.Todo[]; | |
constructor() { | |
this.client = new Cosmos.CosmosClient({ | |
endpoint: `https://${this.cosmosHost}`, | |
auth: { | |
masterKey: this.primaryKey | |
}, | |
consistencyLevel: "Eventual", | |
connectionPolicy: { | |
enableEndpointDiscovery: false | |
} | |
}); | |
this.db = this.client.database(this.database); | |
this.container = this.db.container(this.collection); | |
} | |
async addItem(todo: Model.Todo) { | |
const item = await this.container.items.create(todo) | |
.catch(err => console.error(err)); | |
console.log(item); | |
} | |
async listCollections(): Promise<Model.Todo[]> { | |
const response = await this.container.items | |
.readAll<Model.Todo>().fetchAll(); | |
console.warn(response); | |
this.items = response.resources; | |
console.log(`${this.items.length} items received`); | |
return this.items; | |
} | |
public static getInstance(): CosmosService { | |
if (!CosmosService.instance) | |
CosmosService.instance = new CosmosService(); | |
return CosmosService.instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment