@graffy/pg 0.16.3-alpha.6 → 0.16.3-alpha.8
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/index.cjs +25 -9
- package/index.mjs +25 -9
- package/package.json +2 -2
- package/types/Db.d.ts +3 -3
package/index.cjs
CHANGED
|
@@ -591,14 +591,24 @@ class Db {
|
|
|
591
591
|
this.client = new Pool(connection);
|
|
592
592
|
}
|
|
593
593
|
}
|
|
594
|
-
async query(sql2) {
|
|
594
|
+
async query(sql2, tableOptions) {
|
|
595
|
+
var _a, _b;
|
|
595
596
|
log(`Making SQL query: ${sql2.text}`, sql2.values);
|
|
597
|
+
const cubeOid = parseInt(((_b = (_a = tableOptions == null ? void 0 : tableOptions.schema) == null ? void 0 : _a.typeOids) == null ? void 0 : _b.cube) || "0") || null;
|
|
596
598
|
try {
|
|
597
599
|
sql2.types = {
|
|
598
600
|
getTypeParser: (oid, format) => {
|
|
599
601
|
if (oid === types.builtins.INT8) {
|
|
600
602
|
return (value) => parseInt(value, 10);
|
|
601
603
|
}
|
|
604
|
+
if (oid === cubeOid) {
|
|
605
|
+
return (value) => {
|
|
606
|
+
const array = value.slice(1, -1).split(/\)\s*,\s*\(/).map(
|
|
607
|
+
(corner) => corner.split(",").map((coord) => parseFloat(coord.trim()))
|
|
608
|
+
);
|
|
609
|
+
return array.length > 1 ? array : array[0];
|
|
610
|
+
};
|
|
611
|
+
}
|
|
602
612
|
return types.getTypeParser(oid, format);
|
|
603
613
|
}
|
|
604
614
|
};
|
|
@@ -615,13 +625,13 @@ class Db {
|
|
|
615
625
|
throw Error(`pg.sql_error ${message}`);
|
|
616
626
|
}
|
|
617
627
|
}
|
|
618
|
-
async readSql(sql2) {
|
|
619
|
-
const result = (await this.query(sql2)).rows;
|
|
628
|
+
async readSql(sql2, tableOptions) {
|
|
629
|
+
const result = (await this.query(sql2, tableOptions)).rows;
|
|
620
630
|
log("Read result", result);
|
|
621
631
|
return result;
|
|
622
632
|
}
|
|
623
|
-
async writeSql(sql2) {
|
|
624
|
-
const res = await this.query(sql2);
|
|
633
|
+
async writeSql(sql2, tableOptions) {
|
|
634
|
+
const res = await this.query(sql2, tableOptions);
|
|
625
635
|
log("Rows written", res.rowCount);
|
|
626
636
|
if (!res.rowCount) {
|
|
627
637
|
throw Error(`pg.nothing_written ${sql2.text} with ${sql2.values}`);
|
|
@@ -646,6 +656,10 @@ class Db {
|
|
|
646
656
|
table_schema = ${tableSchema}`)).rows[0].column_types;
|
|
647
657
|
if (!types2)
|
|
648
658
|
throw Error(`pg.missing_table ${table}`);
|
|
659
|
+
const typeOids = (await this.query(sql`
|
|
660
|
+
SELECT jsonb_object_agg(typname, oid) AS type_oids
|
|
661
|
+
FROM pg_type
|
|
662
|
+
WHERE typname = 'cube'`)).rows[0].type_oids;
|
|
649
663
|
const verDefault = (await this.query(sql`
|
|
650
664
|
SELECT column_default
|
|
651
665
|
FROM information_schema.columns
|
|
@@ -657,7 +671,7 @@ class Db {
|
|
|
657
671
|
throw Error(`pg.verCol_without_default ${verCol}`);
|
|
658
672
|
}
|
|
659
673
|
log("ensureSchema", types2);
|
|
660
|
-
tableOptions.schema = { types: types2 };
|
|
674
|
+
tableOptions.schema = { types: types2, typeOids };
|
|
661
675
|
tableOptions.verDefault = verDefault;
|
|
662
676
|
}
|
|
663
677
|
async read(rootQuery, tableOptions) {
|
|
@@ -669,7 +683,8 @@ class Db {
|
|
|
669
683
|
await this.ensureSchema(tableOptions);
|
|
670
684
|
const getByArgs = async (args, projection) => {
|
|
671
685
|
const result = await this.readSql(
|
|
672
|
-
selectByArgs(args, projection, tableOptions)
|
|
686
|
+
selectByArgs(args, projection, tableOptions),
|
|
687
|
+
tableOptions
|
|
673
688
|
);
|
|
674
689
|
const wrappedGraph = common.encodeGraph(common.wrapObject(result, rawPrefix));
|
|
675
690
|
log("getByArgs", wrappedGraph);
|
|
@@ -677,7 +692,8 @@ class Db {
|
|
|
677
692
|
};
|
|
678
693
|
const getByIds = async () => {
|
|
679
694
|
const result = await this.readSql(
|
|
680
|
-
selectByIds(Object.keys(idQueries), null, tableOptions)
|
|
695
|
+
selectByIds(Object.keys(idQueries), null, tableOptions),
|
|
696
|
+
tableOptions
|
|
681
697
|
);
|
|
682
698
|
result.forEach((object) => {
|
|
683
699
|
const wrappedGraph = common.encodeGraph(common.wrapObject(object, rawPrefix));
|
|
@@ -735,7 +751,7 @@ class Db {
|
|
|
735
751
|
const result = [];
|
|
736
752
|
await Promise.all(
|
|
737
753
|
sqls.map(
|
|
738
|
-
(sql2) => this.writeSql(sql2).then((object) => {
|
|
754
|
+
(sql2) => this.writeSql(sql2, tableOptions).then((object) => {
|
|
739
755
|
common.merge(result, common.encodeGraph(common.wrapObject(object, rawPrefix)));
|
|
740
756
|
})
|
|
741
757
|
)
|
package/index.mjs
CHANGED
|
@@ -589,14 +589,24 @@ class Db {
|
|
|
589
589
|
this.client = new Pool(connection);
|
|
590
590
|
}
|
|
591
591
|
}
|
|
592
|
-
async query(sql2) {
|
|
592
|
+
async query(sql2, tableOptions) {
|
|
593
|
+
var _a, _b;
|
|
593
594
|
log(`Making SQL query: ${sql2.text}`, sql2.values);
|
|
595
|
+
const cubeOid = parseInt(((_b = (_a = tableOptions == null ? void 0 : tableOptions.schema) == null ? void 0 : _a.typeOids) == null ? void 0 : _b.cube) || "0") || null;
|
|
594
596
|
try {
|
|
595
597
|
sql2.types = {
|
|
596
598
|
getTypeParser: (oid, format) => {
|
|
597
599
|
if (oid === types.builtins.INT8) {
|
|
598
600
|
return (value) => parseInt(value, 10);
|
|
599
601
|
}
|
|
602
|
+
if (oid === cubeOid) {
|
|
603
|
+
return (value) => {
|
|
604
|
+
const array = value.slice(1, -1).split(/\)\s*,\s*\(/).map(
|
|
605
|
+
(corner) => corner.split(",").map((coord) => parseFloat(coord.trim()))
|
|
606
|
+
);
|
|
607
|
+
return array.length > 1 ? array : array[0];
|
|
608
|
+
};
|
|
609
|
+
}
|
|
600
610
|
return types.getTypeParser(oid, format);
|
|
601
611
|
}
|
|
602
612
|
};
|
|
@@ -613,13 +623,13 @@ class Db {
|
|
|
613
623
|
throw Error(`pg.sql_error ${message}`);
|
|
614
624
|
}
|
|
615
625
|
}
|
|
616
|
-
async readSql(sql2) {
|
|
617
|
-
const result = (await this.query(sql2)).rows;
|
|
626
|
+
async readSql(sql2, tableOptions) {
|
|
627
|
+
const result = (await this.query(sql2, tableOptions)).rows;
|
|
618
628
|
log("Read result", result);
|
|
619
629
|
return result;
|
|
620
630
|
}
|
|
621
|
-
async writeSql(sql2) {
|
|
622
|
-
const res = await this.query(sql2);
|
|
631
|
+
async writeSql(sql2, tableOptions) {
|
|
632
|
+
const res = await this.query(sql2, tableOptions);
|
|
623
633
|
log("Rows written", res.rowCount);
|
|
624
634
|
if (!res.rowCount) {
|
|
625
635
|
throw Error(`pg.nothing_written ${sql2.text} with ${sql2.values}`);
|
|
@@ -644,6 +654,10 @@ class Db {
|
|
|
644
654
|
table_schema = ${tableSchema}`)).rows[0].column_types;
|
|
645
655
|
if (!types2)
|
|
646
656
|
throw Error(`pg.missing_table ${table}`);
|
|
657
|
+
const typeOids = (await this.query(sql`
|
|
658
|
+
SELECT jsonb_object_agg(typname, oid) AS type_oids
|
|
659
|
+
FROM pg_type
|
|
660
|
+
WHERE typname = 'cube'`)).rows[0].type_oids;
|
|
647
661
|
const verDefault = (await this.query(sql`
|
|
648
662
|
SELECT column_default
|
|
649
663
|
FROM information_schema.columns
|
|
@@ -655,7 +669,7 @@ class Db {
|
|
|
655
669
|
throw Error(`pg.verCol_without_default ${verCol}`);
|
|
656
670
|
}
|
|
657
671
|
log("ensureSchema", types2);
|
|
658
|
-
tableOptions.schema = { types: types2 };
|
|
672
|
+
tableOptions.schema = { types: types2, typeOids };
|
|
659
673
|
tableOptions.verDefault = verDefault;
|
|
660
674
|
}
|
|
661
675
|
async read(rootQuery, tableOptions) {
|
|
@@ -667,7 +681,8 @@ class Db {
|
|
|
667
681
|
await this.ensureSchema(tableOptions);
|
|
668
682
|
const getByArgs = async (args, projection) => {
|
|
669
683
|
const result = await this.readSql(
|
|
670
|
-
selectByArgs(args, projection, tableOptions)
|
|
684
|
+
selectByArgs(args, projection, tableOptions),
|
|
685
|
+
tableOptions
|
|
671
686
|
);
|
|
672
687
|
const wrappedGraph = encodeGraph(wrapObject(result, rawPrefix));
|
|
673
688
|
log("getByArgs", wrappedGraph);
|
|
@@ -675,7 +690,8 @@ class Db {
|
|
|
675
690
|
};
|
|
676
691
|
const getByIds = async () => {
|
|
677
692
|
const result = await this.readSql(
|
|
678
|
-
selectByIds(Object.keys(idQueries), null, tableOptions)
|
|
693
|
+
selectByIds(Object.keys(idQueries), null, tableOptions),
|
|
694
|
+
tableOptions
|
|
679
695
|
);
|
|
680
696
|
result.forEach((object) => {
|
|
681
697
|
const wrappedGraph = encodeGraph(wrapObject(object, rawPrefix));
|
|
@@ -733,7 +749,7 @@ class Db {
|
|
|
733
749
|
const result = [];
|
|
734
750
|
await Promise.all(
|
|
735
751
|
sqls.map(
|
|
736
|
-
(sql2) => this.writeSql(sql2).then((object) => {
|
|
752
|
+
(sql2) => this.writeSql(sql2, tableOptions).then((object) => {
|
|
737
753
|
merge(result, encodeGraph(wrapObject(object, rawPrefix)));
|
|
738
754
|
})
|
|
739
755
|
)
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@graffy/pg",
|
|
3
3
|
"description": "The standard Postgres module for Graffy. Each instance this module mounts a Postgres table as a Graffy subtree.",
|
|
4
4
|
"author": "aravind (https://github.com/aravindet)",
|
|
5
|
-
"version": "0.16.3-alpha.
|
|
5
|
+
"version": "0.16.3-alpha.8",
|
|
6
6
|
"main": "./index.cjs",
|
|
7
7
|
"exports": {
|
|
8
8
|
"import": "./index.mjs",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"license": "Apache-2.0",
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@graffy/common": "0.16.3-alpha.
|
|
19
|
+
"@graffy/common": "0.16.3-alpha.8",
|
|
20
20
|
"debug": "^4.3.3"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
package/types/Db.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export default class Db {
|
|
2
2
|
constructor(connection: any);
|
|
3
3
|
client: any;
|
|
4
|
-
query(sql: any): Promise<any>;
|
|
5
|
-
readSql(sql: any): Promise<any>;
|
|
6
|
-
writeSql(sql: any): Promise<any>;
|
|
4
|
+
query(sql: any, tableOptions: any): Promise<any>;
|
|
5
|
+
readSql(sql: any, tableOptions: any): Promise<any>;
|
|
6
|
+
writeSql(sql: any, tableOptions: any): Promise<any>;
|
|
7
7
|
ensureSchema(tableOptions: any): Promise<void>;
|
|
8
8
|
read(rootQuery: any, tableOptions: any): Promise<any>;
|
|
9
9
|
write(rootChange: any, tableOptions: any): Promise<any[]>;
|