@sonicjs-cms/core 2.0.0-beta.3 → 2.0.1
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/dist/{chunk-4MZPFGLT.cjs → chunk-5FDDDD4J.cjs} +4385 -352
- package/dist/chunk-5FDDDD4J.cjs.map +1 -0
- package/dist/chunk-5XTB4FE5.js +1030 -0
- package/dist/chunk-5XTB4FE5.js.map +1 -0
- package/dist/{chunk-ET5I4GBD.cjs → chunk-ALOS2CBJ.cjs} +194 -4
- package/dist/chunk-ALOS2CBJ.cjs.map +1 -0
- package/dist/{chunk-7N3HK7ZK.js → chunk-CDBVZEWR.js} +7 -904
- package/dist/chunk-CDBVZEWR.js.map +1 -0
- package/dist/chunk-EGFHFM4N.cjs +76 -0
- package/dist/chunk-EGFHFM4N.cjs.map +1 -0
- package/dist/chunk-KM4AJFXI.cjs +101 -0
- package/dist/chunk-KM4AJFXI.cjs.map +1 -0
- package/dist/{chunk-RNR4HA23.cjs → chunk-LEG4KNFP.cjs} +6 -945
- package/dist/chunk-LEG4KNFP.cjs.map +1 -0
- package/dist/{chunk-P3VS4DV3.js → chunk-O46XKBFM.js} +193 -5
- package/dist/chunk-O46XKBFM.js.map +1 -0
- package/dist/chunk-P2PTTBO5.js +74 -0
- package/dist/chunk-P2PTTBO5.js.map +1 -0
- package/dist/{chunk-Q3KCKPHE.js → chunk-QSF34IYQ.js} +4244 -214
- package/dist/chunk-QSF34IYQ.js.map +1 -0
- package/dist/chunk-SRCY43RN.cjs +1076 -0
- package/dist/chunk-SRCY43RN.cjs.map +1 -0
- package/dist/chunk-TY3NHEBN.js +80 -0
- package/dist/chunk-TY3NHEBN.js.map +1 -0
- package/dist/index.cjs +196 -196
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +9 -9
- package/dist/index.js.map +1 -1
- package/dist/middleware.cjs +22 -22
- package/dist/middleware.js +2 -2
- package/dist/routes.cjs +34 -22
- package/dist/routes.js +5 -5
- package/dist/services.cjs +28 -28
- package/dist/services.js +2 -2
- package/dist/templates.cjs +24 -24
- package/dist/templates.js +2 -2
- package/package.json +2 -16
- package/dist/chunk-3MNMOLSA.js +0 -133
- package/dist/chunk-3MNMOLSA.js.map +0 -1
- package/dist/chunk-4MZPFGLT.cjs.map +0 -1
- package/dist/chunk-4XI3YBKU.cjs +0 -266
- package/dist/chunk-4XI3YBKU.cjs.map +0 -1
- package/dist/chunk-7N3HK7ZK.js.map +0 -1
- package/dist/chunk-AGOE25LF.cjs +0 -137
- package/dist/chunk-AGOE25LF.cjs.map +0 -1
- package/dist/chunk-BUKT6HP5.cjs +0 -776
- package/dist/chunk-BUKT6HP5.cjs.map +0 -1
- package/dist/chunk-ET5I4GBD.cjs.map +0 -1
- package/dist/chunk-LU6J53IX.js +0 -262
- package/dist/chunk-LU6J53IX.js.map +0 -1
- package/dist/chunk-P3VS4DV3.js.map +0 -1
- package/dist/chunk-Q3KCKPHE.js.map +0 -1
- package/dist/chunk-RNR4HA23.cjs.map +0 -1
- package/dist/chunk-WESS2U3K.js +0 -755
- package/dist/chunk-WESS2U3K.js.map +0 -1
|
@@ -0,0 +1,1030 @@
|
|
|
1
|
+
import { __export } from './chunk-V4OQ3NZ2.js';
|
|
2
|
+
import { sqliteTable, integer, text } from 'drizzle-orm/sqlite-core';
|
|
3
|
+
import { z } from 'zod/v4';
|
|
4
|
+
import { getTableColumns, getViewSelectedFields, isTable, is, Column, SQL, isView, inArray, eq, like, gte, lte, and, count, asc, desc } from 'drizzle-orm';
|
|
5
|
+
import { drizzle } from 'drizzle-orm/d1';
|
|
6
|
+
|
|
7
|
+
// src/db/schema.ts
|
|
8
|
+
var schema_exports = {};
|
|
9
|
+
__export(schema_exports, {
|
|
10
|
+
apiTokens: () => apiTokens,
|
|
11
|
+
collections: () => collections,
|
|
12
|
+
content: () => content,
|
|
13
|
+
contentVersions: () => contentVersions,
|
|
14
|
+
insertCollectionSchema: () => insertCollectionSchema,
|
|
15
|
+
insertContentSchema: () => insertContentSchema,
|
|
16
|
+
insertLogConfigSchema: () => insertLogConfigSchema,
|
|
17
|
+
insertMediaSchema: () => insertMediaSchema,
|
|
18
|
+
insertPluginActivityLogSchema: () => insertPluginActivityLogSchema,
|
|
19
|
+
insertPluginAssetSchema: () => insertPluginAssetSchema,
|
|
20
|
+
insertPluginHookSchema: () => insertPluginHookSchema,
|
|
21
|
+
insertPluginRouteSchema: () => insertPluginRouteSchema,
|
|
22
|
+
insertPluginSchema: () => insertPluginSchema,
|
|
23
|
+
insertSystemLogSchema: () => insertSystemLogSchema,
|
|
24
|
+
insertUserSchema: () => insertUserSchema,
|
|
25
|
+
insertWorkflowHistorySchema: () => insertWorkflowHistorySchema,
|
|
26
|
+
logConfig: () => logConfig,
|
|
27
|
+
media: () => media,
|
|
28
|
+
pluginActivityLog: () => pluginActivityLog,
|
|
29
|
+
pluginAssets: () => pluginAssets,
|
|
30
|
+
pluginHooks: () => pluginHooks,
|
|
31
|
+
pluginRoutes: () => pluginRoutes,
|
|
32
|
+
plugins: () => plugins,
|
|
33
|
+
selectCollectionSchema: () => selectCollectionSchema,
|
|
34
|
+
selectContentSchema: () => selectContentSchema,
|
|
35
|
+
selectLogConfigSchema: () => selectLogConfigSchema,
|
|
36
|
+
selectMediaSchema: () => selectMediaSchema,
|
|
37
|
+
selectPluginActivityLogSchema: () => selectPluginActivityLogSchema,
|
|
38
|
+
selectPluginAssetSchema: () => selectPluginAssetSchema,
|
|
39
|
+
selectPluginHookSchema: () => selectPluginHookSchema,
|
|
40
|
+
selectPluginRouteSchema: () => selectPluginRouteSchema,
|
|
41
|
+
selectPluginSchema: () => selectPluginSchema,
|
|
42
|
+
selectSystemLogSchema: () => selectSystemLogSchema,
|
|
43
|
+
selectUserSchema: () => selectUserSchema,
|
|
44
|
+
selectWorkflowHistorySchema: () => selectWorkflowHistorySchema,
|
|
45
|
+
systemLogs: () => systemLogs,
|
|
46
|
+
users: () => users,
|
|
47
|
+
workflowHistory: () => workflowHistory
|
|
48
|
+
});
|
|
49
|
+
var CONSTANTS = {
|
|
50
|
+
INT8_MIN: -128,
|
|
51
|
+
INT8_MAX: 127,
|
|
52
|
+
INT8_UNSIGNED_MAX: 255,
|
|
53
|
+
INT16_MIN: -32768,
|
|
54
|
+
INT16_MAX: 32767,
|
|
55
|
+
INT16_UNSIGNED_MAX: 65535,
|
|
56
|
+
INT24_MIN: -8388608,
|
|
57
|
+
INT24_MAX: 8388607,
|
|
58
|
+
INT24_UNSIGNED_MAX: 16777215,
|
|
59
|
+
INT32_MIN: -2147483648,
|
|
60
|
+
INT32_MAX: 2147483647,
|
|
61
|
+
INT32_UNSIGNED_MAX: 4294967295,
|
|
62
|
+
INT48_MIN: -140737488355328,
|
|
63
|
+
INT48_MAX: 140737488355327,
|
|
64
|
+
INT48_UNSIGNED_MAX: 281474976710655,
|
|
65
|
+
INT64_MIN: -9223372036854775808n,
|
|
66
|
+
INT64_MAX: 9223372036854775807n,
|
|
67
|
+
INT64_UNSIGNED_MAX: 18446744073709551615n
|
|
68
|
+
};
|
|
69
|
+
function isColumnType(column, columnTypes) {
|
|
70
|
+
return columnTypes.includes(column.columnType);
|
|
71
|
+
}
|
|
72
|
+
function isWithEnum(column) {
|
|
73
|
+
return "enumValues" in column && Array.isArray(column.enumValues) && column.enumValues.length > 0;
|
|
74
|
+
}
|
|
75
|
+
var isPgEnum = isWithEnum;
|
|
76
|
+
var literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]);
|
|
77
|
+
var jsonSchema = z.union([
|
|
78
|
+
literalSchema,
|
|
79
|
+
z.record(z.string(), z.any()),
|
|
80
|
+
z.array(z.any())
|
|
81
|
+
]);
|
|
82
|
+
var bufferSchema = z.custom((v) => v instanceof Buffer);
|
|
83
|
+
function columnToSchema(column, factory) {
|
|
84
|
+
const z$1 = z;
|
|
85
|
+
const coerce = {};
|
|
86
|
+
let schema;
|
|
87
|
+
if (isWithEnum(column)) {
|
|
88
|
+
schema = column.enumValues.length ? z$1.enum(column.enumValues) : z$1.string();
|
|
89
|
+
}
|
|
90
|
+
if (!schema) {
|
|
91
|
+
if (isColumnType(column, ["PgGeometry", "PgPointTuple"])) {
|
|
92
|
+
schema = z$1.tuple([z$1.number(), z$1.number()]);
|
|
93
|
+
} else if (isColumnType(column, ["PgGeometryObject", "PgPointObject"])) {
|
|
94
|
+
schema = z$1.object({ x: z$1.number(), y: z$1.number() });
|
|
95
|
+
} else if (isColumnType(column, ["PgHalfVector", "PgVector"])) {
|
|
96
|
+
schema = z$1.array(z$1.number());
|
|
97
|
+
schema = column.dimensions ? schema.length(column.dimensions) : schema;
|
|
98
|
+
} else if (isColumnType(column, ["PgLine"])) {
|
|
99
|
+
schema = z$1.tuple([z$1.number(), z$1.number(), z$1.number()]);
|
|
100
|
+
} else if (isColumnType(column, ["PgLineABC"])) {
|
|
101
|
+
schema = z$1.object({
|
|
102
|
+
a: z$1.number(),
|
|
103
|
+
b: z$1.number(),
|
|
104
|
+
c: z$1.number()
|
|
105
|
+
});
|
|
106
|
+
} else if (isColumnType(column, ["PgArray"])) {
|
|
107
|
+
schema = z$1.array(columnToSchema(column.baseColumn));
|
|
108
|
+
schema = column.size ? schema.length(column.size) : schema;
|
|
109
|
+
} else if (column.dataType === "array") {
|
|
110
|
+
schema = z$1.array(z$1.any());
|
|
111
|
+
} else if (column.dataType === "number") {
|
|
112
|
+
schema = numberColumnToSchema(column, z$1, coerce);
|
|
113
|
+
} else if (column.dataType === "bigint") {
|
|
114
|
+
schema = bigintColumnToSchema(column, z$1, coerce);
|
|
115
|
+
} else if (column.dataType === "boolean") {
|
|
116
|
+
schema = coerce === true || coerce.boolean ? z$1.coerce.boolean() : z$1.boolean();
|
|
117
|
+
} else if (column.dataType === "date") {
|
|
118
|
+
schema = coerce === true || coerce.date ? z$1.coerce.date() : z$1.date();
|
|
119
|
+
} else if (column.dataType === "string") {
|
|
120
|
+
schema = stringColumnToSchema(column, z$1, coerce);
|
|
121
|
+
} else if (column.dataType === "json") {
|
|
122
|
+
schema = jsonSchema;
|
|
123
|
+
} else if (column.dataType === "custom") {
|
|
124
|
+
schema = z$1.any();
|
|
125
|
+
} else if (column.dataType === "buffer") {
|
|
126
|
+
schema = bufferSchema;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (!schema) {
|
|
130
|
+
schema = z$1.any();
|
|
131
|
+
}
|
|
132
|
+
return schema;
|
|
133
|
+
}
|
|
134
|
+
function numberColumnToSchema(column, z2, coerce) {
|
|
135
|
+
let unsigned = column.getSQLType().includes("unsigned");
|
|
136
|
+
let min;
|
|
137
|
+
let max;
|
|
138
|
+
let integer2 = false;
|
|
139
|
+
if (isColumnType(column, ["MySqlTinyInt", "SingleStoreTinyInt"])) {
|
|
140
|
+
min = unsigned ? 0 : CONSTANTS.INT8_MIN;
|
|
141
|
+
max = unsigned ? CONSTANTS.INT8_UNSIGNED_MAX : CONSTANTS.INT8_MAX;
|
|
142
|
+
integer2 = true;
|
|
143
|
+
} else if (isColumnType(column, [
|
|
144
|
+
"PgSmallInt",
|
|
145
|
+
"PgSmallSerial",
|
|
146
|
+
"MySqlSmallInt",
|
|
147
|
+
"SingleStoreSmallInt"
|
|
148
|
+
])) {
|
|
149
|
+
min = unsigned ? 0 : CONSTANTS.INT16_MIN;
|
|
150
|
+
max = unsigned ? CONSTANTS.INT16_UNSIGNED_MAX : CONSTANTS.INT16_MAX;
|
|
151
|
+
integer2 = true;
|
|
152
|
+
} else if (isColumnType(column, [
|
|
153
|
+
"PgReal",
|
|
154
|
+
"MySqlFloat",
|
|
155
|
+
"MySqlMediumInt",
|
|
156
|
+
"SingleStoreMediumInt",
|
|
157
|
+
"SingleStoreFloat"
|
|
158
|
+
])) {
|
|
159
|
+
min = unsigned ? 0 : CONSTANTS.INT24_MIN;
|
|
160
|
+
max = unsigned ? CONSTANTS.INT24_UNSIGNED_MAX : CONSTANTS.INT24_MAX;
|
|
161
|
+
integer2 = isColumnType(column, ["MySqlMediumInt", "SingleStoreMediumInt"]);
|
|
162
|
+
} else if (isColumnType(column, [
|
|
163
|
+
"PgInteger",
|
|
164
|
+
"PgSerial",
|
|
165
|
+
"MySqlInt",
|
|
166
|
+
"SingleStoreInt"
|
|
167
|
+
])) {
|
|
168
|
+
min = unsigned ? 0 : CONSTANTS.INT32_MIN;
|
|
169
|
+
max = unsigned ? CONSTANTS.INT32_UNSIGNED_MAX : CONSTANTS.INT32_MAX;
|
|
170
|
+
integer2 = true;
|
|
171
|
+
} else if (isColumnType(column, [
|
|
172
|
+
"PgDoublePrecision",
|
|
173
|
+
"MySqlReal",
|
|
174
|
+
"MySqlDouble",
|
|
175
|
+
"SingleStoreReal",
|
|
176
|
+
"SingleStoreDouble",
|
|
177
|
+
"SQLiteReal"
|
|
178
|
+
])) {
|
|
179
|
+
min = unsigned ? 0 : CONSTANTS.INT48_MIN;
|
|
180
|
+
max = unsigned ? CONSTANTS.INT48_UNSIGNED_MAX : CONSTANTS.INT48_MAX;
|
|
181
|
+
} else if (isColumnType(column, [
|
|
182
|
+
"PgBigInt53",
|
|
183
|
+
"PgBigSerial53",
|
|
184
|
+
"MySqlBigInt53",
|
|
185
|
+
"MySqlSerial",
|
|
186
|
+
"SingleStoreBigInt53",
|
|
187
|
+
"SingleStoreSerial",
|
|
188
|
+
"SQLiteInteger"
|
|
189
|
+
])) {
|
|
190
|
+
unsigned = unsigned || isColumnType(column, ["MySqlSerial", "SingleStoreSerial"]);
|
|
191
|
+
min = unsigned ? 0 : Number.MIN_SAFE_INTEGER;
|
|
192
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
193
|
+
integer2 = true;
|
|
194
|
+
} else if (isColumnType(column, ["MySqlYear", "SingleStoreYear"])) {
|
|
195
|
+
min = 1901;
|
|
196
|
+
max = 2155;
|
|
197
|
+
integer2 = true;
|
|
198
|
+
} else {
|
|
199
|
+
min = Number.MIN_SAFE_INTEGER;
|
|
200
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
201
|
+
}
|
|
202
|
+
let schema = coerce === true || coerce?.number ? integer2 ? z2.coerce.number() : z2.coerce.number().int() : integer2 ? z2.int() : z2.number();
|
|
203
|
+
schema = schema.gte(min).lte(max);
|
|
204
|
+
return schema;
|
|
205
|
+
}
|
|
206
|
+
function bigintColumnToSchema(column, z2, coerce) {
|
|
207
|
+
const unsigned = column.getSQLType().includes("unsigned");
|
|
208
|
+
const min = unsigned ? 0n : CONSTANTS.INT64_MIN;
|
|
209
|
+
const max = unsigned ? CONSTANTS.INT64_UNSIGNED_MAX : CONSTANTS.INT64_MAX;
|
|
210
|
+
const schema = coerce === true || coerce?.bigint ? z2.coerce.bigint() : z2.bigint();
|
|
211
|
+
return schema.gte(min).lte(max);
|
|
212
|
+
}
|
|
213
|
+
function stringColumnToSchema(column, z2, coerce) {
|
|
214
|
+
if (isColumnType(column, ["PgUUID"])) {
|
|
215
|
+
return z2.uuid();
|
|
216
|
+
}
|
|
217
|
+
let max;
|
|
218
|
+
let regex;
|
|
219
|
+
let fixed = false;
|
|
220
|
+
if (isColumnType(column, ["PgVarchar", "SQLiteText"])) {
|
|
221
|
+
max = column.length;
|
|
222
|
+
} else if (isColumnType(column, ["MySqlVarChar", "SingleStoreVarChar"])) {
|
|
223
|
+
max = column.length ?? CONSTANTS.INT16_UNSIGNED_MAX;
|
|
224
|
+
} else if (isColumnType(column, ["MySqlText", "SingleStoreText"])) {
|
|
225
|
+
if (column.textType === "longtext") {
|
|
226
|
+
max = CONSTANTS.INT32_UNSIGNED_MAX;
|
|
227
|
+
} else if (column.textType === "mediumtext") {
|
|
228
|
+
max = CONSTANTS.INT24_UNSIGNED_MAX;
|
|
229
|
+
} else if (column.textType === "text") {
|
|
230
|
+
max = CONSTANTS.INT16_UNSIGNED_MAX;
|
|
231
|
+
} else {
|
|
232
|
+
max = CONSTANTS.INT8_UNSIGNED_MAX;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (isColumnType(column, [
|
|
236
|
+
"PgChar",
|
|
237
|
+
"MySqlChar",
|
|
238
|
+
"SingleStoreChar"
|
|
239
|
+
])) {
|
|
240
|
+
max = column.length;
|
|
241
|
+
fixed = true;
|
|
242
|
+
}
|
|
243
|
+
if (isColumnType(column, ["PgBinaryVector"])) {
|
|
244
|
+
regex = /^[01]+$/;
|
|
245
|
+
max = column.dimensions;
|
|
246
|
+
}
|
|
247
|
+
let schema = coerce === true || coerce?.string ? z2.coerce.string() : z2.string();
|
|
248
|
+
schema = regex ? schema.regex(regex) : schema;
|
|
249
|
+
return max && fixed ? schema.length(max) : max ? schema.max(max) : schema;
|
|
250
|
+
}
|
|
251
|
+
function getColumns(tableLike) {
|
|
252
|
+
return isTable(tableLike) ? getTableColumns(tableLike) : getViewSelectedFields(tableLike);
|
|
253
|
+
}
|
|
254
|
+
function handleColumns(columns, refinements, conditions, factory) {
|
|
255
|
+
const columnSchemas = {};
|
|
256
|
+
for (const [key, selected] of Object.entries(columns)) {
|
|
257
|
+
if (!is(selected, Column) && !is(selected, SQL) && !is(selected, SQL.Aliased) && typeof selected === "object") {
|
|
258
|
+
const columns2 = isTable(selected) || isView(selected) ? getColumns(selected) : selected;
|
|
259
|
+
columnSchemas[key] = handleColumns(columns2, refinements[key] ?? {}, conditions);
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
const refinement = refinements[key];
|
|
263
|
+
if (refinement !== void 0 && typeof refinement !== "function") {
|
|
264
|
+
columnSchemas[key] = refinement;
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
const column = is(selected, Column) ? selected : void 0;
|
|
268
|
+
const schema = column ? columnToSchema(column) : z.any();
|
|
269
|
+
const refined = typeof refinement === "function" ? refinement(schema) : schema;
|
|
270
|
+
if (conditions.never(column)) {
|
|
271
|
+
continue;
|
|
272
|
+
} else {
|
|
273
|
+
columnSchemas[key] = refined;
|
|
274
|
+
}
|
|
275
|
+
if (column) {
|
|
276
|
+
if (conditions.nullable(column)) {
|
|
277
|
+
columnSchemas[key] = columnSchemas[key].nullable();
|
|
278
|
+
}
|
|
279
|
+
if (conditions.optional(column)) {
|
|
280
|
+
columnSchemas[key] = columnSchemas[key].optional();
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return z.object(columnSchemas);
|
|
285
|
+
}
|
|
286
|
+
function handleEnum(enum_, factory) {
|
|
287
|
+
const zod = z;
|
|
288
|
+
return zod.enum(enum_.enumValues);
|
|
289
|
+
}
|
|
290
|
+
var selectConditions = {
|
|
291
|
+
never: () => false,
|
|
292
|
+
optional: () => false,
|
|
293
|
+
nullable: (column) => !column.notNull
|
|
294
|
+
};
|
|
295
|
+
var insertConditions = {
|
|
296
|
+
never: (column) => column?.generated?.type === "always" || column?.generatedIdentity?.type === "always",
|
|
297
|
+
optional: (column) => !column.notNull || column.notNull && column.hasDefault,
|
|
298
|
+
nullable: (column) => !column.notNull
|
|
299
|
+
};
|
|
300
|
+
var createSelectSchema = (entity, refine) => {
|
|
301
|
+
if (isPgEnum(entity)) {
|
|
302
|
+
return handleEnum(entity);
|
|
303
|
+
}
|
|
304
|
+
const columns = getColumns(entity);
|
|
305
|
+
return handleColumns(columns, {}, selectConditions);
|
|
306
|
+
};
|
|
307
|
+
var createInsertSchema = (entity, refine) => {
|
|
308
|
+
const columns = getColumns(entity);
|
|
309
|
+
return handleColumns(columns, refine ?? {}, insertConditions);
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
// src/db/schema.ts
|
|
313
|
+
var users = sqliteTable("users", {
|
|
314
|
+
id: text("id").primaryKey(),
|
|
315
|
+
email: text("email").notNull().unique(),
|
|
316
|
+
username: text("username").notNull().unique(),
|
|
317
|
+
firstName: text("first_name").notNull(),
|
|
318
|
+
lastName: text("last_name").notNull(),
|
|
319
|
+
passwordHash: text("password_hash"),
|
|
320
|
+
// Hashed password, nullable for OAuth users
|
|
321
|
+
role: text("role").notNull().default("viewer"),
|
|
322
|
+
// 'admin', 'editor', 'author', 'viewer'
|
|
323
|
+
avatar: text("avatar"),
|
|
324
|
+
isActive: integer("is_active", { mode: "boolean" }).notNull().default(true),
|
|
325
|
+
lastLoginAt: integer("last_login_at"),
|
|
326
|
+
createdAt: integer("created_at").notNull(),
|
|
327
|
+
updatedAt: integer("updated_at").notNull()
|
|
328
|
+
});
|
|
329
|
+
var collections = sqliteTable("collections", {
|
|
330
|
+
id: text("id").primaryKey(),
|
|
331
|
+
name: text("name").notNull().unique(),
|
|
332
|
+
displayName: text("display_name").notNull(),
|
|
333
|
+
description: text("description"),
|
|
334
|
+
schema: text("schema", { mode: "json" }).notNull(),
|
|
335
|
+
// JSON schema definition
|
|
336
|
+
isActive: integer("is_active", { mode: "boolean" }).notNull().default(true),
|
|
337
|
+
managed: integer("managed", { mode: "boolean" }).notNull().default(false),
|
|
338
|
+
// Config-managed collections cannot be edited in UI
|
|
339
|
+
createdAt: integer("created_at", { mode: "timestamp" }).notNull().$defaultFn(() => /* @__PURE__ */ new Date()),
|
|
340
|
+
updatedAt: integer("updated_at", { mode: "timestamp" }).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
|
|
341
|
+
});
|
|
342
|
+
var content = sqliteTable("content", {
|
|
343
|
+
id: text("id").primaryKey(),
|
|
344
|
+
collectionId: text("collection_id").notNull().references(() => collections.id),
|
|
345
|
+
slug: text("slug").notNull(),
|
|
346
|
+
title: text("title").notNull(),
|
|
347
|
+
data: text("data", { mode: "json" }).notNull(),
|
|
348
|
+
// JSON content data
|
|
349
|
+
status: text("status").notNull().default("draft"),
|
|
350
|
+
// 'draft', 'published', 'archived'
|
|
351
|
+
publishedAt: integer("published_at", { mode: "timestamp" }),
|
|
352
|
+
authorId: text("author_id").notNull().references(() => users.id),
|
|
353
|
+
createdAt: integer("created_at", { mode: "timestamp" }).notNull().$defaultFn(() => /* @__PURE__ */ new Date()),
|
|
354
|
+
updatedAt: integer("updated_at", { mode: "timestamp" }).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
|
|
355
|
+
});
|
|
356
|
+
var contentVersions = sqliteTable("content_versions", {
|
|
357
|
+
id: text("id").primaryKey(),
|
|
358
|
+
contentId: text("content_id").notNull().references(() => content.id),
|
|
359
|
+
version: integer("version").notNull(),
|
|
360
|
+
data: text("data", { mode: "json" }).notNull(),
|
|
361
|
+
authorId: text("author_id").notNull().references(() => users.id),
|
|
362
|
+
createdAt: integer("created_at", { mode: "timestamp" }).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
|
|
363
|
+
});
|
|
364
|
+
var media = sqliteTable("media", {
|
|
365
|
+
id: text("id").primaryKey(),
|
|
366
|
+
filename: text("filename").notNull(),
|
|
367
|
+
originalName: text("original_name").notNull(),
|
|
368
|
+
mimeType: text("mime_type").notNull(),
|
|
369
|
+
size: integer("size").notNull(),
|
|
370
|
+
width: integer("width"),
|
|
371
|
+
height: integer("height"),
|
|
372
|
+
folder: text("folder").notNull().default("uploads"),
|
|
373
|
+
r2Key: text("r2_key").notNull(),
|
|
374
|
+
// R2 storage key
|
|
375
|
+
publicUrl: text("public_url").notNull(),
|
|
376
|
+
// CDN URL
|
|
377
|
+
thumbnailUrl: text("thumbnail_url"),
|
|
378
|
+
alt: text("alt"),
|
|
379
|
+
caption: text("caption"),
|
|
380
|
+
tags: text("tags", { mode: "json" }),
|
|
381
|
+
// JSON array of tags
|
|
382
|
+
uploadedBy: text("uploaded_by").notNull().references(() => users.id),
|
|
383
|
+
uploadedAt: integer("uploaded_at").notNull(),
|
|
384
|
+
updatedAt: integer("updated_at"),
|
|
385
|
+
publishedAt: integer("published_at"),
|
|
386
|
+
scheduledAt: integer("scheduled_at"),
|
|
387
|
+
archivedAt: integer("archived_at"),
|
|
388
|
+
deletedAt: integer("deleted_at")
|
|
389
|
+
});
|
|
390
|
+
var apiTokens = sqliteTable("api_tokens", {
|
|
391
|
+
id: text("id").primaryKey(),
|
|
392
|
+
name: text("name").notNull(),
|
|
393
|
+
token: text("token").notNull().unique(),
|
|
394
|
+
userId: text("user_id").notNull().references(() => users.id),
|
|
395
|
+
permissions: text("permissions", { mode: "json" }).notNull(),
|
|
396
|
+
// Array of permissions
|
|
397
|
+
expiresAt: integer("expires_at", { mode: "timestamp" }),
|
|
398
|
+
lastUsedAt: integer("last_used_at", { mode: "timestamp" }),
|
|
399
|
+
createdAt: integer("created_at", { mode: "timestamp" }).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
|
|
400
|
+
});
|
|
401
|
+
var workflowHistory = sqliteTable("workflow_history", {
|
|
402
|
+
id: text("id").primaryKey(),
|
|
403
|
+
contentId: text("content_id").notNull().references(() => content.id),
|
|
404
|
+
action: text("action").notNull(),
|
|
405
|
+
fromStatus: text("from_status").notNull(),
|
|
406
|
+
toStatus: text("to_status").notNull(),
|
|
407
|
+
userId: text("user_id").notNull().references(() => users.id),
|
|
408
|
+
comment: text("comment"),
|
|
409
|
+
createdAt: integer("created_at", { mode: "timestamp" }).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
|
|
410
|
+
});
|
|
411
|
+
var plugins = sqliteTable("plugins", {
|
|
412
|
+
id: text("id").primaryKey(),
|
|
413
|
+
name: text("name").notNull().unique(),
|
|
414
|
+
displayName: text("display_name").notNull(),
|
|
415
|
+
description: text("description"),
|
|
416
|
+
version: text("version").notNull(),
|
|
417
|
+
author: text("author").notNull(),
|
|
418
|
+
category: text("category").notNull(),
|
|
419
|
+
icon: text("icon"),
|
|
420
|
+
status: text("status").notNull().default("inactive"),
|
|
421
|
+
// 'active', 'inactive', 'error'
|
|
422
|
+
isCore: integer("is_core", { mode: "boolean" }).notNull().default(false),
|
|
423
|
+
settings: text("settings", { mode: "json" }),
|
|
424
|
+
permissions: text("permissions", { mode: "json" }),
|
|
425
|
+
dependencies: text("dependencies", { mode: "json" }),
|
|
426
|
+
downloadCount: integer("download_count").notNull().default(0),
|
|
427
|
+
rating: integer("rating").notNull().default(0),
|
|
428
|
+
installedAt: integer("installed_at").notNull(),
|
|
429
|
+
activatedAt: integer("activated_at"),
|
|
430
|
+
lastUpdated: integer("last_updated").notNull(),
|
|
431
|
+
errorMessage: text("error_message"),
|
|
432
|
+
createdAt: integer("created_at").notNull().$defaultFn(() => Math.floor(Date.now() / 1e3)),
|
|
433
|
+
updatedAt: integer("updated_at").notNull().$defaultFn(() => Math.floor(Date.now() / 1e3))
|
|
434
|
+
});
|
|
435
|
+
var pluginHooks = sqliteTable("plugin_hooks", {
|
|
436
|
+
id: text("id").primaryKey(),
|
|
437
|
+
pluginId: text("plugin_id").notNull().references(() => plugins.id),
|
|
438
|
+
hookName: text("hook_name").notNull(),
|
|
439
|
+
handlerName: text("handler_name").notNull(),
|
|
440
|
+
priority: integer("priority").notNull().default(10),
|
|
441
|
+
isActive: integer("is_active", { mode: "boolean" }).notNull().default(true),
|
|
442
|
+
createdAt: integer("created_at").notNull().$defaultFn(() => Math.floor(Date.now() / 1e3))
|
|
443
|
+
});
|
|
444
|
+
var pluginRoutes = sqliteTable("plugin_routes", {
|
|
445
|
+
id: text("id").primaryKey(),
|
|
446
|
+
pluginId: text("plugin_id").notNull().references(() => plugins.id),
|
|
447
|
+
path: text("path").notNull(),
|
|
448
|
+
method: text("method").notNull(),
|
|
449
|
+
handlerName: text("handler_name").notNull(),
|
|
450
|
+
middleware: text("middleware", { mode: "json" }),
|
|
451
|
+
isActive: integer("is_active", { mode: "boolean" }).notNull().default(true),
|
|
452
|
+
createdAt: integer("created_at").notNull().$defaultFn(() => Math.floor(Date.now() / 1e3))
|
|
453
|
+
});
|
|
454
|
+
var pluginAssets = sqliteTable("plugin_assets", {
|
|
455
|
+
id: text("id").primaryKey(),
|
|
456
|
+
pluginId: text("plugin_id").notNull().references(() => plugins.id),
|
|
457
|
+
assetType: text("asset_type").notNull(),
|
|
458
|
+
// 'css', 'js', 'image', 'font'
|
|
459
|
+
assetPath: text("asset_path").notNull(),
|
|
460
|
+
loadOrder: integer("load_order").notNull().default(100),
|
|
461
|
+
loadLocation: text("load_location").notNull().default("footer"),
|
|
462
|
+
// 'header', 'footer'
|
|
463
|
+
isActive: integer("is_active", { mode: "boolean" }).notNull().default(true),
|
|
464
|
+
createdAt: integer("created_at").notNull().$defaultFn(() => Math.floor(Date.now() / 1e3))
|
|
465
|
+
});
|
|
466
|
+
var pluginActivityLog = sqliteTable("plugin_activity_log", {
|
|
467
|
+
id: text("id").primaryKey(),
|
|
468
|
+
pluginId: text("plugin_id").notNull().references(() => plugins.id),
|
|
469
|
+
action: text("action").notNull(),
|
|
470
|
+
userId: text("user_id"),
|
|
471
|
+
details: text("details", { mode: "json" }),
|
|
472
|
+
timestamp: integer("timestamp").notNull().$defaultFn(() => Math.floor(Date.now() / 1e3))
|
|
473
|
+
});
|
|
474
|
+
var insertUserSchema = createInsertSchema(users, {
|
|
475
|
+
email: (schema) => schema.email(),
|
|
476
|
+
firstName: (schema) => schema.min(1),
|
|
477
|
+
lastName: (schema) => schema.min(1),
|
|
478
|
+
username: (schema) => schema.min(3)
|
|
479
|
+
});
|
|
480
|
+
var selectUserSchema = createSelectSchema(users);
|
|
481
|
+
var insertCollectionSchema = createInsertSchema(collections, {
|
|
482
|
+
name: (schema) => schema.min(1).regex(/^[a-z0-9_]+$/, "Collection name must be lowercase with underscores"),
|
|
483
|
+
displayName: (schema) => schema.min(1)
|
|
484
|
+
});
|
|
485
|
+
var selectCollectionSchema = createSelectSchema(collections);
|
|
486
|
+
var insertContentSchema = createInsertSchema(content, {
|
|
487
|
+
slug: (schema) => schema.min(1).regex(/^[a-zA-Z0-9_-]+$/, "Slug must contain only letters, numbers, underscores, and hyphens"),
|
|
488
|
+
title: (schema) => schema.min(1),
|
|
489
|
+
status: (schema) => schema
|
|
490
|
+
});
|
|
491
|
+
var selectContentSchema = createSelectSchema(content);
|
|
492
|
+
var insertMediaSchema = createInsertSchema(media, {
|
|
493
|
+
filename: (schema) => schema.min(1),
|
|
494
|
+
originalName: (schema) => schema.min(1),
|
|
495
|
+
mimeType: (schema) => schema.min(1),
|
|
496
|
+
size: (schema) => schema.positive(),
|
|
497
|
+
r2Key: (schema) => schema.min(1),
|
|
498
|
+
publicUrl: (schema) => schema.url(),
|
|
499
|
+
folder: (schema) => schema.min(1)
|
|
500
|
+
});
|
|
501
|
+
var selectMediaSchema = createSelectSchema(media);
|
|
502
|
+
var insertWorkflowHistorySchema = createInsertSchema(workflowHistory, {
|
|
503
|
+
action: (schema) => schema.min(1),
|
|
504
|
+
fromStatus: (schema) => schema.min(1),
|
|
505
|
+
toStatus: (schema) => schema.min(1)
|
|
506
|
+
});
|
|
507
|
+
var selectWorkflowHistorySchema = createSelectSchema(workflowHistory);
|
|
508
|
+
var insertPluginSchema = createInsertSchema(plugins, {
|
|
509
|
+
name: (schema) => schema.min(1),
|
|
510
|
+
displayName: (schema) => schema.min(1),
|
|
511
|
+
version: (schema) => schema.min(1),
|
|
512
|
+
author: (schema) => schema.min(1),
|
|
513
|
+
category: (schema) => schema.min(1)
|
|
514
|
+
});
|
|
515
|
+
var selectPluginSchema = createSelectSchema(plugins);
|
|
516
|
+
var insertPluginHookSchema = createInsertSchema(pluginHooks, {
|
|
517
|
+
hookName: (schema) => schema.min(1),
|
|
518
|
+
handlerName: (schema) => schema.min(1)
|
|
519
|
+
});
|
|
520
|
+
var selectPluginHookSchema = createSelectSchema(pluginHooks);
|
|
521
|
+
var insertPluginRouteSchema = createInsertSchema(pluginRoutes, {
|
|
522
|
+
path: (schema) => schema.min(1),
|
|
523
|
+
method: (schema) => schema.min(1),
|
|
524
|
+
handlerName: (schema) => schema.min(1)
|
|
525
|
+
});
|
|
526
|
+
var selectPluginRouteSchema = createSelectSchema(pluginRoutes);
|
|
527
|
+
var insertPluginAssetSchema = createInsertSchema(pluginAssets, {
|
|
528
|
+
assetType: (schema) => schema.min(1),
|
|
529
|
+
assetPath: (schema) => schema.min(1)
|
|
530
|
+
});
|
|
531
|
+
var selectPluginAssetSchema = createSelectSchema(pluginAssets);
|
|
532
|
+
var insertPluginActivityLogSchema = createInsertSchema(pluginActivityLog, {
|
|
533
|
+
action: (schema) => schema.min(1)
|
|
534
|
+
});
|
|
535
|
+
var selectPluginActivityLogSchema = createSelectSchema(pluginActivityLog);
|
|
536
|
+
var systemLogs = sqliteTable("system_logs", {
|
|
537
|
+
id: text("id").primaryKey(),
|
|
538
|
+
level: text("level").notNull(),
|
|
539
|
+
// 'debug', 'info', 'warn', 'error', 'fatal'
|
|
540
|
+
category: text("category").notNull(),
|
|
541
|
+
// 'auth', 'api', 'workflow', 'plugin', 'media', 'system', etc.
|
|
542
|
+
message: text("message").notNull(),
|
|
543
|
+
data: text("data", { mode: "json" }),
|
|
544
|
+
// Additional structured data
|
|
545
|
+
userId: text("user_id").references(() => users.id),
|
|
546
|
+
sessionId: text("session_id"),
|
|
547
|
+
requestId: text("request_id"),
|
|
548
|
+
ipAddress: text("ip_address"),
|
|
549
|
+
userAgent: text("user_agent"),
|
|
550
|
+
method: text("method"),
|
|
551
|
+
// HTTP method for API logs
|
|
552
|
+
url: text("url"),
|
|
553
|
+
// Request URL for API logs
|
|
554
|
+
statusCode: integer("status_code"),
|
|
555
|
+
// HTTP status code for API logs
|
|
556
|
+
duration: integer("duration"),
|
|
557
|
+
// Request duration in milliseconds
|
|
558
|
+
stackTrace: text("stack_trace"),
|
|
559
|
+
// Error stack trace for error logs
|
|
560
|
+
tags: text("tags", { mode: "json" }),
|
|
561
|
+
// Array of tags for categorization
|
|
562
|
+
source: text("source"),
|
|
563
|
+
// Source component/module that generated the log
|
|
564
|
+
createdAt: integer("created_at", { mode: "timestamp" }).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
|
|
565
|
+
});
|
|
566
|
+
var logConfig = sqliteTable("log_config", {
|
|
567
|
+
id: text("id").primaryKey(),
|
|
568
|
+
category: text("category").notNull().unique(),
|
|
569
|
+
enabled: integer("enabled", { mode: "boolean" }).notNull().default(true),
|
|
570
|
+
level: text("level").notNull().default("info"),
|
|
571
|
+
// minimum log level to store
|
|
572
|
+
retention: integer("retention").notNull().default(30),
|
|
573
|
+
// days to keep logs
|
|
574
|
+
maxSize: integer("max_size").default(1e4),
|
|
575
|
+
// max number of logs per category
|
|
576
|
+
createdAt: integer("created_at", { mode: "timestamp" }).notNull().$defaultFn(() => /* @__PURE__ */ new Date()),
|
|
577
|
+
updatedAt: integer("updated_at", { mode: "timestamp" }).notNull().$defaultFn(() => /* @__PURE__ */ new Date())
|
|
578
|
+
});
|
|
579
|
+
var insertSystemLogSchema = createInsertSchema(systemLogs, {
|
|
580
|
+
level: (schema) => schema.min(1),
|
|
581
|
+
category: (schema) => schema.min(1),
|
|
582
|
+
message: (schema) => schema.min(1)
|
|
583
|
+
});
|
|
584
|
+
var selectSystemLogSchema = createSelectSchema(systemLogs);
|
|
585
|
+
var insertLogConfigSchema = createInsertSchema(logConfig, {
|
|
586
|
+
category: (schema) => schema.min(1),
|
|
587
|
+
level: (schema) => schema.min(1)
|
|
588
|
+
});
|
|
589
|
+
var selectLogConfigSchema = createSelectSchema(logConfig);
|
|
590
|
+
var Logger = class {
|
|
591
|
+
db;
|
|
592
|
+
enabled = true;
|
|
593
|
+
configCache = /* @__PURE__ */ new Map();
|
|
594
|
+
lastConfigRefresh = 0;
|
|
595
|
+
configRefreshInterval = 6e4;
|
|
596
|
+
// 1 minute
|
|
597
|
+
constructor(database) {
|
|
598
|
+
this.db = drizzle(database);
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Log a debug message
|
|
602
|
+
*/
|
|
603
|
+
async debug(category, message, data, context) {
|
|
604
|
+
return this.log("debug", category, message, data, context);
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Log an info message
|
|
608
|
+
*/
|
|
609
|
+
async info(category, message, data, context) {
|
|
610
|
+
return this.log("info", category, message, data, context);
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Log a warning message
|
|
614
|
+
*/
|
|
615
|
+
async warn(category, message, data, context) {
|
|
616
|
+
return this.log("warn", category, message, data, context);
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Log an error message
|
|
620
|
+
*/
|
|
621
|
+
async error(category, message, error, context) {
|
|
622
|
+
const errorData = error instanceof Error ? {
|
|
623
|
+
name: error.name,
|
|
624
|
+
message: error.message,
|
|
625
|
+
stack: error.stack
|
|
626
|
+
} : error;
|
|
627
|
+
return this.log("error", category, message, errorData, {
|
|
628
|
+
...context,
|
|
629
|
+
stackTrace: error instanceof Error ? error.stack : void 0
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Log a fatal message
|
|
634
|
+
*/
|
|
635
|
+
async fatal(category, message, error, context) {
|
|
636
|
+
const errorData = error instanceof Error ? {
|
|
637
|
+
name: error.name,
|
|
638
|
+
message: error.message,
|
|
639
|
+
stack: error.stack
|
|
640
|
+
} : error;
|
|
641
|
+
return this.log("fatal", category, message, errorData, {
|
|
642
|
+
...context,
|
|
643
|
+
stackTrace: error instanceof Error ? error.stack : void 0
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* Log an API request
|
|
648
|
+
*/
|
|
649
|
+
async logRequest(method, url, statusCode, duration, context) {
|
|
650
|
+
const level = statusCode >= 500 ? "error" : statusCode >= 400 ? "warn" : "info";
|
|
651
|
+
return this.log(level, "api", `${method} ${url} - ${statusCode}`, {
|
|
652
|
+
method,
|
|
653
|
+
url,
|
|
654
|
+
statusCode,
|
|
655
|
+
duration
|
|
656
|
+
}, {
|
|
657
|
+
...context,
|
|
658
|
+
method,
|
|
659
|
+
url,
|
|
660
|
+
statusCode,
|
|
661
|
+
duration
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Log an authentication event
|
|
666
|
+
*/
|
|
667
|
+
async logAuth(action, userId, success = true, context) {
|
|
668
|
+
const level = success ? "info" : "warn";
|
|
669
|
+
return this.log(level, "auth", `Authentication ${action}: ${success ? "success" : "failed"}`, {
|
|
670
|
+
action,
|
|
671
|
+
success,
|
|
672
|
+
userId
|
|
673
|
+
}, {
|
|
674
|
+
...context,
|
|
675
|
+
userId,
|
|
676
|
+
tags: ["authentication", action]
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* Log a security event
|
|
681
|
+
*/
|
|
682
|
+
async logSecurity(event, severity, context) {
|
|
683
|
+
const level = severity === "critical" ? "fatal" : severity === "high" ? "error" : "warn";
|
|
684
|
+
return this.log(level, "security", `Security event: ${event}`, {
|
|
685
|
+
event,
|
|
686
|
+
severity
|
|
687
|
+
}, {
|
|
688
|
+
...context,
|
|
689
|
+
tags: ["security", severity]
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Core logging method
|
|
694
|
+
*/
|
|
695
|
+
async log(level, category, message, data, context) {
|
|
696
|
+
if (!this.enabled) return;
|
|
697
|
+
try {
|
|
698
|
+
const config = await this.getConfig(category);
|
|
699
|
+
if (!config || !config.enabled || !this.shouldLog(level, config.level)) {
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
const logEntry = {
|
|
703
|
+
id: crypto.randomUUID(),
|
|
704
|
+
level,
|
|
705
|
+
category,
|
|
706
|
+
message,
|
|
707
|
+
data: data ? JSON.stringify(data) : null,
|
|
708
|
+
userId: context?.userId || null,
|
|
709
|
+
sessionId: context?.sessionId || null,
|
|
710
|
+
requestId: context?.requestId || null,
|
|
711
|
+
ipAddress: context?.ipAddress || null,
|
|
712
|
+
userAgent: context?.userAgent || null,
|
|
713
|
+
method: context?.method || null,
|
|
714
|
+
url: context?.url || null,
|
|
715
|
+
statusCode: context?.statusCode || null,
|
|
716
|
+
duration: context?.duration || null,
|
|
717
|
+
stackTrace: context?.stackTrace || null,
|
|
718
|
+
tags: context?.tags ? JSON.stringify(context.tags) : null,
|
|
719
|
+
source: context?.source || null,
|
|
720
|
+
createdAt: /* @__PURE__ */ new Date()
|
|
721
|
+
};
|
|
722
|
+
await this.db.insert(systemLogs).values(logEntry);
|
|
723
|
+
if (config.maxSize) {
|
|
724
|
+
await this.cleanupCategory(category, config.maxSize);
|
|
725
|
+
}
|
|
726
|
+
} catch (error) {
|
|
727
|
+
console.error("Logger error:", error);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* Get logs with filtering and pagination
|
|
732
|
+
*/
|
|
733
|
+
async getLogs(filter = {}) {
|
|
734
|
+
try {
|
|
735
|
+
const conditions = [];
|
|
736
|
+
if (filter.level && filter.level.length > 0) {
|
|
737
|
+
conditions.push(inArray(systemLogs.level, filter.level));
|
|
738
|
+
}
|
|
739
|
+
if (filter.category && filter.category.length > 0) {
|
|
740
|
+
conditions.push(inArray(systemLogs.category, filter.category));
|
|
741
|
+
}
|
|
742
|
+
if (filter.userId) {
|
|
743
|
+
conditions.push(eq(systemLogs.userId, filter.userId));
|
|
744
|
+
}
|
|
745
|
+
if (filter.source) {
|
|
746
|
+
conditions.push(eq(systemLogs.source, filter.source));
|
|
747
|
+
}
|
|
748
|
+
if (filter.search) {
|
|
749
|
+
conditions.push(
|
|
750
|
+
like(systemLogs.message, `%${filter.search}%`)
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
if (filter.startDate) {
|
|
754
|
+
conditions.push(gte(systemLogs.createdAt, filter.startDate));
|
|
755
|
+
}
|
|
756
|
+
if (filter.endDate) {
|
|
757
|
+
conditions.push(lte(systemLogs.createdAt, filter.endDate));
|
|
758
|
+
}
|
|
759
|
+
const whereClause = conditions.length > 0 ? and(...conditions) : void 0;
|
|
760
|
+
const totalResult = await this.db.select({ count: count() }).from(systemLogs).where(whereClause);
|
|
761
|
+
const total = totalResult[0]?.count || 0;
|
|
762
|
+
const sortColumn = filter.sortBy === "level" ? systemLogs.level : filter.sortBy === "category" ? systemLogs.category : systemLogs.createdAt;
|
|
763
|
+
const sortFn = filter.sortOrder === "asc" ? asc : desc;
|
|
764
|
+
const logs = await this.db.select().from(systemLogs).where(whereClause).orderBy(sortFn(sortColumn)).limit(filter.limit || 50).offset(filter.offset || 0);
|
|
765
|
+
return { logs, total };
|
|
766
|
+
} catch (error) {
|
|
767
|
+
console.error("Error getting logs:", error);
|
|
768
|
+
return { logs: [], total: 0 };
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
772
|
+
* Get log configuration for a category
|
|
773
|
+
*/
|
|
774
|
+
async getConfig(category) {
|
|
775
|
+
try {
|
|
776
|
+
const now = Date.now();
|
|
777
|
+
if (this.configCache.has(category) && now - this.lastConfigRefresh < this.configRefreshInterval) {
|
|
778
|
+
return this.configCache.get(category) || null;
|
|
779
|
+
}
|
|
780
|
+
const configs = await this.db.select().from(logConfig).where(eq(logConfig.category, category));
|
|
781
|
+
const config = configs[0] || null;
|
|
782
|
+
if (config) {
|
|
783
|
+
this.configCache.set(category, config);
|
|
784
|
+
this.lastConfigRefresh = now;
|
|
785
|
+
}
|
|
786
|
+
return config;
|
|
787
|
+
} catch (error) {
|
|
788
|
+
console.error("Error getting log config:", error);
|
|
789
|
+
return null;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
/**
|
|
793
|
+
* Update log configuration
|
|
794
|
+
*/
|
|
795
|
+
async updateConfig(category, updates) {
|
|
796
|
+
try {
|
|
797
|
+
await this.db.update(logConfig).set({
|
|
798
|
+
...updates,
|
|
799
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
800
|
+
}).where(eq(logConfig.category, category));
|
|
801
|
+
this.configCache.delete(category);
|
|
802
|
+
} catch (error) {
|
|
803
|
+
console.error("Error updating log config:", error);
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
/**
|
|
807
|
+
* Get all log configurations
|
|
808
|
+
*/
|
|
809
|
+
async getAllConfigs() {
|
|
810
|
+
try {
|
|
811
|
+
return await this.db.select().from(logConfig);
|
|
812
|
+
} catch (error) {
|
|
813
|
+
console.error("Error getting log configs:", error);
|
|
814
|
+
return [];
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* Clean up old logs for a category
|
|
819
|
+
*/
|
|
820
|
+
async cleanupCategory(category, maxSize) {
|
|
821
|
+
try {
|
|
822
|
+
const countResult = await this.db.select({ count: count() }).from(systemLogs).where(eq(systemLogs.category, category));
|
|
823
|
+
const currentCount = countResult[0]?.count || 0;
|
|
824
|
+
if (currentCount > maxSize) {
|
|
825
|
+
const cutoffLogs = await this.db.select({ createdAt: systemLogs.createdAt }).from(systemLogs).where(eq(systemLogs.category, category)).orderBy(desc(systemLogs.createdAt)).limit(1).offset(maxSize - 1);
|
|
826
|
+
if (cutoffLogs[0]) {
|
|
827
|
+
await this.db.delete(systemLogs).where(
|
|
828
|
+
and(
|
|
829
|
+
eq(systemLogs.category, category),
|
|
830
|
+
lte(systemLogs.createdAt, cutoffLogs[0].createdAt)
|
|
831
|
+
)
|
|
832
|
+
);
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
} catch (error) {
|
|
836
|
+
console.error("Error cleaning up logs:", error);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
/**
|
|
840
|
+
* Clean up logs based on retention policy
|
|
841
|
+
*/
|
|
842
|
+
async cleanupByRetention() {
|
|
843
|
+
try {
|
|
844
|
+
const configs = await this.getAllConfigs();
|
|
845
|
+
for (const config of configs) {
|
|
846
|
+
if (config.retention > 0) {
|
|
847
|
+
const cutoffDate = /* @__PURE__ */ new Date();
|
|
848
|
+
cutoffDate.setDate(cutoffDate.getDate() - config.retention);
|
|
849
|
+
await this.db.delete(systemLogs).where(
|
|
850
|
+
and(
|
|
851
|
+
eq(systemLogs.category, config.category),
|
|
852
|
+
lte(systemLogs.createdAt, cutoffDate)
|
|
853
|
+
)
|
|
854
|
+
);
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
} catch (error) {
|
|
858
|
+
console.error("Error cleaning up logs by retention:", error);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
/**
|
|
862
|
+
* Check if a log level should be recorded based on configuration
|
|
863
|
+
*/
|
|
864
|
+
shouldLog(level, configLevel) {
|
|
865
|
+
const levels = ["debug", "info", "warn", "error", "fatal"];
|
|
866
|
+
const levelIndex = levels.indexOf(level);
|
|
867
|
+
const configLevelIndex = levels.indexOf(configLevel);
|
|
868
|
+
return levelIndex >= configLevelIndex;
|
|
869
|
+
}
|
|
870
|
+
/**
|
|
871
|
+
* Enable or disable logging
|
|
872
|
+
*/
|
|
873
|
+
setEnabled(enabled) {
|
|
874
|
+
this.enabled = enabled;
|
|
875
|
+
}
|
|
876
|
+
/**
|
|
877
|
+
* Check if logging is enabled
|
|
878
|
+
*/
|
|
879
|
+
isEnabled() {
|
|
880
|
+
return this.enabled;
|
|
881
|
+
}
|
|
882
|
+
};
|
|
883
|
+
var loggerInstance = null;
|
|
884
|
+
function getLogger(database) {
|
|
885
|
+
if (!loggerInstance && database) {
|
|
886
|
+
loggerInstance = new Logger(database);
|
|
887
|
+
}
|
|
888
|
+
if (!loggerInstance) {
|
|
889
|
+
throw new Error("Logger not initialized. Call getLogger with a database instance first.");
|
|
890
|
+
}
|
|
891
|
+
return loggerInstance;
|
|
892
|
+
}
|
|
893
|
+
function initLogger(database) {
|
|
894
|
+
loggerInstance = new Logger(database);
|
|
895
|
+
return loggerInstance;
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
// src/services/cache.ts
|
|
899
|
+
var CacheService = class {
|
|
900
|
+
config;
|
|
901
|
+
memoryCache = /* @__PURE__ */ new Map();
|
|
902
|
+
constructor(config) {
|
|
903
|
+
this.config = config;
|
|
904
|
+
}
|
|
905
|
+
/**
|
|
906
|
+
* Generate cache key with prefix
|
|
907
|
+
*/
|
|
908
|
+
generateKey(type, identifier) {
|
|
909
|
+
const parts = [this.config.keyPrefix, type];
|
|
910
|
+
if (identifier) {
|
|
911
|
+
parts.push(identifier);
|
|
912
|
+
}
|
|
913
|
+
return parts.join(":");
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* Get value from cache
|
|
917
|
+
*/
|
|
918
|
+
async get(key) {
|
|
919
|
+
const cached = this.memoryCache.get(key);
|
|
920
|
+
if (!cached) {
|
|
921
|
+
return null;
|
|
922
|
+
}
|
|
923
|
+
if (Date.now() > cached.expires) {
|
|
924
|
+
this.memoryCache.delete(key);
|
|
925
|
+
return null;
|
|
926
|
+
}
|
|
927
|
+
return cached.value;
|
|
928
|
+
}
|
|
929
|
+
/**
|
|
930
|
+
* Get value from cache with source information
|
|
931
|
+
*/
|
|
932
|
+
async getWithSource(key) {
|
|
933
|
+
const cached = this.memoryCache.get(key);
|
|
934
|
+
if (!cached) {
|
|
935
|
+
return {
|
|
936
|
+
hit: false,
|
|
937
|
+
data: null,
|
|
938
|
+
source: "none"
|
|
939
|
+
};
|
|
940
|
+
}
|
|
941
|
+
if (Date.now() > cached.expires) {
|
|
942
|
+
this.memoryCache.delete(key);
|
|
943
|
+
return {
|
|
944
|
+
hit: false,
|
|
945
|
+
data: null,
|
|
946
|
+
source: "expired"
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
return {
|
|
950
|
+
hit: true,
|
|
951
|
+
data: cached.value,
|
|
952
|
+
source: "memory",
|
|
953
|
+
ttl: (cached.expires - Date.now()) / 1e3
|
|
954
|
+
// TTL in seconds
|
|
955
|
+
};
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* Set value in cache
|
|
959
|
+
*/
|
|
960
|
+
async set(key, value, ttl) {
|
|
961
|
+
const expires = Date.now() + (ttl || this.config.ttl) * 1e3;
|
|
962
|
+
this.memoryCache.set(key, { value, expires });
|
|
963
|
+
}
|
|
964
|
+
/**
|
|
965
|
+
* Delete specific key from cache
|
|
966
|
+
*/
|
|
967
|
+
async delete(key) {
|
|
968
|
+
this.memoryCache.delete(key);
|
|
969
|
+
}
|
|
970
|
+
/**
|
|
971
|
+
* Invalidate cache keys matching a pattern
|
|
972
|
+
* For memory cache, we do simple string matching
|
|
973
|
+
*/
|
|
974
|
+
async invalidate(pattern) {
|
|
975
|
+
const regexPattern = pattern.replace(/\*/g, ".*").replace(/\?/g, ".");
|
|
976
|
+
const regex = new RegExp(`^${regexPattern}$`);
|
|
977
|
+
for (const key of this.memoryCache.keys()) {
|
|
978
|
+
if (regex.test(key)) {
|
|
979
|
+
this.memoryCache.delete(key);
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
/**
|
|
984
|
+
* Clear all cache
|
|
985
|
+
*/
|
|
986
|
+
async clear() {
|
|
987
|
+
this.memoryCache.clear();
|
|
988
|
+
}
|
|
989
|
+
/**
|
|
990
|
+
* Get value from cache or set it using a callback
|
|
991
|
+
*/
|
|
992
|
+
async getOrSet(key, callback, ttl) {
|
|
993
|
+
const cached = await this.get(key);
|
|
994
|
+
if (cached !== null) {
|
|
995
|
+
return cached;
|
|
996
|
+
}
|
|
997
|
+
const value = await callback();
|
|
998
|
+
await this.set(key, value, ttl);
|
|
999
|
+
return value;
|
|
1000
|
+
}
|
|
1001
|
+
};
|
|
1002
|
+
var CACHE_CONFIGS = {
|
|
1003
|
+
api: {
|
|
1004
|
+
ttl: 300,
|
|
1005
|
+
// 5 minutes
|
|
1006
|
+
keyPrefix: "api"
|
|
1007
|
+
},
|
|
1008
|
+
user: {
|
|
1009
|
+
ttl: 600,
|
|
1010
|
+
// 10 minutes
|
|
1011
|
+
keyPrefix: "user"
|
|
1012
|
+
},
|
|
1013
|
+
content: {
|
|
1014
|
+
ttl: 300,
|
|
1015
|
+
// 5 minutes
|
|
1016
|
+
keyPrefix: "content"
|
|
1017
|
+
},
|
|
1018
|
+
collection: {
|
|
1019
|
+
ttl: 600,
|
|
1020
|
+
// 10 minutes
|
|
1021
|
+
keyPrefix: "collection"
|
|
1022
|
+
}
|
|
1023
|
+
};
|
|
1024
|
+
function getCacheService(config) {
|
|
1025
|
+
return new CacheService(config);
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
export { CACHE_CONFIGS, CacheService, Logger, apiTokens, collections, content, contentVersions, getCacheService, getLogger, initLogger, insertCollectionSchema, insertContentSchema, insertLogConfigSchema, insertMediaSchema, insertPluginActivityLogSchema, insertPluginAssetSchema, insertPluginHookSchema, insertPluginRouteSchema, insertPluginSchema, insertSystemLogSchema, insertUserSchema, insertWorkflowHistorySchema, logConfig, media, pluginActivityLog, pluginAssets, pluginHooks, pluginRoutes, plugins, schema_exports, selectCollectionSchema, selectContentSchema, selectLogConfigSchema, selectMediaSchema, selectPluginActivityLogSchema, selectPluginAssetSchema, selectPluginHookSchema, selectPluginRouteSchema, selectPluginSchema, selectSystemLogSchema, selectUserSchema, selectWorkflowHistorySchema, systemLogs, users, workflowHistory };
|
|
1029
|
+
//# sourceMappingURL=chunk-5XTB4FE5.js.map
|
|
1030
|
+
//# sourceMappingURL=chunk-5XTB4FE5.js.map
|