remult 0.14.33 → 0.15.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/README.md +5 -3
- package/esm/postgres/postgresColumnSyntax.d.ts +2 -0
- package/esm/postgres/postgresColumnSyntax.js +30 -0
- package/esm/remult-express.js +2 -2
- package/esm/remult-fastify.js +2 -2
- package/esm/remult-fresh.d.ts +8 -3
- package/esm/remult-fresh.js +8 -7
- package/esm/remult-middleware.d.ts +2 -0
- package/esm/remult-middleware.js +65 -0
- package/esm/server/JsonEntityFileStorage.d.ts +4 -1
- package/esm/server/JsonEntityFileStorage.js +4 -0
- package/esm/server/expressBridge.d.ts +1 -1
- package/esm/server/expressBridge.js +3 -5
- package/esm/server/index.d.ts +2 -2
- package/esm/server/index.js +2 -2
- package/esm/server/register-actions-on-server.d.ts +3 -0
- package/esm/server/register-actions-on-server.js +24 -0
- package/esm/server/register-entities-on-server.d.ts +3 -0
- package/esm/server/register-entities-on-server.js +15 -0
- package/esm/src/backend-tests/test-http-server.backend-spec.js +37 -7
- package/esm/valueConverters.d.ts +12 -0
- package/esm/valueConverters.js +169 -0
- package/package.json +65 -64
- package/postgres/postgresColumnSyntax.d.ts +2 -0
- package/postgres/postgresColumnSyntax.js +34 -0
- package/remult-express.js +1 -1
- package/remult-fastify.js +1 -1
- package/remult-fresh.d.ts +8 -3
- package/remult-fresh.js +7 -6
- package/remult-middleware.d.ts +2 -0
- package/remult-middleware.js +70 -0
- package/server/JsonEntityFileStorage.d.ts +4 -1
- package/server/JsonEntityFileStorage.js +11 -1
- package/server/expressBridge.d.ts +1 -1
- package/server/expressBridge.js +14 -10
- package/server/index.d.ts +2 -2
- package/server/index.js +4 -3
- package/server/register-actions-on-server.d.ts +3 -0
- package/server/register-actions-on-server.js +50 -0
- package/server/register-entities-on-server.d.ts +3 -0
- package/server/register-entities-on-server.js +19 -0
- package/src/backend-tests/test-http-server.backend-spec.js +47 -7
- package/valueConverters.d.ts +12 -0
- package/valueConverters.js +168 -0
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
</div>
|
|
28
28
|
<hr/>
|
|
29
29
|
|
|
30
|
-
[Getting Started](#getting-started) | [Documentation](#documentation) | [Example
|
|
30
|
+
[Getting Started](#getting-started) | [Documentation](#documentation) | [Example Apps](#example-apps)
|
|
31
31
|
|
|
32
32
|
## What is Remult?
|
|
33
33
|
|
|
@@ -222,9 +222,11 @@ The best way to learn Remult is by following a tutorial of a simple Todo web app
|
|
|
222
222
|
## Documentation
|
|
223
223
|
The [documentation](https://remult.dev/docs) covers the main features of Remult. However, it is still a work-in-progress.
|
|
224
224
|
|
|
225
|
-
## Example
|
|
225
|
+
## Example Apps
|
|
226
226
|
|
|
227
|
-
[
|
|
227
|
+
* Fullstack TodoMVC example with React and Express. ([Source code](https://github.com/remult/TodoMVC-eample) | [CodeSandbox](https://codesandbox.io/s/github/remult/TodoMVC-example?file=/src/frontend/App.tsx))
|
|
228
|
+
|
|
229
|
+
* [CRM demo](https://github.com/remult/crm-demo) with a React + [MUI](https://mui.com) front-end and Postgres database.
|
|
228
230
|
|
|
229
231
|
## Contributing
|
|
230
232
|
Contributions are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ValueConverters } from '../valueConverters';
|
|
2
|
+
export function postgresColumnSyntax(x, dbName) {
|
|
3
|
+
let result = dbName;
|
|
4
|
+
if (x.valueType == Number) {
|
|
5
|
+
if (!x.valueConverter.fieldTypeInDb)
|
|
6
|
+
result += " numeric" + (x.allowNull ? "" : " default 0 not null");
|
|
7
|
+
else
|
|
8
|
+
result += " " + x.valueConverter.fieldTypeInDb + (x.allowNull ? "" : " default 0 not null");
|
|
9
|
+
}
|
|
10
|
+
else if (x.valueType == Date) {
|
|
11
|
+
if (!x.valueConverter.fieldTypeInDb)
|
|
12
|
+
if (x.valueConverter == ValueConverters.DateOnly)
|
|
13
|
+
result += " date";
|
|
14
|
+
else
|
|
15
|
+
result += " timestamp";
|
|
16
|
+
else
|
|
17
|
+
result += " " + x.valueConverter.fieldTypeInDb;
|
|
18
|
+
}
|
|
19
|
+
else if (x.valueType == Boolean)
|
|
20
|
+
result += " boolean" + (x.allowNull ? "" : " default false not null");
|
|
21
|
+
else if (x.valueConverter.fieldTypeInDb) {
|
|
22
|
+
result += " " + x.valueConverter.fieldTypeInDb;
|
|
23
|
+
if (!x.allowNull && x.valueConverter.fieldTypeInDb == 'integer') {
|
|
24
|
+
result += " default 0 not null";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
else
|
|
28
|
+
result += " varchar" + (x.allowNull ? "" : " default '' not null");
|
|
29
|
+
return result;
|
|
30
|
+
}
|
package/esm/remult-express.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as express from 'express';
|
|
2
|
-
import {
|
|
2
|
+
import { createRemultServer } from './server/expressBridge';
|
|
3
3
|
export function remultExpress(options) {
|
|
4
4
|
let app = express.Router();
|
|
5
5
|
if (!options) {
|
|
@@ -12,7 +12,7 @@ export function remultExpress(options) {
|
|
|
12
12
|
app.use(express.json({ limit: options.bodySizeLimit }));
|
|
13
13
|
app.use(express.urlencoded({ extended: true, limit: options.bodySizeLimit }));
|
|
14
14
|
}
|
|
15
|
-
const server =
|
|
15
|
+
const server = createRemultServer(options);
|
|
16
16
|
server.registerRouter(app);
|
|
17
17
|
return Object.assign(app, {
|
|
18
18
|
getRemult: (req) => server.getRemult(req),
|
package/esm/remult-fastify.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createRemultServer } from './server/expressBridge';
|
|
2
2
|
export function remultFastify(options) {
|
|
3
3
|
function fastifyHandler(handler) {
|
|
4
4
|
const response = (req, res) => {
|
|
@@ -18,7 +18,7 @@ export function remultFastify(options) {
|
|
|
18
18
|
};
|
|
19
19
|
return response;
|
|
20
20
|
}
|
|
21
|
-
const api =
|
|
21
|
+
const api = createRemultServer(options);
|
|
22
22
|
const pluginFunction = async (instance, op) => {
|
|
23
23
|
//@ts-ignore
|
|
24
24
|
let fastifyRouter = {
|
package/esm/remult-fresh.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import { RemultServerOptions,
|
|
1
|
+
import { RemultServerOptions, GenericRequest } from "./server/expressBridge";
|
|
2
|
+
import { Remult } from "./src/context";
|
|
2
3
|
export declare function remultFresh(options: RemultServerOptions, response: FreshResponse): RemultFresh;
|
|
3
|
-
export interface RemultFresh
|
|
4
|
-
|
|
4
|
+
export interface RemultFresh {
|
|
5
|
+
getRemult(req: GenericRequest): Promise<Remult>;
|
|
6
|
+
openApiDoc(options: {
|
|
7
|
+
title: string;
|
|
8
|
+
}): any;
|
|
9
|
+
handle(req: FreshRequest, ctx: FreshContext): Promise<any>;
|
|
5
10
|
}
|
|
6
11
|
export interface FreshRequest {
|
|
7
12
|
url: string;
|
package/esm/remult-fresh.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createRemultServer } from "./server/expressBridge";
|
|
2
2
|
export function remultFresh(options, response) {
|
|
3
|
-
const server =
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
const server = createRemultServer(options);
|
|
4
|
+
return {
|
|
5
|
+
getRemult: r => server.getRemult(r),
|
|
6
|
+
openApiDoc: x => server.openApiDoc(x),
|
|
7
|
+
handle: async (req, ctx) => {
|
|
7
8
|
const theReq = {
|
|
8
9
|
method: req.method,
|
|
9
10
|
url: req.url,
|
|
@@ -16,7 +17,7 @@ export function remultFresh(options, response) {
|
|
|
16
17
|
break;
|
|
17
18
|
}
|
|
18
19
|
let init = {};
|
|
19
|
-
const res = await
|
|
20
|
+
const res = await server.handle(theReq);
|
|
20
21
|
if (res) {
|
|
21
22
|
init.status = res.statusCode;
|
|
22
23
|
if (res.data) {
|
|
@@ -30,7 +31,7 @@ export function remultFresh(options, response) {
|
|
|
30
31
|
}
|
|
31
32
|
;
|
|
32
33
|
}
|
|
33
|
-
}
|
|
34
|
+
};
|
|
34
35
|
}
|
|
35
36
|
;
|
|
36
37
|
;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { remultMiddlewareBase } from './server/expressBridge';
|
|
2
|
+
export function remultMiddleware(options) {
|
|
3
|
+
const m = new middleware();
|
|
4
|
+
let app = Object.assign((req, res, next) => m.handleRequest(req, res, next), {
|
|
5
|
+
route: (path) => m.route(path)
|
|
6
|
+
});
|
|
7
|
+
return remultMiddlewareBase(app, options);
|
|
8
|
+
}
|
|
9
|
+
class middleware {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.map = new Map();
|
|
12
|
+
}
|
|
13
|
+
route(path) {
|
|
14
|
+
//consider using:
|
|
15
|
+
//* https://raw.githubusercontent.com/cmorten/opine/main/src/utils/pathToRegex.ts
|
|
16
|
+
//* https://github.com/pillarjs/path-to-regexp
|
|
17
|
+
let r = path.toLowerCase();
|
|
18
|
+
let m = new Map();
|
|
19
|
+
this.map.set(r, m);
|
|
20
|
+
const route = {
|
|
21
|
+
get: (h) => {
|
|
22
|
+
m.set("get", h);
|
|
23
|
+
return route;
|
|
24
|
+
},
|
|
25
|
+
put: (h) => {
|
|
26
|
+
m.set("put", h);
|
|
27
|
+
return route;
|
|
28
|
+
},
|
|
29
|
+
post: (h) => {
|
|
30
|
+
m.set("post", h);
|
|
31
|
+
return route;
|
|
32
|
+
},
|
|
33
|
+
delete: (h) => {
|
|
34
|
+
m.set("delete", h);
|
|
35
|
+
return route;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
return route;
|
|
39
|
+
}
|
|
40
|
+
handleRequest(req, res, next) {
|
|
41
|
+
let lowerPath = req.path.toLowerCase();
|
|
42
|
+
let m = this.map.get(lowerPath);
|
|
43
|
+
if (m) {
|
|
44
|
+
let h = m.get(req.method.toLowerCase());
|
|
45
|
+
if (h) {
|
|
46
|
+
h(req, res, next);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
let idPosition = req.path.lastIndexOf('/');
|
|
51
|
+
if (idPosition >= 0) {
|
|
52
|
+
lowerPath = req.path.substring(0, idPosition) + '/:id';
|
|
53
|
+
m = this.map.get(lowerPath);
|
|
54
|
+
if (m) {
|
|
55
|
+
let h = m.get(req.method.toLowerCase());
|
|
56
|
+
if (h) {
|
|
57
|
+
req.params.id = req.path.substring(idPosition + 1);
|
|
58
|
+
h(req, res, next);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
next();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { JsonEntityStorage } from '../index';
|
|
1
|
+
import { JsonDataProvider, JsonEntityStorage } from '../index';
|
|
2
2
|
export declare class JsonEntityFileStorage implements JsonEntityStorage {
|
|
3
3
|
private folderPath;
|
|
4
4
|
getItem(entityDbName: string): string;
|
|
5
5
|
setItem(entityDbName: string, json: string): void;
|
|
6
6
|
constructor(folderPath: string);
|
|
7
7
|
}
|
|
8
|
+
export declare class JsonFileDataProvider extends JsonDataProvider {
|
|
9
|
+
constructor(folderPath: string);
|
|
10
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as path from 'path';
|
|
2
2
|
import * as fs from 'fs';
|
|
3
|
+
import { JsonDataProvider } from '../index';
|
|
3
4
|
export class JsonEntityFileStorage {
|
|
4
5
|
constructor(folderPath) {
|
|
5
6
|
this.folderPath = folderPath;
|
|
@@ -18,3 +19,6 @@ export class JsonEntityFileStorage {
|
|
|
18
19
|
return fs.writeFileSync(path.join(this.folderPath, entityDbName) + '.json', json);
|
|
19
20
|
}
|
|
20
21
|
}
|
|
22
|
+
export class JsonFileDataProvider extends JsonDataProvider {
|
|
23
|
+
constructor(folderPath) { super(new JsonEntityFileStorage(folderPath)); }
|
|
24
|
+
}
|
|
@@ -20,7 +20,7 @@ export declare type RemultServerOptions = {
|
|
|
20
20
|
controllers?: ClassType<any>[];
|
|
21
21
|
rootPath?: string;
|
|
22
22
|
};
|
|
23
|
-
export declare function
|
|
23
|
+
export declare function createRemultServer(options?: RemultServerOptions): RemultServer;
|
|
24
24
|
export declare type GenericRequestHandler = (req: GenericRequest, res: GenericResponse, next: VoidFunction) => void;
|
|
25
25
|
export interface ServerHandleResponse {
|
|
26
26
|
data?: any;
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { __decorate, __metadata } from "tslib";
|
|
2
|
-
import { JsonEntityFileStorage } from './JsonEntityFileStorage';
|
|
3
2
|
import { Action, actionInfo, classBackendMethodsArray, serverActionField } from '../src/server-action';
|
|
4
3
|
import { DataApi, serializeError } from '../src/data-api';
|
|
5
4
|
import { allEntities, Remult } from '../src/context';
|
|
6
5
|
import { Entity, Fields, getEntityKey } from '../src/remult3';
|
|
7
|
-
import { JsonDataProvider } from '../src/data-providers/json-data-provider';
|
|
8
6
|
import { IdEntity } from '../src/id-entity';
|
|
9
|
-
export function
|
|
7
|
+
export function createRemultServer(options) {
|
|
10
8
|
if (!options) {
|
|
11
9
|
options = {};
|
|
12
10
|
}
|
|
@@ -23,10 +21,10 @@ export function buildRemultServer(options) {
|
|
|
23
21
|
}
|
|
24
22
|
else
|
|
25
23
|
dataProvider = Promise.resolve(options.dataProvider);
|
|
26
|
-
dataProvider = dataProvider.then(dp => {
|
|
24
|
+
dataProvider = dataProvider.then(async (dp) => {
|
|
27
25
|
if (dp)
|
|
28
26
|
return dp;
|
|
29
|
-
return new
|
|
27
|
+
return new (await import('./JsonEntityFileStorage')).JsonFileDataProvider('./db');
|
|
30
28
|
});
|
|
31
29
|
if (options.initApi) {
|
|
32
30
|
dataProvider = dataProvider.then(async (dp) => {
|
package/esm/server/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
1
|
+
export { queuedJobInfo, QueueStorage, GenericRequest, GenericRequestHandler, GenericResponse, GenericRouter, createRemultServer, RemultServerOptions, SpecificRoute, RemultServer } from './expressBridge';
|
|
2
|
+
export { JsonEntityFileStorage, JsonFileDataProvider } from './JsonEntityFileStorage';
|
package/esm/server/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
1
|
+
export { createRemultServer } from './expressBridge';
|
|
2
|
+
export { JsonEntityFileStorage, JsonFileDataProvider } from './JsonEntityFileStorage';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { serverActionField, actionInfo, classBackendMethodsArray } from '../src/server-action';
|
|
2
|
+
export function registerActionsOnServer(area, controllers) {
|
|
3
|
+
var addAction = (a) => {
|
|
4
|
+
let x = a[serverActionField];
|
|
5
|
+
if (!x) {
|
|
6
|
+
throw 'failed to set server action, did you forget the BackendMethod Decorator?';
|
|
7
|
+
}
|
|
8
|
+
area.addAction(x);
|
|
9
|
+
};
|
|
10
|
+
actionInfo.runningOnServer = true;
|
|
11
|
+
if (controllers && controllers.length > 0) {
|
|
12
|
+
for (const c of controllers) {
|
|
13
|
+
let z = c[classBackendMethodsArray];
|
|
14
|
+
if (z)
|
|
15
|
+
for (const a of z) {
|
|
16
|
+
addAction(a);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
else
|
|
21
|
+
actionInfo.allActions.forEach((a) => {
|
|
22
|
+
addAction(a);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getEntityKey } from "../src/remult3";
|
|
2
|
+
import { allEntities } from "../src/context";
|
|
3
|
+
import { DataApi } from "../src/data-api";
|
|
4
|
+
export function registerEntitiesOnServer(area, entities) {
|
|
5
|
+
if (!entities)
|
|
6
|
+
entities = allEntities;
|
|
7
|
+
//add Api Entries
|
|
8
|
+
entities.forEach(e => {
|
|
9
|
+
let key = getEntityKey(e);
|
|
10
|
+
if (key != undefined)
|
|
11
|
+
area.add(key, c => {
|
|
12
|
+
return new DataApi(c.repo(e), c);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
}
|
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
import { __decorate, __metadata } from "tslib";
|
|
2
2
|
import axios from 'axios';
|
|
3
|
+
import { remultFresh } from '../../remult-fresh';
|
|
3
4
|
import { HttpProviderBridgeToRestDataProviderHttpProvider, Remult } from '../context';
|
|
5
|
+
import { InMemoryDataProvider } from '../data-providers/in-memory-database';
|
|
4
6
|
import { Entity, Fields } from '../remult3';
|
|
5
7
|
import { Action, BackendMethod } from '../server-action';
|
|
8
|
+
import { Categories } from '../tests/remult-3-entities';
|
|
6
9
|
Remult.apiBaseUrl = 'http://localhost:3003/api';
|
|
7
10
|
let path = Remult.apiBaseUrl + '/tasks';
|
|
8
11
|
const environments = [
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
// ["next", 3000],
|
|
13
|
+
// ["nest", 3001],
|
|
14
|
+
// ["koa", 3002],
|
|
15
|
+
// ["fastify", 3003],
|
|
16
|
+
// ["express", 3004],
|
|
17
|
+
// ["generic server", 3005],
|
|
18
|
+
// ["optine", 3006],
|
|
19
|
+
// ["fresh", 8000]
|
|
16
20
|
];
|
|
17
21
|
function test(name, test) {
|
|
18
22
|
for (const [env, port] of environments) {
|
|
@@ -92,6 +96,32 @@ test("test regular api call", async () => {
|
|
|
92
96
|
let result = await axios.get(Remult.apiBaseUrl + "/test");
|
|
93
97
|
expect(result.data.result).toBe(3);
|
|
94
98
|
});
|
|
99
|
+
class FreshResponseTest {
|
|
100
|
+
constructor(body, init) {
|
|
101
|
+
this.body = body;
|
|
102
|
+
this.init = init;
|
|
103
|
+
}
|
|
104
|
+
static json(data, init) {
|
|
105
|
+
return new this(data, init);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
it("test fresh", async () => {
|
|
109
|
+
const api = remultFresh({ entities: [Categories],
|
|
110
|
+
dataProvider: new InMemoryDataProvider(),
|
|
111
|
+
rootPath: '/api' }, FreshResponseTest);
|
|
112
|
+
const r = await api.handle({
|
|
113
|
+
url: '/api/Categories',
|
|
114
|
+
json() {
|
|
115
|
+
return undefined;
|
|
116
|
+
},
|
|
117
|
+
method: "get"
|
|
118
|
+
}, {
|
|
119
|
+
next() {
|
|
120
|
+
return undefined;
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
expect(r.body).toEqual([]);
|
|
124
|
+
});
|
|
95
125
|
let Task = class Task {
|
|
96
126
|
constructor() {
|
|
97
127
|
this.title = '';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ValueConverter } from "./src/column-interfaces";
|
|
2
|
+
export declare class ValueConverters {
|
|
3
|
+
static readonly Date: ValueConverter<Date>;
|
|
4
|
+
static readonly DateOnly: ValueConverter<Date>;
|
|
5
|
+
static readonly DateOnlyString: ValueConverter<Date>;
|
|
6
|
+
static readonly Boolean: ValueConverter<Boolean>;
|
|
7
|
+
static readonly Number: ValueConverter<number>;
|
|
8
|
+
static readonly Integer: ValueConverter<number>;
|
|
9
|
+
static readonly Default: ValueConverter<any>;
|
|
10
|
+
static readonly JsonString: ValueConverter<any>;
|
|
11
|
+
static readonly JsonValue: ValueConverter<any>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { InputTypes } from "./inputTypes";
|
|
2
|
+
export class ValueConverters {
|
|
3
|
+
}
|
|
4
|
+
ValueConverters.Date = {
|
|
5
|
+
toJson: (val) => {
|
|
6
|
+
if (!val)
|
|
7
|
+
return '';
|
|
8
|
+
if (typeof (val) === "string")
|
|
9
|
+
val = new Date(val);
|
|
10
|
+
if (val instanceof Date) {
|
|
11
|
+
return val.toISOString();
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
console.log("ToJsonError", val);
|
|
15
|
+
throw new Error("Expected date but got val");
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
fromJson: (val) => {
|
|
19
|
+
if (val == undefined)
|
|
20
|
+
return undefined;
|
|
21
|
+
if (val == '')
|
|
22
|
+
return undefined;
|
|
23
|
+
if (val.startsWith('0000-00-00'))
|
|
24
|
+
return undefined;
|
|
25
|
+
return new Date(Date.parse(val));
|
|
26
|
+
},
|
|
27
|
+
toDb: x => x,
|
|
28
|
+
fromDb: x => x,
|
|
29
|
+
fromInput: x => ValueConverters.Date.fromJson(x),
|
|
30
|
+
toInput: x => ValueConverters.Date.toJson(x),
|
|
31
|
+
displayValue: (val) => {
|
|
32
|
+
if (!val)
|
|
33
|
+
return '';
|
|
34
|
+
return val.toLocaleString();
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
ValueConverters.DateOnly = {
|
|
38
|
+
fromInput: x => ValueConverters.DateOnly.fromJson(x),
|
|
39
|
+
toInput: x => ValueConverters.DateOnly.toJson(x),
|
|
40
|
+
toJson: (val) => {
|
|
41
|
+
var d = val;
|
|
42
|
+
if (typeof d === "string" || typeof (d) === "number")
|
|
43
|
+
d = new Date(d);
|
|
44
|
+
if (!d || d == null)
|
|
45
|
+
return null;
|
|
46
|
+
if (d.getHours() == 0)
|
|
47
|
+
return new Date(d.valueOf() - d.getTimezoneOffset() * 60000).toISOString().substring(0, 10);
|
|
48
|
+
else
|
|
49
|
+
return d.toISOString().substring(0, 10);
|
|
50
|
+
},
|
|
51
|
+
fromJson: (value) => {
|
|
52
|
+
if (!value || value == '' || value == '0000-00-00')
|
|
53
|
+
return null;
|
|
54
|
+
let d = new Date(Date.parse(value));
|
|
55
|
+
d.setMinutes(d.getMinutes() + d.getTimezoneOffset());
|
|
56
|
+
return d;
|
|
57
|
+
},
|
|
58
|
+
inputType: InputTypes.date,
|
|
59
|
+
toDb: (val) => {
|
|
60
|
+
if (!val)
|
|
61
|
+
return null;
|
|
62
|
+
return ValueConverters.DateOnly.fromJson(ValueConverters.DateOnly.toJson(val));
|
|
63
|
+
} //when using date storage, the database expects and returns a date local and every where else we reflect on date iso
|
|
64
|
+
,
|
|
65
|
+
fromDb: (val) => {
|
|
66
|
+
var d = val;
|
|
67
|
+
if (!d)
|
|
68
|
+
return null;
|
|
69
|
+
return val;
|
|
70
|
+
},
|
|
71
|
+
fieldTypeInDb: 'date',
|
|
72
|
+
displayValue: (value) => {
|
|
73
|
+
if (!value)
|
|
74
|
+
return '';
|
|
75
|
+
return value.toLocaleDateString(undefined);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
ValueConverters.DateOnlyString = {
|
|
79
|
+
...ValueConverters.DateOnly,
|
|
80
|
+
toDb: (d) => {
|
|
81
|
+
let val = ValueConverters.DateOnly.toJson(d);
|
|
82
|
+
if (!val)
|
|
83
|
+
return undefined;
|
|
84
|
+
return val.replace(/-/g, '');
|
|
85
|
+
},
|
|
86
|
+
fromDb: (val) => {
|
|
87
|
+
if (val === null)
|
|
88
|
+
return null;
|
|
89
|
+
if (!val)
|
|
90
|
+
return undefined;
|
|
91
|
+
return new Date(val.substring(0, 4) + '-' + val.substring(4, 6) + '-' + val.substring(6, 8));
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
ValueConverters.Boolean = {
|
|
95
|
+
toDb: (val) => val,
|
|
96
|
+
inputType: InputTypes.checkbox,
|
|
97
|
+
fromDb: (value) => {
|
|
98
|
+
return ValueConverters.Boolean.fromJson(value);
|
|
99
|
+
},
|
|
100
|
+
fromJson: value => {
|
|
101
|
+
if (typeof value === "boolean")
|
|
102
|
+
return value;
|
|
103
|
+
if (value !== undefined && value !== null) {
|
|
104
|
+
return value.toString().trim().toLowerCase() == 'true';
|
|
105
|
+
}
|
|
106
|
+
return value;
|
|
107
|
+
},
|
|
108
|
+
toJson: x => x,
|
|
109
|
+
fromInput: x => ValueConverters.Boolean.fromJson(x),
|
|
110
|
+
toInput: x => ValueConverters.Boolean.toJson(x)
|
|
111
|
+
};
|
|
112
|
+
ValueConverters.Number = {
|
|
113
|
+
fromDb: value => {
|
|
114
|
+
if (value === null)
|
|
115
|
+
return null;
|
|
116
|
+
if (value !== undefined)
|
|
117
|
+
return +value;
|
|
118
|
+
return undefined;
|
|
119
|
+
},
|
|
120
|
+
toDb: value => value,
|
|
121
|
+
fromJson: value => ValueConverters.Number.fromDb(value),
|
|
122
|
+
toJson: value => ValueConverters.Number.toDb(value),
|
|
123
|
+
fromInput: (x, type) => {
|
|
124
|
+
let r = +x;
|
|
125
|
+
if (!x)
|
|
126
|
+
return undefined;
|
|
127
|
+
return r;
|
|
128
|
+
},
|
|
129
|
+
toInput: (x, type) => {
|
|
130
|
+
return x?.toString();
|
|
131
|
+
},
|
|
132
|
+
inputType: InputTypes.number
|
|
133
|
+
};
|
|
134
|
+
ValueConverters.Integer = {
|
|
135
|
+
...ValueConverters.Number,
|
|
136
|
+
toJson: value => {
|
|
137
|
+
let val = ValueConverters.Number.toDb(value);
|
|
138
|
+
if (!val)
|
|
139
|
+
return val;
|
|
140
|
+
return +(+val).toFixed(0);
|
|
141
|
+
},
|
|
142
|
+
toDb: value => ValueConverters.Integer.toJson(value),
|
|
143
|
+
fieldTypeInDb: 'integer'
|
|
144
|
+
};
|
|
145
|
+
ValueConverters.Default = {
|
|
146
|
+
fromJson: x => x,
|
|
147
|
+
toJson: x => x,
|
|
148
|
+
fromDb: x => ValueConverters.JsonString.fromDb(x),
|
|
149
|
+
toDb: x => ValueConverters.JsonString.toDb(x),
|
|
150
|
+
fromInput: x => ValueConverters.Default.fromJson(x),
|
|
151
|
+
toInput: x => ValueConverters.Default.toJson(x)
|
|
152
|
+
};
|
|
153
|
+
ValueConverters.JsonString = {
|
|
154
|
+
fromJson: x => x,
|
|
155
|
+
toJson: x => x,
|
|
156
|
+
fromDb: x => x == null ? null : x ? JSON.parse(ValueConverters.JsonString.fromJson(x)) : undefined,
|
|
157
|
+
toDb: x => x !== undefined ? x === null ? null : JSON.stringify(ValueConverters.JsonString.toJson(x)) : undefined,
|
|
158
|
+
fromInput: x => ValueConverters.JsonString.fromJson(x),
|
|
159
|
+
toInput: x => ValueConverters.JsonString.toJson(x)
|
|
160
|
+
};
|
|
161
|
+
ValueConverters.JsonValue = {
|
|
162
|
+
fromJson: x => x,
|
|
163
|
+
toJson: x => x,
|
|
164
|
+
fromDb: x => x,
|
|
165
|
+
toDb: x => x,
|
|
166
|
+
fromInput: x => ValueConverters.JsonString.fromJson(x),
|
|
167
|
+
toInput: x => ValueConverters.JsonString.toJson(x),
|
|
168
|
+
fieldTypeInDb: 'json'
|
|
169
|
+
};
|