Skip to content

Commit e329095

Browse files
feat: introduce import/export + Index class (#734)
1 parent 98e5bb9 commit e329095

File tree

14 files changed

+1870
-40
lines changed

14 files changed

+1870
-40
lines changed

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
],
2525
"scripts": {
2626
"clean": "gts clean",
27-
"compile": "tsc -p . && cp -r proto* build/",
27+
"compile": "tsc -p . && cp -r proto* build/ && cp -r system-test/data build/system-test",
2828
"compile-protos": "compileProtos src",
2929
"docs": "jsdoc -c .jsdoc.js",
3030
"predocs-test": "npm run docs",
@@ -49,34 +49,40 @@
4949
"extend": "^3.0.2",
5050
"google-gax": "^2.2.0",
5151
"is": "^3.3.0",
52+
"pumpify": "^2.0.1",
5253
"split-array-stream": "^2.0.0",
5354
"stream-events": "^1.0.5"
5455
},
5556
"devDependencies": {
57+
"@google-cloud/storage": "^5.3.0",
58+
"@microsoft/api-documenter": "^7.8.10",
59+
"@microsoft/api-extractor": "^7.8.10",
5660
"@types/extend": "^3.0.1",
5761
"@types/is": "0.0.21",
62+
"@types/js-yaml": "^3.12.5",
5863
"@types/mocha": "^8.0.0",
5964
"@types/node": "^13.9.0",
6065
"@types/proxyquire": "^1.3.28",
66+
"@types/pumpify": "^1.4.1",
6167
"@types/sinon": "^9.0.0",
6268
"c8": "^7.1.0",
6369
"codecov": "^3.6.5",
6470
"gts": "^2.0.0",
71+
"js-yaml": "^3.14.0",
6572
"jsdoc": "^3.6.3",
6673
"jsdoc-fresh": "^1.0.2",
6774
"jsdoc-region-tag": "^1.0.4",
6875
"linkinator": "^2.0.3",
6976
"mocha": "^8.0.0",
7077
"null-loader": "^4.0.0",
78+
"p-queue": "^6.6.1",
7179
"pack-n-play": "^1.0.0-2",
7280
"proxyquire": "^2.1.3",
7381
"sinon": "^9.0.1",
7482
"ts-loader": "^8.0.0",
7583
"typescript": "^3.8.3",
7684
"webpack": "^4.42.0",
77-
"webpack-cli": "^3.3.11",
78-
"@microsoft/api-documenter": "^7.8.10",
79-
"@microsoft/api-extractor": "^7.8.10"
85+
"webpack-cli": "^3.3.11"
8086
},
8187
"engines": {
8288
"node": ">=10"

samples/export.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
async function main(bucket = 'YOUR_BUCKET_NAME') {
16+
// [START datastore_admin_entities_export]
17+
const {Datastore} = require('@google-cloud/datastore');
18+
const datastore = new Datastore();
19+
20+
async function exportEntities() {
21+
/**
22+
* TODO(developer): Uncomment these variables before running the sample.
23+
*/
24+
// const bucket = 'YOUR_BUCKET_NAME';
25+
26+
const [exportOperation] = await datastore.export({bucket});
27+
await exportOperation.promise();
28+
29+
// The export operation has created a new file in your bucket, e.g.
30+
// gs://{YOUR_BUCKET_NAME}/{timestamp}/{timestamp}.overall_export.metadata
31+
console.log(`Export file created: ${exportOperation.result.outputUrl}`);
32+
33+
// You may also choose to include only specific kinds and namespaces.
34+
const [specificExportOperation] = await datastore.export({
35+
bucket,
36+
kinds: ['Employee', 'Task'],
37+
namespaces: ['Company'],
38+
});
39+
await specificExportOperation.promise();
40+
console.log(specificExportOperation.result.outputUrl);
41+
}
42+
43+
await exportEntities();
44+
// [END datastore_admin_entities_export]
45+
}
46+
47+
const args = process.argv.slice(2);
48+
main(...args).catch(console.error);

samples/import.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
async function main(file = 'YOUR_FILE_NAME') {
16+
// [START datastore_admin_entities_import]
17+
const {Datastore} = require('@google-cloud/datastore');
18+
const datastore = new Datastore();
19+
20+
async function importEntities() {
21+
/**
22+
* TODO(developer): Uncomment these variables before running the sample.
23+
*/
24+
// const file = 'YOUR_FILE_NAME';
25+
26+
const [importOperation] = await datastore.import({file});
27+
28+
// Uncomment to await the results of the operation.
29+
// await importOperation.promise();
30+
31+
// Or cancel the operation.
32+
await importOperation.cancel();
33+
34+
// You may also choose to include only specific kinds and namespaces.
35+
const [specificImportOperation] = await datastore.import({
36+
file,
37+
kinds: ['Employee', 'Task'],
38+
namespaces: ['Company'],
39+
});
40+
41+
// Uncomment to await the results of the operation.
42+
// await specificImportOperation.promise();
43+
44+
// Or cancel the operation.
45+
await specificImportOperation.cancel();
46+
}
47+
48+
await importEntities();
49+
// [END datastore_admin_entities_import]
50+
}
51+
52+
const args = process.argv.slice(2);
53+
main(...args).catch(console.error);

samples/indexes.get.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
async function main(indexId = 'YOUR_INDEX_ID') {
16+
// [START datastore_admin_index_get]
17+
const {Datastore} = require('@google-cloud/datastore');
18+
const datastore = new Datastore();
19+
20+
async function getIndex() {
21+
/**
22+
* TODO(developer): Uncomment these variables before running the sample.
23+
*/
24+
// const indexId = 'YOUR_INDEX_ID';
25+
26+
// Create a reference to the index before performing operations on it.
27+
const index = datastore.index(indexId);
28+
29+
// Get the index's metadata, including its state, properties, and more.
30+
const [metadata] = await index.getMetadata();
31+
console.log('Properties:', metadata.properties);
32+
}
33+
34+
await getIndex();
35+
// [END datastore_admin_index_get]
36+
}
37+
38+
const args = process.argv.slice(2);
39+
main(...args).catch(console.error);

samples/indexes.list.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
async function main() {
16+
// [START datastore_admin_index_list]
17+
const {Datastore} = require('@google-cloud/datastore');
18+
const datastore = new Datastore();
19+
20+
async function listIndexes() {
21+
const [indexes] = await datastore.getIndexes();
22+
23+
console.log(`${indexes.length} indexes returned.`);
24+
25+
// Each returned Index object includes metadata about the index.
26+
const [firstIndex] = indexes;
27+
console.log('Properties:', firstIndex.metadata.properties);
28+
29+
// You may also get Index references as a readable object stream.
30+
datastore
31+
.getIndexesStream()
32+
.on('data', index => {
33+
console.log('Properties:', index.metadata.properties);
34+
})
35+
.on('end', () => {
36+
// All matching Index objects have been returned.
37+
});
38+
}
39+
40+
await listIndexes();
41+
// [END datastore_admin_index_list]
42+
}
43+
44+
const args = process.argv.slice(2);
45+
main(...args).catch(console.error);

samples/test/import-export.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {Datastore} = require('@google-cloud/datastore');
18+
const {assert} = require('chai');
19+
const cp = require('child_process');
20+
const {before, describe, it} = require('mocha');
21+
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
23+
24+
describe('import/export entities', async () => {
25+
const datastore = new Datastore();
26+
27+
const BUCKET_ID = 'nodejs-datastore-system-tests';
28+
let EXPORTED_FILE;
29+
30+
before(async () => {
31+
const [exportOperation] = await datastore.export({
32+
bucket: BUCKET_ID,
33+
});
34+
await exportOperation.promise();
35+
EXPORTED_FILE = exportOperation.result.outputUrl;
36+
});
37+
38+
it('should export entities', async () => {
39+
const stdout = execSync(`node ./export.js ${BUCKET_ID}`);
40+
assert.include(stdout, 'Export file created:');
41+
});
42+
43+
it('should import entities', () => {
44+
execSync(`node ./import.js ${EXPORTED_FILE}`);
45+
});
46+
});

samples/test/indexes.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {Datastore} = require('@google-cloud/datastore');
18+
const {assert} = require('chai');
19+
const cp = require('child_process');
20+
const {describe, it} = require('mocha');
21+
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
23+
24+
describe('indexes', async () => {
25+
const datastore = new Datastore();
26+
27+
// @TODO: Until the protos support creating indexes, these tests depend on
28+
// the remote state of declared indexes. Could be flaky!
29+
30+
it('should list indexes', async () => {
31+
const [indexes] = await datastore.getIndexes();
32+
const stdout = execSync('node ./indexes.list.js');
33+
assert.include(stdout, `${indexes.length} indexes returned.`);
34+
});
35+
36+
it('should get a specific index', async () => {
37+
const [indexes] = await datastore.getIndexes();
38+
const stdout = execSync(`node ./indexes.get.js ${indexes[0].id}`);
39+
assert.include(stdout, 'Properties:');
40+
});
41+
});

0 commit comments

Comments
 (0)