@easy-cms/db-postgres 0.7.0 → 0.9.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 +31 -5
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -20,6 +20,11 @@ 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
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`;
23
28
  var postgresDialect = {
24
29
  name: "postgres",
25
30
  table: (name, columns, indexes) => pgTable(name, columns, indexes),
@@ -32,12 +37,33 @@ var postgresDialect = {
32
37
  boolean: (name) => boolean(name),
33
38
  json: (name) => jsonb(name),
34
39
  like: (column, pattern) => sql`${column} ILIKE ${pattern} ESCAPE '\\'`,
35
- someElement: (column, condition) => {
36
- const where = condition((path, type) => {
37
- const text2 = sql`(_el.value #>> ${sql.raw(`'{${path.join(",")}}'`)})`;
38
- return type === "text" ? text2 : sql`${text2}::${sql.raw(PG_TYPES[type])}`;
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)})`
39
45
  });
40
- return sql`exists (select 1 from jsonb_array_elements(case when jsonb_typeof(${column}) = 'array' then ${column} end) as _el(value)${where ? sql` where ${where}` : sql``})`;
46
+ return sql`exists (select 1 from jsonb_array_elements(${onlyArray(array)}) as ${el}(value)${where ? sql` where ${where}` : sql``})`;
47
+ },
48
+ firstElementValue: (array, lists, path, type) => {
49
+ const els = [alias()];
50
+ const from = [
51
+ sql`jsonb_array_elements(${onlyArray(array)}) with ordinality as ${els[0]}(value, idx)`
52
+ ];
53
+ for (const list of lists) {
54
+ const outer = els.at(-1);
55
+ const el = alias();
56
+ els.push(el);
57
+ from.push(
58
+ sql`jsonb_array_elements(${onlyArray(sql`(${outer}.value #> ${jsonPath(list)})`)}) with ordinality as ${el}(value, idx)`
59
+ );
60
+ }
61
+ const text2 = sql`(${els.at(-1)}.value #>> ${jsonPath(path)})`;
62
+ const order = sql.join(
63
+ els.map((el) => sql`${el}.idx`),
64
+ sql`, `
65
+ );
66
+ return sql`(select ${cast(text2, type)} from ${sql.join(from, sql`, `)} where ${text2} is not null order by ${order} limit 1)`;
41
67
  },
42
68
  param: (n) => `$${n}`,
43
69
  migrationsTableSQL: (name) => `CREATE TABLE IF NOT EXISTS "${name}" (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/db-postgres",
3
- "version": "0.7.0",
3
+ "version": "0.9.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.7.0"
25
+ "@easy-cms/drizzle": "^0.9.0"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "@electric-sql/pglite": ">=0.3.0",
29
- "@easy-cms/core": "^0.7.0"
29
+ "@easy-cms/core": "^0.9.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.7.0"
38
+ "@easy-cms/core": "^0.9.0"
39
39
  },
40
40
  "publishConfig": {
41
41
  "access": "public"