inibase 1.0.0-rc.7 → 1.0.0-rc.70

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.
@@ -0,0 +1,203 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import "dotenv/config";
3
+ export interface Data {
4
+ id?: number | string;
5
+ [key: string]: any;
6
+ createdAt?: number;
7
+ updatedAt?: number;
8
+ }
9
+ export type FieldType = "string" | "number" | "boolean" | "date" | "email" | "url" | "table" | "object" | "array" | "password" | "html" | "ip" | "json" | "id";
10
+ export type Field = {
11
+ id?: string | number;
12
+ key: string;
13
+ type: FieldType | FieldType[];
14
+ required?: boolean;
15
+ table?: string;
16
+ unique?: boolean;
17
+ children?: FieldType | FieldType[] | Schema;
18
+ };
19
+ export type Schema = Field[];
20
+ export interface Options {
21
+ page?: number;
22
+ perPage?: number;
23
+ columns?: string[] | string;
24
+ order?: Record<string, "asc" | "desc">;
25
+ }
26
+ export interface Config {
27
+ compression?: boolean;
28
+ cache?: boolean;
29
+ prepend?: boolean;
30
+ }
31
+ export interface TableObject {
32
+ schema?: Schema;
33
+ config: Config;
34
+ }
35
+ export type ComparisonOperator = "=" | "!=" | ">" | "<" | ">=" | "<=" | "*" | "!*" | "[]" | "![]";
36
+ export type pageInfo = {
37
+ total?: number;
38
+ totalPages?: number;
39
+ } & Options;
40
+ export type Criteria = ({
41
+ [logic in "and" | "or"]?: Criteria | (string | number | boolean | null)[];
42
+ } & {
43
+ [key: string]: string | number | boolean | undefined | Criteria;
44
+ }) | null;
45
+ declare global {
46
+ type Entries<T> = {
47
+ [K in keyof T]: [K, T[K]];
48
+ }[keyof T][];
49
+ interface ObjectConstructor {
50
+ entries<T extends object>(o: T): Entries<T>;
51
+ }
52
+ }
53
+ export type ErrorCodes = "FIELD_UNIQUE" | "FIELD_REQUIRED" | "NO_SCHEMA" | "NO_ITEMS" | "NO_RESULTS" | "INVALID_ID" | "INVALID_TYPE" | "INVALID_PARAMETERS" | "NO_ENV" | "TABLE_EXISTS" | "TABLE_NOT_EXISTS";
54
+ export type ErrorLang = "en";
55
+ export default class Inibase {
56
+ pageInfo: Record<string, pageInfo>;
57
+ salt: Buffer;
58
+ private databasePath;
59
+ private tables;
60
+ private fileExtension;
61
+ private checkIFunique;
62
+ private totalItems;
63
+ constructor(database: string, mainFolder?: string);
64
+ private throwError;
65
+ private getFileExtension;
66
+ private _schemaToIdsPath;
67
+ /**
68
+ * Create a new table inside database, with predefined schema and config
69
+ *
70
+ * @param {string} tableName
71
+ * @param {Schema} [schema]
72
+ * @param {Config} [config]
73
+ */
74
+ createTable(tableName: string, schema?: Schema, config?: Config): Promise<void>;
75
+ /**
76
+ * Update table schema or config
77
+ *
78
+ * @param {string} tableName
79
+ * @param {Schema} [schema]
80
+ * @param {Config} [config]
81
+ */
82
+ updateTable(tableName: string, schema?: Schema, config?: Config): Promise<void>;
83
+ /**
84
+ * Get table schema and config
85
+ *
86
+ * @param {string} tableName
87
+ * @return {*} {Promise<TableObject>}
88
+ */
89
+ getTable(tableName: string): Promise<TableObject>;
90
+ getTableSchema(tableName: string, encodeIDs?: boolean): Promise<Schema | undefined>;
91
+ private throwErrorIfTableEmpty;
92
+ private validateData;
93
+ private formatField;
94
+ private checkUnique;
95
+ private formatData;
96
+ private getDefaultValue;
97
+ private _combineObjectsToArray;
98
+ private _CombineData;
99
+ private joinPathesContents;
100
+ private _getItemsFromSchemaHelper;
101
+ private getItemsFromSchema;
102
+ private applyCriteria;
103
+ private _filterSchemaByColumns;
104
+ /**
105
+ * Clear table cache
106
+ *
107
+ * @param {string} tableName
108
+ */
109
+ clearCache(tableName: string): Promise<void>;
110
+ /**
111
+ * Retrieve item(s) from a table
112
+ *
113
+ * @param {string} tableName
114
+ * @param {(string | number | (string | number)[] | Criteria | undefined)} [where]
115
+ * @param {(Options | undefined)} [options]
116
+ * @param {true} [onlyOne]
117
+ * @param {undefined} [onlyLinesNumbers]
118
+ * @param {boolean} [_skipIdColumn]
119
+ * @return {*} {(Promise<Data[] | Data | number[] | null>)}
120
+ */
121
+ get(tableName: string, where?: string | number | (string | number)[] | Criteria | undefined, options?: Options | undefined, onlyOne?: true, onlyLinesNumbers?: undefined, _skipIdColumn?: boolean): Promise<Data | null>;
122
+ get(tableName: string, where?: string | number | (string | number)[] | Criteria | undefined, options?: Options | undefined, onlyOne?: boolean | undefined, onlyLinesNumbers?: true, _skipIdColumn?: boolean): Promise<number[]>;
123
+ /**
124
+ * Create new item(s) in a table
125
+ *
126
+ * @param {string} tableName
127
+ * @param {(Data | Data[])} data Can be array of objects or a single object
128
+ * @param {Options} [options] Pagination options, useful when the returnPostedData param is true
129
+ * @param {boolean} [returnPostedData] By default function returns void, if you want to get the posted data, set this param to true
130
+ * @return {*} {Promise<Data | Data[] | null | void>}
131
+ */
132
+ post(tableName: string, data: Data | Data[], options?: Options, returnPostedData?: boolean): Promise<void>;
133
+ post(tableName: string, data: Data, options: Options | undefined, returnPostedData: true): Promise<Data | null>;
134
+ post(tableName: string, data: Data[], options: Options | undefined, returnPostedData: true): Promise<Data[] | null>;
135
+ /**
136
+ * Update item(s) in a table
137
+ *
138
+ * @param {string} tableName
139
+ * @param {(Data | Data[])} data
140
+ * @param {(number | string | (number | string)[] | Criteria)} [where]
141
+ * @param {Options} [options]
142
+ * @param {false} [returnUpdatedData]
143
+ * @return {*} {Promise<Data | Data[] | null | undefined | void>}
144
+ */
145
+ put(tableName: string, data: Data | Data[], where?: number | string | (number | string)[] | Criteria, options?: Options, returnUpdatedData?: false): Promise<void>;
146
+ put(tableName: string, data: Data, where: number | string | (number | string)[] | Criteria | undefined, options: Options | undefined, returnUpdatedData: true): Promise<Data | null>;
147
+ put(tableName: string, data: Data[], where: number | string | (number | string)[] | Criteria | undefined, options: Options | undefined, returnUpdatedData: true): Promise<Data[] | null>;
148
+ /**
149
+ * Delete item(s) in a table
150
+ *
151
+ * @param {string} tableName
152
+ * @param {(number | string | (number | string)[] | Criteria)} [where]
153
+ * @return {*} {(Promise<string | number | (string | number)[] | null>)}
154
+ */
155
+ delete(tableName: string, where?: number | string, _id?: string | string[]): Promise<string | null>;
156
+ delete(tableName: string, where?: (number | string)[] | Criteria, _id?: string | string[]): Promise<string[] | null>;
157
+ delete(tableName: string, where?: number, _id?: string | string[]): Promise<number | null>;
158
+ delete(tableName: string, where?: number[], _id?: string | string[]): Promise<number[] | null>;
159
+ /**
160
+ * Generate sum of column(s) in a table
161
+ *
162
+ * @param {string} tableName
163
+ * @param {string} columns
164
+ * @param {(number | string | (number | string)[] | Criteria)} [where]
165
+ * @return {*} {Promise<number | Record<string, number>>}
166
+ */
167
+ sum(tableName: string, columns: string, where?: number | string | (number | string)[] | Criteria): Promise<number>;
168
+ sum(tableName: string, columns: string[], where?: number | string | (number | string)[] | Criteria): Promise<Record<string, number>>;
169
+ /**
170
+ * Generate max of column(s) in a table
171
+ *
172
+ * @param {string} tableName
173
+ * @param {string} columns
174
+ * @param {(number | string | (number | string)[] | Criteria)} [where]
175
+ * @return {*} {Promise<number>}
176
+ */
177
+ max(tableName: string, columns: string, where?: number | string | (number | string)[] | Criteria): Promise<number>;
178
+ max(tableName: string, columns: string[], where?: number | string | (number | string)[] | Criteria): Promise<Record<string, number>>;
179
+ /**
180
+ * Generate min of column(s) in a table
181
+ *
182
+ * @param {string} tableName
183
+ * @param {string} columns
184
+ * @param {(number | string | (number | string)[] | Criteria)} [where]
185
+ * @return {*} {Promise<number>}
186
+ */
187
+ min(tableName: string, columns: string, where?: number | string | (number | string)[] | Criteria): Promise<number>;
188
+ min(tableName: string, columns: string[], where?: number | string | (number | string)[] | Criteria): Promise<Record<string, number>>;
189
+ /**
190
+ * Sort column(s) of a table
191
+ *
192
+ * @param {string} tableName
193
+ * @param {(string
194
+ * | string[]
195
+ * | Record<string, 1 | -1 | "asc" | "ASC" | "desc" | "DESC">)} columns
196
+ * @param {(string | number | (string | number)[] | Criteria)} [where]
197
+ * @param {Options} [options={
198
+ * page: 1,
199
+ * perPage: 15,
200
+ * }]
201
+ */
202
+ sort(tableName: string, columns: string | string[] | Record<string, 1 | -1 | "asc" | "ASC" | "desc" | "DESC">, where?: string | number | (string | number)[] | Criteria, options?: Options): Promise<any[]>;
203
+ }