rads-db 0.1.0 → 0.1.1
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/drivers/memory.cjs +175 -0
- package/drivers/memory.d.ts +8 -0
- package/drivers/memory.mjs +161 -0
- package/drivers/restApi.cjs +37 -0
- package/drivers/restApi.d.ts +5 -0
- package/drivers/restApi.mjs +24 -0
- package/integrations/node.cjs +213 -0
- package/integrations/node.d.ts +1 -0
- package/integrations/node.mjs +168 -0
- package/integrations/nuxtModule.cjs +25 -0
- package/integrations/nuxtModule.d.ts +1 -0
- package/integrations/nuxtModule.mjs +16 -0
- package/integrations/nuxtModuleHandler.cjs +38 -0
- package/integrations/nuxtModuleHandler.d.ts +6 -0
- package/integrations/nuxtModuleHandler.mjs +28 -0
- package/integrations/vite.cjs +37 -0
- package/integrations/vite.d.ts +6 -0
- package/integrations/vite.mjs +31 -0
- package/package.json +4 -2
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
module.exports = void 0;
|
|
7
|
+
var _lodash = _interopRequireDefault(require("lodash"));
|
|
8
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
9
|
+
const operatorFns = {
|
|
10
|
+
eq: (x, w) => x === w,
|
|
11
|
+
ieq: (x, w) => x?.toLowerCase() === w?.toLowerCase(),
|
|
12
|
+
in: (x, w) => w.includes(x),
|
|
13
|
+
startsWith: (x, w) => x?.startsWith(w),
|
|
14
|
+
istartsWith: (x, w) => x?.toLowerCase()?.startsWith(w.toLowerCase()),
|
|
15
|
+
endsWith: (x, w) => x?.endsWith(w),
|
|
16
|
+
iendsWith: (x, w) => x?.toLowerCase()?.endsWith(w.toLowerCase()),
|
|
17
|
+
contains: (x, w) => x?.includes(w),
|
|
18
|
+
icontains: (x, w) => x?.toLowerCase()?.includes(w.toLowerCase()),
|
|
19
|
+
isNull: (x, w) => {
|
|
20
|
+
if (w === true) return x == null;
|
|
21
|
+
if (w === false) return x != null;
|
|
22
|
+
return true;
|
|
23
|
+
},
|
|
24
|
+
gt: (x, w) => x > w,
|
|
25
|
+
gte: (x, w) => x >= w,
|
|
26
|
+
lt: (x, w) => x < w,
|
|
27
|
+
lte: (x, w) => x <= w,
|
|
28
|
+
between: (x, w) => {
|
|
29
|
+
const {
|
|
30
|
+
start,
|
|
31
|
+
end
|
|
32
|
+
} = w || {};
|
|
33
|
+
let result = true;
|
|
34
|
+
if (start) result = result && x >= start;
|
|
35
|
+
if (end) result = result && x >= end;
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var _default = (schema, entity) => {
|
|
40
|
+
const cache = {};
|
|
41
|
+
function getItemById(id) {
|
|
42
|
+
return cache[id] ?? null;
|
|
43
|
+
}
|
|
44
|
+
function getItemByIds(ids) {
|
|
45
|
+
return ids.map(id => cache[id]);
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
// getItemById,
|
|
49
|
+
// getItemByIds,
|
|
50
|
+
putMany(items) {
|
|
51
|
+
for (const item of items) {
|
|
52
|
+
if (!item?.id) throw new Error(`You must provide an id`);
|
|
53
|
+
cache[item.id] = item;
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
getMany(args) {
|
|
57
|
+
args = args || {};
|
|
58
|
+
const where = args.where || {};
|
|
59
|
+
const whereKeys = _lodash.default.keys(where);
|
|
60
|
+
if (whereKeys.length === 1) {
|
|
61
|
+
if (whereKeys[0] === "id") return {
|
|
62
|
+
nodes: [getItemById(where.id)].filter(x => x),
|
|
63
|
+
cursor: null
|
|
64
|
+
};
|
|
65
|
+
if (whereKeys[0] === "id_in") return {
|
|
66
|
+
nodes: getItemByIds(where.id_in).filter(x => x),
|
|
67
|
+
cursor: null
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return queryArray(Object.values(cache), args);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
module.exports = _default;
|
|
75
|
+
function queryArray(array, args) {
|
|
76
|
+
if (!array) return array;
|
|
77
|
+
let result = array;
|
|
78
|
+
const {
|
|
79
|
+
where,
|
|
80
|
+
orderByProperty,
|
|
81
|
+
orderByDirection,
|
|
82
|
+
maxItemCount,
|
|
83
|
+
cursor
|
|
84
|
+
} = prepareArgs(args);
|
|
85
|
+
const startIndex = Number(cursor) || 0;
|
|
86
|
+
const endIndex = startIndex + maxItemCount;
|
|
87
|
+
const f = getFilter(where);
|
|
88
|
+
if (f) result = result.filter(f);
|
|
89
|
+
if (orderByProperty) result = _lodash.default.orderBy(result, [orderByProperty], [orderByDirection]);
|
|
90
|
+
if (maxItemCount) result = result.slice(startIndex, endIndex);
|
|
91
|
+
const newCursor = endIndex >= array.length ? null : endIndex;
|
|
92
|
+
return {
|
|
93
|
+
nodes: result,
|
|
94
|
+
cursor: newCursor?.toString() || null
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function getFilter(where, namePrefix = "") {
|
|
98
|
+
if (_lodash.default.isEmpty(where)) return null;
|
|
99
|
+
const andClauses = [];
|
|
100
|
+
for (const key in where) {
|
|
101
|
+
const [nameFromWhere, operator] = getWhereNameOperatorPair(key);
|
|
102
|
+
const name = [namePrefix, nameFromWhere].filter(x => x).join(".");
|
|
103
|
+
const whereVal = where[key];
|
|
104
|
+
if (whereVal == null) continue;
|
|
105
|
+
const f = getFilterInner(operator, whereVal, name, namePrefix);
|
|
106
|
+
if (f) andClauses.push(f);
|
|
107
|
+
}
|
|
108
|
+
if (andClauses.length === 0) return null;
|
|
109
|
+
return x => andClauses.every(ac => ac(x));
|
|
110
|
+
}
|
|
111
|
+
function getWhereNameOperatorPair(whereKey) {
|
|
112
|
+
if (whereKey.startsWith("_type")) {
|
|
113
|
+
const operator = whereKey.split("_")[2];
|
|
114
|
+
return ["_type", operator];
|
|
115
|
+
}
|
|
116
|
+
return whereKey.split("_");
|
|
117
|
+
}
|
|
118
|
+
function getFilterInner(operator, whereVal, name, namePrefix) {
|
|
119
|
+
if (!operator && _lodash.default.isObject(whereVal)) {
|
|
120
|
+
return getFilter(whereVal, name);
|
|
121
|
+
}
|
|
122
|
+
if (operator === "some") {
|
|
123
|
+
const f = getFilter(whereVal, namePrefix);
|
|
124
|
+
return f && (x => {
|
|
125
|
+
const val = _lodash.default.get(x, name);
|
|
126
|
+
return val === void 0 || val?.some(f);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
if (operator === "none") {
|
|
130
|
+
const f = getFilter(whereVal, namePrefix);
|
|
131
|
+
return f && (x => !_lodash.default.get(x, name)?.some(f));
|
|
132
|
+
}
|
|
133
|
+
if (operator === "and") {
|
|
134
|
+
if (!_lodash.default.isArray(whereVal)) throw new Error(`Value for where._and must be an array`);
|
|
135
|
+
const newAndClauses = whereVal.map(whereValAnd => getFilter(whereValAnd, namePrefix)).flatMap(x => x ? [x] : []);
|
|
136
|
+
if (!newAndClauses.length) return null;
|
|
137
|
+
return x => newAndClauses.every(ac => ac(x));
|
|
138
|
+
}
|
|
139
|
+
if (operator === "not") {
|
|
140
|
+
const f = getFilter(whereVal, namePrefix);
|
|
141
|
+
return f && (x => !f(x));
|
|
142
|
+
}
|
|
143
|
+
if (operator === "or") {
|
|
144
|
+
if (!_lodash.default.isArray(whereVal)) throw new Error(`Value for where._or must be an array`);
|
|
145
|
+
const orClauses = whereVal.map(whereValOr => getFilter(whereValOr, namePrefix)).flatMap(x => x ? [x] : []);
|
|
146
|
+
if (!orClauses.length) return null;
|
|
147
|
+
return x => orClauses.some(oc => oc(x));
|
|
148
|
+
}
|
|
149
|
+
const operatorFn = operatorFns[operator || "eq"];
|
|
150
|
+
if (operatorFn) {
|
|
151
|
+
return x => operatorFn(_lodash.default.get(x, name), whereVal);
|
|
152
|
+
}
|
|
153
|
+
console.warn(`[rads-cache] Operator not supported: ${operator || "eq"}`);
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
function prepareArgs(args) {
|
|
157
|
+
args = args || {};
|
|
158
|
+
const where = {
|
|
159
|
+
...args.where
|
|
160
|
+
};
|
|
161
|
+
let orderBy = args.orderBy || "";
|
|
162
|
+
if (Array.isArray(orderBy)) orderBy = orderBy[0] || "";
|
|
163
|
+
const orderByParts = orderBy.split("_");
|
|
164
|
+
const orderByProperty = orderByParts.slice(0, -1).join(".");
|
|
165
|
+
const orderByDirection = orderByParts.slice(-1)[0];
|
|
166
|
+
let maxItemCount = args.maxItemCount;
|
|
167
|
+
maxItemCount = maxItemCount || 100;
|
|
168
|
+
return {
|
|
169
|
+
where,
|
|
170
|
+
orderByProperty,
|
|
171
|
+
orderByDirection,
|
|
172
|
+
maxItemCount,
|
|
173
|
+
cursor: args.cursor
|
|
174
|
+
};
|
|
175
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import _ from "lodash";
|
|
2
|
+
const operatorFns = {
|
|
3
|
+
eq: (x, w) => x === w,
|
|
4
|
+
ieq: (x, w) => x?.toLowerCase() === w?.toLowerCase(),
|
|
5
|
+
in: (x, w) => w.includes(x),
|
|
6
|
+
startsWith: (x, w) => x?.startsWith(w),
|
|
7
|
+
istartsWith: (x, w) => x?.toLowerCase()?.startsWith(w.toLowerCase()),
|
|
8
|
+
endsWith: (x, w) => x?.endsWith(w),
|
|
9
|
+
iendsWith: (x, w) => x?.toLowerCase()?.endsWith(w.toLowerCase()),
|
|
10
|
+
contains: (x, w) => x?.includes(w),
|
|
11
|
+
icontains: (x, w) => x?.toLowerCase()?.includes(w.toLowerCase()),
|
|
12
|
+
isNull: (x, w) => {
|
|
13
|
+
if (w === true)
|
|
14
|
+
return x == null;
|
|
15
|
+
if (w === false)
|
|
16
|
+
return x != null;
|
|
17
|
+
return true;
|
|
18
|
+
},
|
|
19
|
+
gt: (x, w) => x > w,
|
|
20
|
+
gte: (x, w) => x >= w,
|
|
21
|
+
lt: (x, w) => x < w,
|
|
22
|
+
lte: (x, w) => x <= w,
|
|
23
|
+
between: (x, w) => {
|
|
24
|
+
const { start, end } = w || {};
|
|
25
|
+
let result = true;
|
|
26
|
+
if (start)
|
|
27
|
+
result = result && x >= start;
|
|
28
|
+
if (end)
|
|
29
|
+
result = result && x >= end;
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
export default (schema, entity) => {
|
|
34
|
+
const cache = {};
|
|
35
|
+
function getItemById(id) {
|
|
36
|
+
return cache[id] ?? null;
|
|
37
|
+
}
|
|
38
|
+
function getItemByIds(ids) {
|
|
39
|
+
return ids.map((id) => cache[id]);
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
// getItemById,
|
|
43
|
+
// getItemByIds,
|
|
44
|
+
putMany(items) {
|
|
45
|
+
for (const item of items) {
|
|
46
|
+
if (!item?.id)
|
|
47
|
+
throw new Error(`You must provide an id`);
|
|
48
|
+
cache[item.id] = item;
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
getMany(args) {
|
|
52
|
+
args = args || {};
|
|
53
|
+
const where = args.where || {};
|
|
54
|
+
const whereKeys = _.keys(where);
|
|
55
|
+
if (whereKeys.length === 1) {
|
|
56
|
+
if (whereKeys[0] === "id")
|
|
57
|
+
return { nodes: [getItemById(where.id)].filter((x) => x), cursor: null };
|
|
58
|
+
if (whereKeys[0] === "id_in")
|
|
59
|
+
return { nodes: getItemByIds(where.id_in).filter((x) => x), cursor: null };
|
|
60
|
+
}
|
|
61
|
+
return queryArray(Object.values(cache), args);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
function queryArray(array, args) {
|
|
66
|
+
if (!array)
|
|
67
|
+
return array;
|
|
68
|
+
let result = array;
|
|
69
|
+
const { where, orderByProperty, orderByDirection, maxItemCount, cursor } = prepareArgs(args);
|
|
70
|
+
const startIndex = Number(cursor) || 0;
|
|
71
|
+
const endIndex = startIndex + maxItemCount;
|
|
72
|
+
const f = getFilter(where);
|
|
73
|
+
if (f)
|
|
74
|
+
result = result.filter(f);
|
|
75
|
+
if (orderByProperty)
|
|
76
|
+
result = _.orderBy(result, [orderByProperty], [orderByDirection]);
|
|
77
|
+
if (maxItemCount)
|
|
78
|
+
result = result.slice(startIndex, endIndex);
|
|
79
|
+
const newCursor = endIndex >= array.length ? null : endIndex;
|
|
80
|
+
return { nodes: result, cursor: newCursor?.toString() || null };
|
|
81
|
+
}
|
|
82
|
+
function getFilter(where, namePrefix = "") {
|
|
83
|
+
if (_.isEmpty(where))
|
|
84
|
+
return null;
|
|
85
|
+
const andClauses = [];
|
|
86
|
+
for (const key in where) {
|
|
87
|
+
const [nameFromWhere, operator] = getWhereNameOperatorPair(key);
|
|
88
|
+
const name = [namePrefix, nameFromWhere].filter((x) => x).join(".");
|
|
89
|
+
const whereVal = where[key];
|
|
90
|
+
if (whereVal == null)
|
|
91
|
+
continue;
|
|
92
|
+
const f = getFilterInner(operator, whereVal, name, namePrefix);
|
|
93
|
+
if (f)
|
|
94
|
+
andClauses.push(f);
|
|
95
|
+
}
|
|
96
|
+
if (andClauses.length === 0)
|
|
97
|
+
return null;
|
|
98
|
+
return (x) => andClauses.every((ac) => ac(x));
|
|
99
|
+
}
|
|
100
|
+
function getWhereNameOperatorPair(whereKey) {
|
|
101
|
+
if (whereKey.startsWith("_type")) {
|
|
102
|
+
const operator = whereKey.split("_")[2];
|
|
103
|
+
return ["_type", operator];
|
|
104
|
+
}
|
|
105
|
+
return whereKey.split("_");
|
|
106
|
+
}
|
|
107
|
+
function getFilterInner(operator, whereVal, name, namePrefix) {
|
|
108
|
+
if (!operator && _.isObject(whereVal)) {
|
|
109
|
+
return getFilter(whereVal, name);
|
|
110
|
+
}
|
|
111
|
+
if (operator === "some") {
|
|
112
|
+
const f = getFilter(whereVal, namePrefix);
|
|
113
|
+
return f && ((x) => {
|
|
114
|
+
const val = _.get(x, name);
|
|
115
|
+
return val === void 0 || val?.some(f);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
if (operator === "none") {
|
|
119
|
+
const f = getFilter(whereVal, namePrefix);
|
|
120
|
+
return f && ((x) => !_.get(x, name)?.some(f));
|
|
121
|
+
}
|
|
122
|
+
if (operator === "and") {
|
|
123
|
+
if (!_.isArray(whereVal))
|
|
124
|
+
throw new Error(`Value for where._and must be an array`);
|
|
125
|
+
const newAndClauses = whereVal.map((whereValAnd) => getFilter(whereValAnd, namePrefix)).flatMap((x) => x ? [x] : []);
|
|
126
|
+
if (!newAndClauses.length)
|
|
127
|
+
return null;
|
|
128
|
+
return (x) => newAndClauses.every((ac) => ac(x));
|
|
129
|
+
}
|
|
130
|
+
if (operator === "not") {
|
|
131
|
+
const f = getFilter(whereVal, namePrefix);
|
|
132
|
+
return f && ((x) => !f(x));
|
|
133
|
+
}
|
|
134
|
+
if (operator === "or") {
|
|
135
|
+
if (!_.isArray(whereVal))
|
|
136
|
+
throw new Error(`Value for where._or must be an array`);
|
|
137
|
+
const orClauses = whereVal.map((whereValOr) => getFilter(whereValOr, namePrefix)).flatMap((x) => x ? [x] : []);
|
|
138
|
+
if (!orClauses.length)
|
|
139
|
+
return null;
|
|
140
|
+
return (x) => orClauses.some((oc) => oc(x));
|
|
141
|
+
}
|
|
142
|
+
const operatorFn = operatorFns[operator || "eq"];
|
|
143
|
+
if (operatorFn) {
|
|
144
|
+
return (x) => operatorFn(_.get(x, name), whereVal);
|
|
145
|
+
}
|
|
146
|
+
console.warn(`[rads-cache] Operator not supported: ${operator || "eq"}`);
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
function prepareArgs(args) {
|
|
150
|
+
args = args || {};
|
|
151
|
+
const where = { ...args.where };
|
|
152
|
+
let orderBy = args.orderBy || "";
|
|
153
|
+
if (Array.isArray(orderBy))
|
|
154
|
+
orderBy = orderBy[0] || "";
|
|
155
|
+
const orderByParts = orderBy.split("_");
|
|
156
|
+
const orderByProperty = orderByParts.slice(0, -1).join(".");
|
|
157
|
+
const orderByDirection = orderByParts.slice(-1)[0];
|
|
158
|
+
let maxItemCount = args.maxItemCount;
|
|
159
|
+
maxItemCount = maxItemCount || 100;
|
|
160
|
+
return { where, orderByProperty, orderByDirection, maxItemCount, cursor: args.cursor };
|
|
161
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
module.exports = void 0;
|
|
7
|
+
var _lodash = _interopRequireDefault(require("lodash"));
|
|
8
|
+
var _pluralize = _interopRequireDefault(require("pluralize"));
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
var _default = (schema, entity, options) => {
|
|
11
|
+
options = {
|
|
12
|
+
baseUrl: "/api",
|
|
13
|
+
fetch: globalThis.fetch,
|
|
14
|
+
...options
|
|
15
|
+
};
|
|
16
|
+
const fetch = options.fetch || global.fetch;
|
|
17
|
+
if (!options.fetch) throw new Error('Please provide "fetch" argument to rest driver definition');
|
|
18
|
+
const pluralEntityName = _lodash.default.lowerFirst((0, _pluralize.default)(entity));
|
|
19
|
+
return {
|
|
20
|
+
async getMany(args) {
|
|
21
|
+
const response = await fetch(`${options.baseUrl}/${pluralEntityName}`, {
|
|
22
|
+
method: "POST",
|
|
23
|
+
body: JSON.stringify(args)
|
|
24
|
+
});
|
|
25
|
+
return await response?.json();
|
|
26
|
+
},
|
|
27
|
+
async putMany(item) {
|
|
28
|
+
await fetch(`${options.baseUrl}/${pluralEntityName}`, {
|
|
29
|
+
method: "PUT",
|
|
30
|
+
body: JSON.stringify({
|
|
31
|
+
data: item
|
|
32
|
+
})
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
module.exports = _default;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import _ from "lodash";
|
|
2
|
+
import pluralize from "pluralize";
|
|
3
|
+
export default (schema, entity, options) => {
|
|
4
|
+
options = { baseUrl: "/api", fetch: globalThis.fetch, ...options };
|
|
5
|
+
const fetch = options.fetch || global.fetch;
|
|
6
|
+
if (!options.fetch)
|
|
7
|
+
throw new Error('Please provide "fetch" argument to rest driver definition');
|
|
8
|
+
const pluralEntityName = _.lowerFirst(pluralize(entity));
|
|
9
|
+
return {
|
|
10
|
+
async getMany(args) {
|
|
11
|
+
const response = await fetch(`${options.baseUrl}/${pluralEntityName}`, {
|
|
12
|
+
method: "POST",
|
|
13
|
+
body: JSON.stringify(args)
|
|
14
|
+
});
|
|
15
|
+
return await response?.json();
|
|
16
|
+
},
|
|
17
|
+
async putMany(item) {
|
|
18
|
+
await fetch(`${options.baseUrl}/${pluralEntityName}`, {
|
|
19
|
+
method: "PUT",
|
|
20
|
+
body: JSON.stringify({ data: item })
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
};
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.parseSchema = parseSchema;
|
|
7
|
+
var _typescript = require("typescript");
|
|
8
|
+
var _lodash = _interopRequireDefault(require("lodash"));
|
|
9
|
+
var _pluralize = _interopRequireDefault(require("pluralize"));
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
function parseSchema(typescriptFiles) {
|
|
12
|
+
const result = {};
|
|
13
|
+
const typesMap = {};
|
|
14
|
+
for (const key in typescriptFiles) {
|
|
15
|
+
const text = typescriptFiles[key];
|
|
16
|
+
const sourceFile = (0, _typescript.createSourceFile)(`${key}.ts`, text, _typescript.ScriptTarget.Latest);
|
|
17
|
+
const classDeclarations = getClassDeclarations(sourceFile);
|
|
18
|
+
for (const cd of classDeclarations) {
|
|
19
|
+
const nameNode = cd.name;
|
|
20
|
+
if (!nameNode || nameNode.kind !== _typescript.SyntaxKind.Identifier) throw new Error("Cannot detect class name");
|
|
21
|
+
const name = nameNode.text;
|
|
22
|
+
typesMap[name] = {
|
|
23
|
+
name,
|
|
24
|
+
node: cd,
|
|
25
|
+
sourceFile
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
for (const key in typesMap) {
|
|
30
|
+
const {
|
|
31
|
+
node,
|
|
32
|
+
sourceFile
|
|
33
|
+
} = typesMap[key];
|
|
34
|
+
const parsedClass = parseType(node, {
|
|
35
|
+
typesMap,
|
|
36
|
+
sourceFile
|
|
37
|
+
});
|
|
38
|
+
if (parsedClass.name !== key) throw new Error(`File name must correspond to exported class name ("${key}", "${parsedClass.name}")`);
|
|
39
|
+
result[key] = parsedClass;
|
|
40
|
+
}
|
|
41
|
+
for (const key in result) {
|
|
42
|
+
const isExtendingType = result[key].isExtending;
|
|
43
|
+
if (!isExtendingType) continue;
|
|
44
|
+
if (!result[isExtendingType]) throw new Error(`Unknown type: "${isExtendingType}"`);
|
|
45
|
+
result[key].fields = {
|
|
46
|
+
...result[isExtendingType].fields,
|
|
47
|
+
...result[key].fields
|
|
48
|
+
};
|
|
49
|
+
result[key].decorators = {
|
|
50
|
+
...result[isExtendingType].decorators,
|
|
51
|
+
...result[key].decorators
|
|
52
|
+
};
|
|
53
|
+
if (result[key].decorators.entity && !result[key].fields?.id) {
|
|
54
|
+
throw new Error(`Entity "${key}" must have an id`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
function parseType(typeDeclaration, ctx) {
|
|
60
|
+
const {
|
|
61
|
+
modifiers
|
|
62
|
+
} = typeDeclaration;
|
|
63
|
+
const nameNode = typeDeclaration.name;
|
|
64
|
+
if (!nameNode || nameNode.kind !== _typescript.SyntaxKind.Identifier) throw new Error("Cannot detect class name");
|
|
65
|
+
const name = nameNode.text;
|
|
66
|
+
const comment = typeDeclaration.jsDoc?.[0]?.comment;
|
|
67
|
+
const decoratorNodes = modifiers?.filter(x => x.kind === _typescript.SyntaxKind.Decorator) || [];
|
|
68
|
+
const decoratorsArray = decoratorNodes?.map(x => parseDecorator(x, ctx));
|
|
69
|
+
const decorators = {};
|
|
70
|
+
for (const d of decoratorsArray) {
|
|
71
|
+
decorators[d.name] = d.args;
|
|
72
|
+
}
|
|
73
|
+
if (typeDeclaration.kind === _typescript.SyntaxKind.ClassDeclaration) {
|
|
74
|
+
const classDeclaration = typeDeclaration;
|
|
75
|
+
const {
|
|
76
|
+
members,
|
|
77
|
+
heritageClauses
|
|
78
|
+
} = classDeclaration;
|
|
79
|
+
const isExtendingExpr = heritageClauses?.[0]?.types?.[0]?.expression;
|
|
80
|
+
const isExtending = isExtendingExpr?.text;
|
|
81
|
+
for (const m of members) {
|
|
82
|
+
if (m.kind !== _typescript.SyntaxKind.PropertyDeclaration) {
|
|
83
|
+
throw new Error(`Unexpected class member - only properties are allowed("${m.getText(ctx.sourceFile)}")`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const fieldsArray = members.map(m => parseClassMember(m, ctx));
|
|
87
|
+
const fields = {};
|
|
88
|
+
for (const f of fieldsArray) {
|
|
89
|
+
fields[f.name] = f;
|
|
90
|
+
}
|
|
91
|
+
const handle = _lodash.default.lowerFirst(name);
|
|
92
|
+
const handlePlural = (0, _pluralize.default)(handle);
|
|
93
|
+
return {
|
|
94
|
+
name,
|
|
95
|
+
handle,
|
|
96
|
+
handlePlural,
|
|
97
|
+
decorators,
|
|
98
|
+
fields,
|
|
99
|
+
isExtending,
|
|
100
|
+
comment
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
if (typeDeclaration.kind === _typescript.SyntaxKind.TypeAliasDeclaration) {
|
|
104
|
+
const typeAliasDeclaration = typeDeclaration;
|
|
105
|
+
if (typeAliasDeclaration.type.kind !== _typescript.SyntaxKind.UnionType) throw new Error(`Unexpected type definition - ${typeDeclaration.getText(ctx.sourceFile)}`);
|
|
106
|
+
const typeAliasValue = typeAliasDeclaration.type;
|
|
107
|
+
const enumValues = typeAliasValue.types.map(node => {
|
|
108
|
+
if (node.kind !== _typescript.SyntaxKind.LiteralType) {
|
|
109
|
+
throw new Error(`Unexpected type definition - ${typeDeclaration.getText(ctx.sourceFile)}`);
|
|
110
|
+
}
|
|
111
|
+
const {
|
|
112
|
+
literal
|
|
113
|
+
} = node;
|
|
114
|
+
if (literal.kind !== _typescript.SyntaxKind.StringLiteral) throw new Error(`Unexpected type definition - ${typeDeclaration.getText(ctx.sourceFile)}`);
|
|
115
|
+
return literal.text;
|
|
116
|
+
});
|
|
117
|
+
if (typeDeclaration) return {
|
|
118
|
+
name,
|
|
119
|
+
decorators,
|
|
120
|
+
enumValues,
|
|
121
|
+
comment
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
throw new Error(`Unexpected type kind - "${name}"`);
|
|
125
|
+
}
|
|
126
|
+
function parseClassMember(node, ctx) {
|
|
127
|
+
const supportedPrimitiveTypes = ["string", "number", "boolean"];
|
|
128
|
+
const name = node.name.getText(ctx.sourceFile);
|
|
129
|
+
const defaultValueDescription = parseLiteralNode(node.initializer, ctx);
|
|
130
|
+
const defaultValue = defaultValueDescription?.value;
|
|
131
|
+
const isRequired = !!node.exclamationToken || !!defaultValueDescription;
|
|
132
|
+
const comment = node.jsDoc?.[0]?.comment;
|
|
133
|
+
const type = node.type?.getText(ctx.sourceFile) ?? defaultValueDescription?.type;
|
|
134
|
+
if (!type) throw new Error(`Cannot detect property type: '${node.getText(ctx.sourceFile)}'`);
|
|
135
|
+
if (!supportedPrimitiveTypes.includes(type) && !ctx.typesMap[type]) throw new Error(`Unexpected property type: '${type}'`);
|
|
136
|
+
if (defaultValueDescription) {
|
|
137
|
+
if (supportedPrimitiveTypes.includes(type) && defaultValueDescription.type !== type) {
|
|
138
|
+
throw new Error(`Default value type is different from field type: '${type}'`);
|
|
139
|
+
}
|
|
140
|
+
const enumValues = ctx.typesMap[type]?.enumValues;
|
|
141
|
+
if (enumValues && enumValues.includes(defaultValue)) {
|
|
142
|
+
throw new Error(`Default value type is different from field type: '${type}'`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
type,
|
|
147
|
+
defaultValue,
|
|
148
|
+
name,
|
|
149
|
+
isRequired,
|
|
150
|
+
comment
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function parseDecorator(decoratorNode, ctx) {
|
|
154
|
+
const expr = decoratorNode.expression;
|
|
155
|
+
if (expr.kind !== _typescript.SyntaxKind.CallExpression) throw new Error(`Unexpected decorator format: "${expr.getText(ctx.sourceFile)}"`);
|
|
156
|
+
const nameNode = expr.expression;
|
|
157
|
+
if (nameNode.kind !== _typescript.SyntaxKind.Identifier) throw new Error(`Unexpected decorator format: "${nameNode.getText(ctx.sourceFile)}"`);
|
|
158
|
+
return {
|
|
159
|
+
name: nameNode.text,
|
|
160
|
+
args: parseDecoratorArguments(expr, ctx)
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function parseDecoratorArguments(expr, ctx) {
|
|
164
|
+
const args = expr.arguments;
|
|
165
|
+
if (args.length === 0) return {};
|
|
166
|
+
if (args.length > 1) throw new Error(`Too many arguments - one expected: "${expr.getText(ctx.sourceFile)}"`);
|
|
167
|
+
const arg = args[0];
|
|
168
|
+
return parseLiteralNode(arg, ctx)?.value ?? {};
|
|
169
|
+
}
|
|
170
|
+
function parseLiteralNode(expr, ctx) {
|
|
171
|
+
if (!expr) return void 0;
|
|
172
|
+
if (expr.kind === _typescript.SyntaxKind.StringLiteral) return {
|
|
173
|
+
type: "string",
|
|
174
|
+
value: expr.text
|
|
175
|
+
};
|
|
176
|
+
if (expr.kind === _typescript.SyntaxKind.FalseKeyword) return {
|
|
177
|
+
type: "boolean",
|
|
178
|
+
value: false
|
|
179
|
+
};
|
|
180
|
+
if (expr.kind === _typescript.SyntaxKind.TrueKeyword) return {
|
|
181
|
+
type: "boolean",
|
|
182
|
+
value: true
|
|
183
|
+
};
|
|
184
|
+
if (expr.kind === _typescript.SyntaxKind.NumericLiteral) return {
|
|
185
|
+
type: "number",
|
|
186
|
+
value: Number.parseFloat(expr.text)
|
|
187
|
+
};
|
|
188
|
+
if (expr.kind === _typescript.SyntaxKind.ObjectLiteralExpression) return {
|
|
189
|
+
type: "object",
|
|
190
|
+
value: parseObjectLiteral(expr, ctx)
|
|
191
|
+
};
|
|
192
|
+
throw new Error(`Unexpected property expression: "${expr.getText(ctx.sourceFile)}"`);
|
|
193
|
+
}
|
|
194
|
+
function parseObjectLiteral(arg, ctx) {
|
|
195
|
+
const result = {};
|
|
196
|
+
for (const p of arg.properties) {
|
|
197
|
+
if (!p.name || ![_typescript.SyntaxKind.Identifier, _typescript.SyntaxKind.StringLiteral].includes(p.name.kind)) throw new Error(`Unexpected property name: "${p.getText(ctx.sourceFile)}"`);
|
|
198
|
+
const nameNode = p.name;
|
|
199
|
+
const name = nameNode.text;
|
|
200
|
+
if (p.kind !== _typescript.SyntaxKind.PropertyAssignment) throw new Error(`Unexpected property value: "${p.getText(ctx.sourceFile)}"`);
|
|
201
|
+
const p2 = p;
|
|
202
|
+
const valueExpression = p2.initializer;
|
|
203
|
+
const value = parseLiteralNode(valueExpression, ctx)?.value;
|
|
204
|
+
result[name] = value;
|
|
205
|
+
}
|
|
206
|
+
return result;
|
|
207
|
+
}
|
|
208
|
+
function getClassDeclarations(sourceFile) {
|
|
209
|
+
const children = sourceFile.getChildren();
|
|
210
|
+
const syntaxListNode = children.find(c => c.kind === _typescript.SyntaxKind.SyntaxList);
|
|
211
|
+
const result = syntaxListNode?.getChildren().filter(c => [_typescript.SyntaxKind.ClassDeclaration, _typescript.SyntaxKind.TypeAliasDeclaration].includes(c.kind));
|
|
212
|
+
return result || [];
|
|
213
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function parseSchema(typescriptFiles: Record<string, string>): Record<string, any>;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { ScriptTarget, SyntaxKind, createSourceFile } from "typescript";
|
|
2
|
+
import _ from "lodash";
|
|
3
|
+
import pluralize from "pluralize";
|
|
4
|
+
export function parseSchema(typescriptFiles) {
|
|
5
|
+
const result = {};
|
|
6
|
+
const typesMap = {};
|
|
7
|
+
for (const key in typescriptFiles) {
|
|
8
|
+
const text = typescriptFiles[key];
|
|
9
|
+
const sourceFile = createSourceFile(`${key}.ts`, text, ScriptTarget.Latest);
|
|
10
|
+
const classDeclarations = getClassDeclarations(sourceFile);
|
|
11
|
+
for (const cd of classDeclarations) {
|
|
12
|
+
const nameNode = cd.name;
|
|
13
|
+
if (!nameNode || nameNode.kind !== SyntaxKind.Identifier)
|
|
14
|
+
throw new Error("Cannot detect class name");
|
|
15
|
+
const name = nameNode.text;
|
|
16
|
+
typesMap[name] = { name, node: cd, sourceFile };
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
for (const key in typesMap) {
|
|
20
|
+
const { node, sourceFile } = typesMap[key];
|
|
21
|
+
const parsedClass = parseType(node, { typesMap, sourceFile });
|
|
22
|
+
if (parsedClass.name !== key)
|
|
23
|
+
throw new Error(`File name must correspond to exported class name ("${key}", "${parsedClass.name}")`);
|
|
24
|
+
result[key] = parsedClass;
|
|
25
|
+
}
|
|
26
|
+
for (const key in result) {
|
|
27
|
+
const isExtendingType = result[key].isExtending;
|
|
28
|
+
if (!isExtendingType)
|
|
29
|
+
continue;
|
|
30
|
+
if (!result[isExtendingType])
|
|
31
|
+
throw new Error(`Unknown type: "${isExtendingType}"`);
|
|
32
|
+
result[key].fields = { ...result[isExtendingType].fields, ...result[key].fields };
|
|
33
|
+
result[key].decorators = { ...result[isExtendingType].decorators, ...result[key].decorators };
|
|
34
|
+
if (result[key].decorators.entity && !result[key].fields?.id) {
|
|
35
|
+
throw new Error(`Entity "${key}" must have an id`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
function parseType(typeDeclaration, ctx) {
|
|
41
|
+
const { modifiers } = typeDeclaration;
|
|
42
|
+
const nameNode = typeDeclaration.name;
|
|
43
|
+
if (!nameNode || nameNode.kind !== SyntaxKind.Identifier)
|
|
44
|
+
throw new Error("Cannot detect class name");
|
|
45
|
+
const name = nameNode.text;
|
|
46
|
+
const comment = typeDeclaration.jsDoc?.[0]?.comment;
|
|
47
|
+
const decoratorNodes = modifiers?.filter((x) => x.kind === SyntaxKind.Decorator) || [];
|
|
48
|
+
const decoratorsArray = decoratorNodes?.map((x) => parseDecorator(x, ctx));
|
|
49
|
+
const decorators = {};
|
|
50
|
+
for (const d of decoratorsArray) {
|
|
51
|
+
decorators[d.name] = d.args;
|
|
52
|
+
}
|
|
53
|
+
if (typeDeclaration.kind === SyntaxKind.ClassDeclaration) {
|
|
54
|
+
const classDeclaration = typeDeclaration;
|
|
55
|
+
const { members, heritageClauses } = classDeclaration;
|
|
56
|
+
const isExtendingExpr = heritageClauses?.[0]?.types?.[0]?.expression;
|
|
57
|
+
const isExtending = isExtendingExpr?.text;
|
|
58
|
+
for (const m of members) {
|
|
59
|
+
if (m.kind !== SyntaxKind.PropertyDeclaration) {
|
|
60
|
+
throw new Error(`Unexpected class member - only properties are allowed("${m.getText(ctx.sourceFile)}")`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const fieldsArray = members.map((m) => parseClassMember(m, ctx));
|
|
64
|
+
const fields = {};
|
|
65
|
+
for (const f of fieldsArray) {
|
|
66
|
+
fields[f.name] = f;
|
|
67
|
+
}
|
|
68
|
+
const handle = _.lowerFirst(name);
|
|
69
|
+
const handlePlural = pluralize(handle);
|
|
70
|
+
return { name, handle, handlePlural, decorators, fields, isExtending, comment };
|
|
71
|
+
}
|
|
72
|
+
if (typeDeclaration.kind === SyntaxKind.TypeAliasDeclaration) {
|
|
73
|
+
const typeAliasDeclaration = typeDeclaration;
|
|
74
|
+
if (typeAliasDeclaration.type.kind !== SyntaxKind.UnionType)
|
|
75
|
+
throw new Error(`Unexpected type definition - ${typeDeclaration.getText(ctx.sourceFile)}`);
|
|
76
|
+
const typeAliasValue = typeAliasDeclaration.type;
|
|
77
|
+
const enumValues = typeAliasValue.types.map((node) => {
|
|
78
|
+
if (node.kind !== SyntaxKind.LiteralType) {
|
|
79
|
+
throw new Error(`Unexpected type definition - ${typeDeclaration.getText(ctx.sourceFile)}`);
|
|
80
|
+
}
|
|
81
|
+
const { literal } = node;
|
|
82
|
+
if (literal.kind !== SyntaxKind.StringLiteral)
|
|
83
|
+
throw new Error(`Unexpected type definition - ${typeDeclaration.getText(ctx.sourceFile)}`);
|
|
84
|
+
return literal.text;
|
|
85
|
+
});
|
|
86
|
+
if (typeDeclaration)
|
|
87
|
+
return { name, decorators, enumValues, comment };
|
|
88
|
+
}
|
|
89
|
+
throw new Error(`Unexpected type kind - "${name}"`);
|
|
90
|
+
}
|
|
91
|
+
function parseClassMember(node, ctx) {
|
|
92
|
+
const supportedPrimitiveTypes = ["string", "number", "boolean"];
|
|
93
|
+
const name = node.name.getText(ctx.sourceFile);
|
|
94
|
+
const defaultValueDescription = parseLiteralNode(node.initializer, ctx);
|
|
95
|
+
const defaultValue = defaultValueDescription?.value;
|
|
96
|
+
const isRequired = !!node.exclamationToken || !!defaultValueDescription;
|
|
97
|
+
const comment = node.jsDoc?.[0]?.comment;
|
|
98
|
+
const type = node.type?.getText(ctx.sourceFile) ?? defaultValueDescription?.type;
|
|
99
|
+
if (!type)
|
|
100
|
+
throw new Error(`Cannot detect property type: '${node.getText(ctx.sourceFile)}'`);
|
|
101
|
+
if (!supportedPrimitiveTypes.includes(type) && !ctx.typesMap[type])
|
|
102
|
+
throw new Error(`Unexpected property type: '${type}'`);
|
|
103
|
+
if (defaultValueDescription) {
|
|
104
|
+
if (supportedPrimitiveTypes.includes(type) && defaultValueDescription.type !== type) {
|
|
105
|
+
throw new Error(`Default value type is different from field type: '${type}'`);
|
|
106
|
+
}
|
|
107
|
+
const enumValues = ctx.typesMap[type]?.enumValues;
|
|
108
|
+
if (enumValues && enumValues.includes(defaultValue)) {
|
|
109
|
+
throw new Error(`Default value type is different from field type: '${type}'`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return { type, defaultValue, name, isRequired, comment };
|
|
113
|
+
}
|
|
114
|
+
function parseDecorator(decoratorNode, ctx) {
|
|
115
|
+
const expr = decoratorNode.expression;
|
|
116
|
+
if (expr.kind !== SyntaxKind.CallExpression)
|
|
117
|
+
throw new Error(`Unexpected decorator format: "${expr.getText(ctx.sourceFile)}"`);
|
|
118
|
+
const nameNode = expr.expression;
|
|
119
|
+
if (nameNode.kind !== SyntaxKind.Identifier)
|
|
120
|
+
throw new Error(`Unexpected decorator format: "${nameNode.getText(ctx.sourceFile)}"`);
|
|
121
|
+
return { name: nameNode.text, args: parseDecoratorArguments(expr, ctx) };
|
|
122
|
+
}
|
|
123
|
+
function parseDecoratorArguments(expr, ctx) {
|
|
124
|
+
const args = expr.arguments;
|
|
125
|
+
if (args.length === 0)
|
|
126
|
+
return {};
|
|
127
|
+
if (args.length > 1)
|
|
128
|
+
throw new Error(`Too many arguments - one expected: "${expr.getText(ctx.sourceFile)}"`);
|
|
129
|
+
const arg = args[0];
|
|
130
|
+
return parseLiteralNode(arg, ctx)?.value ?? {};
|
|
131
|
+
}
|
|
132
|
+
function parseLiteralNode(expr, ctx) {
|
|
133
|
+
if (!expr)
|
|
134
|
+
return void 0;
|
|
135
|
+
if (expr.kind === SyntaxKind.StringLiteral)
|
|
136
|
+
return { type: "string", value: expr.text };
|
|
137
|
+
if (expr.kind === SyntaxKind.FalseKeyword)
|
|
138
|
+
return { type: "boolean", value: false };
|
|
139
|
+
if (expr.kind === SyntaxKind.TrueKeyword)
|
|
140
|
+
return { type: "boolean", value: true };
|
|
141
|
+
if (expr.kind === SyntaxKind.NumericLiteral)
|
|
142
|
+
return { type: "number", value: Number.parseFloat(expr.text) };
|
|
143
|
+
if (expr.kind === SyntaxKind.ObjectLiteralExpression)
|
|
144
|
+
return { type: "object", value: parseObjectLiteral(expr, ctx) };
|
|
145
|
+
throw new Error(`Unexpected property expression: "${expr.getText(ctx.sourceFile)}"`);
|
|
146
|
+
}
|
|
147
|
+
function parseObjectLiteral(arg, ctx) {
|
|
148
|
+
const result = {};
|
|
149
|
+
for (const p of arg.properties) {
|
|
150
|
+
if (!p.name || ![SyntaxKind.Identifier, SyntaxKind.StringLiteral].includes(p.name.kind))
|
|
151
|
+
throw new Error(`Unexpected property name: "${p.getText(ctx.sourceFile)}"`);
|
|
152
|
+
const nameNode = p.name;
|
|
153
|
+
const name = nameNode.text;
|
|
154
|
+
if (p.kind !== SyntaxKind.PropertyAssignment)
|
|
155
|
+
throw new Error(`Unexpected property value: "${p.getText(ctx.sourceFile)}"`);
|
|
156
|
+
const p2 = p;
|
|
157
|
+
const valueExpression = p2.initializer;
|
|
158
|
+
const value = parseLiteralNode(valueExpression, ctx)?.value;
|
|
159
|
+
result[name] = value;
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
function getClassDeclarations(sourceFile) {
|
|
164
|
+
const children = sourceFile.getChildren();
|
|
165
|
+
const syntaxListNode = children.find((c) => c.kind === SyntaxKind.SyntaxList);
|
|
166
|
+
const result = syntaxListNode?.getChildren().filter((c) => [SyntaxKind.ClassDeclaration, SyntaxKind.TypeAliasDeclaration].includes(c.kind));
|
|
167
|
+
return result || [];
|
|
168
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.radsDbNuxt = void 0;
|
|
7
|
+
var _kit = require("@nuxt/kit");
|
|
8
|
+
const radsDbNuxt = (0, _kit.defineNuxtModule)({
|
|
9
|
+
meta: {
|
|
10
|
+
name: "rads-db",
|
|
11
|
+
compatibility: {
|
|
12
|
+
nuxt: "^3.0.0"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
defaults: {},
|
|
16
|
+
setup(options, nuxt) {
|
|
17
|
+
const {
|
|
18
|
+
resolve
|
|
19
|
+
} = (0, _kit.createResolver)(require('url').pathToFileURL(__filename).toString());
|
|
20
|
+
(0, _kit.addServerHandler)({
|
|
21
|
+
handler: resolve("./nuxtModuleHandler")
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
exports.radsDbNuxt = radsDbNuxt;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const radsDbNuxt: import("@nuxt/schema").NuxtModule<{}>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { addServerHandler, createResolver, defineNuxtModule } from "@nuxt/kit";
|
|
2
|
+
export const radsDbNuxt = defineNuxtModule({
|
|
3
|
+
meta: {
|
|
4
|
+
name: "rads-db",
|
|
5
|
+
compatibility: {
|
|
6
|
+
nuxt: "^3.0.0"
|
|
7
|
+
}
|
|
8
|
+
},
|
|
9
|
+
defaults: {},
|
|
10
|
+
setup(options, nuxt) {
|
|
11
|
+
const { resolve } = createResolver(import.meta.url);
|
|
12
|
+
addServerHandler({
|
|
13
|
+
handler: resolve("./nuxtModuleHandler")
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
module.exports = void 0;
|
|
7
|
+
var _h = require("h3");
|
|
8
|
+
var _schemaGenerated = _interopRequireDefault(require("@@/schema.generated.json"));
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
const routes = getRoutes(_schemaGenerated.default);
|
|
11
|
+
var _default = (0, _h.defineEventHandler)(event => {
|
|
12
|
+
const path = event.path.split("?")[0];
|
|
13
|
+
const method = (0, _h.getMethod)(event);
|
|
14
|
+
const p = (0, _h.getRequestPath)(event);
|
|
15
|
+
const query = (0, _h.getQuery)(event);
|
|
16
|
+
const rp = (0, _h.getRouterParams)(event);
|
|
17
|
+
console.log(path, p, query, rp);
|
|
18
|
+
if (method === "POST") {}
|
|
19
|
+
if (path === `${prefix}tcUser`) {
|
|
20
|
+
console.log("tcUser!");
|
|
21
|
+
return {
|
|
22
|
+
id: "15"
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
if (path === `${prefix}tcUsers`) {
|
|
26
|
+
console.log("tcUsers!");
|
|
27
|
+
return [{
|
|
28
|
+
id: "15"
|
|
29
|
+
}];
|
|
30
|
+
}
|
|
31
|
+
console.log(_schemaGenerated.default);
|
|
32
|
+
});
|
|
33
|
+
module.exports = _default;
|
|
34
|
+
function getRoutes(entities) {
|
|
35
|
+
const prefix2 = "/api/";
|
|
36
|
+
const routes2 = {};
|
|
37
|
+
for (const entityKey in entities) {}
|
|
38
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { defineEventHandler, getMethod, getQuery, getRequestPath, getRouterParams } from "h3";
|
|
2
|
+
import schema from "@@/schema.generated.json";
|
|
3
|
+
const routes = getRoutes(schema);
|
|
4
|
+
export default defineEventHandler((event) => {
|
|
5
|
+
const path = event.path.split("?")[0];
|
|
6
|
+
const method = getMethod(event);
|
|
7
|
+
const p = getRequestPath(event);
|
|
8
|
+
const query = getQuery(event);
|
|
9
|
+
const rp = getRouterParams(event);
|
|
10
|
+
console.log(path, p, query, rp);
|
|
11
|
+
if (method === "POST") {
|
|
12
|
+
}
|
|
13
|
+
if (path === `${prefix}tcUser`) {
|
|
14
|
+
console.log("tcUser!");
|
|
15
|
+
return { id: "15" };
|
|
16
|
+
}
|
|
17
|
+
if (path === `${prefix}tcUsers`) {
|
|
18
|
+
console.log("tcUsers!");
|
|
19
|
+
return [{ id: "15" }];
|
|
20
|
+
}
|
|
21
|
+
console.log(schema);
|
|
22
|
+
});
|
|
23
|
+
function getRoutes(entities) {
|
|
24
|
+
const prefix2 = "/api/";
|
|
25
|
+
const routes2 = {};
|
|
26
|
+
for (const entityKey in entities) {
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.radsDbVite = radsDbVite;
|
|
7
|
+
var _promises = _interopRequireDefault(require("node:fs/promises"));
|
|
8
|
+
var _nodePath = _interopRequireDefault(require("node:path"));
|
|
9
|
+
var _node = require("./node.cjs");
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
function radsDbVite(options) {
|
|
12
|
+
let isDev = false;
|
|
13
|
+
return {
|
|
14
|
+
name: "rads-db",
|
|
15
|
+
config(config, env) {
|
|
16
|
+
isDev = env.mode === "development";
|
|
17
|
+
},
|
|
18
|
+
async configResolved(config) {
|
|
19
|
+
if (!isDev) return;
|
|
20
|
+
console.time("[rads-db] Schema generated in");
|
|
21
|
+
const entitiesDir = options?.entitiesDir || "./entities";
|
|
22
|
+
const response = await _promises.default.readdir(entitiesDir, {
|
|
23
|
+
withFileTypes: true
|
|
24
|
+
});
|
|
25
|
+
const entities = {};
|
|
26
|
+
for (const file of response) {
|
|
27
|
+
if (!file.isFile()) continue;
|
|
28
|
+
if (!file.name.endsWith(".ts")) continue;
|
|
29
|
+
const text = await _promises.default.readFile(_nodePath.default.resolve(entitiesDir, file.name), "utf-8");
|
|
30
|
+
entities[file.name.slice(0, -3)] = text;
|
|
31
|
+
}
|
|
32
|
+
const schema = (0, _node.parseSchema)(entities);
|
|
33
|
+
await _promises.default.writeFile("./schema.generated.json", JSON.stringify(schema, null, 2));
|
|
34
|
+
console.timeEnd("[rads-db] Schema generated in");
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { parseSchema } from "./node.mjs";
|
|
4
|
+
export function radsDbVite(options) {
|
|
5
|
+
let isDev = false;
|
|
6
|
+
return {
|
|
7
|
+
name: "rads-db",
|
|
8
|
+
config(config, env) {
|
|
9
|
+
isDev = env.mode === "development";
|
|
10
|
+
},
|
|
11
|
+
async configResolved(config) {
|
|
12
|
+
if (!isDev)
|
|
13
|
+
return;
|
|
14
|
+
console.time("[rads-db] Schema generated in");
|
|
15
|
+
const entitiesDir = options?.entitiesDir || "./entities";
|
|
16
|
+
const response = await fs.readdir(entitiesDir, { withFileTypes: true });
|
|
17
|
+
const entities = {};
|
|
18
|
+
for (const file of response) {
|
|
19
|
+
if (!file.isFile())
|
|
20
|
+
continue;
|
|
21
|
+
if (!file.name.endsWith(".ts"))
|
|
22
|
+
continue;
|
|
23
|
+
const text = await fs.readFile(path.resolve(entitiesDir, file.name), "utf-8");
|
|
24
|
+
entities[file.name.slice(0, -3)] = text;
|
|
25
|
+
}
|
|
26
|
+
const schema = parseSchema(entities);
|
|
27
|
+
await fs.writeFile("./schema.generated.json", JSON.stringify(schema, null, 2));
|
|
28
|
+
console.timeEnd("[rads-db] Schema generated in");
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rads-db",
|
|
3
3
|
"files": [
|
|
4
|
-
"dist"
|
|
4
|
+
"dist",
|
|
5
|
+
"drivers",
|
|
6
|
+
"integrations"
|
|
5
7
|
],
|
|
6
8
|
"main": "./dist/index.cjs",
|
|
7
9
|
"module": "./dist/index.mjs",
|
|
@@ -23,7 +25,7 @@
|
|
|
23
25
|
"require": "./integrations/*.cjs"
|
|
24
26
|
}
|
|
25
27
|
},
|
|
26
|
-
"version": "0.1.
|
|
28
|
+
"version": "0.1.1",
|
|
27
29
|
"description": "Say goodbye to boilerplate code and hello to efficient and elegant syntax.",
|
|
28
30
|
"keywords": [],
|
|
29
31
|
"author": "",
|