Using ES Modules

This commit is contained in:
Daniel Hutzel
2024-07-16 15:29:45 +02:00
parent 7a2e345cd5
commit cba8e0cc5c
12 changed files with 25 additions and 37 deletions

View File

@@ -1,4 +1,4 @@
const cds = require('@sap/cds')
import cds from '@sap/cds'
/**
* In order to keep basic bookshop sample as simple as possible, we don't add
@@ -8,7 +8,7 @@ const cds = require('@sap/cds')
// NOTE: We use cds.on('served') to delay the UPSERTs after the db init
// to run after all INSERTs from .csv files happened.
module.exports = cds.on('served', ()=>
export default cds.on('served', ()=>
UPSERT.into ('sap.common.Currencies') .columns (
[ 'code', 'symbol', 'name' ]
) .rows (

View File

@@ -2,6 +2,7 @@
"name": "@capire/bookshop",
"version": "1.0.0",
"description": "A simple self-contained bookshop service.",
"type": "module",
"files": [
"app",
"srv",

View File

@@ -1,6 +1,6 @@
const cds = require('@sap/cds')
import cds from '@sap/cds'
module.exports = class AdminService extends cds.ApplicationService { init(){
export class AdminService extends cds.ApplicationService { init(){
this.before (['NEW','CREATE'],'Authors', genid)
this.before (['NEW','CREATE'],'Books', genid)
return super.init()

View File

@@ -1,6 +1,6 @@
const cds = require('@sap/cds')
import cds from '@sap/cds'
class CatalogService extends cds.ApplicationService { init() {
export class CatalogService extends cds.ApplicationService { init() {
const { Books } = cds.entities('sap.capire.bookshop')
const { ListOfBooks } = this.entities
@@ -34,5 +34,3 @@ class CatalogService extends cds.ApplicationService { init() {
// Delegate requests to the underlying generic service
return super.init()
}}
module.exports = CatalogService

View File

@@ -1,5 +1,5 @@
const cds = require('@sap/cds')
module.exports = class UserService extends cds.Service { init(){
import cds from '@sap/cds'
export class UserService extends cds.Service { init(){
this.on('READ', 'me', ({ tenant, user, locale }) => ({ id: user.id, locale, tenant }))
this.on('login', (req) => {
if (req.user._is_anonymous)

View File

@@ -1,6 +1,7 @@
{
"name": "@capire/bookstore",
"version": "1.0.0",
"type": "module",
"dependencies": {
"@capire/bookshop": "*",
"@capire/reviews": "*",

View File

@@ -1,7 +1,8 @@
const cds = require ('@sap/cds')
import mashup from './srv/mashup.js'
import cds from '@sap/cds'
// Add mashup logic
cds.once('served', require('./srv/mashup'))
cds.once('served', mashup)
// Add routes to UIs from imported packages
cds.once('bootstrap',(app)=>{
@@ -10,6 +11,3 @@ cds.once('bootstrap',(app)=>{
app.serve ('/orders') .from('@capire/orders','app/orders')
app.serve ('/data') .from('@capire/data-viewer','app/viewer')
})
// Add Swagger UI
require('./srv/swagger-ui')

View File

@@ -1,10 +1,11 @@
import cds from '@sap/cds'
////////////////////////////////////////////////////////////////////////////
//
// Mashing up bookshop services with required services...
//
module.exports = async()=>{ // called by server.js
export default async()=>{ // called by server.js
const cds = require('@sap/cds')
const CatalogService = await cds.connect.to ('CatalogService')
const ReviewsService = await cds.connect.to ('ReviewsService')
const OrdersService = await cds.connect.to ('OrdersService')

View File

@@ -1,10 +0,0 @@
// -----------------------------------------------------------------------
// Adding Swagger UI - see https://cap.cloud.sap/docs/advanced/openapi
const cds = require ('@sap/cds')
try {
const cds_swagger = require ('cds-swagger-ui-express')
cds.once ('bootstrap', app => app.use (cds_swagger()) )
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') throw err
}

View File

@@ -1,6 +1,7 @@
{
"name": "@capire/hello-world",
"version": "1.0.0",
"type": "module",
"scripts": {
"test": "npx jest --silent",
"start": "cds-serve srv/world.cds",

View File

@@ -1,7 +1,6 @@
module.exports = class say {
hello(req) {
let {to} = req.data
if (to === 'me') to = require('os').userInfo().username
import cds from '@sap/cds'
export class say extends cds.ApplicationService {
hello (to = 'World') {
return `Hello ${to}!`
}
}

View File

@@ -1,7 +1,6 @@
import { Request } from "@sap/cds"
module.exports = class say {
hello(req: Request) {
return `Hello ${req.data.to} from a TypeScript file!`
}
import cds from '@sap/cds'
export class say extends cds.ApplicationService {
hello (to : String = 'World') {
return `Hello ${to} from TypeScript!`
}
}