Skip to content

Commit

Permalink
Clean up (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
mchirico authored Mar 30, 2020
1 parent 91a5fa4 commit 70ad979
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
10 changes: 6 additions & 4 deletions node/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import { Datastore } from '@google-cloud/datastore';

process.env.GOOGLE_APPLICATION_CREDENTIALS = './credentials/access.json';

// Ref: https://cloud.google.com/datastore/docs/concepts/queries

const datastore = new Datastore();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function addEntry(kind: string, name: string, data: Record<string, any>) {
export function addEntry(kind: string[], data: Record<string, any>) {
// The kind for the new entity
//const kind = 'Task';

// The name/ID for the new entity
//const name = 'sampletask1';

// The Cloud Datastore key for the new entity
const pKey = datastore.key([kind, name]);
const pKey = datastore.key(kind);

// Prepares the new entity
const doc = {
Expand All @@ -26,8 +28,8 @@ export function addEntry(kind: string, name: string, data: Record<string, any>)
// console.log(`Saved ${task.key.name}: ${task.data.description}`);
}

export function query(kind: string) {
const query = datastore.createQuery(kind);
export function query(kind: string, name: string) {
const query = datastore.createQuery(kind).filter('__key__', '=', datastore.key([kind, name]));

//const [tasks] = await datastore.runQuery(query);
return datastore.runQuery(query);
Expand Down
55 changes: 42 additions & 13 deletions node/tests/datastore.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
import { addEntry, query } from '../src/datastore/datastore';
import { addEntry, query, } from '../src/datastore/datastore';
import { expect } from "chai";

describe('Datastore', function() {
it('Create datastore', function(done) {
this.timeout(14000);
const kind = 'kind';
const name = 'name';
const value = 'value';
const kind = ['kind', 'name'];

const data = [
{
name: 'category',
value: 'Personal',
},
{
name: 'created',
value: new Date(),
},
{
name: 'done',
value: false,
},
{
name: 'priority',
value: 4,
},
{
name: 'tags',
value: ['fun', 'programming'],
},
{
name: 'percent_complete',
value: 10.0,
},
{
name: 'description',
value: 'Learn Cloud Datastore',
excludeFromIndexes: false,
},
];

const data = {
description: value,
stuff: 'stuff',
timeStamp: Date.now(),
};

addEntry(kind, name, data)
addEntry(kind, data)
.then(r => {
console.log('done');
console.log('added...');
done();
})
.catch(r => {
Expand All @@ -28,9 +53,10 @@ describe('Datastore', function() {

it('query datastore', function(done) {
this.timeout(14000);
const kind = 'kind';

query(kind)
const kind = 'kind';
const name = 'name'
query(kind, name)
.then(r => {
const [recs] = r;
recs.forEach(rec => {
Expand All @@ -46,4 +72,7 @@ describe('Datastore', function() {
});
});




});

0 comments on commit 70ad979

Please sign in to comment.