@easy-cms/db-postgres 0.6.0 → 0.8.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.
Files changed (2) hide show
  1. package/dist/index.js +19 -0
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -19,6 +19,12 @@ import { drizzle as drizzlePostgresJs } from "drizzle-orm/postgres-js";
19
19
  import postgresJs from "postgres";
20
20
  import { DEFAULT_MIGRATION_DIR, DEFAULT_TABLE_PREFIX } from "@easy-cms/drizzle";
21
21
  var kit = () => import("drizzle-kit/api");
22
+ var PG_TYPES = { number: "double precision", integer: "integer", boolean: "boolean" };
23
+ var aliases = 0;
24
+ var alias = () => sql.raw(`_el${++aliases % 1e6}`);
25
+ var jsonPath = (path) => sql.raw(`'{${path.join(",")}}'`);
26
+ var cast = (text2, type) => type === "text" ? text2 : sql`${text2}::${sql.raw(PG_TYPES[type])}`;
27
+ var onlyArray = (array) => sql`case when jsonb_typeof(${array}) = 'array' then ${array} end`;
22
28
  var postgresDialect = {
23
29
  name: "postgres",
24
30
  table: (name, columns, indexes) => pgTable(name, columns, indexes),
@@ -31,6 +37,19 @@ var postgresDialect = {
31
37
  boolean: (name) => boolean(name),
32
38
  json: (name) => jsonb(name),
33
39
  like: (column, pattern) => sql`${column} ILIKE ${pattern} ESCAPE '\\'`,
40
+ someElement: (array, condition) => {
41
+ const el = alias();
42
+ const where = condition({
43
+ get: (path, type) => cast(sql`(${el}.value #>> ${jsonPath(path)})`, type),
44
+ list: (path) => sql`(${el}.value #> ${jsonPath(path)})`
45
+ });
46
+ return sql`exists (select 1 from jsonb_array_elements(${onlyArray(array)}) as ${el}(value)${where ? sql` where ${where}` : sql``})`;
47
+ },
48
+ firstElementValue: (array, path, type) => {
49
+ const el = alias();
50
+ const text2 = sql`(${el}.value #>> ${jsonPath(path)})`;
51
+ return sql`(select ${cast(text2, type)} from jsonb_array_elements(${onlyArray(array)}) with ordinality as ${el}(value, idx) where ${text2} is not null order by ${el}.idx limit 1)`;
52
+ },
34
53
  param: (n) => `$${n}`,
35
54
  migrationsTableSQL: (name) => `CREATE TABLE IF NOT EXISTS "${name}" (
36
55
  "id" serial PRIMARY KEY NOT NULL,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/db-postgres",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "description": "PostgreSQL database adapter for Easy CMS",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -22,11 +22,11 @@
22
22
  "drizzle-kit": "0.31.11",
23
23
  "drizzle-orm": "0.45.3",
24
24
  "postgres": "^3.4.9",
25
- "@easy-cms/drizzle": "^0.6.0"
25
+ "@easy-cms/drizzle": "^0.8.0"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "@electric-sql/pglite": ">=0.3.0",
29
- "@easy-cms/core": "^0.6.0"
29
+ "@easy-cms/core": "^0.8.0"
30
30
  },
31
31
  "peerDependenciesMeta": {
32
32
  "@electric-sql/pglite": {
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "devDependencies": {
37
37
  "@electric-sql/pglite": "^0.5.8",
38
- "@easy-cms/core": "^0.6.0"
38
+ "@easy-cms/core": "^0.8.0"
39
39
  },
40
40
  "publishConfig": {
41
41
  "access": "public"