inibase 1.0.0-rc.8 → 1.0.0-rc.81

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,186 @@
1
+ import "dotenv/config";
2
+ export interface Data {
3
+ id?: number | string;
4
+ [key: string]: any;
5
+ createdAt?: number;
6
+ updatedAt?: number;
7
+ }
8
+ export type FieldType = "string" | "number" | "boolean" | "date" | "email" | "url" | "table" | "object" | "array" | "password" | "html" | "ip" | "json" | "id";
9
+ export type Field = {
10
+ id?: string | number;
11
+ key: string;
12
+ type: FieldType | FieldType[];
13
+ required?: boolean;
14
+ table?: string;
15
+ unique?: boolean;
16
+ children?: FieldType | FieldType[] | Schema;
17
+ };
18
+ export type Schema = Field[];
19
+ export interface Options {
20
+ page?: number;
21
+ perPage?: number;
22
+ columns?: string[] | string;
23
+ sort?: Record<string, 1 | -1 | "asc" | "ASC" | "desc" | "DESC"> | string[] | string;
24
+ }
25
+ export interface Config {
26
+ compression?: boolean;
27
+ cache?: boolean;
28
+ prepend?: boolean;
29
+ }
30
+ export interface TableObject {
31
+ schema?: Schema;
32
+ config: Config;
33
+ }
34
+ export type ComparisonOperator = "=" | "!=" | ">" | "<" | ">=" | "<=" | "*" | "!*" | "[]" | "![]";
35
+ export type pageInfo = {
36
+ total?: number;
37
+ totalPages?: number;
38
+ } & Options;
39
+ export type Criteria = ({
40
+ [logic in "and" | "or"]?: Criteria | (string | number | boolean | null)[];
41
+ } & {
42
+ [key: string]: string | number | boolean | undefined | Criteria;
43
+ }) | null;
44
+ declare global {
45
+ type Entries<T> = {
46
+ [K in keyof T]: [K, T[K]];
47
+ }[keyof T][];
48
+ interface ObjectConstructor {
49
+ entries<T extends object>(o: T): Entries<T>;
50
+ }
51
+ }
52
+ 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";
53
+ export type ErrorLang = "en";
54
+ export default class Inibase {
55
+ pageInfo: Record<string, pageInfo>;
56
+ salt: Buffer;
57
+ private databasePath;
58
+ private tables;
59
+ private fileExtension;
60
+ private checkIFunique;
61
+ private totalItems;
62
+ constructor(database: string, mainFolder?: string);
63
+ private throwError;
64
+ private getFileExtension;
65
+ private _schemaToIdsPath;
66
+ /**
67
+ * Create a new table inside database, with predefined schema and config
68
+ *
69
+ * @param {string} tableName
70
+ * @param {Schema} [schema]
71
+ * @param {Config} [config]
72
+ */
73
+ createTable(tableName: string, schema?: Schema, config?: Config): Promise<void>;
74
+ /**
75
+ * Update table schema or config
76
+ *
77
+ * @param {string} tableName
78
+ * @param {Schema} [schema]
79
+ * @param {Config} [config]
80
+ */
81
+ updateTable(tableName: string, schema?: Schema, config?: Config): Promise<void>;
82
+ /**
83
+ * Get table schema and config
84
+ *
85
+ * @param {string} tableName
86
+ * @return {*} {Promise<TableObject>}
87
+ */
88
+ getTable(tableName: string): Promise<TableObject>;
89
+ getTableSchema(tableName: string, encodeIDs?: boolean): Promise<Schema | undefined>;
90
+ private throwErrorIfTableEmpty;
91
+ private validateData;
92
+ private formatField;
93
+ private checkUnique;
94
+ private formatData;
95
+ private getDefaultValue;
96
+ private _combineObjectsToArray;
97
+ private _CombineData;
98
+ private joinPathesContents;
99
+ private _getItemsFromSchemaHelper;
100
+ private getItemsFromSchema;
101
+ private applyCriteria;
102
+ private _filterSchemaByColumns;
103
+ /**
104
+ * Clear table cache
105
+ *
106
+ * @param {string} tableName
107
+ */
108
+ clearCache(tableName: string): Promise<void>;
109
+ /**
110
+ * Retrieve item(s) from a table
111
+ *
112
+ * @param {string} tableName
113
+ * @param {(string | number | (string | number)[] | Criteria | undefined)} [where]
114
+ * @param {(Options | undefined)} [options]
115
+ * @param {boolean} [onlyOne]
116
+ * @param {boolean} [onlyLinesNumbers]
117
+ * @return {*} {(Promise<Data[] | Data | number[] | null>)}
118
+ */
119
+ get(tableName: string, where: string | number | (string | number)[] | Criteria | undefined, options: Options | undefined, onlyOne: true, onlyLinesNumbers?: false): Promise<Data | null>;
120
+ get(tableName: string, where: string | number, options?: Options, onlyOne?: boolean, onlyLinesNumbers?: false): Promise<Data | null>;
121
+ get(tableName: string, where?: string | number | (string | number)[] | Criteria, options?: Options, onlyOne?: boolean, onlyLinesNumbers?: false): Promise<Data[] | null>;
122
+ get(tableName: string, where: string | number | (string | number)[] | Criteria | undefined, options: Options | undefined, onlyOne: boolean | undefined, onlyLinesNumbers: true): 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 {boolean | null} {(Promise<boolean | null>)}
154
+ */
155
+ delete(tableName: string, where?: number | string | (number | string)[] | Criteria, _id?: string | string[]): Promise<boolean | null>;
156
+ /**
157
+ * Generate sum of column(s) in a table
158
+ *
159
+ * @param {string} tableName
160
+ * @param {string} columns
161
+ * @param {(number | string | (number | string)[] | Criteria)} [where]
162
+ * @return {*} {Promise<number | Record<string, number>>}
163
+ */
164
+ sum(tableName: string, columns: string, where?: number | string | (number | string)[] | Criteria): Promise<number>;
165
+ sum(tableName: string, columns: string[], where?: number | string | (number | string)[] | Criteria): Promise<Record<string, number>>;
166
+ /**
167
+ * Generate max of column(s) in a table
168
+ *
169
+ * @param {string} tableName
170
+ * @param {string} columns
171
+ * @param {(number | string | (number | string)[] | Criteria)} [where]
172
+ * @return {*} {Promise<number>}
173
+ */
174
+ max(tableName: string, columns: string, where?: number | string | (number | string)[] | Criteria): Promise<number>;
175
+ max(tableName: string, columns: string[], where?: number | string | (number | string)[] | Criteria): Promise<Record<string, number>>;
176
+ /**
177
+ * Generate min of column(s) in a table
178
+ *
179
+ * @param {string} tableName
180
+ * @param {string} columns
181
+ * @param {(number | string | (number | string)[] | Criteria)} [where]
182
+ * @return {*} {Promise<number>}
183
+ */
184
+ min(tableName: string, columns: string, where?: number | string | (number | string)[] | Criteria): Promise<number>;
185
+ min(tableName: string, columns: string[], where?: number | string | (number | string)[] | Criteria): Promise<Record<string, number>>;
186
+ }