Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using ES Modules #741

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bookshop/db/init.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions bookshop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@capire/bookshop",
"version": "1.0.0",
"description": "A simple self-contained bookshop service.",
"type": "module",
"files": [
"app",
"srv",
Expand Down
4 changes: 2 additions & 2 deletions bookshop/srv/admin-service.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
6 changes: 2 additions & 4 deletions bookshop/srv/cat-service.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -34,5 +34,3 @@ class CatalogService extends cds.ApplicationService { init() {
// Delegate requests to the underlying generic service
return super.init()
}}

module.exports = CatalogService
4 changes: 2 additions & 2 deletions bookshop/srv/user-service.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
1 change: 1 addition & 0 deletions bookstore/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@capire/bookstore",
"version": "1.0.0",
"type": "module",
"dependencies": {
"@capire/bookshop": "*",
"@capire/reviews": "*",
Expand Down
8 changes: 3 additions & 5 deletions bookstore/server.js
Original file line number Diff line number Diff line change
@@ -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)=>{
Expand All @@ -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')
5 changes: 3 additions & 2 deletions bookstore/srv/mashup.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
10 changes: 0 additions & 10 deletions bookstore/srv/swagger-ui.js

This file was deleted.

1 change: 1 addition & 0 deletions hello/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
7 changes: 3 additions & 4 deletions hello/srv/world.js
Original file line number Diff line number Diff line change
@@ -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}!`
}
}
11 changes: 5 additions & 6 deletions hello/srv/world.ts
Original file line number Diff line number Diff line change
@@ -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!`
}
}
Loading