add cli tool
This commit is contained in:
committed by
Daniel Hutzel
parent
e33a455154
commit
e7be207911
9
media-store/.vscode/launch.json
vendored
9
media-store/.vscode/launch.json
vendored
@@ -4,12 +4,19 @@
|
|||||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Launch Program",
|
||||||
|
"command": "npm run import:data",
|
||||||
|
"request": "launch",
|
||||||
|
"skipFiles": ["<node_internals>/**"],
|
||||||
|
"type": "node-terminal"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"command": "cds run --with-mocks --in-memory?",
|
"command": "cds run --with-mocks --in-memory?",
|
||||||
"name": "cds run",
|
"name": "cds run",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"type": "node-terminal",
|
"type": "node-terminal",
|
||||||
"skipFiles": [ "<node_internals>/**" ]
|
"skipFiles": ["<node_internals>/**"]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,9 @@ aspect NamedEntityAspect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
aspect ContactInfoAspect {
|
aspect ContactInfoAspect {
|
||||||
firstName : String(40);
|
key ID : Integer;
|
||||||
lastName : String(20);
|
lastName : String(20);
|
||||||
|
firstName : String(40);
|
||||||
city : String(40);
|
city : String(40);
|
||||||
state : String(40);
|
state : String(40);
|
||||||
adress : String(70);
|
adress : String(70);
|
||||||
@@ -35,18 +36,51 @@ entity Albums {
|
|||||||
artist : Association to Artists;
|
artist : Association to Artists;
|
||||||
}
|
}
|
||||||
|
|
||||||
entity Employees : ContactInfoAspect {
|
// entity Employees : ContactInfoAspect {
|
||||||
key ID : Integer;
|
// reportsTo : Association to Employees;
|
||||||
reportsTo : Association to Employees;
|
// title : String(20);
|
||||||
title : String(20);
|
// birthDate : DateTime;
|
||||||
birthDate : DateTime;
|
// hireDate : DateTime;
|
||||||
hireDate : DateTime;
|
// }
|
||||||
|
|
||||||
|
// entity Customers : ContactInfoAspect {
|
||||||
|
// company : String(80);
|
||||||
|
// supportRep : Association to Employees;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// keep columns order for importing data
|
||||||
|
entity Employees {
|
||||||
|
key ID : Integer;
|
||||||
|
lastName : String(20);
|
||||||
|
firstName : String(40);
|
||||||
|
title : String(20);
|
||||||
|
reportsTo : Association to Employees;
|
||||||
|
birthDate : DateTime;
|
||||||
|
hireDate : DateTime;
|
||||||
|
adress : String(70);
|
||||||
|
city : String(40);
|
||||||
|
state : String(40);
|
||||||
|
country : String(40);
|
||||||
|
postalCode : String(10);
|
||||||
|
phone : String(24);
|
||||||
|
fax : String(24);
|
||||||
|
email : String(60);
|
||||||
}
|
}
|
||||||
|
|
||||||
entity Customers : ContactInfoAspect {
|
entity Customers {
|
||||||
key ID : Integer;
|
key ID : Integer;
|
||||||
company : String(80);
|
firstName : String(40);
|
||||||
supportRepId : Association to Employees;
|
lastName : String(20);
|
||||||
|
company : String(80);
|
||||||
|
adress : String(70);
|
||||||
|
city : String(40);
|
||||||
|
state : String(40);
|
||||||
|
country : String(40);
|
||||||
|
postalCode : String(10);
|
||||||
|
phone : String(24);
|
||||||
|
fax : String(24);
|
||||||
|
email : String(60);
|
||||||
|
supportRep : Association to Employees;
|
||||||
}
|
}
|
||||||
|
|
||||||
entity Invoices {
|
entity Invoices {
|
||||||
|
|||||||
@@ -13,7 +13,8 @@
|
|||||||
"sqlite3": "^5"
|
"sqlite3": "^5"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "npx cds run"
|
"start": "npx cds run",
|
||||||
|
"import:data": "node ./util/importData.js sqlite:chinook.db sqlite:mychinook.db ./db/schema.cds"
|
||||||
},
|
},
|
||||||
"cds": {
|
"cds": {
|
||||||
"requires": {
|
"requires": {
|
||||||
|
|||||||
@@ -1,5 +1,15 @@
|
|||||||
using {sap.capire.media.store as my} from '../db/schema';
|
using { sap.capire.media.store as my } from '../db/schema';
|
||||||
|
|
||||||
service MediaService {
|
service MediaService {
|
||||||
entity Employees as projection on my.Employees;
|
entity Employees as projection on my.Employees;
|
||||||
|
entity Customers as projection on my.Customers;
|
||||||
|
entity Albums as projection on my.Albums;
|
||||||
|
entity Artists as projection on my.Artists;
|
||||||
|
entity Genres as projection on my.Genres;
|
||||||
|
entity InvoiceItems as projection on my.InvoiceItems;
|
||||||
|
entity Invoices as projection on my.Invoices;
|
||||||
|
entity MediaTypes as projection on my.MediaTypes;
|
||||||
|
entity PlaylistTrack as projection on my.PlaylistTrack;
|
||||||
|
entity Playlists as projection on my.Playlists;
|
||||||
|
entity Tracks as projection on my.Tracks;
|
||||||
}
|
}
|
||||||
|
|||||||
70
media-store/util/importData.js
Normal file
70
media-store/util/importData.js
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
const cds = require("@sap/cds");
|
||||||
|
|
||||||
|
const FIRST_INDEX = 0;
|
||||||
|
const SECOND_INDEX = 1;
|
||||||
|
const SKIP_CLI_ARGS_NUMBER = 2;
|
||||||
|
const THIRD_INDEX = 3;
|
||||||
|
|
||||||
|
const toValues = (object) => Object.values(object);
|
||||||
|
|
||||||
|
const camelCaseToSnake = (str) =>
|
||||||
|
str.replace(
|
||||||
|
/[a-z][A-Z]/g,
|
||||||
|
(letters) =>
|
||||||
|
`${letters[FIRST_INDEX].toLowerCase()}_${letters[
|
||||||
|
SECOND_INDEX
|
||||||
|
].toLowerCase()}`
|
||||||
|
);
|
||||||
|
|
||||||
|
const elementsToColumns = (elements) => {
|
||||||
|
return Object.values(elements).map((element) =>
|
||||||
|
element.target ? `${element.name}_ID` : element.name
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const constructInsertQuery = (targetEntityName, columns) => {
|
||||||
|
return (data) => INSERT.into(targetEntityName).columns(columns).rows(data);
|
||||||
|
};
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
const args = process.argv.slice(SKIP_CLI_ARGS_NUMBER);
|
||||||
|
const SRC_STORAGE_NAME = args[FIRST_INDEX];
|
||||||
|
const TARGET_STORAGE_NAME = args[SECOND_INDEX];
|
||||||
|
const TARGET_SCHEMA_PATH = args[THIRD_INDEX];
|
||||||
|
console.log(
|
||||||
|
"[LOG]: Import data from",
|
||||||
|
SRC_STORAGE_NAME,
|
||||||
|
"to",
|
||||||
|
TARGET_STORAGE_NAME
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const srcStorage = await cds.connect.to(SRC_STORAGE_NAME);
|
||||||
|
const targetStorage = await cds.connect.to(TARGET_STORAGE_NAME);
|
||||||
|
|
||||||
|
const eList = targetStorage.entities;
|
||||||
|
|
||||||
|
const targetCSNModel = await cds.load(TARGET_SCHEMA_PATH);
|
||||||
|
const reflectedCSNModel = cds.reflect(targetCSNModel);
|
||||||
|
const targetCSNEntities = Object.values(reflectedCSNModel.entities);
|
||||||
|
|
||||||
|
for (index in targetCSNEntities) {
|
||||||
|
const { name: targetEntityName, elements } = targetCSNEntities[index];
|
||||||
|
const targetColumns = elementsToColumns(elements);
|
||||||
|
const insertQuery = constructInsertQuery(targetEntityName, targetColumns);
|
||||||
|
|
||||||
|
const srcEntityName = camelCaseToSnake(targetEntityName.split(".").pop());
|
||||||
|
const srcResultRows = (await srcStorage.read(srcEntityName)).map(
|
||||||
|
toValues
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log("[LOG]: From", srcEntityName, "to", targetEntityName);
|
||||||
|
|
||||||
|
const transaction = await targetStorage.tx();
|
||||||
|
await transaction.run(srcResultRows.map(insertQuery));
|
||||||
|
await transaction.commit();
|
||||||
|
}
|
||||||
|
} catch (errors) {
|
||||||
|
console.error(errors);
|
||||||
|
}
|
||||||
|
})();
|
||||||
Reference in New Issue
Block a user