This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.d.ts
303 lines (247 loc) · 7.79 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* TypeScript definitions for @augu/maru
*
* Author: Chris "August" Hernandez <august@augu.dev>
* Project: https://github.com/auguwu/Maru
* License: MIT
*/
declare module '@augu/maru' {
import { PoolClient, Pool } from 'pg';
import { Collection } from '@augu/immutable';
namespace Maru {
/**
* Returns the version of Maru
*/
export const version: string;
/** All of the avaliable pipelines */
export namespace pipelines {
interface InsertOptions<T> {
/** The values */
values: { [P in keyof T]?: T[P] }
/** The table */
table: string;
}
interface UpdateOptions<V> {
/** Return any specific values when it's updated */
returning?: string[];
/** The values to insert */
values: { [P in keyof V]?: V[P]; }
/** The query (to find it) */
query?: [string, unknown];
/** The table */
table: string;
/** The type */
type: 'set';
}
type SupportedTypes = 'string' | 'float' | 'number' | 'boolean' | 'array' | 'bigint';
type TableSchema<T> = {
[P in keyof T]?: SupportedTypes | Maru.pipelines.CreateSchemaOptions;
};
interface CreateSchemaOptions {
/** If the value is nullable */
nullable?: boolean;
/** If the value is a primary key */
primary?: boolean;
/** If this valie is an Array or not */
array?: boolean;
/** Allocate a custom size (only used in Arrays and Strings) */
size?: number;
/** The type */
type: 'string' | 'float' | 'number' | 'boolean' | 'array' | 'bigint' | 'object';
}
interface CreateTableOptions<V> {
/**
* If we should create the table if it doesn't exist
* @default true
*/
exists?: boolean;
/** The values to add */
schema: TableSchema<V>;
}
/**
* Pipeline to create a database
* @param db The database name
*/
export function CreateDatabase(db: string): Maru.Pipeline;
/**
* Pipeline to drop a database
* @param db The database name
*/
export function DropDatabase(db: string): Maru.Pipeline;
/**
* Pipeline to check if a database exist
* @param db The database name
*/
export function DatabaseExists(db: string): Maru.Pipeline;
/**
* Pipeline to create a table
* @param table The table name
* @param values Any values to add
* @param exists If we should apply `IF NOT EXISTS`
*/
// eslint-disable-next-line
export function CreateTable<T extends object>(table: string, options: CreateTableOptions<T>): Maru.Pipeline;
/**
* Pipeline to drop the table
* @param table The table name
* @param exists If we should apply `IF EXISTS`
*/
export function DropTable(table: string, exists?: boolean): Maru.Pipeline;
/**
* Pipeline to check if a table exists
* @param table The table name
*/
export function TableExists(table: string): Maru.Pipeline;
/**
* Counts the amount of documents
* @param table The table to find
* @param amount The amount (defaults to `-1`, which counts all of them)
*/
export function Count(table: string, amount?: number): Maru.Pipeline;
/**
* Pipeline to delete an entry
* @param table The table name
* @param column The column to find
*/
export function Delete(table: string, column: [string, unknown]): Maru.Pipeline;
/**
* Pipeline to insert an entry
* @param options Additional options
* @typeparam T: The insert-one object
*/
// eslint-disable-next-line
export function Insert<T extends object>(options: Maru.pipelines.InsertOptions<T>): Maru.Pipeline;
/**
* Pipeline to get an entry
* @param table The table name
* @param column The column to find
*/
export function Select(table: string, column: [string, unknown]): Maru.Pipeline;
/**
* Pipeline to get all entries
* @param table The table name
* @param column The column to find
*/
export function SelectAll(table: string): Maru.Pipeline;
/**
* Pipeline to update an entry
* @param options Additional options
* @typeparam T: The update object
*/
// eslint-disable-next-line
export function Update<T extends object>(options: Maru.pipelines.UpdateOptions<T>): Maru.Pipeline;
}
/**
* A pipeline is a line of SQL code that will be executed
*/
export interface Pipeline {
/**
* Gets the SQL string
*/
getSql(): string;
/** The ID of the pipe */
id: string;
}
/**
* The core of actually connecting to the database
*/
class Connection {
/** Check if the connection exists */
public connected: boolean;
/** The client */
private client: PoolClient;
/** The connection pool */
private pool: Pool;
/**
* Creates a new Connection
* @param pool The pool instance
*/
constructor(pool: Pool);
/**
* Connects to the database
*/
connect(): Promise<void>;
/**
* Creates a new Batch
*/
createBatch(): Maru.Batch;
/**
* Queries SQL and returns the value or `null` if not found
* @param sql The SQL to execute
*/
query<T>(sql: string | Pipeline, array?: false): Promise<T | null>;
/**
* Queries SQL and returns the values as an array
* @param sql The SQL to execute
*/
query<T>(sql: string | Pipeline, array?: true): Promise<T[] | null>;
}
interface DialectOptions {
/** Number of active connections */
activeConnections?: number;
username: string;
password: string;
database: string;
host: string;
port: number;
}
export class Dialect {
/** Number of active connections */
public activeConnections: number;
/** The connections */
private connections: Collection<Connection>;
/** The pool instance */
private pool: Pool;
/**
* Creates a new Dialect
* @param options The options
*/
constructor(options: DialectOptions);
/**
* Creates a new connection
*/
createConnection(): Maru.Connection;
/**
* Destroys all connections
*/
destroy(): Promise<void>;
}
class Batch {
/** The number of pipes to add until a MAX_PIPE_ERROR occurs */
private numOfPipes: number;
/** The connection */
public connection: Connection;
/** A list of pipelines that were piped using Batch#pipe */
public pipelines: Collection<Pipeline>;
/** If it was executed already */
public executed: boolean;
/**
* Creates a new Batch
* @param connection The connection
*/
constructor(connection: Connection);
/**
* Sets the number of pipes to be executed
* @param a The number of pipes
* @returns This transaction to chain methods
*/
setNumOfPipes(a: number): this;
/**
* Pipes a new Pipeline
* @param line The pipeline
* @returns This transaction to chain methods
*/
pipe(line: Pipeline): this;
/**
* Executes the next pipeline avaliable
* @param array If we should an Array or not
*/
next<T = unknown>(array?: boolean): Promise<T | null>;
/**
* Executes the batch
*/
all<T extends unknown[] = unknown[]>(): Promise<T[]>;
}
}
export = Maru;
}