@onyx.dev/onyx-database 0.2.10 → 1.0.0

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/index.cjs CHANGED
@@ -342,7 +342,8 @@ var HttpClient = class {
342
342
  ...method === "DELETE" ? { Prefer: "return=representation" } : {},
343
343
  ...extraHeaders ?? {}
344
344
  });
345
- if (body == null) delete headers["Content-Type"];
345
+ const hasExplicitContentType = extraHeaders && "Content-Type" in extraHeaders || Object.prototype.hasOwnProperty.call(this.defaults, "Content-Type");
346
+ if (body == null && !hasExplicitContentType) delete headers["Content-Type"];
346
347
  if (this.requestLoggingEnabled) {
347
348
  console.log(`${method} ${url}`);
348
349
  if (body != null) {
@@ -832,6 +833,56 @@ var QueryResults = class extends Array {
832
833
  }
833
834
  };
834
835
 
836
+ // src/helpers/condition-normalizer.ts
837
+ function isQueryBuilderLike(value) {
838
+ return !!value && typeof value.toSerializableQueryObject === "function";
839
+ }
840
+ function normalizeCriteriaValue(value) {
841
+ if (Array.isArray(value)) {
842
+ let changed = false;
843
+ const normalized = value.map((item) => {
844
+ const result = normalizeCriteriaValue(item);
845
+ if (result.changed) changed = true;
846
+ return result.value;
847
+ });
848
+ if (!changed) {
849
+ for (let i = 0; i < normalized.length; i += 1) {
850
+ if (normalized[i] !== value[i]) {
851
+ changed = true;
852
+ break;
853
+ }
854
+ }
855
+ }
856
+ return { value: changed ? normalized : value, changed };
857
+ }
858
+ if (isQueryBuilderLike(value)) {
859
+ return { value: value.toSerializableQueryObject(), changed: true };
860
+ }
861
+ return { value, changed: false };
862
+ }
863
+ function normalizeConditionInternal(condition) {
864
+ if (condition.conditionType === "SingleCondition") {
865
+ const { value, changed: changed2 } = normalizeCriteriaValue(condition.criteria.value);
866
+ if (!changed2) return condition;
867
+ return {
868
+ ...condition,
869
+ criteria: { ...condition.criteria, value }
870
+ };
871
+ }
872
+ let changed = false;
873
+ const normalizedConditions = condition.conditions.map((child) => {
874
+ const normalized = normalizeConditionInternal(child);
875
+ if (normalized !== child) changed = true;
876
+ return normalized;
877
+ });
878
+ if (!changed) return condition;
879
+ return { ...condition, conditions: normalizedConditions };
880
+ }
881
+ function normalizeCondition(condition) {
882
+ if (!condition) return condition;
883
+ return normalizeConditionInternal(condition);
884
+ }
885
+
835
886
  // src/builders/cascade-relationship-builder.ts
836
887
  var CascadeRelationshipBuilder = class {
837
888
  graphName;
@@ -959,6 +1010,37 @@ function serializeDates(value) {
959
1010
  }
960
1011
  return value;
961
1012
  }
1013
+ function stripEntityText(input) {
1014
+ const { entityText, ...rest } = input;
1015
+ return rest;
1016
+ }
1017
+ function normalizeSecretMetadata(input) {
1018
+ return { ...input, updatedAt: new Date(input.updatedAt) };
1019
+ }
1020
+ function normalizeSecretRecord(input) {
1021
+ return { ...input, updatedAt: new Date(input.updatedAt) };
1022
+ }
1023
+ function normalizeDate(value) {
1024
+ if (value == null) return void 0;
1025
+ if (value instanceof Date) return value;
1026
+ const ts = new Date(value);
1027
+ return Number.isNaN(ts.getTime()) ? void 0 : ts;
1028
+ }
1029
+ function normalizeSchemaRevision(input, fallbackDatabaseId) {
1030
+ const { meta, createdAt, publishedAt, revisionId, entityText, ...rest } = input;
1031
+ const mergedMeta = {
1032
+ revisionId: meta?.revisionId ?? revisionId,
1033
+ createdAt: normalizeDate(meta?.createdAt ?? createdAt),
1034
+ publishedAt: normalizeDate(meta?.publishedAt ?? publishedAt)
1035
+ };
1036
+ const cleanedMeta = mergedMeta.revisionId || mergedMeta.createdAt || mergedMeta.publishedAt ? mergedMeta : void 0;
1037
+ return {
1038
+ ...rest,
1039
+ databaseId: input.databaseId ?? fallbackDatabaseId,
1040
+ meta: cleanedMeta,
1041
+ entities: input.entities ?? []
1042
+ };
1043
+ }
962
1044
  var OnyxDatabaseImpl = class {
963
1045
  cfgPromise;
964
1046
  resolved = null;
@@ -1092,6 +1174,94 @@ var OnyxDatabaseImpl = class {
1092
1174
  )}`;
1093
1175
  return http.request("DELETE", path);
1094
1176
  }
1177
+ async getSchema(options) {
1178
+ const { http, databaseId } = await this.ensureClient();
1179
+ const params = new URLSearchParams();
1180
+ const tables = options?.tables;
1181
+ const tableList = Array.isArray(tables) ? tables : typeof tables === "string" ? tables.split(",") : [];
1182
+ const normalizedTables = tableList.map((t) => t.trim()).filter(Boolean);
1183
+ if (normalizedTables.length) {
1184
+ params.append("tables", normalizedTables.map(encodeURIComponent).join(","));
1185
+ }
1186
+ const path = `/schemas/${encodeURIComponent(databaseId)}${params.size ? `?${params.toString()}` : ""}`;
1187
+ const res = await http.request("GET", path);
1188
+ return normalizeSchemaRevision(res, databaseId);
1189
+ }
1190
+ async getSchemaHistory() {
1191
+ const { http, databaseId } = await this.ensureClient();
1192
+ const path = `/schemas/history/${encodeURIComponent(databaseId)}`;
1193
+ const res = await http.request("GET", path);
1194
+ return Array.isArray(res) ? res.map((entry) => normalizeSchemaRevision(entry, databaseId)) : [];
1195
+ }
1196
+ async updateSchema(schema, options) {
1197
+ const { http, databaseId } = await this.ensureClient();
1198
+ const params = new URLSearchParams();
1199
+ if (options?.publish) params.append("publish", "true");
1200
+ const path = `/schemas/${encodeURIComponent(databaseId)}${params.size ? `?${params.toString()}` : ""}`;
1201
+ const body = stripEntityText({ ...schema, databaseId: schema.databaseId ?? databaseId });
1202
+ const res = await http.request(
1203
+ "PUT",
1204
+ path,
1205
+ serializeDates(body)
1206
+ );
1207
+ return normalizeSchemaRevision(res, databaseId);
1208
+ }
1209
+ async validateSchema(schema) {
1210
+ const { http, databaseId } = await this.ensureClient();
1211
+ const path = `/schemas/${encodeURIComponent(databaseId)}/validate`;
1212
+ const body = stripEntityText({ ...schema, databaseId: schema.databaseId ?? databaseId });
1213
+ const res = await http.request(
1214
+ "POST",
1215
+ path,
1216
+ serializeDates(body)
1217
+ );
1218
+ const normalizedSchema = res.schema ? normalizeSchemaRevision(res.schema, databaseId) : void 0;
1219
+ return {
1220
+ ...res,
1221
+ valid: res.valid ?? true,
1222
+ schema: normalizedSchema
1223
+ };
1224
+ }
1225
+ async listSecrets() {
1226
+ const { http, databaseId } = await this.ensureClient();
1227
+ const path = `/database/${encodeURIComponent(databaseId)}/secret`;
1228
+ const response = await http.request(
1229
+ "GET",
1230
+ path,
1231
+ void 0,
1232
+ { "Content-Type": "application/json" }
1233
+ );
1234
+ const records = (response.records ?? []).map(normalizeSecretMetadata);
1235
+ return {
1236
+ ...response,
1237
+ records,
1238
+ meta: response.meta ?? { totalRecords: records.length }
1239
+ };
1240
+ }
1241
+ async getSecret(key) {
1242
+ const { http, databaseId } = await this.ensureClient();
1243
+ const path = `/database/${encodeURIComponent(databaseId)}/secret/${encodeURIComponent(key)}`;
1244
+ const record = await http.request("GET", path, void 0, {
1245
+ "Content-Type": "application/json"
1246
+ });
1247
+ return normalizeSecretRecord(record);
1248
+ }
1249
+ async putSecret(key, input) {
1250
+ const { http, databaseId } = await this.ensureClient();
1251
+ const path = `/database/${encodeURIComponent(databaseId)}/secret/${encodeURIComponent(key)}`;
1252
+ const response = await http.request("PUT", path, serializeDates(input));
1253
+ return normalizeSecretMetadata(response);
1254
+ }
1255
+ async deleteSecret(key) {
1256
+ const { http, databaseId } = await this.ensureClient();
1257
+ const path = `/database/${encodeURIComponent(databaseId)}/secret/${encodeURIComponent(key)}`;
1258
+ const response = await http.request(
1259
+ "DELETE",
1260
+ path
1261
+ );
1262
+ const deletedKey = response && typeof response === "object" && "key" in response ? response.key : void 0;
1263
+ return { key: deletedKey ?? key };
1264
+ }
1095
1265
  close() {
1096
1266
  for (const h of Array.from(this.streams)) {
1097
1267
  try {
@@ -1206,11 +1376,14 @@ var QueryBuilderImpl = class {
1206
1376
  if (!this.table) throw new Error("Table is not defined. Call from(<table>) first.");
1207
1377
  return this.table;
1208
1378
  }
1379
+ serializableConditions() {
1380
+ return normalizeCondition(this.conditions);
1381
+ }
1209
1382
  toSelectQuery() {
1210
1383
  return {
1211
1384
  type: "SelectQuery",
1212
1385
  fields: this.fields,
1213
- conditions: this.conditions,
1386
+ conditions: this.serializableConditions(),
1214
1387
  sort: this.sort,
1215
1388
  limit: this.limitValue,
1216
1389
  distinct: this.distinctValue,
@@ -1219,6 +1392,21 @@ var QueryBuilderImpl = class {
1219
1392
  resolvers: this.resolvers
1220
1393
  };
1221
1394
  }
1395
+ toUpdateQuery() {
1396
+ return {
1397
+ type: "UpdateQuery",
1398
+ conditions: this.serializableConditions(),
1399
+ updates: this.updates ?? {},
1400
+ sort: this.sort,
1401
+ limit: this.limitValue,
1402
+ partition: this.partitionValue ?? null
1403
+ };
1404
+ }
1405
+ toSerializableQueryObject() {
1406
+ const table = this.ensureTable();
1407
+ const payload = this.mode === "update" ? this.toUpdateQuery() : this.toSelectQuery();
1408
+ return { ...payload, table };
1409
+ }
1222
1410
  from(table) {
1223
1411
  this.table = table;
1224
1412
  return this;
@@ -1354,14 +1542,7 @@ var QueryBuilderImpl = class {
1354
1542
  async update() {
1355
1543
  if (this.mode !== "update") throw new Error("Call setUpdates(...) before update().");
1356
1544
  const table = this.ensureTable();
1357
- const update = {
1358
- type: "UpdateQuery",
1359
- conditions: this.conditions,
1360
- updates: this.updates ?? {},
1361
- sort: this.sort,
1362
- limit: this.limitValue,
1363
- partition: this.partitionValue ?? null
1364
- };
1545
+ const update = this.toUpdateQuery();
1365
1546
  return this.db._update(table, update, this.partitionValue);
1366
1547
  }
1367
1548
  onItemAdded(listener) {
@@ -1574,12 +1755,20 @@ var ConditionBuilderImpl = class {
1574
1755
  var c = (field, operator, value) => new ConditionBuilderImpl({ field, operator, value });
1575
1756
  var eq = (field, value) => c(field, "EQUAL", value);
1576
1757
  var neq = (field, value) => c(field, "NOT_EQUAL", value);
1577
- var inOp = (field, values) => c(
1578
- field,
1579
- "IN",
1580
- typeof values === "string" ? values.split(",").map((v) => v.trim()).filter((v) => v.length) : values
1581
- );
1582
- var notIn = (field, values) => c(field, "NOT_IN", values);
1758
+ function inOp(field, values) {
1759
+ const parsed = typeof values === "string" ? values.split(",").map((v) => v.trim()).filter((v) => v.length) : values;
1760
+ return c(field, "IN", parsed);
1761
+ }
1762
+ function within(field, values) {
1763
+ return inOp(field, values);
1764
+ }
1765
+ function notIn(field, values) {
1766
+ const parsed = typeof values === "string" ? values.split(",").map((v) => v.trim()).filter((v) => v.length) : values;
1767
+ return c(field, "NOT_IN", parsed);
1768
+ }
1769
+ function notWithin(field, values) {
1770
+ return notIn(field, values);
1771
+ }
1583
1772
  var between = (field, lower2, upper2) => c(field, "BETWEEN", [lower2, upper2]);
1584
1773
  var gt = (field, value) => c(field, "GREATER_THAN", value);
1585
1774
  var gte = (field, value) => c(field, "GREATER_THAN_EQUAL", value);
@@ -1646,6 +1835,7 @@ exports.notLike = notLike;
1646
1835
  exports.notMatches = notMatches;
1647
1836
  exports.notNull = notNull;
1648
1837
  exports.notStartsWith = notStartsWith;
1838
+ exports.notWithin = notWithin;
1649
1839
  exports.onyx = onyx;
1650
1840
  exports.percentile = percentile;
1651
1841
  exports.replace = replace;
@@ -1657,5 +1847,6 @@ exports.substring = substring;
1657
1847
  exports.sum = sum;
1658
1848
  exports.upper = upper;
1659
1849
  exports.variance = variance;
1850
+ exports.within = within;
1660
1851
  //# sourceMappingURL=index.cjs.map
1661
1852
  //# sourceMappingURL=index.cjs.map