uranio 0.1.12 → 0.1.14

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.
@@ -23,6 +23,6 @@
23
23
  "typescript": "^5.2.2"
24
24
  },
25
25
  "dependencies": {
26
- "uranio": "0.1.12"
26
+ "uranio": "0.1.14"
27
27
  }
28
28
  }
@@ -9,15 +9,17 @@
9
9
  export * from './query';
10
10
 
11
11
  export interface atom {
12
- _id: primary<string>
12
+ _id: string
13
13
  }
14
14
 
15
- export type primary<T> = T & {__uranio: 'primary'};
15
+ // export type primary<T> = T & {__uranio: 'primary'};
16
16
 
17
17
  export type unique<T> = T & {__uranio: 'unique'};
18
18
 
19
- type PrimaryAttribute<A extends atom> = {
20
- [K in keyof A]: A[K] extends {__uranio: 'primary'} ? K : never;
21
- }[keyof A];
19
+ // type PrimaryAttribute<A extends atom> = {
20
+ // [K in keyof A]: A[K] extends {__uranio: 'primary'} ? K : never;
21
+ // }[keyof A];
22
22
 
23
- export type Shape<A extends atom> = Omit<A, PrimaryAttribute<A>>;
23
+ // export type Shape<A extends atom> = Omit<A, PrimaryAttribute<A>>;
24
+
25
+ export type Shape<A extends atom> = Omit<A, '_id'>;
package/README.md CHANGED
@@ -64,7 +64,31 @@ For example:
64
64
 
65
65
  ```typescript
66
66
  interface Customer extends uranio.atom {
67
- _id: uranio.primary<string>;
68
67
  email: uranio.unique<string>;
69
68
  }
70
69
  ```
70
+
71
+ ### Primary index `_id`
72
+
73
+ When extending an interface with `uranio.atom` this add a primary index
74
+ attribute `_id` to the interface, so there is no need to add it manually.
75
+
76
+ ```typescript
77
+ import uranio from 'uranio';
78
+
79
+ interface Product extends uranio.atom {
80
+ title: string;
81
+ description: string;
82
+ price: number;
83
+ }
84
+ // It resolves in:
85
+ // {
86
+ // _id: string;
87
+ // title: string;
88
+ // description: string;
89
+ // price: number;
90
+ // }
91
+ ```
92
+
93
+
94
+
package/dist/bin.js CHANGED
@@ -1,13 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
3
  Object.defineProperty(exports, "__esModule", { value: true });
7
- const i0n_1 = __importDefault(require("i0n"));
8
- const log = i0n_1.default.create();
9
4
  const index_1 = require("./generate/index");
10
- const tsconfig_path = `/Users/x71c9/repos/uranio/builder/tsconfig.json`;
11
- const response = (0, index_1.generate)(tsconfig_path);
12
- log.debug(response);
5
+ const root = `/Users/x71c9/repos/uranio/builder`;
6
+ (0, index_1.generate)({ root });
13
7
  //# sourceMappingURL=bin.js.map
package/dist/bin.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"bin.js","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";;;;;;AAEA,8CAAsB;AACtB,MAAM,GAAG,GAAG,aAAG,CAAC,MAAM,EAAE,CAAC;AACzB,4CAA0C;AAC1C,MAAM,aAAa,GAAG,iDAAiD,CAAC;AACxE,MAAM,QAAQ,GAAG,IAAA,gBAAQ,EAAC,aAAa,CAAC,CAAC;AACzC,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC"}
1
+ {"version":3,"file":"bin.js","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";;;AAEA,4CAA0C;AAC1C,MAAM,IAAI,GAAG,mCAAmC,CAAC;AACjD,IAAA,gBAAQ,EAAC,EAAC,IAAI,EAAC,CAAC,CAAC"}
@@ -2,5 +2,11 @@
2
2
  *
3
3
  * Generate index module
4
4
  *
5
+ * @packageDocumentation
5
6
  */
6
- export declare function generate(tsconfig_path: string): import("plutonio/dist/scanner/types").Scanned;
7
+ type GenerateParams = {
8
+ root: string;
9
+ tsconfig_path?: string;
10
+ };
11
+ export declare function generate(params: GenerateParams): Promise<void>;
12
+ export {};
@@ -3,16 +3,176 @@
3
3
  *
4
4
  * Generate index module
5
5
  *
6
+ * @packageDocumentation
6
7
  */
8
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
+ if (k2 === undefined) k2 = k;
10
+ var desc = Object.getOwnPropertyDescriptor(m, k);
11
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
12
+ desc = { enumerable: true, get: function() { return m[k]; } };
13
+ }
14
+ Object.defineProperty(o, k2, desc);
15
+ }) : (function(o, m, k, k2) {
16
+ if (k2 === undefined) k2 = k;
17
+ o[k2] = m[k];
18
+ }));
19
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
20
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
21
+ }) : function(o, v) {
22
+ o["default"] = v;
23
+ });
24
+ var __importStar = (this && this.__importStar) || function (mod) {
25
+ if (mod && mod.__esModule) return mod;
26
+ var result = {};
27
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
28
+ __setModuleDefault(result, mod);
29
+ return result;
30
+ };
7
31
  var __importDefault = (this && this.__importDefault) || function (mod) {
8
32
  return (mod && mod.__esModule) ? mod : { "default": mod };
9
33
  };
10
34
  Object.defineProperty(exports, "__esModule", { value: true });
11
35
  exports.generate = void 0;
36
+ const fs_1 = __importDefault(require("fs"));
37
+ const r4y_1 = __importDefault(require("r4y"));
12
38
  const plutonio_1 = __importDefault(require("plutonio"));
13
- function generate(tsconfig_path) {
14
- const scanned = plutonio_1.default.scanner(tsconfig_path);
15
- return scanned;
39
+ const index_1 = require("./log/index");
40
+ const utils = __importStar(require("../utils/index"));
41
+ const debug = (process.env.NODE_ENV === 'dev');
42
+ r4y_1.default.config.set({ debug, spinner: index_1.log.spinner });
43
+ async function generate(params) {
44
+ index_1.log.spinner.start();
45
+ index_1.log.spinner.text(`Generating...`);
46
+ await _copy_dot_uranio(params);
47
+ await _update_dot_uranio(params);
48
+ await _build_dot_uranio(params);
49
+ index_1.log.spinner.stop();
50
+ index_1.log.success(`Uranio successfully generated client`);
16
51
  }
17
52
  exports.generate = generate;
53
+ async function _copy_dot_uranio(params) {
54
+ index_1.log.spinner.text(`Coping dot uranio...`);
55
+ const dot_path = `${params.root}/node_modules/uranio/.uranio`;
56
+ const destination_path = `${params.root}/node_modules/.uranio`;
57
+ index_1.log.spinner.stop();
58
+ await r4y_1.default.spawn(`rm -rf ${destination_path}`);
59
+ await r4y_1.default.spawn(`cp -rf ${dot_path} ${destination_path}`);
60
+ index_1.log.spinner.start();
61
+ index_1.log.debug(`Copied dot uranio directory`);
62
+ }
63
+ async function _update_dot_uranio(params) {
64
+ index_1.log.spinner.text(`Updating dot uranio files...`);
65
+ const uranio_extended_interfaces = _get_uranio_extended_interfaces(params);
66
+ const text = _generate_uranio_client_module_text(uranio_extended_interfaces);
67
+ const uranio_client_path = `${params.root}/node_modules/.uranio/src/uranio-client.ts`;
68
+ fs_1.default.writeFileSync(uranio_client_path, text);
69
+ index_1.log.debug(`Updated dot uranio files`);
70
+ }
71
+ async function _build_dot_uranio(params) {
72
+ index_1.log.spinner.text(`Transpiling dot uranio files...`);
73
+ const copied_dot_uranio_tsconfig_path = `${params.root}/node_modules/.uranio/tsconfig.json`;
74
+ await r4y_1.default.spawn(`yarn tsc --project ${copied_dot_uranio_tsconfig_path}`);
75
+ index_1.log.debug(`Transpiled dot uranio files`);
76
+ }
77
+ function _resolve_tsconfig_path(params) {
78
+ if (utils.valid.string(params.tsconfig_path)) {
79
+ return params.tsconfig_path;
80
+ }
81
+ return `${params.root}/tsconfig.json`;
82
+ }
83
+ function _get_uranio_extended_interfaces(params) {
84
+ const tsconfig_path = _resolve_tsconfig_path(params);
85
+ const scanned = plutonio_1.default.scan(tsconfig_path);
86
+ // log.trace(scanned);
87
+ const uranio_extended_interfaces = {};
88
+ for (const [_source_path, source] of Object.entries(scanned)) {
89
+ const interfaces = source.interfaces;
90
+ const imports = source.imports;
91
+ if (!interfaces || !imports) {
92
+ continue;
93
+ }
94
+ let uranio_clause;
95
+ for (const [_name, imp] of Object.entries(imports)) {
96
+ if (imp.module === 'uranio') {
97
+ uranio_clause = imp.clause;
98
+ break;
99
+ }
100
+ }
101
+ const atom_definition = `${uranio_clause}.atom`;
102
+ for (const [name, inter] of Object.entries(interfaces)) {
103
+ if (!inter.extends || !inter.extends.includes(atom_definition)) {
104
+ continue;
105
+ }
106
+ if (name in uranio_extended_interfaces) {
107
+ index_1.log.warn(`An Atom with the same name [${name}] already exists. Overriding...`);
108
+ }
109
+ uranio_extended_interfaces[name] = inter;
110
+ }
111
+ }
112
+ // log.trace(uranio_extended_interfaces);
113
+ return uranio_extended_interfaces;
114
+ }
115
+ function _generate_uranio_client_module_text(interfaces) {
116
+ let text = '';
117
+ text += `/**\n`;
118
+ text += ` *\n`;
119
+ text += ` * [Auto-generated module by "uranio generate" command]\n`;
120
+ text += ` *\n`;
121
+ text += ` * UranioClient module\n`;
122
+ text += ` *\n`;
123
+ text += ` */\n`;
124
+ text += `\n`;
125
+ text += `import {Client, ClientParams} from './client';\n`;
126
+ text += `import {AtomClient} from './atom';\n`;
127
+ text += `import {atom} from './types';\n`;
128
+ text += `\n`;
129
+ text += _generate_interface_definitions(interfaces);
130
+ text += `export class UranioClient extends Client{\n`;
131
+ text += _generate_client_class_attributes(interfaces);
132
+ text += ` constructor(params: ClientParams) {\n`;
133
+ text += ` super(params);\n`;
134
+ text += _generate_client_initialization(interfaces);
135
+ text += ` }\n`;
136
+ text += `}\n`;
137
+ return text;
138
+ }
139
+ function _generate_interface_definitions(interfaces) {
140
+ let text = '';
141
+ for (const [name, inter] of Object.entries(interfaces)) {
142
+ text += `interface ${name} extends atom {\n`;
143
+ if (!inter.properties) {
144
+ text += `}\n`;
145
+ text += `\n`;
146
+ continue;
147
+ }
148
+ for (const [prop_name, prop] of Object.entries(inter.properties)) {
149
+ text += ` ${prop_name}: ${prop.primitive};\n`;
150
+ }
151
+ text += `}\n`;
152
+ text += `\n`;
153
+ }
154
+ return text;
155
+ }
156
+ function _generate_client_class_attributes(interfaces) {
157
+ let text = '';
158
+ for (const [name, _inter] of Object.entries(interfaces)) {
159
+ let lc = _first_letter_lowercase(name);
160
+ text += ` public ${lc}: AtomClient<${name}>;\n`;
161
+ }
162
+ return text;
163
+ }
164
+ function _generate_client_initialization(interfaces) {
165
+ let text = '';
166
+ for (const [name, _inter] of Object.entries(interfaces)) {
167
+ let lc = _first_letter_lowercase(name);
168
+ text += ` this.${lc} = new AtomClient<${name}>(this.db, '${lc}');\n`;
169
+ }
170
+ return text;
171
+ }
172
+ function _first_letter_lowercase(str) {
173
+ if (typeof str !== 'string' || str.length === 0) {
174
+ return str;
175
+ }
176
+ return str.charAt(0).toLowerCase() + str.slice(1);
177
+ }
18
178
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/generate/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;;;;AAEH,wDAAgC;AAEhC,SAAgB,QAAQ,CAAC,aAAqB;IAC5C,MAAM,OAAO,GAAG,kBAAQ,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;IAChD,OAAO,OAAO,CAAC;AACjB,CAAC;AAHD,4BAGC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/generate/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,4CAAoB;AACpB,8CAAsB;AACtB,wDAAgC;AAChC,uCAAgC;AAChC,sDAAwC;AAExC,MAAM,KAAK,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,KAAK,CAAC,CAAC;AAC/C,aAAG,CAAC,MAAM,CAAC,GAAG,CAAC,EAAC,KAAK,EAAE,OAAO,EAAE,WAAG,CAAC,OAAO,EAAC,CAAC,CAAC;AAOvC,KAAK,UAAU,QAAQ,CAAC,MAAsB;IACnD,WAAG,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;IACpB,WAAG,CAAC,OAAO,CAAC,IAAI,CAAC,e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AAC,OAAO,CAAC,UAAU,CAAC,EAAC,CAAC;QACtD,IAAI,EAAE,GAAG,uBAAuB,CAAC,IAAI,CAAC,CAAC;QACvC,IAAI,IAAI,YAAY,EAAE,qBAAqB,IAAI,eAAe,EAAE,OAAO,CAAC;IAC1E,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,uBAAuB,CAAC,GAAW;IAC1C,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAChD,OAAO,GAAG,CAAC;IACb,CAAC;IACD,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACpD,CAAC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ *
3
+ * Log index module
4
+ *
5
+ * @packageDocumentation
6
+ *
7
+ */
8
+ import ion from 'i0n';
9
+ export declare const log: ReturnType<typeof ion.create>;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ /**
3
+ *
4
+ * Log index module
5
+ *
6
+ * @packageDocumentation
7
+ *
8
+ */
9
+ var __importDefault = (this && this.__importDefault) || function (mod) {
10
+ return (mod && mod.__esModule) ? mod : { "default": mod };
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.log = void 0;
14
+ const i0n_1 = __importDefault(require("i0n"));
15
+ exports.log = i0n_1.default.create({
16
+ log_level: i0n_1.default.LOG_LEVEL.TRACE
17
+ });
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/generate/log/index.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;;;AAEH,8CAAsB;AACT,QAAA,GAAG,GAAiC,aAAG,CAAC,MAAM,CAAC;IAC1D,SAAS,EAAE,aAAG,CAAC,SAAS,CAAC,KAAK;CAC/B,CAAC,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ *
3
+ * Utils index module
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+ import * as valid from './valid';
8
+ export { valid };
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ /**
3
+ *
4
+ * Utils index module
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
+ if (k2 === undefined) k2 = k;
10
+ var desc = Object.getOwnPropertyDescriptor(m, k);
11
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
12
+ desc = { enumerable: true, get: function() { return m[k]; } };
13
+ }
14
+ Object.defineProperty(o, k2, desc);
15
+ }) : (function(o, m, k, k2) {
16
+ if (k2 === undefined) k2 = k;
17
+ o[k2] = m[k];
18
+ }));
19
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
20
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
21
+ }) : function(o, v) {
22
+ o["default"] = v;
23
+ });
24
+ var __importStar = (this && this.__importStar) || function (mod) {
25
+ if (mod && mod.__esModule) return mod;
26
+ var result = {};
27
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
28
+ __setModuleDefault(result, mod);
29
+ return result;
30
+ };
31
+ Object.defineProperty(exports, "__esModule", { value: true });
32
+ exports.valid = void 0;
33
+ const valid = __importStar(require("./valid"));
34
+ exports.valid = valid;
35
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,+CAAiC;AACzB,sBAAK"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ *
3
+ * Valid module
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+ /**
8
+ * Check if
9
+ * typeof s === 'string' && s !== ''
10
+ */
11
+ export declare function string(s: unknown): s is string;
12
+ /**
13
+ * Check if
14
+ * !!o && typeof o === 'object
15
+ */
16
+ export declare function object(o: unknown): o is Record<string, any>;
17
+ /**
18
+ * Check if
19
+ * typeof n === 'number' && !isNaN(n);
20
+ */
21
+ export declare function number(n: unknown): n is number;
22
+ /**
23
+ * Check if
24
+ * typeof n === 'boolean';
25
+ */
26
+ export declare function boolean(b: unknown): b is boolean;
27
+ /**
28
+ * Check if string is a valid email
29
+ * /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/
30
+ */
31
+ export declare function email(e: unknown): e is string;
32
+ /**
33
+ * Check if string is a alphanumeric only (including space)
34
+ * /^[a-zA-Z0-9\s]+$/
35
+ */
36
+ export declare function alphanumeric(s: unknown): s is string;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ /**
3
+ *
4
+ * Valid module
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.alphanumeric = exports.email = exports.boolean = exports.number = exports.object = exports.string = void 0;
10
+ /**
11
+ * Check if
12
+ * typeof s === 'string' && s !== ''
13
+ */
14
+ function string(s) {
15
+ return typeof s === 'string' && s !== '';
16
+ }
17
+ exports.string = string;
18
+ /**
19
+ * Check if
20
+ * !!o && typeof o === 'object
21
+ */
22
+ function object(o) {
23
+ return !!o && typeof o === 'object';
24
+ }
25
+ exports.object = object;
26
+ /**
27
+ * Check if
28
+ * typeof n === 'number' && !isNaN(n);
29
+ */
30
+ function number(n) {
31
+ return typeof n === 'number' && !isNaN(n);
32
+ }
33
+ exports.number = number;
34
+ /**
35
+ * Check if
36
+ * typeof n === 'boolean';
37
+ */
38
+ function boolean(b) {
39
+ return typeof b === 'boolean';
40
+ }
41
+ exports.boolean = boolean;
42
+ /**
43
+ * Check if string is a valid email
44
+ * /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/
45
+ */
46
+ function email(e) {
47
+ if (typeof e !== 'string') {
48
+ return false;
49
+ }
50
+ const pattern = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
51
+ return pattern.test(e);
52
+ }
53
+ exports.email = email;
54
+ /**
55
+ * Check if string is a alphanumeric only (including space)
56
+ * /^[a-zA-Z0-9\s]+$/
57
+ */
58
+ function alphanumeric(s) {
59
+ if (typeof s !== 'string') {
60
+ return false;
61
+ }
62
+ const pattern = /^[a-zA-Z0-9\s]+$/;
63
+ return pattern.test(s);
64
+ }
65
+ exports.alphanumeric = alphanumeric;
66
+ //# sourceMappingURL=valid.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"valid.js","sourceRoot":"","sources":["../../src/utils/valid.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH;;;GAGG;AACH,SAAgB,MAAM,CAAC,CAAU;IAC/B,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,EAAE,CAAC;AAC3C,CAAC;AAFD,wBAEC;AAED;;;GAGG;AACH,SAAgB,MAAM,CAAC,CAAU;IAC/B,OAAO,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,KAAK,QAAQ,CAAC;AACtC,CAAC;AAFD,wBAEC;AAED;;;GAGG;AACH,SAAgB,MAAM,CAAC,CAAU;IAC/B,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAC5C,CAAC;AAFD,wBAEC;AAED;;;GAGG;AACH,SAAgB,OAAO,CAAC,CAAU;IAChC,OAAO,OAAO,CAAC,KAAK,SAAS,CAAC;AAChC,CAAC;AAFD,0BAEC;AAED;;;GAGG;AACH,SAAgB,KAAK,CAAC,CAAU;IAC9B,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAC;IACf,CAAC;IACD,MAAM,OAAO,GAAG,kDAAkD,CAAC;IACnE,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACzB,CAAC;AAND,sBAMC;AAED;;;GAGG;AACH,SAAgB,YAAY,CAAC,CAAU;IACrC,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAC;IACf,CAAC;IACD,MAAM,OAAO,GAAG,kBAAkB,CAAC;IACnC,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACzB,CAAC;AAND,oCAMC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uranio",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "Uranio is a type-safe ODM for MongoDB",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
@@ -20,11 +20,12 @@
20
20
  "build": "yarn tsc -b",
21
21
  "dev": "yarn tsc -w",
22
22
  "dev:base": "node ./dist/index.js",
23
- "dev:bin": "node ./dist/bin.js",
23
+ "dev:bin": "NODE_ENV=dev node ./dist/bin.js",
24
24
  "dev:run": "yarn tsc-watch --onSuccess \"yarn dev:bin\"",
25
25
  "push:patch": "bash ./scripts/version.sh patch",
26
26
  "push:minor": "bash ./scripts/version.sh minor",
27
- "push:major": "bash ./scripts/version.sh major"
27
+ "push:major": "bash ./scripts/version.sh major",
28
+ "upgrade:plutonio": "yarn remove plutonio && yarn add plutonio"
28
29
  },
29
30
  "devDependencies": {
30
31
  "@types/node": "^20.9.0",
@@ -41,8 +42,9 @@
41
42
  "typescript": "^5.2.2"
42
43
  },
43
44
  "dependencies": {
44
- "i0n": "^0.7.3",
45
+ "i0n": "^0.8.1",
45
46
  "mongodb": "^6.3.0",
46
- "plutonio": "^0.4.1"
47
+ "plutonio": "^0.5.1",
48
+ "r4y": "^0.6.0"
47
49
  }
48
50
  }
package/src/bin.ts CHANGED
@@ -1,8 +1,5 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import ion from 'i0n';
4
- const log = ion.create();
5
3
  import {generate} from './generate/index';
6
- const tsconfig_path = `/Users/x71c9/repos/uranio/builder/tsconfig.json`;
7
- const response = generate(tsconfig_path);
8
- log.debug(response);
4
+ const root = `/Users/x71c9/repos/uranio/builder`;
5
+ generate({root});
@@ -2,12 +2,167 @@
2
2
  *
3
3
  * Generate index module
4
4
  *
5
+ * @packageDocumentation
5
6
  */
6
7
 
8
+ import fs from 'fs';
9
+ import ray from 'r4y';
7
10
  import plutonio from 'plutonio';
11
+ import {log} from './log/index';
12
+ import * as utils from '../utils/index';
8
13
 
9
- export function generate(tsconfig_path: string){
10
- const scanned = plutonio.scanner(tsconfig_path);
11
- return scanned;
14
+ const debug = (process.env.NODE_ENV === 'dev');
15
+ ray.config.set({debug, spinner: log.spinner});
16
+
17
+ type GenerateParams = {
18
+ root: string
19
+ tsconfig_path?: string
20
+ }
21
+
22
+ export async function generate(params: GenerateParams){
23
+ log.spinner.start();
24
+ log.spinner.text(`Generating...`);
25
+ await _copy_dot_uranio(params);
26
+ await _update_dot_uranio(params);
27
+ await _build_dot_uranio(params);
28
+ log.spinner.stop();
29
+ log.success(`Uranio successfully generated client`);
30
+ }
31
+
32
+ async function _copy_dot_uranio(params: GenerateParams){
33
+ log.spinner.text(`Coping dot uranio...`);
34
+ const dot_path = `${params.root}/node_modules/uranio/.uranio`;
35
+ const destination_path = `${params.root}/node_modules/.uranio`;
36
+ log.spinner.stop();
37
+ await ray.spawn(`rm -rf ${destination_path}`);
38
+ await ray.spawn(`cp -rf ${dot_path} ${destination_path}`);
39
+ log.spinner.start();
40
+ log.debug(`Copied dot uranio directory`);
41
+ }
42
+
43
+ async function _update_dot_uranio(params: GenerateParams){
44
+ log.spinner.text(`Updating dot uranio files...`);
45
+ const uranio_extended_interfaces = _get_uranio_extended_interfaces(params);
46
+ const text = _generate_uranio_client_module_text(uranio_extended_interfaces);
47
+ const uranio_client_path =
48
+ `${params.root}/node_modules/.uranio/src/uranio-client.ts`;
49
+ fs.writeFileSync(uranio_client_path, text);
50
+ log.debug(`Updated dot uranio files`);
51
+ }
52
+
53
+ async function _build_dot_uranio(params: GenerateParams){
54
+ log.spinner.text(`Transpiling dot uranio files...`);
55
+ const copied_dot_uranio_tsconfig_path = `${params.root}/node_modules/.uranio/tsconfig.json`;
56
+ await ray.spawn(`yarn tsc --project ${copied_dot_uranio_tsconfig_path}`);
57
+ log.debug(`Transpiled dot uranio files`);
58
+ }
59
+
60
+ function _resolve_tsconfig_path(params: GenerateParams){
61
+ if(utils.valid.string(params.tsconfig_path)){
62
+ return params.tsconfig_path;
63
+ }
64
+ return `${params.root}/tsconfig.json`;
12
65
  }
13
66
 
67
+ function _get_uranio_extended_interfaces(params: GenerateParams){
68
+ const tsconfig_path = _resolve_tsconfig_path(params);
69
+ const scanned = plutonio.scan(tsconfig_path);
70
+ // log.trace(scanned);
71
+ const uranio_extended_interfaces:plutonio.Interfaces = {};
72
+ for(const [_source_path, source] of Object.entries(scanned)){
73
+ const interfaces = source.interfaces;
74
+ const imports = source.imports;
75
+ if(!interfaces || !imports){
76
+ continue;
77
+ }
78
+ let uranio_clause;
79
+ for(const [_name, imp] of Object.entries(imports)){
80
+ if(imp.module === 'uranio'){
81
+ uranio_clause = imp.clause;
82
+ break;
83
+ }
84
+ }
85
+ const atom_definition = `${uranio_clause}.atom`;
86
+ for(const [name, inter] of Object.entries(interfaces)){
87
+ if(!inter.extends || !inter.extends.includes(atom_definition)){
88
+ continue;
89
+ }
90
+ if(name in uranio_extended_interfaces){
91
+ log.warn(
92
+ `An Atom with the same name [${name}] already exists. Overriding...`
93
+ );
94
+ }
95
+ uranio_extended_interfaces[name] = inter;
96
+ }
97
+ }
98
+ // log.trace(uranio_extended_interfaces);
99
+ return uranio_extended_interfaces;
100
+ }
101
+
102
+ function _generate_uranio_client_module_text(interfaces: plutonio.Interfaces){
103
+ let text = '';
104
+ text += `/**\n`;
105
+ text += ` *\n`;
106
+ text += ` * [Auto-generated module by "uranio generate" command]\n`;
107
+ text += ` *\n`;
108
+ text += ` * UranioClient module\n`;
109
+ text += ` *\n`;
110
+ text += ` */\n`;
111
+ text += `\n`;
112
+ text += `import {Client, ClientParams} from './client';\n`;
113
+ text += `import {AtomClient} from './atom';\n`;
114
+ text += `import {atom} from './types';\n`;
115
+ text += `\n`;
116
+ text += _generate_interface_definitions(interfaces);
117
+ text += `export class UranioClient extends Client{\n`;
118
+ text += _generate_client_class_attributes(interfaces);
119
+ text += ` constructor(params: ClientParams) {\n`;
120
+ text += ` super(params);\n`;
121
+ text += _generate_client_initialization(interfaces);
122
+ text += ` }\n`;
123
+ text += `}\n`;
124
+ return text;
125
+ }
126
+
127
+ function _generate_interface_definitions(interfaces: plutonio.Interfaces){
128
+ let text = '';
129
+ for(const [name, inter] of Object.entries(interfaces)){
130
+ text += `interface ${name} extends atom {\n`;
131
+ if(!inter.properties){
132
+ text += `}\n`;
133
+ text += `\n`;
134
+ continue;
135
+ }
136
+ for(const [prop_name, prop] of Object.entries(inter.properties)){
137
+ text += ` ${prop_name}: ${prop.primitive};\n`;
138
+ }
139
+ text += `}\n`;
140
+ text += `\n`;
141
+ }
142
+ return text;
143
+ }
144
+
145
+ function _generate_client_class_attributes(interfaces: plutonio.Interfaces){
146
+ let text = '';
147
+ for(const [name, _inter] of Object.entries(interfaces)){
148
+ let lc = _first_letter_lowercase(name);
149
+ text += ` public ${lc}: AtomClient<${name}>;\n`;
150
+ }
151
+ return text;
152
+ }
153
+
154
+ function _generate_client_initialization(interfaces: plutonio.Interfaces){
155
+ let text = '';
156
+ for(const [name, _inter] of Object.entries(interfaces)){
157
+ let lc = _first_letter_lowercase(name);
158
+ text += ` this.${lc} = new AtomClient<${name}>(this.db, '${lc}');\n`;
159
+ }
160
+ return text;
161
+ }
162
+
163
+ function _first_letter_lowercase(str: string): string{
164
+ if (typeof str !== 'string' || str.length === 0) {
165
+ return str;
166
+ }
167
+ return str.charAt(0).toLowerCase() + str.slice(1);
168
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ *
3
+ * Log index module
4
+ *
5
+ * @packageDocumentation
6
+ *
7
+ */
8
+
9
+ import ion from 'i0n';
10
+ export const log:ReturnType<typeof ion.create> = ion.create({
11
+ log_level: ion.LOG_LEVEL.TRACE
12
+ });
@@ -0,0 +1,9 @@
1
+ /**
2
+ *
3
+ * Utils index module
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+
8
+ import * as valid from './valid';
9
+ export {valid};
@@ -0,0 +1,62 @@
1
+ /**
2
+ *
3
+ * Valid module
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+
8
+ /**
9
+ * Check if
10
+ * typeof s === 'string' && s !== ''
11
+ */
12
+ export function string(s: unknown): s is string {
13
+ return typeof s === 'string' && s !== '';
14
+ }
15
+
16
+ /**
17
+ * Check if
18
+ * !!o && typeof o === 'object
19
+ */
20
+ export function object(o: unknown): o is Record<string, any> {
21
+ return !!o && typeof o === 'object';
22
+ }
23
+
24
+ /**
25
+ * Check if
26
+ * typeof n === 'number' && !isNaN(n);
27
+ */
28
+ export function number(n: unknown): n is number {
29
+ return typeof n === 'number' && !isNaN(n);
30
+ }
31
+
32
+ /**
33
+ * Check if
34
+ * typeof n === 'boolean';
35
+ */
36
+ export function boolean(b: unknown): b is boolean {
37
+ return typeof b === 'boolean';
38
+ }
39
+
40
+ /**
41
+ * Check if string is a valid email
42
+ * /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/
43
+ */
44
+ export function email(e: unknown): e is string {
45
+ if (typeof e !== 'string') {
46
+ return false;
47
+ }
48
+ const pattern = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
49
+ return pattern.test(e);
50
+ }
51
+
52
+ /**
53
+ * Check if string is a alphanumeric only (including space)
54
+ * /^[a-zA-Z0-9\s]+$/
55
+ */
56
+ export function alphanumeric(s: unknown): s is string {
57
+ if (typeof s !== 'string') {
58
+ return false;
59
+ }
60
+ const pattern = /^[a-zA-Z0-9\s]+$/;
61
+ return pattern.test(s);
62
+ }