cormo-graphql 1.4.0 → 2.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/eslint.config.mjs +42 -0
- package/lib/{decorators.js → cjs/decorators.js} +7 -8
- package/lib/cjs/index.d.ts +2 -0
- package/lib/{index.js → cjs/index.js} +2 -2
- package/lib/cjs/package.json +1 -0
- package/lib/{schema.js → cjs/schema.js} +1 -2
- package/lib/cjs/tsconfig.build.cjs.tsbuildinfo +1 -0
- package/lib/esm/decorators.d.ts +53 -0
- package/lib/esm/decorators.js +72 -0
- package/lib/esm/index.d.ts +2 -0
- package/lib/esm/index.js +2 -0
- package/lib/esm/schema.d.ts +11 -0
- package/lib/esm/schema.js +358 -0
- package/lib/esm/tsconfig.build.esm.tsbuildinfo +1 -0
- package/package.json +24 -19
- package/tsconfig.json +3 -2
- package/.eslintrc.js +0 -24
- package/lib/index.d.ts +0 -2
- /package/lib/{decorators.d.ts → cjs/decorators.d.ts} +0 -0
- /package/lib/{schema.d.ts → cjs/schema.d.ts} +0 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import baseConfig from '@croquiscom/eslint-config/requiring-type-checking.mjs';
|
|
2
|
+
import globals from 'globals';
|
|
3
|
+
import tseslint from 'typescript-eslint';
|
|
4
|
+
|
|
5
|
+
export default tseslint.config(
|
|
6
|
+
...baseConfig,
|
|
7
|
+
{
|
|
8
|
+
languageOptions: {
|
|
9
|
+
globals: {
|
|
10
|
+
...globals.node,
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
parserOptions: {
|
|
14
|
+
projectService: true,
|
|
15
|
+
tsconfigRootDir: import.meta.dirname,
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
settings: {
|
|
20
|
+
'import/resolver': {
|
|
21
|
+
typescript: true,
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
rules: {
|
|
26
|
+
'@typescript-eslint/no-unsafe-call': 'off',
|
|
27
|
+
'@typescript-eslint/no-unsafe-member-access': 'off',
|
|
28
|
+
'@typescript-eslint/no-unsafe-assignment': 'off',
|
|
29
|
+
'@typescript-eslint/no-unsafe-return': 'off',
|
|
30
|
+
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
|
31
|
+
'@typescript-eslint/no-floating-promises': 'off',
|
|
32
|
+
'@typescript-eslint/restrict-template-expressions': 'off',
|
|
33
|
+
'@typescript-eslint/restrict-plus-operands': 'off',
|
|
34
|
+
'@typescript-eslint/ban-types': 'off',
|
|
35
|
+
'@typescript-eslint/no-unsafe-argument': 'off',
|
|
36
|
+
'@typescript-eslint/consistent-indexed-object-style': 'off',
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
ignores: ['eslint.config.mjs', 'lib/**'],
|
|
41
|
+
},
|
|
42
|
+
);
|
|
@@ -23,7 +23,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
26
|
+
exports.Model = Model;
|
|
27
|
+
exports.Column = Column;
|
|
28
|
+
exports.ObjectColumn = ObjectColumn;
|
|
29
|
+
exports.HasMany = HasMany;
|
|
30
|
+
exports.HasOne = HasOne;
|
|
31
|
+
exports.BelongsTo = BelongsTo;
|
|
32
|
+
exports.Index = Index;
|
|
27
33
|
const cormo = __importStar(require("cormo"));
|
|
28
34
|
function Model(options = {}) {
|
|
29
35
|
const c = cormo.Model({ connection: options.connection, name: options.name, description: options.description });
|
|
@@ -31,7 +37,6 @@ function Model(options = {}) {
|
|
|
31
37
|
c(ctor);
|
|
32
38
|
};
|
|
33
39
|
}
|
|
34
|
-
exports.Model = Model;
|
|
35
40
|
function Column(options) {
|
|
36
41
|
let cormo_type;
|
|
37
42
|
if (options.enum) {
|
|
@@ -52,14 +57,12 @@ function Column(options) {
|
|
|
52
57
|
c(target, propertyKey);
|
|
53
58
|
};
|
|
54
59
|
}
|
|
55
|
-
exports.Column = Column;
|
|
56
60
|
function ObjectColumn(options) {
|
|
57
61
|
const c = cormo.ObjectColumn(options.type);
|
|
58
62
|
return (target, propertyKey) => {
|
|
59
63
|
c(target, propertyKey);
|
|
60
64
|
};
|
|
61
65
|
}
|
|
62
|
-
exports.ObjectColumn = ObjectColumn;
|
|
63
66
|
function HasMany(options) {
|
|
64
67
|
const c_options = {
|
|
65
68
|
foreign_key: options.foreign_key,
|
|
@@ -71,7 +74,6 @@ function HasMany(options) {
|
|
|
71
74
|
c(target, propertyKey);
|
|
72
75
|
};
|
|
73
76
|
}
|
|
74
|
-
exports.HasMany = HasMany;
|
|
75
77
|
function HasOne(options) {
|
|
76
78
|
const c_options = {
|
|
77
79
|
foreign_key: options.foreign_key,
|
|
@@ -83,7 +85,6 @@ function HasOne(options) {
|
|
|
83
85
|
c(target, propertyKey);
|
|
84
86
|
};
|
|
85
87
|
}
|
|
86
|
-
exports.HasOne = HasOne;
|
|
87
88
|
function BelongsTo(options) {
|
|
88
89
|
const c_options = {
|
|
89
90
|
foreign_key: options.foreign_key,
|
|
@@ -95,11 +96,9 @@ function BelongsTo(options) {
|
|
|
95
96
|
c(target, propertyKey);
|
|
96
97
|
};
|
|
97
98
|
}
|
|
98
|
-
exports.BelongsTo = BelongsTo;
|
|
99
99
|
function Index(columns, options) {
|
|
100
100
|
const c = cormo.Index(columns, options);
|
|
101
101
|
return (ctor) => {
|
|
102
102
|
c(ctor);
|
|
103
103
|
};
|
|
104
104
|
}
|
|
105
|
-
exports.Index = Index;
|
|
@@ -14,5 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./decorators"), exports);
|
|
18
|
-
__exportStar(require("./schema"), exports);
|
|
17
|
+
__exportStar(require("./decorators.js"), exports);
|
|
18
|
+
__exportStar(require("./schema.js"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.createDefaultCrudSchema =
|
|
29
|
+
exports.createDefaultCrudSchema = createDefaultCrudSchema;
|
|
30
30
|
const crary_graphql_1 = require("@croquiscom/crary-graphql");
|
|
31
31
|
const cormo = __importStar(require("cormo"));
|
|
32
32
|
const graphql_1 = require("graphql");
|
|
@@ -385,4 +385,3 @@ function createDefaultCrudSchema(model_class, options = {}) {
|
|
|
385
385
|
}),
|
|
386
386
|
});
|
|
387
387
|
}
|
|
388
|
-
exports.createDefaultCrudSchema = createDefaultCrudSchema;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["../../src/decorators.ts","../../src/index.ts","../../src/schema.ts"],"version":"5.6.3"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as cormo from 'cormo';
|
|
2
|
+
export declare function Model(options?: {
|
|
3
|
+
connection?: cormo.Connection;
|
|
4
|
+
name?: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
}): (ctor: typeof cormo.BaseModel) => void;
|
|
7
|
+
interface ColumnBasicOptions {
|
|
8
|
+
required?: boolean;
|
|
9
|
+
graphql_required?: boolean;
|
|
10
|
+
unique?: boolean;
|
|
11
|
+
name?: string;
|
|
12
|
+
description?: string;
|
|
13
|
+
default_value?: string | number | (() => string | number);
|
|
14
|
+
}
|
|
15
|
+
export declare function Column(options: ({
|
|
16
|
+
enum: any;
|
|
17
|
+
} & ColumnBasicOptions) | ({
|
|
18
|
+
type: cormo.types.ColumnType | cormo.types.ColumnType[];
|
|
19
|
+
} & ColumnBasicOptions)): PropertyDecorator;
|
|
20
|
+
interface ObjectColumnOptions {
|
|
21
|
+
type: any;
|
|
22
|
+
required?: boolean;
|
|
23
|
+
description?: string;
|
|
24
|
+
}
|
|
25
|
+
export declare function ObjectColumn(options: ObjectColumnOptions): PropertyDecorator;
|
|
26
|
+
interface HasManyBasicOptions {
|
|
27
|
+
type?: string;
|
|
28
|
+
foreign_key?: string;
|
|
29
|
+
integrity?: 'ignore' | 'nullify' | 'restrict' | 'delete';
|
|
30
|
+
description?: string;
|
|
31
|
+
}
|
|
32
|
+
export declare function HasMany(options: HasManyBasicOptions): (target: cormo.BaseModel, propertyKey: string) => void;
|
|
33
|
+
interface HasOneBasicOptions {
|
|
34
|
+
type?: string;
|
|
35
|
+
foreign_key?: string;
|
|
36
|
+
integrity?: 'ignore' | 'nullify' | 'restrict' | 'delete';
|
|
37
|
+
description?: string;
|
|
38
|
+
}
|
|
39
|
+
export declare function HasOne(options: HasOneBasicOptions): (target: cormo.BaseModel, propertyKey: string) => void;
|
|
40
|
+
interface BelongsToBasicOptions {
|
|
41
|
+
type?: string;
|
|
42
|
+
required?: boolean;
|
|
43
|
+
foreign_key?: string;
|
|
44
|
+
description?: string;
|
|
45
|
+
}
|
|
46
|
+
export declare function BelongsTo(options: BelongsToBasicOptions): (target: cormo.BaseModel, propertyKey: string) => void;
|
|
47
|
+
export declare function Index(columns: {
|
|
48
|
+
[column: string]: 1 | -1;
|
|
49
|
+
}, options?: {
|
|
50
|
+
name?: string;
|
|
51
|
+
unique?: boolean;
|
|
52
|
+
}): (ctor: typeof cormo.BaseModel) => void;
|
|
53
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import * as cormo from 'cormo';
|
|
2
|
+
export function Model(options = {}) {
|
|
3
|
+
const c = cormo.Model({ connection: options.connection, name: options.name, description: options.description });
|
|
4
|
+
return (ctor) => {
|
|
5
|
+
c(ctor);
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
export function Column(options) {
|
|
9
|
+
let cormo_type;
|
|
10
|
+
if (options.enum) {
|
|
11
|
+
cormo_type = cormo.types.Integer;
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
cormo_type = options.type;
|
|
15
|
+
}
|
|
16
|
+
const c = cormo.Column({
|
|
17
|
+
default_value: options.default_value,
|
|
18
|
+
name: options.name,
|
|
19
|
+
required: options.required,
|
|
20
|
+
type: cormo_type,
|
|
21
|
+
description: options.description,
|
|
22
|
+
unique: options.unique,
|
|
23
|
+
});
|
|
24
|
+
return (target, propertyKey) => {
|
|
25
|
+
c(target, propertyKey);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export function ObjectColumn(options) {
|
|
29
|
+
const c = cormo.ObjectColumn(options.type);
|
|
30
|
+
return (target, propertyKey) => {
|
|
31
|
+
c(target, propertyKey);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export function HasMany(options) {
|
|
35
|
+
const c_options = {
|
|
36
|
+
foreign_key: options.foreign_key,
|
|
37
|
+
integrity: options.integrity,
|
|
38
|
+
type: options.type,
|
|
39
|
+
};
|
|
40
|
+
const c = cormo.HasMany(c_options);
|
|
41
|
+
return (target, propertyKey) => {
|
|
42
|
+
c(target, propertyKey);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export function HasOne(options) {
|
|
46
|
+
const c_options = {
|
|
47
|
+
foreign_key: options.foreign_key,
|
|
48
|
+
integrity: options.integrity,
|
|
49
|
+
type: options.type,
|
|
50
|
+
};
|
|
51
|
+
const c = cormo.HasOne(c_options);
|
|
52
|
+
return (target, propertyKey) => {
|
|
53
|
+
c(target, propertyKey);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export function BelongsTo(options) {
|
|
57
|
+
const c_options = {
|
|
58
|
+
foreign_key: options.foreign_key,
|
|
59
|
+
required: options.required,
|
|
60
|
+
type: options.type,
|
|
61
|
+
};
|
|
62
|
+
const c = cormo.BelongsTo(c_options);
|
|
63
|
+
return (target, propertyKey) => {
|
|
64
|
+
c(target, propertyKey);
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
export function Index(columns, options) {
|
|
68
|
+
const c = cormo.Index(columns, options);
|
|
69
|
+
return (ctor) => {
|
|
70
|
+
c(ctor);
|
|
71
|
+
};
|
|
72
|
+
}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as cormo from 'cormo';
|
|
2
|
+
import { GraphQLSchema } from 'graphql';
|
|
3
|
+
interface Options {
|
|
4
|
+
id_description?: string;
|
|
5
|
+
list_type_description?: string;
|
|
6
|
+
item_list_description?: string;
|
|
7
|
+
created_at_column?: string;
|
|
8
|
+
updated_at_column?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function createDefaultCrudSchema(model_class: typeof cormo.BaseModel, options?: Options): GraphQLSchema;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import { CrJson, CrTimestamp, getFieldList } from '@croquiscom/crary-graphql';
|
|
2
|
+
import * as cormo from 'cormo';
|
|
3
|
+
import { GraphQLBoolean, GraphQLEnumType, GraphQLFloat, GraphQLID, GraphQLInputObjectType, GraphQLInt, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLSchema, GraphQLString, } from 'graphql';
|
|
4
|
+
import _ from 'lodash';
|
|
5
|
+
function getGraphQlType(property) {
|
|
6
|
+
let graphql_type;
|
|
7
|
+
if (property.record_id) {
|
|
8
|
+
return new GraphQLNonNull(GraphQLID);
|
|
9
|
+
}
|
|
10
|
+
else if (property.type_class === cormo.types.Number) {
|
|
11
|
+
graphql_type = GraphQLFloat;
|
|
12
|
+
}
|
|
13
|
+
else if (property.type_class === cormo.types.Integer) {
|
|
14
|
+
graphql_type = GraphQLInt;
|
|
15
|
+
}
|
|
16
|
+
else if (property.type_class === cormo.types.String) {
|
|
17
|
+
graphql_type = GraphQLString;
|
|
18
|
+
}
|
|
19
|
+
else if (property.type_class === cormo.types.Text) {
|
|
20
|
+
graphql_type = GraphQLString;
|
|
21
|
+
}
|
|
22
|
+
else if (property.type_class === cormo.types.Date) {
|
|
23
|
+
graphql_type = CrTimestamp;
|
|
24
|
+
}
|
|
25
|
+
else if (property.type_class === cormo.types.Object) {
|
|
26
|
+
graphql_type = CrJson;
|
|
27
|
+
}
|
|
28
|
+
if (graphql_type && property.required) {
|
|
29
|
+
return new GraphQLNonNull(graphql_type);
|
|
30
|
+
}
|
|
31
|
+
return graphql_type;
|
|
32
|
+
}
|
|
33
|
+
function createSingleType(model_class, options) {
|
|
34
|
+
const fields = {};
|
|
35
|
+
for (const [column, property] of Object.entries(model_class._schema)) {
|
|
36
|
+
if (!property) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const graphql_type = getGraphQlType(property);
|
|
40
|
+
if (graphql_type) {
|
|
41
|
+
const description = column === 'id' ? options.id_description : property.description;
|
|
42
|
+
fields[column] = {
|
|
43
|
+
description,
|
|
44
|
+
type: graphql_type,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return new GraphQLObjectType({
|
|
49
|
+
description: model_class.description,
|
|
50
|
+
fields,
|
|
51
|
+
name: model_class.name,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
function createListType(model_class, options, single_type) {
|
|
55
|
+
return new GraphQLObjectType({
|
|
56
|
+
description: options.list_type_description,
|
|
57
|
+
fields: {
|
|
58
|
+
item_list: {
|
|
59
|
+
description: options.item_list_description,
|
|
60
|
+
async resolve(source, args, context, info) {
|
|
61
|
+
args = source.__args;
|
|
62
|
+
const query = model_class.query();
|
|
63
|
+
for (const [field, value] of Object.entries(args)) {
|
|
64
|
+
if (value) {
|
|
65
|
+
if (field.endsWith('_list')) {
|
|
66
|
+
query.where({ [field.replace(/_list$/, '')]: value });
|
|
67
|
+
}
|
|
68
|
+
else if (field.endsWith('_istartswith')) {
|
|
69
|
+
query.where({ [field.replace(/_istartswith$/, '')]: { $startswith: value } });
|
|
70
|
+
}
|
|
71
|
+
else if (field.endsWith('_icontains')) {
|
|
72
|
+
query.where({ [field.replace(/_icontains$/, '')]: { $contains: value } });
|
|
73
|
+
}
|
|
74
|
+
else if (field.endsWith('_gte')) {
|
|
75
|
+
query.where({ [field.replace(/_gte$/, '')]: { $gte: value } });
|
|
76
|
+
}
|
|
77
|
+
else if (field.endsWith('_gt')) {
|
|
78
|
+
query.where({ [field.replace(/_gt$/, '')]: { $gt: value } });
|
|
79
|
+
}
|
|
80
|
+
else if (field.endsWith('_lte')) {
|
|
81
|
+
query.where({ [field.replace(/_lte$/, '')]: { $lte: value } });
|
|
82
|
+
}
|
|
83
|
+
else if (field.endsWith('_lt')) {
|
|
84
|
+
query.where({ [field.replace(/_lt$/, '')]: { $lt: value } });
|
|
85
|
+
}
|
|
86
|
+
else if (field === 'order') {
|
|
87
|
+
query.order(value);
|
|
88
|
+
}
|
|
89
|
+
else if (field === 'limit_count') {
|
|
90
|
+
query.limit(value);
|
|
91
|
+
}
|
|
92
|
+
else if (field === 'skip_count') {
|
|
93
|
+
query.skip(value);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
query.where({ [field]: value });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return await query.select(getFieldList(info));
|
|
101
|
+
},
|
|
102
|
+
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(single_type))),
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
name: model_class.name + 'List',
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
function createCreateInputType(model_class, options) {
|
|
109
|
+
const fields = {};
|
|
110
|
+
for (const [column, property] of Object.entries(model_class._schema)) {
|
|
111
|
+
if (!property) {
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (column === 'id') {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (column === options.created_at_column) {
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (column === options.updated_at_column) {
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const graphql_type = getGraphQlType(property);
|
|
124
|
+
if (graphql_type) {
|
|
125
|
+
const description = column === 'id' ? options.id_description : property.description;
|
|
126
|
+
fields[column] = {
|
|
127
|
+
description,
|
|
128
|
+
type: graphql_type,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return new GraphQLInputObjectType({
|
|
133
|
+
fields,
|
|
134
|
+
name: `Create${model_class.name}Input`,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
function createUpdateInputType(model_class, options) {
|
|
138
|
+
const fields = {};
|
|
139
|
+
for (const [column, property] of Object.entries(model_class._schema)) {
|
|
140
|
+
if (!property) {
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (column === options.created_at_column) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (column === options.updated_at_column) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
const graphql_type = getGraphQlType(property);
|
|
150
|
+
if (graphql_type) {
|
|
151
|
+
const description = column === 'id' ? options.id_description : property.description;
|
|
152
|
+
fields[column] = {
|
|
153
|
+
description,
|
|
154
|
+
type: graphql_type,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return new GraphQLInputObjectType({
|
|
159
|
+
fields,
|
|
160
|
+
name: `Update${model_class.name}Input`,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
function createDeleteInputType(model_class, options) {
|
|
164
|
+
const fields = {};
|
|
165
|
+
fields.id = {
|
|
166
|
+
description: options.id_description,
|
|
167
|
+
type: new GraphQLNonNull(GraphQLID),
|
|
168
|
+
};
|
|
169
|
+
return new GraphQLInputObjectType({
|
|
170
|
+
fields,
|
|
171
|
+
name: `Delete${model_class.name}Input`,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
function createOrderType(model_class, _options) {
|
|
175
|
+
const values = {};
|
|
176
|
+
for (const [column, property] of Object.entries(model_class._schema)) {
|
|
177
|
+
if (!property) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (column === 'id' ||
|
|
181
|
+
property.type_class === cormo.types.String ||
|
|
182
|
+
property.type_class === cormo.types.Integer ||
|
|
183
|
+
property.type_class === cormo.types.BigInteger) {
|
|
184
|
+
values[column.toUpperCase() + '_ASC'] = {
|
|
185
|
+
value: column,
|
|
186
|
+
};
|
|
187
|
+
values[column.toUpperCase() + '_DESC'] = {
|
|
188
|
+
value: '-' + column,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return new GraphQLEnumType({
|
|
193
|
+
name: `${model_class.name}OrderType`,
|
|
194
|
+
values,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
function buildListQueryArgs(model_class, options) {
|
|
198
|
+
const list_query_args = {};
|
|
199
|
+
for (const [column, property] of Object.entries(model_class._schema)) {
|
|
200
|
+
if (!property) {
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
if (column === 'id') {
|
|
204
|
+
list_query_args[column + '_list'] = {
|
|
205
|
+
type: new GraphQLList(new GraphQLNonNull(GraphQLID)),
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
else if (property.record_id) {
|
|
209
|
+
list_query_args[column] = {
|
|
210
|
+
type: GraphQLID,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
else if (property.type_class === cormo.types.String || property.type_class === cormo.types.Text) {
|
|
214
|
+
list_query_args[column] = {
|
|
215
|
+
type: GraphQLString,
|
|
216
|
+
};
|
|
217
|
+
list_query_args[column + '_istartswith'] = {
|
|
218
|
+
type: GraphQLString,
|
|
219
|
+
};
|
|
220
|
+
list_query_args[column + '_icontains'] = {
|
|
221
|
+
type: GraphQLString,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
else if (property.type_class === cormo.types.Integer) {
|
|
225
|
+
list_query_args[column] = {
|
|
226
|
+
type: GraphQLInt,
|
|
227
|
+
};
|
|
228
|
+
list_query_args[column + '_gte'] = {
|
|
229
|
+
type: GraphQLInt,
|
|
230
|
+
};
|
|
231
|
+
list_query_args[column + '_gt'] = {
|
|
232
|
+
type: GraphQLInt,
|
|
233
|
+
};
|
|
234
|
+
list_query_args[column + '_lte'] = {
|
|
235
|
+
type: GraphQLInt,
|
|
236
|
+
};
|
|
237
|
+
list_query_args[column + '_lt'] = {
|
|
238
|
+
type: GraphQLInt,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
list_query_args.order = {
|
|
243
|
+
type: createOrderType(model_class, options),
|
|
244
|
+
};
|
|
245
|
+
list_query_args.limit_count = {
|
|
246
|
+
type: GraphQLInt,
|
|
247
|
+
};
|
|
248
|
+
list_query_args.skip_count = {
|
|
249
|
+
type: GraphQLInt,
|
|
250
|
+
};
|
|
251
|
+
return list_query_args;
|
|
252
|
+
}
|
|
253
|
+
export function createDefaultCrudSchema(model_class, options = {}) {
|
|
254
|
+
model_class._connection.applyAssociations();
|
|
255
|
+
const camel_name = model_class.name;
|
|
256
|
+
const snake_name = _.snakeCase(camel_name);
|
|
257
|
+
const single_type = createSingleType(model_class, options);
|
|
258
|
+
const list_type = createListType(model_class, options, single_type);
|
|
259
|
+
const single_query_args = {
|
|
260
|
+
id: {
|
|
261
|
+
type: GraphQLID,
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
const list_query_args = buildListQueryArgs(model_class, options);
|
|
265
|
+
const create_input_type = createCreateInputType(model_class, options);
|
|
266
|
+
const update_input_type = createUpdateInputType(model_class, options);
|
|
267
|
+
const delete_input_type = createDeleteInputType(model_class, options);
|
|
268
|
+
return new GraphQLSchema({
|
|
269
|
+
mutation: new GraphQLObjectType({
|
|
270
|
+
fields: {
|
|
271
|
+
[`create${camel_name}`]: {
|
|
272
|
+
args: {
|
|
273
|
+
input: {
|
|
274
|
+
type: create_input_type,
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
async resolve(source, args) {
|
|
278
|
+
const data = args.input;
|
|
279
|
+
const date = Date.now();
|
|
280
|
+
if (options.created_at_column) {
|
|
281
|
+
data[options.created_at_column] = date;
|
|
282
|
+
}
|
|
283
|
+
if (options.updated_at_column) {
|
|
284
|
+
data[options.updated_at_column] = date;
|
|
285
|
+
}
|
|
286
|
+
return await model_class.create(data);
|
|
287
|
+
},
|
|
288
|
+
type: new GraphQLNonNull(single_type),
|
|
289
|
+
},
|
|
290
|
+
[`update${camel_name}`]: {
|
|
291
|
+
args: {
|
|
292
|
+
input: {
|
|
293
|
+
type: update_input_type,
|
|
294
|
+
},
|
|
295
|
+
},
|
|
296
|
+
async resolve(_source, args) {
|
|
297
|
+
const data = args.input;
|
|
298
|
+
const date = Date.now();
|
|
299
|
+
if (options.updated_at_column) {
|
|
300
|
+
data[options.updated_at_column] = date;
|
|
301
|
+
}
|
|
302
|
+
await model_class.find(args.input.id).update(data);
|
|
303
|
+
return await model_class.find(args.input.id);
|
|
304
|
+
},
|
|
305
|
+
type: new GraphQLNonNull(single_type),
|
|
306
|
+
},
|
|
307
|
+
[`delete${camel_name}`]: {
|
|
308
|
+
args: {
|
|
309
|
+
input: {
|
|
310
|
+
type: delete_input_type,
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
async resolve(source, args) {
|
|
314
|
+
const delete_count = await model_class.find(args.input.id).delete();
|
|
315
|
+
if (delete_count === 0) {
|
|
316
|
+
throw new Error('not found');
|
|
317
|
+
}
|
|
318
|
+
return true;
|
|
319
|
+
},
|
|
320
|
+
type: new GraphQLNonNull(GraphQLBoolean),
|
|
321
|
+
},
|
|
322
|
+
},
|
|
323
|
+
name: 'Mutation',
|
|
324
|
+
}),
|
|
325
|
+
query: new GraphQLObjectType({
|
|
326
|
+
fields: {
|
|
327
|
+
[snake_name]: {
|
|
328
|
+
args: single_query_args,
|
|
329
|
+
description: `Single query for ${camel_name}`,
|
|
330
|
+
async resolve(source, args, context, info) {
|
|
331
|
+
const query = model_class.query().one();
|
|
332
|
+
let has_query = false;
|
|
333
|
+
for (const [field, value] of Object.entries(args)) {
|
|
334
|
+
if (value) {
|
|
335
|
+
has_query = true;
|
|
336
|
+
query.where({ [field]: value });
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (!has_query) {
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
342
|
+
return await query.select(getFieldList(info));
|
|
343
|
+
},
|
|
344
|
+
type: single_type,
|
|
345
|
+
},
|
|
346
|
+
[snake_name + '_list']: {
|
|
347
|
+
args: list_query_args,
|
|
348
|
+
description: `List query for ${camel_name}`,
|
|
349
|
+
resolve(source, args) {
|
|
350
|
+
return { __args: args };
|
|
351
|
+
},
|
|
352
|
+
type: new GraphQLNonNull(list_type),
|
|
353
|
+
},
|
|
354
|
+
},
|
|
355
|
+
name: 'Query',
|
|
356
|
+
}),
|
|
357
|
+
});
|
|
358
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["../../src/decorators.ts","../../src/index.ts","../../src/schema.ts"],"version":"5.6.3"}
|
package/package.json
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cormo-graphql",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "GraphQL support for CORMO",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./lib/cjs/index.js",
|
|
7
|
+
"types": "./lib/cjs/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
"import": "./lib/esm/index.js",
|
|
10
|
+
"require": "./lib/cjs/index.js"
|
|
11
|
+
},
|
|
7
12
|
"scripts": {
|
|
8
13
|
"prepublishOnly": "npm run lint && npm run build",
|
|
9
|
-
"build": "rimraf lib && tsc -
|
|
10
|
-
"lint": "../../node_modules/.bin/eslint
|
|
14
|
+
"build": "rimraf lib && tsc -b src/tsconfig.build.cjs.json src/tsconfig.build.esm.json && echo '{\"type\":\"commonjs\"}' > lib/cjs/package.json",
|
|
15
|
+
"lint": "../../node_modules/.bin/eslint .",
|
|
11
16
|
"test:type": "tsc --noEmit",
|
|
12
|
-
"test:unit": "NODE_ENV=test TZ=Etc/UTC mocha
|
|
17
|
+
"test:unit": "NODE_ENV=test TZ=Etc/UTC mocha -n import=tsx ./test/*/*.ts",
|
|
13
18
|
"test": "npm run test:type && npm run lint && npm run test:unit --",
|
|
14
19
|
"test:debug": "npm run test:unit -- --inspect-brk=30000"
|
|
15
20
|
},
|
|
@@ -25,24 +30,24 @@
|
|
|
25
30
|
"homepage": "https://github.com/croquiscom/cormo",
|
|
26
31
|
"dependencies": {
|
|
27
32
|
"@croquiscom/crary-graphql": "^1.3.1",
|
|
28
|
-
"cormo": "^
|
|
29
|
-
"graphql": "^16.
|
|
33
|
+
"cormo": "^2.0.0",
|
|
34
|
+
"graphql": "^16.9.0",
|
|
30
35
|
"lodash": "^4.17.21"
|
|
31
36
|
},
|
|
32
37
|
"devDependencies": {
|
|
33
38
|
"@types/chai": "^4.3.11",
|
|
34
|
-
"@types/lodash": "^4.17.
|
|
35
|
-
"@types/mocha": "^10.0.
|
|
36
|
-
"@types/node": "^
|
|
37
|
-
"@types/sinon": "^17.0.
|
|
39
|
+
"@types/lodash": "^4.17.12",
|
|
40
|
+
"@types/mocha": "^10.0.9",
|
|
41
|
+
"@types/node": "^22.8.1",
|
|
42
|
+
"@types/sinon": "^17.0.3",
|
|
38
43
|
"chai": "^4.3.10",
|
|
39
|
-
"mocha": "^10.
|
|
44
|
+
"mocha": "^10.7.3",
|
|
40
45
|
"mysql": "^2.18.1",
|
|
41
|
-
"mysql2": "^3.
|
|
42
|
-
"rimraf": "^
|
|
43
|
-
"sinon": "^
|
|
44
|
-
"
|
|
45
|
-
"typescript": "^5.
|
|
46
|
+
"mysql2": "^3.11.3",
|
|
47
|
+
"rimraf": "^6.0.1",
|
|
48
|
+
"sinon": "^19.0.2",
|
|
49
|
+
"tsx": "^4.19.1",
|
|
50
|
+
"typescript": "^5.6.3"
|
|
46
51
|
},
|
|
47
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "c857e9bed767d8d84633f266c2f153a88fb380ec"
|
|
48
53
|
}
|
package/tsconfig.json
CHANGED
package/.eslintrc.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
root: true,
|
|
3
|
-
env: {
|
|
4
|
-
node: true,
|
|
5
|
-
},
|
|
6
|
-
extends: ['@croquiscom/eslint-config/requiring-type-checking'],
|
|
7
|
-
parserOptions: {
|
|
8
|
-
project: [`${__dirname}/tsconfig.json`],
|
|
9
|
-
},
|
|
10
|
-
ignorePatterns: ['.eslintrc.js', 'lib/'],
|
|
11
|
-
rules: {
|
|
12
|
-
'@typescript-eslint/no-unsafe-call': 'off',
|
|
13
|
-
'@typescript-eslint/no-unsafe-member-access': 'off',
|
|
14
|
-
'@typescript-eslint/no-unsafe-assignment': 'off',
|
|
15
|
-
'@typescript-eslint/no-unsafe-return': 'off',
|
|
16
|
-
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
|
17
|
-
'@typescript-eslint/no-floating-promises': 'off',
|
|
18
|
-
'@typescript-eslint/restrict-template-expressions': 'off',
|
|
19
|
-
'@typescript-eslint/restrict-plus-operands': 'off',
|
|
20
|
-
'@typescript-eslint/ban-types': 'off',
|
|
21
|
-
'@typescript-eslint/no-unsafe-argument': 'off',
|
|
22
|
-
'@typescript-eslint/consistent-indexed-object-style': 'off',
|
|
23
|
-
},
|
|
24
|
-
};
|
package/lib/index.d.ts
DELETED
|
File without changes
|
|
File without changes
|