prostgles-server 2.0.178 → 2.0.181

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 (103) hide show
  1. package/dist/AuthHandler.d.ts +4 -4
  2. package/dist/AuthHandler.d.ts.map +1 -1
  3. package/dist/DBSchemaBuilder.d.ts +6 -6
  4. package/dist/DBSchemaBuilder.d.ts.map +1 -1
  5. package/dist/DBSchemaBuilder.js +38 -15
  6. package/dist/DBSchemaBuilder.js.map +1 -1
  7. package/dist/DboBuilder.d.ts +20 -21
  8. package/dist/DboBuilder.d.ts.map +1 -1
  9. package/dist/DboBuilder.js +7 -2
  10. package/dist/DboBuilder.js.map +1 -1
  11. package/dist/Prostgles.d.ts +8 -10
  12. package/dist/Prostgles.d.ts.map +1 -1
  13. package/dist/Prostgles.js.map +1 -1
  14. package/dist/PubSubManager.js +1 -1
  15. package/dist/PubSubManager.js.map +1 -1
  16. package/dist/PublishParser.d.ts +37 -37
  17. package/dist/PublishParser.d.ts.map +1 -1
  18. package/dist/PublishParser.js.map +1 -1
  19. package/dist/index.d.ts +2 -3
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js.map +1 -1
  22. package/lib/AuthHandler.d.ts +148 -0
  23. package/lib/AuthHandler.d.ts.map +1 -0
  24. package/lib/AuthHandler.js +411 -0
  25. package/lib/AuthHandler.ts +3 -3
  26. package/lib/DBEventsManager.d.ts +38 -0
  27. package/lib/DBEventsManager.d.ts.map +1 -0
  28. package/lib/DBEventsManager.js +136 -0
  29. package/lib/DBSchemaBuilder.d.ts +11 -0
  30. package/lib/DBSchemaBuilder.d.ts.map +1 -0
  31. package/lib/DBSchemaBuilder.js +108 -0
  32. package/lib/DBSchemaBuilder.ts +75 -33
  33. package/lib/DboBuilder.d.ts +428 -0
  34. package/lib/DboBuilder.d.ts.map +1 -0
  35. package/lib/DboBuilder.js +3083 -0
  36. package/lib/DboBuilder.ts +33 -27
  37. package/lib/FileManager.d.ts +168 -0
  38. package/lib/FileManager.d.ts.map +1 -0
  39. package/lib/FileManager.js +474 -0
  40. package/lib/Filtering.d.ts +15 -0
  41. package/lib/Filtering.d.ts.map +1 -0
  42. package/lib/Filtering.js +299 -0
  43. package/lib/PostgresNotifListenManager.d.ts +27 -0
  44. package/lib/PostgresNotifListenManager.d.ts.map +1 -0
  45. package/lib/PostgresNotifListenManager.js +122 -0
  46. package/lib/Prostgles.d.ts +193 -0
  47. package/lib/Prostgles.d.ts.map +1 -0
  48. package/lib/Prostgles.js +579 -0
  49. package/lib/Prostgles.ts +6 -6
  50. package/lib/PubSubManager.d.ts +157 -0
  51. package/lib/PubSubManager.d.ts.map +1 -0
  52. package/lib/PubSubManager.js +1400 -0
  53. package/lib/PubSubManager.ts +1 -1
  54. package/lib/PublishParser.d.ts +262 -0
  55. package/lib/PublishParser.d.ts.map +1 -0
  56. package/lib/PublishParser.js +390 -0
  57. package/lib/PublishParser.ts +39 -38
  58. package/lib/QueryBuilder.d.ts +124 -0
  59. package/lib/QueryBuilder.d.ts.map +1 -0
  60. package/lib/QueryBuilder.js +1349 -0
  61. package/lib/SyncReplication.d.ts +34 -0
  62. package/lib/SyncReplication.d.ts.map +1 -0
  63. package/lib/SyncReplication.js +411 -0
  64. package/lib/TableConfig.d.ts +175 -0
  65. package/lib/TableConfig.d.ts.map +1 -0
  66. package/lib/TableConfig.js +231 -0
  67. package/lib/index.d.ts +10 -0
  68. package/lib/index.d.ts.map +1 -0
  69. package/lib/index.js +45 -0
  70. package/lib/index.ts +3 -4
  71. package/lib/shortestPath.d.ts +10 -0
  72. package/lib/shortestPath.d.ts.map +1 -0
  73. package/lib/shortestPath.js +111 -0
  74. package/lib/utils.d.ts +2 -0
  75. package/lib/utils.d.ts.map +1 -0
  76. package/lib/utils.js +5 -0
  77. package/package.json +3 -3
  78. package/tests/client/PID.txt +1 -1
  79. package/tests/client/index.d.ts +1 -1
  80. package/tests/client/index.d.ts.map +1 -1
  81. package/tests/client/package-lock.json +15 -15
  82. package/tests/client/package.json +1 -1
  83. package/tests/client_only_queries.d.ts +4 -0
  84. package/tests/client_only_queries.d.ts.map +1 -0
  85. package/tests/isomorphic_queries.d.ts +6 -0
  86. package/tests/isomorphic_queries.d.ts.map +1 -0
  87. package/tests/server/DBoGenerated.d.ts +227 -323
  88. package/tests/server/dboTypeCheck.d.ts +2 -0
  89. package/tests/server/dboTypeCheck.d.ts.map +1 -0
  90. package/tests/server/dboTypeCheck.js +16 -0
  91. package/tests/server/dboTypeCheck.ts +20 -0
  92. package/tests/server/index.d.ts +2 -0
  93. package/tests/server/index.d.ts.map +1 -0
  94. package/tests/server/index.js +11 -11
  95. package/tests/server/index.ts +23 -16
  96. package/tests/server/package-lock.json +5 -5
  97. package/tests/server/publishTypeCheck.d.ts +2 -0
  98. package/tests/server/publishTypeCheck.d.ts.map +1 -0
  99. package/tests/server/publishTypeCheck.js +120 -0
  100. package/tests/server/publishTypeCheck.ts +129 -0
  101. package/tests/server/tsconfig.json +4 -5
  102. package/tests/server_only_queries.d.ts +2 -0
  103. package/tests/server_only_queries.d.ts.map +1 -0
@@ -36,7 +36,7 @@ export type BasicSession = {
36
36
 
37
37
  };
38
38
  export type AuthClientRequest = { socket: any } | { httpReq: ExpressReq }
39
- export type Auth<S extends DBSchema | undefined = undefined> = {
39
+ export type Auth<S = void> = {
40
40
  /**
41
41
  * Name of the cookie or socket hadnshake query param that represents the session id.
42
42
  * Defaults to "session_id"
@@ -136,8 +136,8 @@ export type ClientInfo = {
136
136
  sid?: string;
137
137
  }
138
138
 
139
- export default class AuthHandler<S extends DBSchema | undefined = undefined> {
140
- protected opts?: Auth<S>;
139
+ export default class AuthHandler {
140
+ protected opts?: Auth;
141
141
  dbo: DBHandlerServer;
142
142
  db: DB;
143
143
  sidKeyName?: string;
@@ -0,0 +1,38 @@
1
+ import { PostgresNotifListenManager } from "./PostgresNotifListenManager";
2
+ import { DB, PGP } from "./Prostgles";
3
+ import { PRGLIOSocket } from "./DboBuilder";
4
+ export declare class DBEventsManager {
5
+ notifies: {
6
+ [key: string]: {
7
+ socketChannel: string;
8
+ sockets: any[];
9
+ localFuncs: ((payload: string) => void)[];
10
+ notifMgr: PostgresNotifListenManager;
11
+ };
12
+ };
13
+ notice: {
14
+ socketChannel: string;
15
+ socketUnsubChannel: string;
16
+ sockets: any[];
17
+ };
18
+ notifManager?: PostgresNotifListenManager;
19
+ db_pg: DB;
20
+ pgp: PGP;
21
+ constructor(db_pg: DB, pgp: PGP);
22
+ private onNotif;
23
+ onNotice: (notice: any) => void;
24
+ getNotifChannelName: (channel: string) => Promise<any>;
25
+ addNotify(query: string, socket?: PRGLIOSocket, func?: any): Promise<{
26
+ socketChannel: string;
27
+ socketUnsubChannel: string;
28
+ notifChannel: string;
29
+ unsubscribe?: () => void;
30
+ }>;
31
+ removeNotify(channel?: string, socket?: PRGLIOSocket, func?: any): void;
32
+ addNotice(socket: PRGLIOSocket): {
33
+ socketChannel: string;
34
+ socketUnsubChannel: string;
35
+ };
36
+ removeNotice(socket: PRGLIOSocket): void;
37
+ }
38
+ //# sourceMappingURL=DBEventsManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DBEventsManager.d.ts","sourceRoot":"","sources":["DBEventsManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAqB,MAAM,8BAA8B,CAAC;AAC7F,OAAO,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAEtC,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,qBAAa,eAAe;IAE1B,QAAQ,EAAE;QACR,CAAC,GAAG,EAAE,MAAM,GAAG;YACb,aAAa,EAAE,MAAM,CAAC;YACtB,OAAO,EAAE,GAAG,EAAE,CAAC;YACf,UAAU,EAAE,CAAC,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC,EAAE,CAAC;YAC1C,QAAQ,EAAE,0BAA0B,CAAC;SACtC,CAAA;KACF,CAAM;IAEP,MAAM,EAAE;QACN,aAAa,EAAE,MAAM,CAAC;QACtB,kBAAkB,EAAE,MAAM,CAAC;QAC3B,OAAO,EAAE,GAAG,EAAE,CAAC;KAChB,CAIC;IAEF,YAAY,CAAC,EAAE,0BAA0B,CAAC;IAE1C,KAAK,EAAE,EAAE,CAAC;IACV,GAAG,EAAE,GAAG,CAAA;gBACI,KAAK,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG;IAK/B,OAAO,CAAC,OAAO,CAgBd;IAED,QAAQ,WAAY,GAAG,UAMtB;IAED,mBAAmB,YAAmB,MAAM,kBAG3C;IAEK,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,YAAY,EAAE,IAAI,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC;QACzE,aAAa,EAAE,MAAM,CAAC;QACtB,kBAAkB,EAAE,MAAM,CAAC;QAC3B,YAAY,EAAE,MAAM,CAAC;QACrB,WAAW,CAAC,EAAE,MAAM,IAAI,CAAC;KAC1B,CAAC;IAmEF,YAAY,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,YAAY,EAAE,IAAI,CAAC,EAAE,GAAG;IAkBhE,SAAS,CAAC,MAAM,EAAE,YAAY;;;;IAiB9B,YAAY,CAAC,MAAM,EAAE,YAAY;CAIlC"}
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DBEventsManager = void 0;
4
+ const PostgresNotifListenManager_1 = require("./PostgresNotifListenManager");
5
+ const prostgles_types_1 = require("prostgles-types");
6
+ class DBEventsManager {
7
+ constructor(db_pg, pgp) {
8
+ this.notifies = {};
9
+ this.notice = {
10
+ socketChannel: prostgles_types_1.CHANNELS.NOTICE_EV,
11
+ socketUnsubChannel: prostgles_types_1.CHANNELS.NOTICE_EV + "unsubscribe",
12
+ sockets: []
13
+ };
14
+ this.onNotif = ({ channel, payload }) => {
15
+ // console.log(36, { channel, payload }, Object.keys(this.notifies));
16
+ Object.keys(this.notifies)
17
+ .filter(ch => ch === channel)
18
+ .map(ch => {
19
+ const sub = this.notifies[ch];
20
+ sub.sockets.map(s => {
21
+ s.emit(sub.socketChannel, payload);
22
+ });
23
+ sub.localFuncs.map(lf => {
24
+ lf(payload);
25
+ });
26
+ });
27
+ };
28
+ this.onNotice = (notice) => {
29
+ if (this.notice && this.notice.sockets.length) {
30
+ this.notice.sockets.map(s => {
31
+ s.emit(this.notice.socketChannel, notice);
32
+ });
33
+ }
34
+ };
35
+ this.getNotifChannelName = async (channel) => {
36
+ const c = await this.db_pg.one("SELECT quote_ident($1) as c", channel);
37
+ return c.c;
38
+ };
39
+ this.db_pg = db_pg;
40
+ this.pgp = pgp;
41
+ }
42
+ async addNotify(query, socket, func) {
43
+ if (typeof query !== "string" || (!socket && !func)) {
44
+ throw "Expecting (query: string, socket?, localFunc?) But received: " + JSON.stringify({ query, socket, func });
45
+ }
46
+ /* Remove comments */
47
+ let q = query.trim()
48
+ .replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, '\n')
49
+ .split("\n").map(v => v.trim()).filter(v => v && !v.startsWith("--"))
50
+ .join("\n");
51
+ /* Find the notify channel name */
52
+ if (!q.toLowerCase().startsWith("listen")) {
53
+ throw "Expecting a LISTEN query but got: " + query;
54
+ }
55
+ q = q.slice(7).trim(); // Remove listen
56
+ if (q.endsWith(";"))
57
+ q = q.slice(0, -1);
58
+ if (q.startsWith('"') && q.endsWith('"')) {
59
+ q = q.slice(1, -1);
60
+ }
61
+ else {
62
+ /* Replicate PG by lowercasing identifier if not quoted */
63
+ q = q.toLowerCase();
64
+ }
65
+ q = q.replace(/""/g, `"`);
66
+ let channel = q;
67
+ let notifChannel = await this.getNotifChannelName(channel);
68
+ notifChannel = notifChannel.replace(/""/g, `"`);
69
+ if (notifChannel.startsWith('"'))
70
+ notifChannel = notifChannel.slice(1, -1);
71
+ const socketChannel = prostgles_types_1.CHANNELS.LISTEN_EV + notifChannel, socketUnsubChannel = socketChannel + "unsubscribe";
72
+ if (!this.notifies[notifChannel]) {
73
+ this.notifies[notifChannel] = {
74
+ socketChannel,
75
+ sockets: socket ? [socket] : [],
76
+ localFuncs: func ? [func] : [],
77
+ notifMgr: await PostgresNotifListenManager_1.PostgresNotifListenManager.create(this.db_pg, this.onNotif, channel)
78
+ };
79
+ }
80
+ else {
81
+ if (socket && !this.notifies[notifChannel].sockets.find(s => s.id === socket.id)) {
82
+ this.notifies[notifChannel].sockets.push(socket);
83
+ }
84
+ else if (func) {
85
+ this.notifies[notifChannel].localFuncs.push(func);
86
+ }
87
+ }
88
+ if (socket) {
89
+ socket.removeAllListeners(socketUnsubChannel);
90
+ socket.on(socketUnsubChannel, () => {
91
+ this.removeNotify(notifChannel, socket);
92
+ });
93
+ }
94
+ return {
95
+ // unsubscribe: () => this.removeNotify(notifChannel, socket, func),
96
+ socketChannel,
97
+ socketUnsubChannel,
98
+ notifChannel,
99
+ };
100
+ }
101
+ removeNotify(channel, socket, func) {
102
+ if (channel && this.notifies[channel]) {
103
+ if (socket) {
104
+ this.notifies[channel].sockets = this.notifies[channel].sockets.filter(s => s.id !== socket.id);
105
+ }
106
+ else if (func) {
107
+ this.notifies[channel].localFuncs = this.notifies[channel].localFuncs.filter(f => f !== func);
108
+ }
109
+ /* UNLISTEN if no listeners ?? */
110
+ }
111
+ if (socket) {
112
+ (0, prostgles_types_1.getKeys)(this.notifies).forEach(channel => {
113
+ this.notifies[channel].sockets = this.notifies[channel].sockets.filter(s => s.id !== socket.id);
114
+ });
115
+ }
116
+ }
117
+ addNotice(socket) {
118
+ if (!socket || !socket.id)
119
+ throw "Expecting a socket obj with id";
120
+ if (!this.notice.sockets.find(s => s.id === socket.id)) {
121
+ this.notice.sockets.push(socket);
122
+ }
123
+ const { socketChannel, socketUnsubChannel } = this.notice;
124
+ socket.removeAllListeners(socketUnsubChannel);
125
+ socket.on(socketUnsubChannel, () => {
126
+ this.removeNotice(socket);
127
+ });
128
+ return { socketChannel, socketUnsubChannel, };
129
+ }
130
+ removeNotice(socket) {
131
+ if (!socket || !socket.id)
132
+ throw "Expecting a socket obj with id";
133
+ this.notice.sockets = this.notice.sockets.filter(s => s.id !== socket.id);
134
+ }
135
+ }
136
+ exports.DBEventsManager = DBEventsManager;
@@ -0,0 +1,11 @@
1
+ import { DBSchema, TableHandler, ViewHandler } from "prostgles-types";
2
+ import { DBHandlerServer, DboBuilder } from "./DboBuilder";
3
+ import { PublishAllOrNothing, PublishTableRule, PublishViewRule } from "./PublishParser";
4
+ export declare const getDBSchema: (dboBuilder: DboBuilder) => string;
5
+ export declare type DBOFullyTyped<Schema = void> = Schema extends DBSchema ? ({
6
+ [tov_name in keyof Schema]: Schema[tov_name]["is_view"] extends true ? ViewHandler<Schema[tov_name]["columns"]> : TableHandler<Schema[tov_name]["columns"]>;
7
+ } & Pick<DBHandlerServer, "tx" | "sql">) : DBHandlerServer;
8
+ export declare type PublishFullyTyped<Schema = void> = Schema extends DBSchema ? (PublishAllOrNothing | {
9
+ [tov_name in keyof Partial<Schema>]: PublishAllOrNothing | (Schema[tov_name]["is_view"] extends true ? PublishViewRule<Schema[tov_name]["columns"]> : PublishTableRule<Schema[tov_name]["columns"]>);
10
+ }) : (PublishAllOrNothing | Record<string, PublishViewRule | PublishTableRule | PublishAllOrNothing>);
11
+ //# sourceMappingURL=DBSchemaBuilder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DBSchemaBuilder.d.ts","sourceRoot":"","sources":["DBSchemaBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAGtE,OAAO,EAAE,eAAe,EAAE,UAAU,EAAmC,MAAM,cAAc,CAAC;AAC5F,OAAO,EAAE,mBAAmB,EAAiB,gBAAgB,EAAE,eAAe,EAAI,MAAM,iBAAiB,CAAC;AAG1G,eAAO,MAAM,WAAW,eAAgB,UAAU,KAAG,MAuBpD,CAAA;AAED,oBAAY,aAAa,CAAC,MAAM,GAAG,IAAI,IAAI,MAAM,SAAS,QAAQ,GAAE,CAChE;KACG,QAAQ,IAAI,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,SAAS,CAAC,SAAS,IAAI,GAClE,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,SAAS,CAAC,CAAC,GACxC,YAAY,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,SAAS,CAAC,CAAC;CAC5C,GAAG,IAAI,CAAC,eAAe,EAAE,IAAI,GAAG,KAAK,CAAC,CACxC,GACD,eAAe,CAAC;AA+DlB,oBAAY,iBAAiB,CAAC,MAAM,GAAG,IAAI,IAAI,MAAM,SAAS,QAAQ,GAAE,CACpE,mBAAmB,GACnB;KACC,QAAQ,IAAI,MAAM,OAAO,CAAC,MAAM,CAAC,GAC9B,mBAAmB,GACnB,CACA,MAAM,CAAC,QAAQ,CAAC,CAAC,SAAS,CAAC,SAAS,IAAI,GACtC,eAAe,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,SAAS,CAAC,CAAC,GAC5C,gBAAgB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,SAAS,CAAC,CAAC,CAChD;CACJ,CACF,GAAG,CACA,mBAAmB,GACnB,MAAM,CAAC,MAAM,EAAE,eAAe,GAAG,gBAAgB,GAAG,mBAAmB,CAAC,CAC3E,CAAC"}
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getDBSchema = void 0;
7
+ const _1 = __importDefault(require("."));
8
+ const DboBuilder_1 = require("./DboBuilder");
9
+ const getDBSchema = (dboBuilder) => {
10
+ let tables = [];
11
+ /** Tables and columns are sorted to avoid infinite loops due to changing order */
12
+ dboBuilder.tablesOrViews?.slice(0).sort((a, b) => a.name.localeCompare(b.name)).forEach(tov => {
13
+ const cols = tov.columns.slice(0).sort((a, b) => a.name.localeCompare(b.name));
14
+ tables.push(`${(0, DboBuilder_1.escapeTSNames)(tov.name)}: {
15
+ is_view: ${tov.is_view};
16
+ select: ${tov.privileges.select};
17
+ insert: ${tov.privileges.insert};
18
+ update: ${tov.privileges.update};
19
+ delete: ${tov.privileges.delete};
20
+ columns: {${cols.map(c => `
21
+ ${(0, DboBuilder_1.escapeTSNames)(c.name)}${c.is_nullable || c.has_default ? "?" : ""}: ${(0, DboBuilder_1.postgresToTsType)(c.udt_name)}${c.is_nullable ? " | null" : ""}`).join(";")}
22
+ };
23
+ };\n `);
24
+ });
25
+ return `
26
+ export type DBSchemaGenerated = {
27
+ ${tables.join("")}
28
+ }
29
+ `;
30
+ };
31
+ exports.getDBSchema = getDBSchema;
32
+ /** Type checks */
33
+ (() => {
34
+ const ddb = 1;
35
+ ddb.dwad.insert;
36
+ ddb.dwad.delete;
37
+ const d = 1;
38
+ d.dwad.insert;
39
+ d.dwad.delete;
40
+ const p = 1;
41
+ p.dbo.dwad.insert;
42
+ ddb.dwad.delete;
43
+ (0, _1.default)({
44
+ dbConnection: 1,
45
+ publish: async (params) => {
46
+ const row = await params.dbo.dwadwa.find?.({});
47
+ return "*";
48
+ },
49
+ onReady: (dbo) => {
50
+ dbo.tdwa.find();
51
+ }
52
+ });
53
+ const auth = {
54
+ sidKeyName: "sid_token",
55
+ getUser: async (sid, db, _db) => {
56
+ db.dwadaw.find;
57
+ return 1;
58
+ }
59
+ };
60
+ });
61
+ /** Test the created schema */
62
+ const c = 1;
63
+ const test = c;
64
+ const db = 1;
65
+ const publish = () => {
66
+ const r = {
67
+ tbl1: {
68
+ select: {
69
+ fields: "*",
70
+ forcedFilter: { col1: 32, col2: "" }
71
+ },
72
+ getColumns: true,
73
+ getInfo: true,
74
+ delete: {
75
+ filterFields: { col1: 1 }
76
+ }
77
+ },
78
+ tbl2: {
79
+ delete: {
80
+ filterFields: "*",
81
+ forcedFilter: { col1: 2 }
82
+ }
83
+ }
84
+ };
85
+ const res = {
86
+ tbl1: {
87
+ select: {
88
+ fields: "*",
89
+ forcedFilter: { col1: 32, col2: "" }
90
+ },
91
+ getColumns: true,
92
+ getInfo: true,
93
+ delete: {
94
+ filterFields: { col1: 1 }
95
+ }
96
+ },
97
+ tbl2: {
98
+ delete: {
99
+ filterFields: "*",
100
+ forcedFilter: { col1: 2 }
101
+ }
102
+ }
103
+ };
104
+ const res1 = r;
105
+ const p = 1;
106
+ p.dbo.dwadaw.find();
107
+ return res;
108
+ };
@@ -1,73 +1,95 @@
1
- import { DBSchemaColumns, DBSchemaInsertColumns, DBSchema, TableHandler, ViewHandler } from "prostgles-types";
1
+ import { DBSchema, TableHandler, ViewHandler } from "prostgles-types";
2
+ import prostgles from ".";
3
+ import { Auth } from "./AuthHandler";
2
4
  import { DBHandlerServer, DboBuilder, escapeTSNames, postgresToTsType } from "./DboBuilder";
3
- import { PublishAllOrNothing, PublishParams, PublishTableRule, PublishViewRule, TableRule, ViewRule } from "./PublishParser";
5
+ import { PublishAllOrNothing, PublishParams, PublishTableRule, PublishViewRule, } from "./PublishParser";
4
6
 
5
7
 
6
8
  export const getDBSchema = (dboBuilder: DboBuilder): string => {
7
9
  let tables: string[] = [];
8
10
 
11
+
9
12
  /** Tables and columns are sorted to avoid infinite loops due to changing order */
10
13
  dboBuilder.tablesOrViews?.slice(0).sort((a, b) => a.name.localeCompare(b.name)).forEach(tov => {
11
14
  const cols = tov.columns.slice(0).sort((a, b) => a.name.localeCompare(b.name));
12
15
  tables.push(`${escapeTSNames(tov.name)}: {
13
16
  is_view: ${tov.is_view};
14
- select: ${tov.privileges.select}
15
- insert: ${tov.privileges.insert}
16
- update: ${tov.privileges.update}
17
- delete: ${tov.privileges.delete}
18
- dataTypes: { ${cols.map(c => `${escapeTSNames(c.name)}: ${postgresToTsType(c.udt_name)}${c.is_nullable? " | null" : ""}`).join("; ")} };
17
+ select: ${tov.privileges.select};
18
+ insert: ${tov.privileges.insert};
19
+ update: ${tov.privileges.update};
20
+ delete: ${tov.privileges.delete};
19
21
  columns: {${cols.map(c => `
20
- ${escapeTSNames(c.name)}: { type: ${postgresToTsType(c.udt_name)}; is_nullable: ${c.is_nullable}; is_nullable_or_has_default: ${c.is_nullable || c.has_default}; }`).join(";\n")}
21
- }
22
+ ${escapeTSNames(c.name)}${c.is_nullable || c.has_default? "?" : ""}: ${postgresToTsType(c.udt_name)}${c.is_nullable? " | null" : ""}`).join(";")}
23
+ };
22
24
  };\n `)
23
25
  })
24
26
  return `
25
- type DBSchema = {
27
+ export type DBSchemaGenerated = {
26
28
  ${tables.join("")}
27
29
  }
28
30
  `;
29
31
  }
30
32
 
31
- const ccc: DBSchemaInsertColumns<{ col1: { type: string; }; col2: { type: number; is_nullable_or_has_default: true } }> = {
32
- col1: "",
33
- col2: 22
34
- }
35
-
36
-
37
-
38
- export type DBOFullyTyped<Schema extends DBSchema | undefined = undefined> = Schema extends DBSchema? (
33
+ export type DBOFullyTyped<Schema = void> = Schema extends DBSchema? (
39
34
  {
40
35
  [tov_name in keyof Schema]: Schema[tov_name]["is_view"] extends true?
41
- ViewHandler<DBSchemaColumns<Schema[tov_name]["columns"]>> :
42
- TableHandler<DBSchemaColumns<Schema[tov_name]["columns"]>>
36
+ ViewHandler<Schema[tov_name]["columns"]> :
37
+ TableHandler<Schema[tov_name]["columns"]>
43
38
  } & Pick<DBHandlerServer, "tx" | "sql">
44
39
  ) :
45
40
  DBHandlerServer;
46
41
 
42
+
43
+
44
+
47
45
  /** Type checks */
48
46
  (() => {
47
+
49
48
  const ddb: DBOFullyTyped = 1 as any;
50
49
  ddb.dwad.insert!;
51
50
  ddb.dwad.delete!;
52
51
 
52
+ const d: DBOFullyTyped<undefined> = 1 as any;
53
+ d.dwad.insert!;
54
+ d.dwad.delete!;
55
+
53
56
  const p: PublishParams = 1 as any;
54
57
  p.dbo.dwad.insert!;
55
58
  ddb.dwad.delete!;
59
+
60
+ prostgles({
61
+ dbConnection: 1 as any,
62
+ publish: async (params) => {
63
+ const row = await params.dbo.dwadwa.find?.({});
64
+
65
+ return "*" as "*"
66
+ },
67
+ onReady: (dbo) => {
68
+ dbo.tdwa.find!()
69
+ }
70
+ });
71
+
72
+
73
+ const auth: Auth = {
74
+ sidKeyName: "sid_token",
75
+ getUser: async (sid, db, _db) => {
76
+ db.dwadaw.find;
77
+ return 1 as any;
78
+ }
79
+ }
56
80
  })
57
81
 
58
82
  type S = {
59
83
  tbl1: {
60
- dataTypes: {type: string;};
61
84
  columns: {
62
- col1: { type: number | null;}
63
- col2: { type: string; }
85
+ col1: number | null;
86
+ col2: string;
64
87
  }
65
88
  },
66
89
  tbl2: {
67
- dataTypes: {type: string;};
68
90
  columns: {
69
- col1: { type: number | null;}
70
- col2: { type: string; }
91
+ col1: number | null;
92
+ col2: string;
71
93
  }
72
94
  }
73
95
  }
@@ -78,9 +100,21 @@ const test: DBSchema = c;
78
100
  const db: DBOFullyTyped<S> = 1 as any;
79
101
 
80
102
 
81
- export type PublishFullyTyped<Schema extends DBSchema | undefined = undefined> = Schema extends DBSchema? {
82
- [tov_name in keyof Partial<Schema>]: PublishAllOrNothing | (Schema[tov_name]["is_view"] extends true? PublishViewRule<Schema[tov_name]> : PublishTableRule<Schema[tov_name]>);
83
- } : (PublishAllOrNothing | Record<string, PublishViewRule | PublishTableRule>);
103
+ export type PublishFullyTyped<Schema = void> = Schema extends DBSchema? (
104
+ | PublishAllOrNothing
105
+ | {
106
+ [tov_name in keyof Partial<Schema>]:
107
+ | PublishAllOrNothing
108
+ | (
109
+ Schema[tov_name]["is_view"] extends true?
110
+ PublishViewRule<Schema[tov_name]["columns"]> :
111
+ PublishTableRule<Schema[tov_name]["columns"]>
112
+ );
113
+ }
114
+ ) : (
115
+ | PublishAllOrNothing
116
+ | Record<string, PublishViewRule | PublishTableRule | PublishAllOrNothing>
117
+ );
84
118
 
85
119
 
86
120
  const publish = (): PublishFullyTyped<S> => {
@@ -97,7 +131,10 @@ const publish = (): PublishFullyTyped<S> => {
97
131
  }
98
132
  },
99
133
  tbl2: {
100
- delete: {forcedFilter: {col1: 2}}
134
+ delete: {
135
+ filterFields: "*" as "*",
136
+ forcedFilter: {col1: 2}
137
+ }
101
138
  }
102
139
  }
103
140
  const res: PublishFullyTyped<S> = {
@@ -109,16 +146,21 @@ const publish = (): PublishFullyTyped<S> => {
109
146
  getColumns: true,
110
147
  getInfo: true,
111
148
  delete: {
112
- filterFields: {col1: 1}
149
+ filterFields: { col1: 1 }
113
150
  }
114
151
  },
115
152
  tbl2: {
116
- delete: {forcedFilter: {col1: 2}}
153
+ delete: {
154
+ filterFields: "*" as "*",
155
+ forcedFilter: { col1: 2 }
156
+ }
117
157
  }
118
158
  }
119
159
  const res1: PublishFullyTyped = r
120
160
 
121
- // const res2: PublishFullyTyped = res;
161
+ const p: PublishParams<undefined> = 1 as any;
162
+
163
+ p.dbo.dwadaw.find!();
122
164
 
123
165
  return res;
124
166
  }