remult 0.24.2-exp.0 → 0.25.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.
Files changed (46) hide show
  1. package/README.md +4 -4
  2. package/esm/index.js +2 -2
  3. package/esm/remult-mongo.js +1 -1
  4. package/esm/remult-sql-js.js +113 -0
  5. package/esm/src/data-providers/array-entity-data-provider.js +1 -1
  6. package/esm/src/data-providers/json-data-provider.js +1 -1
  7. package/esm/src/data-providers/{ipfs-data-provider.js → json-entity-opfs-storage.js} +1 -1
  8. package/esm/src/data-providers/sql-database.js +10 -6
  9. package/esm/src/filter/filter-consumer-bridge-to-sql-request.js +4 -5
  10. package/esm/src/remult-proxy.js +4 -0
  11. package/esm/src/remult3/Fields.js +22 -0
  12. package/esm/src/remult3/RepositoryImplementation.js +109 -60
  13. package/esm/src/remult3/entity.js +2 -2
  14. package/esm/src/remult3/getEntityRef.js +5 -5
  15. package/esm/src/remult3/relationInfoMember.js +1 -1
  16. package/esm/src/remult3/remult3.js +6 -7
  17. package/esm/src/server-action-info.js +1 -1
  18. package/esm/src/server-action.js +1 -1
  19. package/esm/src/validators.js +120 -28
  20. package/index.d.ts +3 -3
  21. package/index.js +8 -3
  22. package/package.json +6 -1
  23. package/remult-mongo.js +1 -1
  24. package/remult-sql-js.d.ts +18 -0
  25. package/remult-sql-js.js +211 -0
  26. package/src/column-interfaces.d.ts +12 -6
  27. package/src/data-providers/array-entity-data-provider.js +9 -10
  28. package/src/data-providers/json-data-provider.js +1 -1
  29. package/src/data-providers/{ipfs-data-provider.d.ts → json-entity-opfs-storage.d.ts} +1 -1
  30. package/src/data-providers/{ipfs-data-provider.js → json-entity-opfs-storage.js} +7 -7
  31. package/src/data-providers/sql-database.js +14 -6
  32. package/src/filter/filter-consumer-bridge-to-sql-request.js +4 -5
  33. package/src/remult-proxy.js +11 -0
  34. package/src/remult3/Fields.js +22 -0
  35. package/src/remult3/RepositoryImplementation.d.ts +6 -6
  36. package/src/remult3/RepositoryImplementation.js +136 -78
  37. package/src/remult3/entity.js +2 -2
  38. package/src/remult3/getEntityRef.js +5 -5
  39. package/src/remult3/relationInfoMember.js +1 -1
  40. package/src/remult3/remult3.d.ts +33 -0
  41. package/src/remult3/remult3.js +6 -7
  42. package/src/server-action-info.js +1 -1
  43. package/src/server-action.js +1 -1
  44. package/src/sql-command.d.ts +1 -0
  45. package/src/validators.d.ts +41 -9
  46. package/src/validators.js +183 -54
@@ -1,13 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- /*y1 - investigated sqlite in web, the chrome recommendation seemed hardly used -
4
- - https://developer.chrome.com/blog/sqlite-wasm-in-the-browser-backed-by-the-origin-private-file-system/
5
- - https://www.npmjs.com/package/@sqlite.org/sqlite-wasm
6
- All I could find is correspondance around evolu :) the guy I met in prague last year
7
- Indexed db was also annoying with it's upgrade process etc...
8
- I'm OPFS & json for now.
3
+ //y1 - discuss limit and include queries
4
+ //y1 - tried to upgrade vitest, nuxt tests are failing with loading uuid - sounds familiar?
9
5
  //y2 - from the crm-demo(https://crm-demo.up.railway.app/deals), after editing a deal: - _updateEntityBasedOnApi
10
-
11
6
  //y2 - Backend methods are transactions, it's not intuitive and maybe should be optional / opt in
12
7
  //y2 - how to run a transaction as a user
13
8
  //p1 - enum column
@@ -49,6 +44,8 @@ remult.apiClient.url='localhost:3007/api
49
44
 
50
45
 
51
46
  */
47
+ //p2 - document validators
48
+ //p2 - and validators to reference
52
49
  //y2 - discuss a default date formatter
53
50
  //y2 - add some api testing framework for user unit tests (will help with codesandbox based discussions)
54
51
  //y2 - live query refresh of view on table update
@@ -72,3 +69,5 @@ remult.apiClient.url='localhost:3007/api
72
69
  //[ ] V2 - what to do about for relations count?
73
70
  //[ ] V2 - condition? not to fetch if null etc....
74
71
  //[ ] V3 - all these fields will also appear in the where etc... in the typescript api - but we will not enforce them
72
+ //y2 - consider if field types should include validation in them by default (string,number that it's not NaN etc...) and if so, what message?
73
+ //y2 - should enforce integer - currently we probably round / truncate it
@@ -12,4 +12,4 @@ exports.actionInfo = {
12
12
  close: function () { },
13
13
  }); },
14
14
  };
15
- exports.serverActionField = Symbol('serverActionField');
15
+ exports.serverActionField = Symbol.for('serverActionField');
@@ -630,4 +630,4 @@ function prepareReceivedArgs(types, args, remult, ds, res) {
630
630
  });
631
631
  }
632
632
  exports.prepareReceivedArgs = prepareReceivedArgs;
633
- exports.classBackendMethodsArray = Symbol('classBackendMethodsArray');
633
+ exports.classBackendMethodsArray = Symbol.for('classBackendMethodsArray');
@@ -7,6 +7,7 @@ export interface SqlImplementation {
7
7
  ensureSchema?(entities: EntityMetadata[]): Promise<void>;
8
8
  supportsJsonColumnType?: boolean;
9
9
  wrapIdentifier?(name: string): string;
10
+ afterMutation?: VoidFunction;
10
11
  }
11
12
  export interface SqlCommand extends SqlCommandWithParameters {
12
13
  execute(sql: string): Promise<SqlResult>;
@@ -1,14 +1,46 @@
1
- import type { FieldRef } from './remult3/remult3.js';
1
+ import type { FieldValidator } from './column-interfaces.js';
2
+ import type { ValidateFieldEvent } from './remult3/remult3.js';
2
3
  export declare class Validators {
3
- static required: ((entity: any, col: FieldRef<any, string>, message?: any) => void) & {
4
- withMessage: (message: string) => (entity: any, col: FieldRef<any, string>) => void;
5
- defaultMessage: string;
4
+ static required: Validator<any>;
5
+ static unique: Validator<any>;
6
+ /**
7
+ * @deprecated is `unique` instead - it also runs only on the backend
8
+ */
9
+ static uniqueOnBackend: Validator<any>;
10
+ static regex: ValidatorWithArgs<string, RegExp> & {
11
+ defaultMessage: ValueValidationMessage<RegExp>;
6
12
  };
7
- static unique: ((entity: any, col: FieldRef<any, any>, message?: any) => Promise<void>) & {
8
- withMessage: (message: string) => (entity: any, col: FieldRef<any, any>) => Promise<void>;
9
- defaultMessage: string;
13
+ static email: Validator<string>;
14
+ static url: Validator<string>;
15
+ static in: <T>(value: readonly T[], withMessage?: ValueValidationMessage<T[]>) => FieldValidator<any, T> & {
16
+ withMessage: ValueValidationMessage<T[]>;
10
17
  };
11
- static uniqueOnBackend: ((entity: any, col: FieldRef<any, any>, message?: any) => Promise<void>) & {
12
- withMessage: (message: string) => (entity: any, col: FieldRef<any, any>) => Promise<void>;
18
+ static notNull: Validator<unknown>;
19
+ static enum: ValidatorWithArgs<any, any> & {
20
+ defaultMessage: ValueValidationMessage<any>;
13
21
  };
22
+ static relationExists: Validator<any>;
23
+ static maxLength: ValidatorWithArgs<string, number> & {
24
+ defaultMessage: ValueValidationMessage<number>;
25
+ };
26
+ static defaultMessage: string;
14
27
  }
28
+ export type Validator<valueType> = FieldValidator<any, valueType> & ((message?: ValidationMessage<valueType, undefined>) => FieldValidator<any, valueType>) & {
29
+ defaultMessage: ValidationMessage<valueType, undefined>;
30
+ /**
31
+ * @deprecated use (message:string) instead - for example: Validators.required("Is needed")
32
+ */
33
+ withMessage(message: ValidationMessage<valueType, undefined>): FieldValidator<any, valueType>;
34
+ };
35
+ export declare function createValidator<valueType>(validate: (entity: any, e: ValidateFieldEvent<any, valueType>) => Promise<boolean | string> | boolean | string, defaultMessage?: ValidationMessage<valueType, undefined>): Validator<valueType>;
36
+ export declare function valueValidator<valueType>(validate: (value: valueType) => boolean | string | Promise<boolean | string>, defaultMessage?: string): (entity: any, e: ValidateFieldEvent<any, valueType>) => string | boolean | Promise<string | boolean>;
37
+ export declare function createValueValidator<valueType>(validate: (value: valueType) => boolean | string | Promise<boolean | string>, defaultMessage?: ValidationMessage<valueType, undefined>): Validator<valueType>;
38
+ export declare function createValueValidatorWithArgs<valueType, argsType>(validate: (value: valueType, args: argsType) => boolean | string | Promise<boolean | string>, defaultMessage?: ValueValidationMessage<argsType>): ValidatorWithArgs<valueType, argsType> & {
39
+ defaultMessage: ValueValidationMessage<argsType>;
40
+ };
41
+ export type ValueValidationMessage<argsType> = string | ((args: argsType) => string);
42
+ export type ValidationMessage<valueType, argsType> = string | ((entity: any, event: ValidateFieldEvent<any, valueType>, args: argsType) => string);
43
+ export type ValidatorWithArgs<valueType, argsType> = (args: argsType, message?: ValidationMessage<valueType, argsType>) => FieldValidator<any, valueType>;
44
+ export declare function createValidatorWithArgs<valueType, argsType>(validate: (entity: any, e: ValidateFieldEvent<any, valueType>, args: argsType) => Promise<boolean | string> | boolean | string, defaultMessage: ValidationMessage<valueType, argsType>): ValidatorWithArgs<valueType, argsType> & {
45
+ defaultMessage: ValidationMessage<valueType, argsType>;
46
+ };
package/src/validators.js CHANGED
@@ -1,71 +1,200 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Validators = void 0;
3
+ exports.createValidatorWithArgs = exports.createValueValidatorWithArgs = exports.createValueValidator = exports.valueValidator = exports.createValidator = exports.Validators = void 0;
4
4
  var tslib_1 = require("tslib");
5
- var context_js_1 = require("./context.js");
6
5
  var Validators = /** @class */ (function () {
7
6
  function Validators() {
8
7
  }
9
8
  var _a;
10
9
  _a = Validators;
11
- Validators.required = Object.assign(function (entity, col, message) {
12
- if (message === void 0) { message = undefined; }
13
- if (!col.value ||
14
- (typeof col.value === 'string' && col.value.trim().length == 0))
15
- col.error = message || _a.required.defaultMessage;
16
- }, {
10
+ Validators.required = createValidator(function (_, e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(_a, function (_b) {
11
+ return [2 /*return*/, e.value != null && e.value != undefined && e.value !== '' && e.value != 0];
12
+ }); }); }, 'Should not be empty');
13
+ Validators.unique = createValidator(function (_, e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
14
+ var _b;
15
+ return tslib_1.__generator(_a, function (_c) {
16
+ switch (_c.label) {
17
+ case 0:
18
+ if (!e.entityRef)
19
+ throw 'unique validation may only work on columns that are attached to an entity';
20
+ if (!(e.isBackend() && (e.isNew || e.valueChanged()))) return [3 /*break*/, 2];
21
+ return [4 /*yield*/, e.entityRef.repository.count((_b = {},
22
+ _b[e.metadata.key] = e.value,
23
+ _b))];
24
+ case 1: return [2 /*return*/, ((_c.sent()) == 0)];
25
+ case 2: return [2 /*return*/, true];
26
+ }
27
+ });
28
+ }); }, 'already exists');
29
+ /**
30
+ * @deprecated is `unique` instead - it also runs only on the backend
31
+ */
32
+ Validators.uniqueOnBackend = createValidator(function (_, e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
33
+ var _b;
34
+ return tslib_1.__generator(_a, function (_c) {
35
+ switch (_c.label) {
36
+ case 0:
37
+ if (!(e.isBackend() && (e.isNew || e.valueChanged()))) return [3 /*break*/, 2];
38
+ return [4 /*yield*/, e.entityRef.repository.count((_b = {},
39
+ _b[e.metadata.key] = e.value,
40
+ _b))];
41
+ case 1: return [2 /*return*/, ((_c.sent()) == 0)];
42
+ case 2: return [2 /*return*/, true];
43
+ }
44
+ });
45
+ }); }, _a.unique.defaultMessage);
46
+ Validators.regex = createValueValidatorWithArgs(function (val, regex) {
47
+ return regex.test(val);
48
+ });
49
+ Validators.email = createValueValidator(function (val) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val); }, 'Invalid Email');
50
+ Validators.url = createValueValidator(function (val) { return !!new URL(val); }, 'Invalid Url');
51
+ Validators.in = createValueValidatorWithArgs(function (val, values) { return values.includes(val); }, function (values) { return "Value must be one of ".concat(values.join(', ')); });
52
+ Validators.notNull = createValueValidator(function (val) { return val != null; }, 'Should not be null');
53
+ Validators.enum = createValueValidatorWithArgs(function (value, enumObj) { return Object.values(enumObj).includes(value); }, function (enumObj) {
54
+ return "Value must be one of ".concat(Object.values(enumObj)
55
+ .filter(function (x) { return typeof enumObj[x] !== 'number'; })
56
+ .join(', '));
57
+ });
58
+ Validators.relationExists = createValidator(function (_, e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
59
+ var _b;
60
+ return tslib_1.__generator(_a, function (_c) {
61
+ switch (_c.label) {
62
+ case 0:
63
+ if (e.valueIsNull())
64
+ return [2 /*return*/, true];
65
+ if (!e.isBackend())
66
+ return [2 /*return*/, true];
67
+ _b = Boolean;
68
+ return [4 /*yield*/, e.load()];
69
+ case 1: return [2 /*return*/, _b.apply(void 0, [_c.sent()])];
70
+ }
71
+ });
72
+ }); }, 'Relation value does not exist');
73
+ Validators.maxLength = createValueValidatorWithArgs(function (val, maxLength) { return val.length <= maxLength; }, function (maxLength) { return "Value must be at most ".concat(maxLength, " characters"); });
74
+ Validators.defaultMessage = 'Invalid value';
75
+ return Validators;
76
+ }());
77
+ exports.Validators = Validators;
78
+ function createValidator(validate, defaultMessage) {
79
+ var _this = this;
80
+ var validation = function (entity, e, message) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
81
+ var valid;
82
+ return tslib_1.__generator(this, function (_b) {
83
+ switch (_b.label) {
84
+ case 0: return [4 /*yield*/, validate(entity, e)];
85
+ case 1:
86
+ valid = _b.sent();
87
+ if (typeof valid === 'string' && valid.length > 0)
88
+ e.error = valid;
89
+ else if (!valid)
90
+ e.error =
91
+ (typeof message === 'function' && message(entity, e, undefined)) ||
92
+ message ||
93
+ (typeof defaultMessage === 'function' &&
94
+ defaultMessage(entity, e, undefined)) ||
95
+ defaultMessage ||
96
+ Validators.defaultMessage;
97
+ return [2 /*return*/];
98
+ }
99
+ });
100
+ }); };
101
+ var result = function (entityOrMessage, e, message) {
102
+ if (typeof entityOrMessage === 'string' ||
103
+ entityOrMessage === 'function' ||
104
+ (entityOrMessage === undefined && e === undefined)) {
105
+ return function (entity, e, message) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_b) {
106
+ switch (_b.label) {
107
+ case 0: return [4 /*yield*/, validation(entity, e, entityOrMessage || message)];
108
+ case 1: return [2 /*return*/, _b.sent()];
109
+ }
110
+ }); }); };
111
+ }
112
+ return validation(entityOrMessage, e, message);
113
+ };
114
+ //@ts-ignore
115
+ return Object.assign(result, {
17
116
  withMessage: function (message) {
18
- return function (entity, col) {
19
- return _a.required(entity, col, message);
20
- };
117
+ return function (entity, e) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_b) {
118
+ return [2 /*return*/, result(entity, e, message)];
119
+ }); }); };
21
120
  },
22
- defaultMessage: 'Should not be empty',
121
+ get defaultMessage() {
122
+ return defaultMessage;
123
+ },
124
+ set defaultMessage(val) {
125
+ defaultMessage = val;
126
+ },
127
+ isValid: validate,
128
+ });
129
+ }
130
+ exports.createValidator = createValidator;
131
+ function valueValidator(validate, defaultMessage) {
132
+ return function (entity, e) {
133
+ return validate(e.value) || defaultMessage || false;
134
+ };
135
+ }
136
+ exports.valueValidator = valueValidator;
137
+ function createValueValidator(validate, defaultMessage) {
138
+ return createValidator(function (_, e) {
139
+ if (e.value === undefined || e.value === null)
140
+ return true;
141
+ return validate(e.value);
142
+ }, defaultMessage);
143
+ }
144
+ exports.createValueValidator = createValueValidator;
145
+ function createValueValidatorWithArgs(validate, defaultMessage) {
146
+ var result = createValidatorWithArgs(function (_, e, args) {
147
+ if (e.value === undefined || e.value === null)
148
+ return true;
149
+ return validate(e.value, args);
150
+ }, function (_, e, args) {
151
+ return (typeof defaultMessage === 'function' && defaultMessage(args)) ||
152
+ defaultMessage;
23
153
  });
24
- Validators.unique = Object.assign(function (entity, col, message) {
25
- if (message === void 0) { message = undefined; }
26
- return tslib_1.__awaiter(void 0, void 0, void 0, function () {
27
- var _b;
28
- return tslib_1.__generator(_a, function (_c) {
29
- switch (_c.label) {
30
- case 0:
31
- if (!col.entityRef)
32
- throw 'unique validation may only work on columns that are attached to an entity';
33
- if (!(col.entityRef.isNew() || col.valueChanged())) return [3 /*break*/, 2];
34
- return [4 /*yield*/, col.entityRef.repository.count((_b = {},
35
- _b[col.metadata.key] = col.value,
36
- _b))];
154
+ return Object.assign(function (entity, e) { return result(entity, e); }, {
155
+ get defaultMessage() {
156
+ return defaultMessage;
157
+ },
158
+ set defaultMessage(val) {
159
+ defaultMessage = val;
160
+ },
161
+ });
162
+ }
163
+ exports.createValueValidatorWithArgs = createValueValidatorWithArgs;
164
+ function createValidatorWithArgs(validate, defaultMessage) {
165
+ var _this = this;
166
+ var result = function (args, message) {
167
+ return function (entity, e) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
168
+ var valid;
169
+ return tslib_1.__generator(this, function (_b) {
170
+ switch (_b.label) {
171
+ case 0: return [4 /*yield*/, validate(entity, e, args)];
37
172
  case 1:
38
- if (_c.sent())
39
- col.error = message || _a.unique.defaultMessage;
40
- _c.label = 2;
41
- case 2: return [2 /*return*/];
173
+ valid = _b.sent();
174
+ if (typeof valid === 'string')
175
+ e.error = valid;
176
+ else if (!valid)
177
+ e.error =
178
+ message || defaultMessage
179
+ ? typeof defaultMessage === 'function'
180
+ ? defaultMessage(entity, e, args)
181
+ : defaultMessage
182
+ : Validators.defaultMessage;
183
+ return [2 /*return*/];
42
184
  }
43
185
  });
44
- });
45
- }, {
46
- withMessage: function (message) {
47
- return function (entity, col) {
48
- return _a.unique(entity, col, message);
49
- };
186
+ }); };
187
+ };
188
+ return Object.assign(result, {
189
+ get defaultMessage() {
190
+ return defaultMessage;
50
191
  },
51
- defaultMessage: 'already exists',
52
- });
53
- Validators.uniqueOnBackend = Object.assign(function (entity, col, message) {
54
- if (message === void 0) { message = undefined; }
55
- return tslib_1.__awaiter(void 0, void 0, void 0, function () {
56
- return tslib_1.__generator(_a, function (_b) {
57
- if ((0, context_js_1.isBackend)())
58
- return [2 /*return*/, _a.unique(entity, col, message)];
59
- return [2 /*return*/];
60
- });
61
- });
62
- }, {
63
- withMessage: function (message) {
64
- return function (entity, col) {
65
- return _a.uniqueOnBackend(entity, col, message);
66
- };
192
+ set defaultMessage(val) {
193
+ defaultMessage = val;
67
194
  },
68
195
  });
69
- return Validators;
70
- }());
71
- exports.Validators = Validators;
196
+ }
197
+ exports.createValidatorWithArgs = createValidatorWithArgs;
198
+ //y1 - talk about the 3rd parameter as message
199
+ //y1 - talk about the changed signature breaking existing validations and preventing the usage of unique outside the normal flow
200
+ //y1 - talk about the defaultMessage value now being a function that get's too many arguments, that makes it's reuse that much harder