@cap-js/postgres 1.5.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,27 @@
4
4
  - The format is based on [Keep a Changelog](http://keepachangelog.com/).
5
5
  - This project adheres to [Semantic Versioning](http://semver.org/).
6
6
 
7
+ ## [1.7.0](https://github.com/cap-js/cds-dbs/compare/postgres-v1.6.0...postgres-v1.7.0) (2024-04-12)
8
+
9
+
10
+ ### Added
11
+
12
+ * Odata built-in query functions ([#558](https://github.com/cap-js/cds-dbs/issues/558)) ([6e63367](https://github.com/cap-js/cds-dbs/commit/6e6336757129c4a9dac56f93fd768bb41d071c46))
13
+
14
+ ## [1.6.0](https://github.com/cap-js/cds-dbs/compare/postgres-v1.5.1...postgres-v1.6.0) (2024-03-22)
15
+
16
+
17
+ ### Added
18
+
19
+ * also support lowercase matchespattern function ([#528](https://github.com/cap-js/cds-dbs/issues/528)) ([6ea574e](https://github.com/cap-js/cds-dbs/commit/6ea574ee67ef5e42e4f8ccbe4fe91b46097de129))
20
+ * forUpdate and forShareLock ([#148](https://github.com/cap-js/cds-dbs/issues/148)) ([99a1170](https://github.com/cap-js/cds-dbs/commit/99a1170e61de4fd0c505834c25a9c03fc34da85b))
21
+
22
+
23
+ ### Changed
24
+
25
+ * use new cds build API @sap/cds-dk >= 7.5.0 ([#508](https://github.com/cap-js/cds-dbs/issues/508)) ([ef22ebe](https://github.com/cap-js/cds-dbs/commit/ef22ebe68c6a554d4042a0a19bae3b2e1d56cb01))
26
+ * this package now requires `@cap-js/db-service >= v1.7.0` ([#545](https://github.com/cap-js/cds-dbs/issues/545)) ([2cec27d](https://github.com/cap-js/cds-dbs/commit/2cec27d91402804c3b2da25cc7169f0d81a7406a))
27
+
7
28
  ## [1.5.1](https://github.com/cap-js/cds-dbs/compare/postgres-v1.5.0...postgres-v1.5.1) (2024-02-16)
8
29
 
9
30
 
package/cds-plugin.js CHANGED
@@ -1,11 +1,48 @@
1
1
  const cds = require('@sap/cds')
2
+ const { fs, path } = cds.utils
2
3
 
3
4
  if (!cds.env.fiori.lean_draft) {
4
5
  throw new Error('"@cap-js/postgres" only works if cds.fiori.lean_draft is enabled. Please adapt your configuration.')
5
6
  }
6
7
 
7
- // requires @sap/cds-dk version >= 7.3.2
8
- cds.build?.register?.('postgres', {
9
- impl: '@cap-js/postgres/lib/build.js',
10
- taskDefaults: { src: cds.env.folders.db }
8
+ // requires @sap/cds-dk version >= 7.5.0
9
+ cds.build?.register?.('postgres', class PostgresBuildPlugin extends cds.build.Plugin {
10
+
11
+ static taskDefaults = { src: cds.env.folders.db }
12
+
13
+ static hasTask() { return cds.requires.db?.kind === 'postgres' }
14
+
15
+ init() {
16
+ // different from the default build output structure
17
+ this.task.dest = path.join(cds.root, cds.env.build.target !== '.' ? cds.env.build.target : 'gen', 'pg')
18
+ }
19
+
20
+ async build() {
21
+ const model = await this.model()
22
+ if (!model) return
23
+
24
+ const promises = []
25
+ if (fs.existsSync(path.join(this.task.src, 'package.json'))) {
26
+ promises.push(this.copy(path.join(this.task.src, 'package.json')).to('package.json'))
27
+ } else {
28
+ promises.push(
29
+ this.write({
30
+ dependencies: { '@sap/cds': '^7', '@cap-js/postgres': '^1' },
31
+ scripts: { start: 'cds-deploy' },
32
+ }).to('package.json'),
33
+ )
34
+ }
35
+ promises.push(this.write(cds.compile.to.json(model)).to(path.join('db', 'csn.json')))
36
+
37
+ let data
38
+ if (fs.existsSync(path.join(this.task.src, 'data'))) {
39
+ data = 'data'
40
+ } else if (fs.existsSync(path.join(this.task.src, 'csv'))) {
41
+ data = 'csv'
42
+ }
43
+ if (data) {
44
+ promises.push(this.copy(data).to(path.join('db', 'data')))
45
+ }
46
+ return Promise.all(promises)
47
+ }
11
48
  })
@@ -417,6 +417,20 @@ GROUP BY k
417
417
  else return super.operator(x, i, xpr)
418
418
  }
419
419
 
420
+ // Postgres does not support locking columns only tables which makes of unapplicable
421
+ // Postgres does not support "wait n" it only supports "nowait"
422
+ forUpdate(update) {
423
+ const { wait } = update
424
+ if (wait === 0) return 'FOR UPDATE NOWAIT'
425
+ return 'FOR UPDATE'
426
+ }
427
+
428
+ forShareLock(lock) {
429
+ const { wait } = lock
430
+ if (wait === 0) return 'FOR SHARE NOWAIT'
431
+ return 'FOR SHARE'
432
+ }
433
+
420
434
  defaultValue(defaultValue = this.context.timestamp.toISOString()) {
421
435
  return this.string(`${defaultValue}`)
422
436
  }
package/lib/func.js CHANGED
@@ -11,14 +11,21 @@ const StandardFunctions = {
11
11
  indexof: (x, y) => `strpos(${x},${y}) - 1`, // sqlite instr is 1 indexed
12
12
  startswith: (x, y) => `strpos(${x},${y}) = 1`, // sqlite instr is 1 indexed
13
13
  endswith: (x, y) => `substr(${x},length(${x}) + 1 - length(${y})) = ${y}`,
14
+ matchesPattern: (x, y) => `regexp_like(${x}, ${y})`,
15
+ matchespattern: (x, y) => `regexp_like(${x}, ${y})`,
14
16
 
15
17
  // Date and Time Functions
16
18
  year: x => `date_part('year', ${castVal(x)})`,
17
19
  month: x => `date_part('month', ${castVal(x)})`,
18
20
  day: x => `date_part('day', ${castVal(x)})`,
21
+ time: x => `to_char(${castVal(x)}, 'HH24:MI:SS')`,
19
22
  hour: x => `date_part('hour', ${castVal(x)})`,
20
23
  minute: x => `date_part('minute', ${castVal(x)})`,
21
- second: x => `date_part('second', ${castVal(x)})`,
24
+ second: x => `floor(date_part('second', ${castVal(x)}))`,
25
+ fractionalseconds: x => `CAST(date_part('second', ${castVal(x)}) - floor(date_part('second', ${castVal(x)})) AS DECIMAL)`,
26
+ now: function() {
27
+ return this.session_context({val: '$now'})
28
+ }
22
29
  }
23
30
 
24
31
  const isTime = /^\d{1,2}:\d{1,2}:\d{1,2}$/
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js/postgres",
3
- "version": "1.5.1",
3
+ "version": "1.7.0",
4
4
  "description": "CDS database service for Postgres",
5
5
  "homepage": "https://github.com/cap-js/cds-dbs/tree/main/postgres#cds-database-service-for-postgres",
6
6
  "repository": {
@@ -31,12 +31,12 @@
31
31
  "start": "docker-compose -f pg-stack.yml up -d"
32
32
  },
33
33
  "dependencies": {
34
- "@cap-js/db-service": "^1.6.0",
34
+ "@cap-js/db-service": "^1.7.0",
35
35
  "pg": "^8"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "@sap/cds": ">=7.6",
39
- "@sap/cds-dk": ">=7"
39
+ "@sap/cds-dk": ">=7.5"
40
40
  },
41
41
  "peerDependenciesMeta": {
42
42
  "@sap/cds-dk": {
package/lib/build.js DELETED
@@ -1,42 +0,0 @@
1
- const cds = require('@sap/cds')
2
- const { fs, path } = cds.utils
3
-
4
- module.exports = class PostgresBuildPlugin extends cds.build.BuildPlugin {
5
- static hasTask() {
6
- return cds.requires.db?.kind === 'postgres'
7
- }
8
-
9
- init() {
10
- // different from the default build output structure
11
- this.task.dest = path.join(cds.root, cds.env.build.target !== '.' ? cds.env.build.target : 'gen', 'pg')
12
- }
13
-
14
- async build() {
15
- const model = await this.model()
16
- if (!model) return
17
-
18
- const promises = []
19
- if (fs.existsSync(path.join(this.task.src, 'package.json'))) {
20
- promises.push(this.copy(path.join(this.task.src, 'package.json')).to('package.json'))
21
- } else {
22
- promises.push(
23
- this.write({
24
- dependencies: { '@sap/cds': '^7', '@cap-js/postgres': '^1' },
25
- scripts: { start: 'cds-deploy' },
26
- }).to('package.json'),
27
- )
28
- }
29
- promises.push(this.write(cds.compile.to.json(model)).to(path.join('db', 'csn.json')))
30
-
31
- let data
32
- if (fs.existsSync(path.join(this.task.src, 'data'))) {
33
- data = 'data'
34
- } else if (fs.existsSync(path.join(this.task.src, 'csv'))) {
35
- data = 'csv'
36
- }
37
- if (data) {
38
- promises.push(this.copy(data).to(path.join('db', 'data')))
39
- }
40
- return Promise.all(promises)
41
- }
42
- }