rads-db 0.1.109 → 3.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 +111 -11
- package/dist/index.d.ts +48 -8
- package/dist/index.mjs +109 -11
- package/drivers/restApi.d.ts +1 -11
- package/features/cache.cjs +3 -1
- package/features/cache.mjs +6 -1
- package/fileUploadDrivers/supabaseStorage.cjs +4 -1
- package/fileUploadDrivers/supabaseStorage.d.ts +1 -0
- package/fileUploadDrivers/supabaseStorage.mjs +4 -1
- package/integrations/restEndpoints.cjs +3 -0
- package/integrations/restEndpoints.mjs +3 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -5,11 +5,13 @@ const _ = require('lodash');
|
|
|
5
5
|
const uuid = require('uuid');
|
|
6
6
|
const createMerge = require('@fastify/deepmerge');
|
|
7
7
|
const _radsDb = require('_rads-db');
|
|
8
|
+
const pluralize = require('pluralize');
|
|
8
9
|
|
|
9
10
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
10
11
|
|
|
11
12
|
const ___default = /*#__PURE__*/_interopDefaultCompat(_);
|
|
12
13
|
const createMerge__default = /*#__PURE__*/_interopDefaultCompat(createMerge);
|
|
14
|
+
const pluralize__default = /*#__PURE__*/_interopDefaultCompat(pluralize);
|
|
13
15
|
|
|
14
16
|
function generateValidators(schema) {
|
|
15
17
|
const zodSchemas = {};
|
|
@@ -316,7 +318,7 @@ const computedPresets = {
|
|
|
316
318
|
};
|
|
317
319
|
async function handlePrecomputed(context, docs, ctx) {
|
|
318
320
|
const { schema, typeName, options, db } = context;
|
|
319
|
-
if (options.
|
|
321
|
+
if (options.noComputed)
|
|
320
322
|
return;
|
|
321
323
|
const { precomputedFields, nestedTypeFields, fields } = schema[typeName];
|
|
322
324
|
const computed = options.computed;
|
|
@@ -352,7 +354,7 @@ async function handlePrecomputed(context, docs, ctx) {
|
|
|
352
354
|
}
|
|
353
355
|
async function handleComputed(context, data, ctx) {
|
|
354
356
|
const { schema, typeName, options, db } = context;
|
|
355
|
-
if (options.
|
|
357
|
+
if (options.noComputed)
|
|
356
358
|
return;
|
|
357
359
|
const { computedFields, nestedTypeFields, fields } = schema[typeName];
|
|
358
360
|
const computed = options.computed;
|
|
@@ -977,9 +979,10 @@ function getExistingDriverInstance(entityName, drivers) {
|
|
|
977
979
|
|
|
978
980
|
function generateMethods(schema, validators, options) {
|
|
979
981
|
const drivers = {};
|
|
980
|
-
const opts =
|
|
982
|
+
const opts = normalizeOptions(options);
|
|
981
983
|
const db = {
|
|
982
984
|
_schema: schema,
|
|
985
|
+
_radsUiSlots: getRadsUiSlots(opts.features),
|
|
983
986
|
uploadFile(args) {
|
|
984
987
|
if (!opts.fileUploadDriver)
|
|
985
988
|
throw new Error(`Missing configuration. Please specify "fileUploadDriver" argument in "createRads()".`);
|
|
@@ -990,7 +993,7 @@ function generateMethods(schema, validators, options) {
|
|
|
990
993
|
for (const key in schema) {
|
|
991
994
|
effects[key] = [];
|
|
992
995
|
}
|
|
993
|
-
if (!opts.
|
|
996
|
+
if (!opts.noComputed) {
|
|
994
997
|
verifyComputedPresense(schema, opts.computed, effects);
|
|
995
998
|
}
|
|
996
999
|
verifyEventSourcingSetup(schema, effects);
|
|
@@ -1023,13 +1026,18 @@ function generateMethods(schema, validators, options) {
|
|
|
1023
1026
|
}
|
|
1024
1027
|
return db;
|
|
1025
1028
|
}
|
|
1026
|
-
function getDriverInstance(schema, key, driver,
|
|
1027
|
-
if (!
|
|
1028
|
-
|
|
1029
|
+
function getDriverInstance(schema, key, driver, driverInstances) {
|
|
1030
|
+
if (!driverInstances[key]) {
|
|
1031
|
+
driverInstances[key] = getDriverInstanceInner(schema, key, driver);
|
|
1029
1032
|
}
|
|
1030
|
-
return
|
|
1033
|
+
return driverInstances[key];
|
|
1031
1034
|
}
|
|
1032
|
-
function getDriverInstanceInner(schema, key,
|
|
1035
|
+
function getDriverInstanceInner(schema, key, drivers) {
|
|
1036
|
+
const driverName = schema[key].decorators.entity.driver || "default";
|
|
1037
|
+
const driverConstructor = drivers[driverName];
|
|
1038
|
+
if (!driverConstructor) {
|
|
1039
|
+
throw new Error(`Missing driver "${driverName}" for entity "${key}"`);
|
|
1040
|
+
}
|
|
1033
1041
|
const driverInstance = driverConstructor(schema, key);
|
|
1034
1042
|
addDryRunSupport(driverInstance, key);
|
|
1035
1043
|
async function getAll(args, ctx) {
|
|
@@ -1109,6 +1117,86 @@ function addDryRunSupport(driverInstance, entity) {
|
|
|
1109
1117
|
return putMany(data, ctx);
|
|
1110
1118
|
};
|
|
1111
1119
|
}
|
|
1120
|
+
function getRadsUiSlots(features) {
|
|
1121
|
+
const result = {};
|
|
1122
|
+
if (!features)
|
|
1123
|
+
return result;
|
|
1124
|
+
for (const f of features) {
|
|
1125
|
+
if (!f.radsUiSlots)
|
|
1126
|
+
continue;
|
|
1127
|
+
for (const key in f.radsUiSlots) {
|
|
1128
|
+
const slotKey = key;
|
|
1129
|
+
if (!result[slotKey])
|
|
1130
|
+
result[slotKey] = f.radsUiSlots[slotKey];
|
|
1131
|
+
else
|
|
1132
|
+
result[slotKey] = [...f.radsUiSlots[slotKey], ...result[slotKey]];
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
return result;
|
|
1136
|
+
}
|
|
1137
|
+
function normalizeOptions(options) {
|
|
1138
|
+
let driver = options?.driver;
|
|
1139
|
+
if (!driver)
|
|
1140
|
+
driver = { default: memory() };
|
|
1141
|
+
if (typeof driver === "function")
|
|
1142
|
+
driver = { default: driver };
|
|
1143
|
+
return { computed: {}, features: [], ...options, driver };
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
const restApi = (options) => (schema, entity) => {
|
|
1147
|
+
const opts = {
|
|
1148
|
+
baseUrl: "/api",
|
|
1149
|
+
fetch: globalThis.fetch,
|
|
1150
|
+
getHeaders: () => {
|
|
1151
|
+
return void 0;
|
|
1152
|
+
},
|
|
1153
|
+
...options
|
|
1154
|
+
};
|
|
1155
|
+
const fetch = opts.fetch || global.fetch;
|
|
1156
|
+
async function fetchInner(...args) {
|
|
1157
|
+
const [url, fetchOptions] = args;
|
|
1158
|
+
const response = await fetch(url, fetchOptions);
|
|
1159
|
+
const responseJson = response.status === 204 ? null : await response.json();
|
|
1160
|
+
if (!response.ok) {
|
|
1161
|
+
const msg = responseJson?.message || responseJson?.statusMessage || response.statusText || "Server error.";
|
|
1162
|
+
const err = new Error(msg);
|
|
1163
|
+
if (responseJson?.code)
|
|
1164
|
+
err.code = responseJson?.code;
|
|
1165
|
+
err.fetchResponseJson = responseJson;
|
|
1166
|
+
throw err;
|
|
1167
|
+
}
|
|
1168
|
+
return responseJson;
|
|
1169
|
+
}
|
|
1170
|
+
const pluralEntityName = ___default.lowerFirst(pluralize__default(entity));
|
|
1171
|
+
const instance = {
|
|
1172
|
+
driverName: "restApi",
|
|
1173
|
+
async getMany(args) {
|
|
1174
|
+
args = args || {};
|
|
1175
|
+
const url = `${opts.baseUrl}/${pluralEntityName}`;
|
|
1176
|
+
const fetchOptions = {
|
|
1177
|
+
method: "POST",
|
|
1178
|
+
body: JSON.stringify(args),
|
|
1179
|
+
headers: { "Content-Type": "application/json" }
|
|
1180
|
+
};
|
|
1181
|
+
fetchOptions.headers = { ...fetchOptions.headers, ...opts.getHeaders(url, fetchOptions) };
|
|
1182
|
+
return await fetchInner(url, fetchOptions);
|
|
1183
|
+
},
|
|
1184
|
+
async putMany(item) {
|
|
1185
|
+
const url = `${opts.baseUrl}/${pluralEntityName}`;
|
|
1186
|
+
const fetchOptions = {
|
|
1187
|
+
method: "PUT",
|
|
1188
|
+
body: JSON.stringify({ data: item }),
|
|
1189
|
+
headers: { "Content-Type": "application/json" }
|
|
1190
|
+
};
|
|
1191
|
+
fetchOptions.headers = { ...fetchOptions.headers, ...opts.getHeaders(url, fetchOptions) };
|
|
1192
|
+
return await fetchInner(url, fetchOptions);
|
|
1193
|
+
},
|
|
1194
|
+
async verifyMany(args, ctx) {
|
|
1195
|
+
throw new Error('Calling "verifyMany" via rest api is not supported for security reasons.');
|
|
1196
|
+
}
|
|
1197
|
+
};
|
|
1198
|
+
return instance;
|
|
1199
|
+
};
|
|
1112
1200
|
|
|
1113
1201
|
function entity(meta) {
|
|
1114
1202
|
return function(classConstructor, _ctx) {
|
|
@@ -1135,15 +1223,27 @@ function computed(meta) {
|
|
|
1135
1223
|
};
|
|
1136
1224
|
}
|
|
1137
1225
|
|
|
1138
|
-
function
|
|
1226
|
+
function createRadsDb(args) {
|
|
1139
1227
|
args = { ...args };
|
|
1140
1228
|
const s = args.schema || _radsDb.schema;
|
|
1141
1229
|
const validators = generateValidators(s);
|
|
1142
1230
|
return generateMethods(s, validators, args);
|
|
1143
1231
|
}
|
|
1232
|
+
function createRadsDbClient(args) {
|
|
1233
|
+
const radsDbArgs = {
|
|
1234
|
+
...args,
|
|
1235
|
+
noComputed: true,
|
|
1236
|
+
noCustomDrivers: true,
|
|
1237
|
+
driver: restApi(args?.driver)
|
|
1238
|
+
};
|
|
1239
|
+
const s = radsDbArgs.schema || _radsDb.schema;
|
|
1240
|
+
const validators = generateValidators(s);
|
|
1241
|
+
return generateMethods(s, validators, radsDbArgs);
|
|
1242
|
+
}
|
|
1144
1243
|
|
|
1145
1244
|
exports.computed = computed;
|
|
1146
|
-
exports.
|
|
1245
|
+
exports.createRadsDb = createRadsDb;
|
|
1246
|
+
exports.createRadsDbClient = createRadsDbClient;
|
|
1147
1247
|
exports.entity = entity;
|
|
1148
1248
|
exports.field = field;
|
|
1149
1249
|
exports.getDriverInstance = getDriverInstance;
|
package/dist/index.d.ts
CHANGED
|
@@ -150,12 +150,26 @@ interface FieldDecoratorArgs {
|
|
|
150
150
|
relation?: Function;
|
|
151
151
|
}
|
|
152
152
|
type DriverConstructor = (schema: Schema, entity: string) => MinimalDriver;
|
|
153
|
-
interface
|
|
153
|
+
interface CreateRadsArgsDrivers {
|
|
154
|
+
default: DriverConstructor;
|
|
155
|
+
[driverName: string]: DriverConstructor;
|
|
156
|
+
}
|
|
157
|
+
interface RestDriverOptions {
|
|
158
|
+
/** @default '/api' */
|
|
159
|
+
baseUrl?: string;
|
|
160
|
+
fetch?: (url: string, options?: {
|
|
161
|
+
body?: any;
|
|
162
|
+
headers?: any;
|
|
163
|
+
method?: string;
|
|
164
|
+
}) => any;
|
|
165
|
+
getHeaders?: (url: string, fetchOptions: RequestInit) => Record<string, string> | undefined;
|
|
166
|
+
}
|
|
167
|
+
interface CreateRadsDbArgs {
|
|
154
168
|
schema?: Schema;
|
|
155
|
-
driver?: DriverConstructor;
|
|
156
|
-
drivers?: Record<string, DriverConstructor>;
|
|
169
|
+
driver?: DriverConstructor | CreateRadsArgsDrivers;
|
|
157
170
|
fileUploadDriver?: FileUploadDriver;
|
|
158
|
-
|
|
171
|
+
noComputed?: boolean;
|
|
172
|
+
noCustomDrivers?: boolean;
|
|
159
173
|
computed?: Record<string, Record<string, Function> | {
|
|
160
174
|
_entity?: {
|
|
161
175
|
compute: Function;
|
|
@@ -166,6 +180,9 @@ interface CreateRadsArgs {
|
|
|
166
180
|
context?: Record<string, any> & Omit<RadsRequestContext, 'dryRun' | 'silent'>;
|
|
167
181
|
features?: RadsFeature[];
|
|
168
182
|
}
|
|
183
|
+
type CreateRadsDbClientArgs = Omit<CreateRadsDbArgs, 'noComputed' | 'noCustomDrivers' | 'driver'> & {
|
|
184
|
+
driver?: RestDriverOptions;
|
|
185
|
+
};
|
|
169
186
|
type Schema = Record<string, TypeDefinition>;
|
|
170
187
|
type SchemaValidators = Record<string, (item: any) => any>;
|
|
171
188
|
interface TypeDefinition {
|
|
@@ -238,7 +255,7 @@ interface ComputedContextGlobal {
|
|
|
238
255
|
db: RadsDb;
|
|
239
256
|
schema: Schema;
|
|
240
257
|
validators: SchemaValidators;
|
|
241
|
-
options:
|
|
258
|
+
options: CreateRadsDbArgs;
|
|
242
259
|
drivers: Record<string, Driver>;
|
|
243
260
|
effects: Record<string, PutEffect[]>;
|
|
244
261
|
}
|
|
@@ -304,8 +321,21 @@ interface FileSystemNode {
|
|
|
304
321
|
size?: number;
|
|
305
322
|
type: 'blob' | 'tree';
|
|
306
323
|
}
|
|
324
|
+
type RadsUiSlotName = 'entityListActions' | 'entityActions' | 'fieldActions';
|
|
325
|
+
interface RadsUiSlotDefinition {
|
|
326
|
+
entityName?: string | {
|
|
327
|
+
regExp: string;
|
|
328
|
+
};
|
|
329
|
+
fieldName?: string | {
|
|
330
|
+
regExp: string;
|
|
331
|
+
};
|
|
332
|
+
label: string;
|
|
333
|
+
icon?: string;
|
|
334
|
+
endpoint: string;
|
|
335
|
+
}
|
|
307
336
|
interface RadsFeature {
|
|
308
337
|
name: string;
|
|
338
|
+
radsUiSlots?: Record<RadsUiSlotName, RadsUiSlotDefinition[]>;
|
|
309
339
|
init?: (db: Record<string, any>, context: ComputedContextGlobal) => void;
|
|
310
340
|
enhanceEntityMethods?: (context: ComputedContext, entityMethodsObj: EntityMethods<any, any, any>) => void;
|
|
311
341
|
beforeGet?: (args: GetArgsAny, ctx: RadsRequestContext, context: ComputedContext) => MaybePromise<any>;
|
|
@@ -327,8 +357,18 @@ declare function field(meta?: FieldDecoratorArgs): (a: any, b?: ClassFieldDecora
|
|
|
327
357
|
declare function precomputed(meta?: ComputedDecoratorArgs): (a: any, b?: ClassFieldDecoratorContext | ClassDecoratorContext) => void;
|
|
328
358
|
declare function computed(meta?: ComputedDecoratorArgs): (a: any, b?: ClassFieldDecoratorContext | ClassDecoratorContext) => void;
|
|
329
359
|
|
|
330
|
-
declare function getDriverInstance(schema: Schema, key: string, driver:
|
|
360
|
+
declare function getDriverInstance(schema: Schema, key: string, driver: CreateRadsArgsDrivers, driverInstances: Record<string, Driver>): Driver;
|
|
331
361
|
|
|
332
|
-
|
|
362
|
+
/**
|
|
363
|
+
* Creates instance of rads db - object that provides access to all entities.
|
|
364
|
+
* Intended to be used on the backend. On the client you may want to opt for "createRadsDbClient" instead.
|
|
365
|
+
* */
|
|
366
|
+
declare function createRadsDb(args?: CreateRadsDbArgs): RadsDb;
|
|
367
|
+
/**
|
|
368
|
+
* Creates instance of rads db client - object that provides access to all entities.
|
|
369
|
+
* Intended to be used on the frontend. Uses "fetch" to communicate with the server's API.
|
|
370
|
+
* It is a shortcut for `createRadsDb({ driver: radsRestApi(), noComputed: true, noCustomDrivers: true })`
|
|
371
|
+
*/
|
|
372
|
+
declare function createRadsDbClient(args?: CreateRadsDbClientArgs): RadsDb;
|
|
333
373
|
|
|
334
|
-
export { Change, ComputedContext, ComputedContextGlobal, ComputedDecoratorArgs,
|
|
374
|
+
export { Change, ComputedContext, ComputedContextGlobal, ComputedDecoratorArgs, CreateRadsArgsDrivers, CreateRadsDbArgs, CreateRadsDbClientArgs, DeepPartial, Driver, DriverConstructor, EntityDecoratorArgs, EntityMethods, FieldDecoratorArgs, FieldDefinition, FileSystemNode, FileUploadArgs, FileUploadDriver, GenerateClientNormalizedOptions, GenerateClientOptions, GetAggArgs, GetAggArgsAgg, GetAggArgsAny, GetAggResponse, GetArgs, GetArgsAny, GetArgsInclude, GetManyArgs, GetManyArgsAny, GetManyResponse, GetResponse, GetResponseInclude, GetResponseIncludeSelect, GetResponseNoInclude, GetRestRoutesArgs, GetRestRoutesOptions, GetRestRoutesResponse, MinimalDriver, PutArgs, PutEffect, RadsFeature, RadsRequestContext, RadsUiSlotDefinition, RadsUiSlotName, RadsVitePluginOptions, Relation, RestDriverOptions, RestFileUploadDriverOptions, Schema, SchemaValidators, TypeDefinition, UiDecoratorArgs, UiFieldDecoratorArgs, ValidateEntityDecoratorArgs, ValidateFieldDecoratorArgs, ValidateStringDecoratorArgs, VerifyManyArgs, VerifyManyArgsAny, VerifyManyResponse, computed, createRadsDb, createRadsDbClient, entity, field, getDriverInstance, precomputed, ui, validate };
|
package/dist/index.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import _ from 'lodash';
|
|
|
3
3
|
import { v4 } from 'uuid';
|
|
4
4
|
import createMerge from '@fastify/deepmerge';
|
|
5
5
|
import { schema } from '_rads-db';
|
|
6
|
+
import pluralize from 'pluralize';
|
|
6
7
|
|
|
7
8
|
function generateValidators(schema) {
|
|
8
9
|
const zodSchemas = {};
|
|
@@ -309,7 +310,7 @@ const computedPresets = {
|
|
|
309
310
|
};
|
|
310
311
|
async function handlePrecomputed(context, docs, ctx) {
|
|
311
312
|
const { schema, typeName, options, db } = context;
|
|
312
|
-
if (options.
|
|
313
|
+
if (options.noComputed)
|
|
313
314
|
return;
|
|
314
315
|
const { precomputedFields, nestedTypeFields, fields } = schema[typeName];
|
|
315
316
|
const computed = options.computed;
|
|
@@ -345,7 +346,7 @@ async function handlePrecomputed(context, docs, ctx) {
|
|
|
345
346
|
}
|
|
346
347
|
async function handleComputed(context, data, ctx) {
|
|
347
348
|
const { schema, typeName, options, db } = context;
|
|
348
|
-
if (options.
|
|
349
|
+
if (options.noComputed)
|
|
349
350
|
return;
|
|
350
351
|
const { computedFields, nestedTypeFields, fields } = schema[typeName];
|
|
351
352
|
const computed = options.computed;
|
|
@@ -970,9 +971,10 @@ function getExistingDriverInstance(entityName, drivers) {
|
|
|
970
971
|
|
|
971
972
|
function generateMethods(schema, validators, options) {
|
|
972
973
|
const drivers = {};
|
|
973
|
-
const opts =
|
|
974
|
+
const opts = normalizeOptions(options);
|
|
974
975
|
const db = {
|
|
975
976
|
_schema: schema,
|
|
977
|
+
_radsUiSlots: getRadsUiSlots(opts.features),
|
|
976
978
|
uploadFile(args) {
|
|
977
979
|
if (!opts.fileUploadDriver)
|
|
978
980
|
throw new Error(`Missing configuration. Please specify "fileUploadDriver" argument in "createRads()".`);
|
|
@@ -983,7 +985,7 @@ function generateMethods(schema, validators, options) {
|
|
|
983
985
|
for (const key in schema) {
|
|
984
986
|
effects[key] = [];
|
|
985
987
|
}
|
|
986
|
-
if (!opts.
|
|
988
|
+
if (!opts.noComputed) {
|
|
987
989
|
verifyComputedPresense(schema, opts.computed, effects);
|
|
988
990
|
}
|
|
989
991
|
verifyEventSourcingSetup(schema, effects);
|
|
@@ -1016,13 +1018,18 @@ function generateMethods(schema, validators, options) {
|
|
|
1016
1018
|
}
|
|
1017
1019
|
return db;
|
|
1018
1020
|
}
|
|
1019
|
-
function getDriverInstance(schema, key, driver,
|
|
1020
|
-
if (!
|
|
1021
|
-
|
|
1021
|
+
function getDriverInstance(schema, key, driver, driverInstances) {
|
|
1022
|
+
if (!driverInstances[key]) {
|
|
1023
|
+
driverInstances[key] = getDriverInstanceInner(schema, key, driver);
|
|
1022
1024
|
}
|
|
1023
|
-
return
|
|
1025
|
+
return driverInstances[key];
|
|
1024
1026
|
}
|
|
1025
|
-
function getDriverInstanceInner(schema, key,
|
|
1027
|
+
function getDriverInstanceInner(schema, key, drivers) {
|
|
1028
|
+
const driverName = schema[key].decorators.entity.driver || "default";
|
|
1029
|
+
const driverConstructor = drivers[driverName];
|
|
1030
|
+
if (!driverConstructor) {
|
|
1031
|
+
throw new Error(`Missing driver "${driverName}" for entity "${key}"`);
|
|
1032
|
+
}
|
|
1026
1033
|
const driverInstance = driverConstructor(schema, key);
|
|
1027
1034
|
addDryRunSupport(driverInstance, key);
|
|
1028
1035
|
async function getAll(args, ctx) {
|
|
@@ -1102,6 +1109,86 @@ function addDryRunSupport(driverInstance, entity) {
|
|
|
1102
1109
|
return putMany(data, ctx);
|
|
1103
1110
|
};
|
|
1104
1111
|
}
|
|
1112
|
+
function getRadsUiSlots(features) {
|
|
1113
|
+
const result = {};
|
|
1114
|
+
if (!features)
|
|
1115
|
+
return result;
|
|
1116
|
+
for (const f of features) {
|
|
1117
|
+
if (!f.radsUiSlots)
|
|
1118
|
+
continue;
|
|
1119
|
+
for (const key in f.radsUiSlots) {
|
|
1120
|
+
const slotKey = key;
|
|
1121
|
+
if (!result[slotKey])
|
|
1122
|
+
result[slotKey] = f.radsUiSlots[slotKey];
|
|
1123
|
+
else
|
|
1124
|
+
result[slotKey] = [...f.radsUiSlots[slotKey], ...result[slotKey]];
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
return result;
|
|
1128
|
+
}
|
|
1129
|
+
function normalizeOptions(options) {
|
|
1130
|
+
let driver = options?.driver;
|
|
1131
|
+
if (!driver)
|
|
1132
|
+
driver = { default: memory() };
|
|
1133
|
+
if (typeof driver === "function")
|
|
1134
|
+
driver = { default: driver };
|
|
1135
|
+
return { computed: {}, features: [], ...options, driver };
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
const restApi = (options) => (schema, entity) => {
|
|
1139
|
+
const opts = {
|
|
1140
|
+
baseUrl: "/api",
|
|
1141
|
+
fetch: globalThis.fetch,
|
|
1142
|
+
getHeaders: () => {
|
|
1143
|
+
return void 0;
|
|
1144
|
+
},
|
|
1145
|
+
...options
|
|
1146
|
+
};
|
|
1147
|
+
const fetch = opts.fetch || global.fetch;
|
|
1148
|
+
async function fetchInner(...args) {
|
|
1149
|
+
const [url, fetchOptions] = args;
|
|
1150
|
+
const response = await fetch(url, fetchOptions);
|
|
1151
|
+
const responseJson = response.status === 204 ? null : await response.json();
|
|
1152
|
+
if (!response.ok) {
|
|
1153
|
+
const msg = responseJson?.message || responseJson?.statusMessage || response.statusText || "Server error.";
|
|
1154
|
+
const err = new Error(msg);
|
|
1155
|
+
if (responseJson?.code)
|
|
1156
|
+
err.code = responseJson?.code;
|
|
1157
|
+
err.fetchResponseJson = responseJson;
|
|
1158
|
+
throw err;
|
|
1159
|
+
}
|
|
1160
|
+
return responseJson;
|
|
1161
|
+
}
|
|
1162
|
+
const pluralEntityName = _.lowerFirst(pluralize(entity));
|
|
1163
|
+
const instance = {
|
|
1164
|
+
driverName: "restApi",
|
|
1165
|
+
async getMany(args) {
|
|
1166
|
+
args = args || {};
|
|
1167
|
+
const url = `${opts.baseUrl}/${pluralEntityName}`;
|
|
1168
|
+
const fetchOptions = {
|
|
1169
|
+
method: "POST",
|
|
1170
|
+
body: JSON.stringify(args),
|
|
1171
|
+
headers: { "Content-Type": "application/json" }
|
|
1172
|
+
};
|
|
1173
|
+
fetchOptions.headers = { ...fetchOptions.headers, ...opts.getHeaders(url, fetchOptions) };
|
|
1174
|
+
return await fetchInner(url, fetchOptions);
|
|
1175
|
+
},
|
|
1176
|
+
async putMany(item) {
|
|
1177
|
+
const url = `${opts.baseUrl}/${pluralEntityName}`;
|
|
1178
|
+
const fetchOptions = {
|
|
1179
|
+
method: "PUT",
|
|
1180
|
+
body: JSON.stringify({ data: item }),
|
|
1181
|
+
headers: { "Content-Type": "application/json" }
|
|
1182
|
+
};
|
|
1183
|
+
fetchOptions.headers = { ...fetchOptions.headers, ...opts.getHeaders(url, fetchOptions) };
|
|
1184
|
+
return await fetchInner(url, fetchOptions);
|
|
1185
|
+
},
|
|
1186
|
+
async verifyMany(args, ctx) {
|
|
1187
|
+
throw new Error('Calling "verifyMany" via rest api is not supported for security reasons.');
|
|
1188
|
+
}
|
|
1189
|
+
};
|
|
1190
|
+
return instance;
|
|
1191
|
+
};
|
|
1105
1192
|
|
|
1106
1193
|
function entity(meta) {
|
|
1107
1194
|
return function(classConstructor, _ctx) {
|
|
@@ -1128,11 +1215,22 @@ function computed(meta) {
|
|
|
1128
1215
|
};
|
|
1129
1216
|
}
|
|
1130
1217
|
|
|
1131
|
-
function
|
|
1218
|
+
function createRadsDb(args) {
|
|
1132
1219
|
args = { ...args };
|
|
1133
1220
|
const s = args.schema || schema;
|
|
1134
1221
|
const validators = generateValidators(s);
|
|
1135
1222
|
return generateMethods(s, validators, args);
|
|
1136
1223
|
}
|
|
1224
|
+
function createRadsDbClient(args) {
|
|
1225
|
+
const radsDbArgs = {
|
|
1226
|
+
...args,
|
|
1227
|
+
noComputed: true,
|
|
1228
|
+
noCustomDrivers: true,
|
|
1229
|
+
driver: restApi(args?.driver)
|
|
1230
|
+
};
|
|
1231
|
+
const s = radsDbArgs.schema || schema;
|
|
1232
|
+
const validators = generateValidators(s);
|
|
1233
|
+
return generateMethods(s, validators, radsDbArgs);
|
|
1234
|
+
}
|
|
1137
1235
|
|
|
1138
|
-
export { computed,
|
|
1236
|
+
export { computed, createRadsDb, createRadsDbClient, entity, field, getDriverInstance, precomputed, ui, validate };
|
package/drivers/restApi.d.ts
CHANGED
|
@@ -1,12 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
/** @default '/api' */
|
|
3
|
-
baseUrl?: string;
|
|
4
|
-
fetch?: (url: string, options?: {
|
|
5
|
-
body?: any;
|
|
6
|
-
headers?: any;
|
|
7
|
-
method?: string;
|
|
8
|
-
}) => any;
|
|
9
|
-
getHeaders?: (url: string, fetchOptions: RequestInit) => Record<string, string> | undefined;
|
|
10
|
-
}
|
|
11
|
-
declare const _default: (options: RestDriverOptions) => (schema: Schema, entity: string) => MinimalDriver;
|
|
1
|
+
declare const _default: (options?: any) => (schema: Schema, entity: string) => MinimalDriver;
|
|
12
2
|
export default _default;
|
package/features/cache.cjs
CHANGED
|
@@ -56,7 +56,9 @@ var _default = options => {
|
|
|
56
56
|
handle: cacheEntityName,
|
|
57
57
|
handlePlural: `${cacheEntityName}s`
|
|
58
58
|
};
|
|
59
|
-
cacheDrivers[handle] = (0, _radsDb.getDriverInstance)(schema, cacheEntityName,
|
|
59
|
+
cacheDrivers[handle] = (0, _radsDb.getDriverInstance)(schema, cacheEntityName, {
|
|
60
|
+
default: normalizedOptions[typeName].driver
|
|
61
|
+
}, drivers);
|
|
60
62
|
cacheStatus[handle] = {
|
|
61
63
|
preloadStatus: normalizedOptions[typeName].preload ? "neverLoaded" : void 0
|
|
62
64
|
};
|
package/features/cache.mjs
CHANGED
|
@@ -38,7 +38,12 @@ export default (options) => {
|
|
|
38
38
|
handle: cacheEntityName,
|
|
39
39
|
handlePlural: `${cacheEntityName}s`
|
|
40
40
|
};
|
|
41
|
-
cacheDrivers[handle] = getDriverInstance(
|
|
41
|
+
cacheDrivers[handle] = getDriverInstance(
|
|
42
|
+
schema,
|
|
43
|
+
cacheEntityName,
|
|
44
|
+
{ default: normalizedOptions[typeName].driver },
|
|
45
|
+
drivers
|
|
46
|
+
);
|
|
42
47
|
cacheStatus[handle] = { preloadStatus: normalizedOptions[typeName].preload ? "neverLoaded" : void 0 };
|
|
43
48
|
}
|
|
44
49
|
db.cache = {
|
|
@@ -17,7 +17,10 @@ var _default = options => {
|
|
|
17
17
|
const {
|
|
18
18
|
data,
|
|
19
19
|
error
|
|
20
|
-
} = await bucketClient.upload(args.fileName, args.blob, {
|
|
20
|
+
} = await bucketClient.upload(args.fileName, args.blob, {
|
|
21
|
+
...options.uploadOptions,
|
|
22
|
+
...args.options
|
|
23
|
+
});
|
|
21
24
|
if (error) throw error;
|
|
22
25
|
const resultPath = data?.path;
|
|
23
26
|
if (!resultPath) return {
|
|
@@ -7,7 +7,10 @@ export default (options) => {
|
|
|
7
7
|
driverName: "supabase",
|
|
8
8
|
async uploadFile(args) {
|
|
9
9
|
const bucketClient = supabase.storage.from(args.containerName || defaultBucket);
|
|
10
|
-
const { data, error } = await bucketClient.upload(args.fileName, args.blob, {
|
|
10
|
+
const { data, error } = await bucketClient.upload(args.fileName, args.blob, {
|
|
11
|
+
...options.uploadOptions,
|
|
12
|
+
...args.options
|
|
13
|
+
});
|
|
11
14
|
if (error)
|
|
12
15
|
throw error;
|
|
13
16
|
const resultPath = data?.path;
|