@onyx.dev/onyx-database 0.3.0 → 1.0.2
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/README.md +71 -1
- package/dist/gen/cli/generate.cjs +313 -30
- package/dist/gen/cli/generate.cjs.map +1 -1
- package/dist/index.cjs +329 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +92 -6
- package/dist/index.d.ts +92 -6
- package/dist/index.js +328 -37
- package/dist/index.js.map +1 -1
- package/dist/schema/cli/schema.cjs +496 -35
- package/dist/schema/cli/schema.cjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -67,7 +67,7 @@ function readEnv(targetId) {
|
|
|
67
67
|
return res;
|
|
68
68
|
}
|
|
69
69
|
async function readProjectFile(databaseId) {
|
|
70
|
-
if (!isNode) return {};
|
|
70
|
+
if (!isNode) return { config: {} };
|
|
71
71
|
const fs = await nodeImport("node:fs/promises");
|
|
72
72
|
const path = await nodeImport("node:path");
|
|
73
73
|
const cwd = gProcess?.cwd?.() ?? ".";
|
|
@@ -76,7 +76,7 @@ async function readProjectFile(databaseId) {
|
|
|
76
76
|
const sanitized = txt.replace(/[\r\n]+/g, "");
|
|
77
77
|
const json = dropUndefined(JSON.parse(sanitized));
|
|
78
78
|
dbg("project file:", p, "\u2192", mask(json));
|
|
79
|
-
return json;
|
|
79
|
+
return { config: json, path: p };
|
|
80
80
|
};
|
|
81
81
|
if (databaseId) {
|
|
82
82
|
const specific = path.resolve(cwd, `onyx-database-${databaseId}.json`);
|
|
@@ -91,11 +91,11 @@ async function readProjectFile(databaseId) {
|
|
|
91
91
|
return await tryRead(fallback);
|
|
92
92
|
} catch {
|
|
93
93
|
dbg("project file not found:", fallback);
|
|
94
|
-
return {};
|
|
94
|
+
return { config: {} };
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
async function readHomeProfile(databaseId) {
|
|
98
|
-
if (!isNode) return {};
|
|
98
|
+
if (!isNode) return { config: {} };
|
|
99
99
|
const fs = await nodeImport("node:fs/promises");
|
|
100
100
|
const os = await nodeImport("node:os");
|
|
101
101
|
const path = await nodeImport("node:path");
|
|
@@ -115,7 +115,7 @@ async function readHomeProfile(databaseId) {
|
|
|
115
115
|
const sanitized = txt.replace(/[\r\n]+/g, "");
|
|
116
116
|
const json = dropUndefined(JSON.parse(sanitized));
|
|
117
117
|
dbg("home profile used:", p, "\u2192", mask(json));
|
|
118
|
-
return json;
|
|
118
|
+
return { config: json, path: p };
|
|
119
119
|
} catch (e) {
|
|
120
120
|
const msg = e instanceof Error ? e.message : String(e);
|
|
121
121
|
throw new OnyxConfigError(`Failed to read ${p}: ${msg}`);
|
|
@@ -134,7 +134,7 @@ async function readHomeProfile(databaseId) {
|
|
|
134
134
|
dbg("no home-root fallback:", defaultInHome);
|
|
135
135
|
if (!await fileExists(dir)) {
|
|
136
136
|
dbg("~/.onyx does not exist:", dir);
|
|
137
|
-
return {};
|
|
137
|
+
return { config: {} };
|
|
138
138
|
}
|
|
139
139
|
const files = await fs.readdir(dir).catch(() => []);
|
|
140
140
|
const matches2 = files.filter((f) => f.startsWith("onyx-database-") && f.endsWith(".json"));
|
|
@@ -148,10 +148,10 @@ async function readHomeProfile(databaseId) {
|
|
|
148
148
|
);
|
|
149
149
|
}
|
|
150
150
|
dbg("no usable home profiles found in", dir);
|
|
151
|
-
return {};
|
|
151
|
+
return { config: {} };
|
|
152
152
|
}
|
|
153
153
|
async function readConfigPath(p) {
|
|
154
|
-
if (!isNode) return {};
|
|
154
|
+
if (!isNode) return { config: {} };
|
|
155
155
|
const fs = await nodeImport("node:fs/promises");
|
|
156
156
|
const path = await nodeImport("node:path");
|
|
157
157
|
const cwd = gProcess?.cwd?.() ?? ".";
|
|
@@ -161,7 +161,7 @@ async function readConfigPath(p) {
|
|
|
161
161
|
const sanitized = txt.replace(/[\r\n]+/g, "");
|
|
162
162
|
const json = dropUndefined(JSON.parse(sanitized));
|
|
163
163
|
dbg("config path:", resolved, "\u2192", mask(json));
|
|
164
|
-
return json;
|
|
164
|
+
return { config: json, path: resolved };
|
|
165
165
|
} catch (e) {
|
|
166
166
|
const msg = e instanceof Error ? e.message : String(e);
|
|
167
167
|
throw new OnyxConfigError(`Failed to read ${resolved}: ${msg}`);
|
|
@@ -172,7 +172,8 @@ async function resolveConfig(input) {
|
|
|
172
172
|
const env = readEnv(input?.databaseId);
|
|
173
173
|
let cfgPath = {};
|
|
174
174
|
if (configPath) {
|
|
175
|
-
|
|
175
|
+
const cfgRes = await readConfigPath(configPath);
|
|
176
|
+
cfgPath = cfgRes.config;
|
|
176
177
|
}
|
|
177
178
|
const targetId = input?.databaseId ?? env.databaseId ?? cfgPath.databaseId;
|
|
178
179
|
let haveDbId = !!(input?.databaseId ?? env.databaseId ?? cfgPath.databaseId);
|
|
@@ -180,14 +181,16 @@ async function resolveConfig(input) {
|
|
|
180
181
|
let haveApiSecret = !!(input?.apiSecret ?? env.apiSecret ?? cfgPath.apiSecret);
|
|
181
182
|
let project = {};
|
|
182
183
|
if (!(haveDbId && haveApiKey && haveApiSecret)) {
|
|
183
|
-
|
|
184
|
+
const projRes = await readProjectFile(targetId);
|
|
185
|
+
project = projRes.config;
|
|
184
186
|
if (project.databaseId) haveDbId = true;
|
|
185
187
|
if (project.apiKey) haveApiKey = true;
|
|
186
188
|
if (project.apiSecret) haveApiSecret = true;
|
|
187
189
|
}
|
|
188
190
|
let home = {};
|
|
189
191
|
if (!(haveDbId && haveApiKey && haveApiSecret)) {
|
|
190
|
-
|
|
192
|
+
const homeRes = await readHomeProfile(targetId);
|
|
193
|
+
home = homeRes.config;
|
|
191
194
|
}
|
|
192
195
|
const merged = {
|
|
193
196
|
baseUrl: DEFAULT_BASE_URL,
|
|
@@ -831,6 +834,56 @@ var QueryResults = class extends Array {
|
|
|
831
834
|
}
|
|
832
835
|
};
|
|
833
836
|
|
|
837
|
+
// src/helpers/condition-normalizer.ts
|
|
838
|
+
function isQueryBuilderLike(value) {
|
|
839
|
+
return !!value && typeof value.toSerializableQueryObject === "function";
|
|
840
|
+
}
|
|
841
|
+
function normalizeCriteriaValue(value) {
|
|
842
|
+
if (Array.isArray(value)) {
|
|
843
|
+
let changed = false;
|
|
844
|
+
const normalized = value.map((item) => {
|
|
845
|
+
const result = normalizeCriteriaValue(item);
|
|
846
|
+
if (result.changed) changed = true;
|
|
847
|
+
return result.value;
|
|
848
|
+
});
|
|
849
|
+
if (!changed) {
|
|
850
|
+
for (let i = 0; i < normalized.length; i += 1) {
|
|
851
|
+
if (normalized[i] !== value[i]) {
|
|
852
|
+
changed = true;
|
|
853
|
+
break;
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
return { value: changed ? normalized : value, changed };
|
|
858
|
+
}
|
|
859
|
+
if (isQueryBuilderLike(value)) {
|
|
860
|
+
return { value: value.toSerializableQueryObject(), changed: true };
|
|
861
|
+
}
|
|
862
|
+
return { value, changed: false };
|
|
863
|
+
}
|
|
864
|
+
function normalizeConditionInternal(condition) {
|
|
865
|
+
if (condition.conditionType === "SingleCondition") {
|
|
866
|
+
const { value, changed: changed2 } = normalizeCriteriaValue(condition.criteria.value);
|
|
867
|
+
if (!changed2) return condition;
|
|
868
|
+
return {
|
|
869
|
+
...condition,
|
|
870
|
+
criteria: { ...condition.criteria, value }
|
|
871
|
+
};
|
|
872
|
+
}
|
|
873
|
+
let changed = false;
|
|
874
|
+
const normalizedConditions = condition.conditions.map((child) => {
|
|
875
|
+
const normalized = normalizeConditionInternal(child);
|
|
876
|
+
if (normalized !== child) changed = true;
|
|
877
|
+
return normalized;
|
|
878
|
+
});
|
|
879
|
+
if (!changed) return condition;
|
|
880
|
+
return { ...condition, conditions: normalizedConditions };
|
|
881
|
+
}
|
|
882
|
+
function normalizeCondition(condition) {
|
|
883
|
+
if (!condition) return condition;
|
|
884
|
+
return normalizeConditionInternal(condition);
|
|
885
|
+
}
|
|
886
|
+
|
|
834
887
|
// src/builders/cascade-relationship-builder.ts
|
|
835
888
|
var CascadeRelationshipBuilder = class {
|
|
836
889
|
graphName;
|
|
@@ -905,6 +958,194 @@ var OnyxError = class extends Error {
|
|
|
905
958
|
}
|
|
906
959
|
};
|
|
907
960
|
|
|
961
|
+
// src/helpers/schema-diff.ts
|
|
962
|
+
function mapByName(items) {
|
|
963
|
+
const map = /* @__PURE__ */ new Map();
|
|
964
|
+
for (const item of items ?? []) {
|
|
965
|
+
if (!item?.name) continue;
|
|
966
|
+
map.set(item.name, item);
|
|
967
|
+
}
|
|
968
|
+
return map;
|
|
969
|
+
}
|
|
970
|
+
function normalizeEntities(schema) {
|
|
971
|
+
if (Array.isArray(schema.entities)) {
|
|
972
|
+
return schema.entities ?? [];
|
|
973
|
+
}
|
|
974
|
+
const tables = schema.tables;
|
|
975
|
+
if (!Array.isArray(tables)) return [];
|
|
976
|
+
return tables.map((table) => ({
|
|
977
|
+
name: table.name,
|
|
978
|
+
attributes: table.attributes ?? []
|
|
979
|
+
}));
|
|
980
|
+
}
|
|
981
|
+
function normalizePartition(partition) {
|
|
982
|
+
if (partition == null) return "";
|
|
983
|
+
const trimmed = partition.trim();
|
|
984
|
+
return trimmed;
|
|
985
|
+
}
|
|
986
|
+
function identifiersEqual(a, b) {
|
|
987
|
+
if (!a && !b) return true;
|
|
988
|
+
if (!a || !b) return false;
|
|
989
|
+
return a.name === b.name && a.generator === b.generator && a.type === b.type;
|
|
990
|
+
}
|
|
991
|
+
function diffAttributes(apiAttrs, localAttrs) {
|
|
992
|
+
const apiMap = mapByName(apiAttrs);
|
|
993
|
+
const localMap = mapByName(localAttrs);
|
|
994
|
+
const added = [];
|
|
995
|
+
const removed = [];
|
|
996
|
+
const changed = [];
|
|
997
|
+
for (const [name, local] of localMap.entries()) {
|
|
998
|
+
if (!apiMap.has(name)) {
|
|
999
|
+
added.push(local);
|
|
1000
|
+
continue;
|
|
1001
|
+
}
|
|
1002
|
+
const api = apiMap.get(name);
|
|
1003
|
+
const apiNull = Boolean(api.isNullable);
|
|
1004
|
+
const localNull = Boolean(local.isNullable);
|
|
1005
|
+
if (api.type !== local.type || apiNull !== localNull) {
|
|
1006
|
+
changed.push({
|
|
1007
|
+
name,
|
|
1008
|
+
from: { type: api.type, isNullable: apiNull },
|
|
1009
|
+
to: { type: local.type, isNullable: localNull }
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
for (const name of apiMap.keys()) {
|
|
1014
|
+
if (!localMap.has(name)) removed.push(name);
|
|
1015
|
+
}
|
|
1016
|
+
added.sort((a, b) => a.name.localeCompare(b.name));
|
|
1017
|
+
removed.sort();
|
|
1018
|
+
changed.sort((a, b) => a.name.localeCompare(b.name));
|
|
1019
|
+
if (!added.length && !removed.length && !changed.length) return null;
|
|
1020
|
+
return { added, removed, changed };
|
|
1021
|
+
}
|
|
1022
|
+
function diffIndexes(apiIndexes, localIndexes) {
|
|
1023
|
+
const apiMap = mapByName(apiIndexes);
|
|
1024
|
+
const localMap = mapByName(localIndexes);
|
|
1025
|
+
const added = [];
|
|
1026
|
+
const removed = [];
|
|
1027
|
+
const changed = [];
|
|
1028
|
+
for (const [name, local] of localMap.entries()) {
|
|
1029
|
+
if (!apiMap.has(name)) {
|
|
1030
|
+
added.push(local);
|
|
1031
|
+
continue;
|
|
1032
|
+
}
|
|
1033
|
+
const api = apiMap.get(name);
|
|
1034
|
+
const apiType = api.type ?? "DEFAULT";
|
|
1035
|
+
const localType = local.type ?? "DEFAULT";
|
|
1036
|
+
const apiScore = api.minimumScore;
|
|
1037
|
+
const localScore = local.minimumScore;
|
|
1038
|
+
if (apiType !== localType || apiScore !== localScore) {
|
|
1039
|
+
changed.push({ name, from: api, to: local });
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
for (const name of apiMap.keys()) {
|
|
1043
|
+
if (!localMap.has(name)) removed.push(name);
|
|
1044
|
+
}
|
|
1045
|
+
added.sort((a, b) => a.name.localeCompare(b.name));
|
|
1046
|
+
removed.sort();
|
|
1047
|
+
changed.sort((a, b) => a.name.localeCompare(b.name));
|
|
1048
|
+
if (!added.length && !removed.length && !changed.length) return null;
|
|
1049
|
+
return { added, removed, changed };
|
|
1050
|
+
}
|
|
1051
|
+
function diffResolvers(apiResolvers, localResolvers) {
|
|
1052
|
+
const apiMap = mapByName(apiResolvers);
|
|
1053
|
+
const localMap = mapByName(localResolvers);
|
|
1054
|
+
const added = [];
|
|
1055
|
+
const removed = [];
|
|
1056
|
+
const changed = [];
|
|
1057
|
+
for (const [name, local] of localMap.entries()) {
|
|
1058
|
+
if (!apiMap.has(name)) {
|
|
1059
|
+
added.push(local);
|
|
1060
|
+
continue;
|
|
1061
|
+
}
|
|
1062
|
+
const api = apiMap.get(name);
|
|
1063
|
+
if (api.resolver !== local.resolver) {
|
|
1064
|
+
changed.push({ name, from: api, to: local });
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
for (const name of apiMap.keys()) {
|
|
1068
|
+
if (!localMap.has(name)) removed.push(name);
|
|
1069
|
+
}
|
|
1070
|
+
added.sort((a, b) => a.name.localeCompare(b.name));
|
|
1071
|
+
removed.sort();
|
|
1072
|
+
changed.sort((a, b) => a.name.localeCompare(b.name));
|
|
1073
|
+
if (!added.length && !removed.length && !changed.length) return null;
|
|
1074
|
+
return { added, removed, changed };
|
|
1075
|
+
}
|
|
1076
|
+
function diffTriggers(apiTriggers, localTriggers) {
|
|
1077
|
+
const apiMap = mapByName(apiTriggers);
|
|
1078
|
+
const localMap = mapByName(localTriggers);
|
|
1079
|
+
const added = [];
|
|
1080
|
+
const removed = [];
|
|
1081
|
+
const changed = [];
|
|
1082
|
+
for (const [name, local] of localMap.entries()) {
|
|
1083
|
+
if (!apiMap.has(name)) {
|
|
1084
|
+
added.push(local);
|
|
1085
|
+
continue;
|
|
1086
|
+
}
|
|
1087
|
+
const api = apiMap.get(name);
|
|
1088
|
+
if (api.event !== local.event || api.trigger !== local.trigger) {
|
|
1089
|
+
changed.push({ name, from: api, to: local });
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
for (const name of apiMap.keys()) {
|
|
1093
|
+
if (!localMap.has(name)) removed.push(name);
|
|
1094
|
+
}
|
|
1095
|
+
added.sort((a, b) => a.name.localeCompare(b.name));
|
|
1096
|
+
removed.sort();
|
|
1097
|
+
changed.sort((a, b) => a.name.localeCompare(b.name));
|
|
1098
|
+
if (!added.length && !removed.length && !changed.length) return null;
|
|
1099
|
+
return { added, removed, changed };
|
|
1100
|
+
}
|
|
1101
|
+
function computeSchemaDiff(apiSchema, localSchema) {
|
|
1102
|
+
const apiEntities = normalizeEntities(apiSchema);
|
|
1103
|
+
const localEntities = normalizeEntities(localSchema);
|
|
1104
|
+
const apiMap = mapByName(apiEntities);
|
|
1105
|
+
const localMap = mapByName(localEntities);
|
|
1106
|
+
const newTables = [];
|
|
1107
|
+
const removedTables = [];
|
|
1108
|
+
const changedTables = [];
|
|
1109
|
+
for (const [name, localEntity] of localMap.entries()) {
|
|
1110
|
+
if (!apiMap.has(name)) {
|
|
1111
|
+
newTables.push(name);
|
|
1112
|
+
continue;
|
|
1113
|
+
}
|
|
1114
|
+
const apiEntity = apiMap.get(name);
|
|
1115
|
+
const tableDiff = { name };
|
|
1116
|
+
const partitionFrom = normalizePartition(apiEntity.partition);
|
|
1117
|
+
const partitionTo = normalizePartition(localEntity.partition);
|
|
1118
|
+
if (partitionFrom !== partitionTo) {
|
|
1119
|
+
tableDiff.partition = { from: partitionFrom || null, to: partitionTo || null };
|
|
1120
|
+
}
|
|
1121
|
+
if (!identifiersEqual(apiEntity.identifier, localEntity.identifier)) {
|
|
1122
|
+
tableDiff.identifier = {
|
|
1123
|
+
from: apiEntity.identifier ?? null,
|
|
1124
|
+
to: localEntity.identifier ?? null
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
const attrs = diffAttributes(apiEntity.attributes, localEntity.attributes);
|
|
1128
|
+
if (attrs) tableDiff.attributes = attrs;
|
|
1129
|
+
const indexes = diffIndexes(apiEntity.indexes, localEntity.indexes);
|
|
1130
|
+
if (indexes) tableDiff.indexes = indexes;
|
|
1131
|
+
const resolvers = diffResolvers(apiEntity.resolvers, localEntity.resolvers);
|
|
1132
|
+
if (resolvers) tableDiff.resolvers = resolvers;
|
|
1133
|
+
const triggers = diffTriggers(apiEntity.triggers, localEntity.triggers);
|
|
1134
|
+
if (triggers) tableDiff.triggers = triggers;
|
|
1135
|
+
const hasChange = tableDiff.partition || tableDiff.identifier || tableDiff.attributes || tableDiff.indexes || tableDiff.resolvers || tableDiff.triggers;
|
|
1136
|
+
if (hasChange) {
|
|
1137
|
+
changedTables.push(tableDiff);
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
for (const name of apiMap.keys()) {
|
|
1141
|
+
if (!localMap.has(name)) removedTables.push(name);
|
|
1142
|
+
}
|
|
1143
|
+
newTables.sort();
|
|
1144
|
+
removedTables.sort();
|
|
1145
|
+
changedTables.sort((a, b) => a.name.localeCompare(b.name));
|
|
1146
|
+
return { newTables, removedTables, changedTables };
|
|
1147
|
+
}
|
|
1148
|
+
|
|
908
1149
|
// src/impl/onyx.ts
|
|
909
1150
|
var DEFAULT_CACHE_TTL = 5 * 60 * 1e3;
|
|
910
1151
|
var cachedCfg = null;
|
|
@@ -958,6 +1199,10 @@ function serializeDates(value) {
|
|
|
958
1199
|
}
|
|
959
1200
|
return value;
|
|
960
1201
|
}
|
|
1202
|
+
function stripEntityText(input) {
|
|
1203
|
+
const { entityText, ...rest } = input;
|
|
1204
|
+
return rest;
|
|
1205
|
+
}
|
|
961
1206
|
function normalizeSecretMetadata(input) {
|
|
962
1207
|
return { ...input, updatedAt: new Date(input.updatedAt) };
|
|
963
1208
|
}
|
|
@@ -971,7 +1216,20 @@ function normalizeDate(value) {
|
|
|
971
1216
|
return Number.isNaN(ts.getTime()) ? void 0 : ts;
|
|
972
1217
|
}
|
|
973
1218
|
function normalizeSchemaRevision(input, fallbackDatabaseId) {
|
|
974
|
-
const {
|
|
1219
|
+
const {
|
|
1220
|
+
meta,
|
|
1221
|
+
createdAt,
|
|
1222
|
+
publishedAt,
|
|
1223
|
+
revisionId,
|
|
1224
|
+
entityText,
|
|
1225
|
+
databaseId,
|
|
1226
|
+
entities,
|
|
1227
|
+
revisionDescription,
|
|
1228
|
+
...rest
|
|
1229
|
+
} = input;
|
|
1230
|
+
const dbId = typeof databaseId === "string" ? databaseId : fallbackDatabaseId;
|
|
1231
|
+
const entityList = Array.isArray(entities) ? entities : [];
|
|
1232
|
+
const revisionDesc = typeof revisionDescription === "string" ? revisionDescription : void 0;
|
|
975
1233
|
const mergedMeta = {
|
|
976
1234
|
revisionId: meta?.revisionId ?? revisionId,
|
|
977
1235
|
createdAt: normalizeDate(meta?.createdAt ?? createdAt),
|
|
@@ -979,10 +1237,11 @@ function normalizeSchemaRevision(input, fallbackDatabaseId) {
|
|
|
979
1237
|
};
|
|
980
1238
|
const cleanedMeta = mergedMeta.revisionId || mergedMeta.createdAt || mergedMeta.publishedAt ? mergedMeta : void 0;
|
|
981
1239
|
return {
|
|
982
|
-
|
|
983
|
-
|
|
1240
|
+
databaseId: dbId,
|
|
1241
|
+
revisionDescription: revisionDesc,
|
|
1242
|
+
entities: entityList,
|
|
984
1243
|
meta: cleanedMeta,
|
|
985
|
-
|
|
1244
|
+
...rest
|
|
986
1245
|
};
|
|
987
1246
|
}
|
|
988
1247
|
var OnyxDatabaseImpl = class {
|
|
@@ -1094,7 +1353,8 @@ var OnyxDatabaseImpl = class {
|
|
|
1094
1353
|
const path = `/data/${encodeURIComponent(databaseId)}/${encodeURIComponent(
|
|
1095
1354
|
table
|
|
1096
1355
|
)}/${encodeURIComponent(primaryKey)}${params.toString() ? `?${params.toString()}` : ""}`;
|
|
1097
|
-
|
|
1356
|
+
await http.request("DELETE", path);
|
|
1357
|
+
return true;
|
|
1098
1358
|
}
|
|
1099
1359
|
async saveDocument(doc) {
|
|
1100
1360
|
const { http, databaseId } = await this.ensureClient();
|
|
@@ -1137,20 +1397,32 @@ var OnyxDatabaseImpl = class {
|
|
|
1137
1397
|
const res = await http.request("GET", path);
|
|
1138
1398
|
return Array.isArray(res) ? res.map((entry) => normalizeSchemaRevision(entry, databaseId)) : [];
|
|
1139
1399
|
}
|
|
1400
|
+
async diffSchema(localSchema) {
|
|
1401
|
+
const apiSchema = await this.getSchema();
|
|
1402
|
+
return computeSchemaDiff(apiSchema, localSchema);
|
|
1403
|
+
}
|
|
1140
1404
|
async updateSchema(schema, options) {
|
|
1141
1405
|
const { http, databaseId } = await this.ensureClient();
|
|
1142
1406
|
const params = new URLSearchParams();
|
|
1143
1407
|
if (options?.publish) params.append("publish", "true");
|
|
1144
1408
|
const path = `/schemas/${encodeURIComponent(databaseId)}${params.size ? `?${params.toString()}` : ""}`;
|
|
1145
|
-
const body = { ...schema, databaseId: schema.databaseId ?? databaseId };
|
|
1146
|
-
const res = await http.request(
|
|
1409
|
+
const body = stripEntityText({ ...schema, databaseId: schema.databaseId ?? databaseId });
|
|
1410
|
+
const res = await http.request(
|
|
1411
|
+
"PUT",
|
|
1412
|
+
path,
|
|
1413
|
+
serializeDates(body)
|
|
1414
|
+
);
|
|
1147
1415
|
return normalizeSchemaRevision(res, databaseId);
|
|
1148
1416
|
}
|
|
1149
1417
|
async validateSchema(schema) {
|
|
1150
1418
|
const { http, databaseId } = await this.ensureClient();
|
|
1151
1419
|
const path = `/schemas/${encodeURIComponent(databaseId)}/validate`;
|
|
1152
|
-
const body = { ...schema, databaseId: schema.databaseId ?? databaseId };
|
|
1153
|
-
const res = await http.request(
|
|
1420
|
+
const body = stripEntityText({ ...schema, databaseId: schema.databaseId ?? databaseId });
|
|
1421
|
+
const res = await http.request(
|
|
1422
|
+
"POST",
|
|
1423
|
+
path,
|
|
1424
|
+
serializeDates(body)
|
|
1425
|
+
);
|
|
1154
1426
|
const normalizedSchema = res.schema ? normalizeSchemaRevision(res.schema, databaseId) : void 0;
|
|
1155
1427
|
return {
|
|
1156
1428
|
...res,
|
|
@@ -1312,11 +1584,14 @@ var QueryBuilderImpl = class {
|
|
|
1312
1584
|
if (!this.table) throw new Error("Table is not defined. Call from(<table>) first.");
|
|
1313
1585
|
return this.table;
|
|
1314
1586
|
}
|
|
1587
|
+
serializableConditions() {
|
|
1588
|
+
return normalizeCondition(this.conditions);
|
|
1589
|
+
}
|
|
1315
1590
|
toSelectQuery() {
|
|
1316
1591
|
return {
|
|
1317
1592
|
type: "SelectQuery",
|
|
1318
1593
|
fields: this.fields,
|
|
1319
|
-
conditions: this.
|
|
1594
|
+
conditions: this.serializableConditions(),
|
|
1320
1595
|
sort: this.sort,
|
|
1321
1596
|
limit: this.limitValue,
|
|
1322
1597
|
distinct: this.distinctValue,
|
|
@@ -1325,6 +1600,21 @@ var QueryBuilderImpl = class {
|
|
|
1325
1600
|
resolvers: this.resolvers
|
|
1326
1601
|
};
|
|
1327
1602
|
}
|
|
1603
|
+
toUpdateQuery() {
|
|
1604
|
+
return {
|
|
1605
|
+
type: "UpdateQuery",
|
|
1606
|
+
conditions: this.serializableConditions(),
|
|
1607
|
+
updates: this.updates ?? {},
|
|
1608
|
+
sort: this.sort,
|
|
1609
|
+
limit: this.limitValue,
|
|
1610
|
+
partition: this.partitionValue ?? null
|
|
1611
|
+
};
|
|
1612
|
+
}
|
|
1613
|
+
toSerializableQueryObject() {
|
|
1614
|
+
const table = this.ensureTable();
|
|
1615
|
+
const payload = this.mode === "update" ? this.toUpdateQuery() : this.toSelectQuery();
|
|
1616
|
+
return { ...payload, table };
|
|
1617
|
+
}
|
|
1328
1618
|
from(table) {
|
|
1329
1619
|
this.table = table;
|
|
1330
1620
|
return this;
|
|
@@ -1460,14 +1750,7 @@ var QueryBuilderImpl = class {
|
|
|
1460
1750
|
async update() {
|
|
1461
1751
|
if (this.mode !== "update") throw new Error("Call setUpdates(...) before update().");
|
|
1462
1752
|
const table = this.ensureTable();
|
|
1463
|
-
const update =
|
|
1464
|
-
type: "UpdateQuery",
|
|
1465
|
-
conditions: this.conditions,
|
|
1466
|
-
updates: this.updates ?? {},
|
|
1467
|
-
sort: this.sort,
|
|
1468
|
-
limit: this.limitValue,
|
|
1469
|
-
partition: this.partitionValue ?? null
|
|
1470
|
-
};
|
|
1753
|
+
const update = this.toUpdateQuery();
|
|
1471
1754
|
return this.db._update(table, update, this.partitionValue);
|
|
1472
1755
|
}
|
|
1473
1756
|
onItemAdded(listener) {
|
|
@@ -1680,12 +1963,20 @@ var ConditionBuilderImpl = class {
|
|
|
1680
1963
|
var c = (field, operator, value) => new ConditionBuilderImpl({ field, operator, value });
|
|
1681
1964
|
var eq = (field, value) => c(field, "EQUAL", value);
|
|
1682
1965
|
var neq = (field, value) => c(field, "NOT_EQUAL", value);
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
"IN",
|
|
1686
|
-
|
|
1687
|
-
)
|
|
1688
|
-
|
|
1966
|
+
function inOp(field, values) {
|
|
1967
|
+
const parsed = typeof values === "string" ? values.split(",").map((v) => v.trim()).filter((v) => v.length) : values;
|
|
1968
|
+
return c(field, "IN", parsed);
|
|
1969
|
+
}
|
|
1970
|
+
function within(field, values) {
|
|
1971
|
+
return inOp(field, values);
|
|
1972
|
+
}
|
|
1973
|
+
function notIn(field, values) {
|
|
1974
|
+
const parsed = typeof values === "string" ? values.split(",").map((v) => v.trim()).filter((v) => v.length) : values;
|
|
1975
|
+
return c(field, "NOT_IN", parsed);
|
|
1976
|
+
}
|
|
1977
|
+
function notWithin(field, values) {
|
|
1978
|
+
return notIn(field, values);
|
|
1979
|
+
}
|
|
1689
1980
|
var between = (field, lower2, upper2) => c(field, "BETWEEN", [lower2, upper2]);
|
|
1690
1981
|
var gt = (field, value) => c(field, "GREATER_THAN", value);
|
|
1691
1982
|
var gte = (field, value) => c(field, "GREATER_THAN_EQUAL", value);
|
|
@@ -1723,6 +2014,6 @@ var percentile = (attribute, p) => `percentile(${attribute}, ${p})`;
|
|
|
1723
2014
|
var sdkName = "@onyx.dev/onyx-database";
|
|
1724
2015
|
var sdkVersion = "0.1.0";
|
|
1725
2016
|
|
|
1726
|
-
export { QueryResults, asc, avg, between, contains, containsIgnoreCase, count, desc, eq, gt, gte, inOp, isNull, like, lower, lt, lte, matches, max, median, min, neq, notContains, notContainsIgnoreCase, notIn, notLike, notMatches, notNull, notStartsWith, onyx, percentile, replace, sdkName, sdkVersion, startsWith, std, substring, sum, upper, variance };
|
|
2017
|
+
export { QueryResults, asc, avg, between, contains, containsIgnoreCase, count, desc, eq, gt, gte, inOp, isNull, like, lower, lt, lte, matches, max, median, min, neq, notContains, notContainsIgnoreCase, notIn, notLike, notMatches, notNull, notStartsWith, notWithin, onyx, percentile, replace, sdkName, sdkVersion, startsWith, std, substring, sum, upper, variance, within };
|
|
1727
2018
|
//# sourceMappingURL=index.js.map
|
|
1728
2019
|
//# sourceMappingURL=index.js.map
|