uranio 0.1.13 → 0.1.15

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.13"
26
+ "uranio": "0.1.15"
27
27
  }
28
28
  }
@@ -4,7 +4,7 @@
4
4
  *
5
5
  */
6
6
 
7
- import mongodb from 'mongodb';
7
+ import mongodb, {ObjectId} from 'mongodb';
8
8
 
9
9
  import {atom, Query, Shape} from './types';
10
10
 
@@ -122,7 +122,7 @@ function _remove_id<A extends atom>(atom: Partial<A>): Shape<A> {
122
122
  function _instance_object_id<A extends atom>(query: Query<A>): Query<A> {
123
123
  for (let [key, value] of Object.entries(query)) {
124
124
  if (key === '_id' && typeof value === 'string') {
125
- query['_id'] = new mongodb.ObjectId(value) as any;
125
+ query['_id'] = new ObjectId(value) as any;
126
126
  }
127
127
  if (value && typeof value === 'object') {
128
128
  value = _instance_object_id(value);
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
+
@@ -2,6 +2,7 @@
2
2
  *
3
3
  * Generate index module
4
4
  *
5
+ * @packageDocumentation
5
6
  */
6
7
  type GenerateParams = {
7
8
  root: string;
@@ -3,6 +3,7 @@
3
3
  *
4
4
  * Generate index module
5
5
  *
6
+ * @packageDocumentation
6
7
  */
7
8
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
8
9
  if (k2 === undefined) k2 = k;
@@ -32,33 +33,46 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
32
33
  };
33
34
  Object.defineProperty(exports, "__esModule", { value: true });
34
35
  exports.generate = void 0;
36
+ const fs_1 = __importDefault(require("fs"));
35
37
  const r4y_1 = __importDefault(require("r4y"));
36
38
  const plutonio_1 = __importDefault(require("plutonio"));
37
39
  const index_1 = require("./log/index");
38
40
  const utils = __importStar(require("../utils/index"));
39
- r4y_1.default.config.set({
40
- debug: true,
41
- });
41
+ const debug = (process.env.NODE_ENV === 'dev');
42
+ r4y_1.default.config.set({ debug, spinner: index_1.log.spinner });
42
43
  async function generate(params) {
44
+ index_1.log.spinner.start();
45
+ index_1.log.spinner.text(`Generating...`);
43
46
  await _copy_dot_uranio(params);
44
47
  await _update_dot_uranio(params);
45
48
  await _build_dot_uranio(params);
49
+ index_1.log.spinner.stop();
50
+ index_1.log.success(`Uranio successfully generated client`);
46
51
  }
47
52
  exports.generate = generate;
48
53
  async function _copy_dot_uranio(params) {
54
+ index_1.log.spinner.text(`Coping dot uranio...`);
49
55
  const dot_path = `${params.root}/node_modules/uranio/.uranio`;
50
56
  const destination_path = `${params.root}/node_modules/.uranio`;
51
- await r4y_1.default.execute(`rm -rf ${destination_path}`);
52
- await r4y_1.default.execute(`cp -rf ${dot_path} ${destination_path}`);
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`);
53
62
  }
54
63
  async function _update_dot_uranio(params) {
55
- const tsconfig_path = _resolve_tsconfig_path(params);
56
- const scanned = plutonio_1.default.scanner(tsconfig_path);
57
- index_1.log.info(scanned);
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`);
58
70
  }
59
71
  async function _build_dot_uranio(params) {
72
+ index_1.log.spinner.text(`Transpiling dot uranio files...`);
60
73
  const copied_dot_uranio_tsconfig_path = `${params.root}/node_modules/.uranio/tsconfig.json`;
61
- await r4y_1.default.execute(`yarn tsc --project ${copied_dot_uranio_tsconfig_path}`);
74
+ await r4y_1.default.spawn(`yarn tsc --project ${copied_dot_uranio_tsconfig_path}`);
75
+ index_1.log.debug(`Transpiled dot uranio files`);
62
76
  }
63
77
  function _resolve_tsconfig_path(params) {
64
78
  if (utils.valid.string(params.tsconfig_path)) {
@@ -66,4 +80,99 @@ function _resolve_tsconfig_path(params) {
66
80
  }
67
81
  return `${params.root}/tsconfig.json`;
68
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
+ }
69
178
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/generate/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,8CAAsB;AACtB,wDAAgC;AAChC,uCAAgC;AAChC,sDAAwC;AAExC,aAAG,CAAC,MAAM,CAAC,GAAG,CAAC;IACb,KAAK,EAAE,IAAI;CACZ,CAAC,CAAC;AAOI,KAAK,UAAU,QAAQ,CAAC,MAAsB;IACnD,MAAM,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAC/B,MAAM,kBAAkB,CAAC,MAAM,CAAC,CAAC;IACjC,MAAM,iBAAiB,CAAC,MAAM,CAAC,CAAC;AAClC,CAAC;AAJD,4BAIC;AAED,KAAK,UAAU,gBAAgB,CAAC,MAAsB;IACpD,MAAM,QAAQ,GAAG,GAAG,MAAM,CAAC,IAAI,8BAA8B,CAAC;IAC9D,MAAM,gBAAgB,GAAG,GAAG,MAAM,CAAC,IAAI,uBAAuB,CAAC;IAC/D,MAAM,aAAG,CAAC,OAAO,CAAC,UAAU,gBAAgB,EAAE,CAAC,CAAC;IAChD,MAAM,aAAG,CAAC,OAAO,CAAC,UAAU,QAAQ,IAAI,gBAAgB,EAAE,CAAC,CAAC;AAC9D,CAAC;AAED,KAAK,UAAU,kBAAkB,CAAC,MAAsB;IACtD,MAAM,aAAa,GAAG,sBAAsB,CAAC,MAAM,CAAC,CAAC;IACrD,MAAM,OAAO,GAAG,kBAAQ,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;IAChD,WAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AACpB,CAAC;AAED,KAAK,UAAU,iBAAiB,CAAC,MAAsB;IACrD,MAAM,+B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AAC;IACb,CAAC;IACD,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACpD,CAAC"}
@@ -5,4 +5,5 @@
5
5
  * @packageDocumentation
6
6
  *
7
7
  */
8
- export declare const log: import("i0n/dist/class").Ion;
8
+ import ion from 'i0n';
9
+ export declare const log: ReturnType<typeof ion.create>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/generate/log/index.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;;;AAEH,8CAAsB;AACT,QAAA,GAAG,GAAG,aAAG,CAAC,MAAM,CAAC;IAC5B,SAAS,EAAE,aAAG,CAAC,SAAS,CAAC,KAAK;CAC/B,CAAC,CAAC"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uranio",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
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,9 +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
- "r4y": "^0.5.1"
47
+ "plutonio": "^0.5.1",
48
+ "r4y": "^0.6.0"
48
49
  }
49
50
  }
@@ -2,16 +2,17 @@
2
2
  *
3
3
  * Generate index module
4
4
  *
5
+ * @packageDocumentation
5
6
  */
6
7
 
8
+ import fs from 'fs';
7
9
  import ray from 'r4y';
8
10
  import plutonio from 'plutonio';
9
11
  import {log} from './log/index';
10
12
  import * as utils from '../utils/index';
11
13
 
12
- ray.config.set({
13
- debug: true,
14
- });
14
+ const debug = (process.env.NODE_ENV === 'dev');
15
+ ray.config.set({debug, spinner: log.spinner});
15
16
 
16
17
  type GenerateParams = {
17
18
  root: string
@@ -19,27 +20,41 @@ type GenerateParams = {
19
20
  }
20
21
 
21
22
  export async function generate(params: GenerateParams){
23
+ log.spinner.start();
24
+ log.spinner.text(`Generating...`);
22
25
  await _copy_dot_uranio(params);
23
26
  await _update_dot_uranio(params);
24
27
  await _build_dot_uranio(params);
28
+ log.spinner.stop();
29
+ log.success(`Uranio successfully generated client`);
25
30
  }
26
31
 
27
32
  async function _copy_dot_uranio(params: GenerateParams){
33
+ log.spinner.text(`Coping dot uranio...`);
28
34
  const dot_path = `${params.root}/node_modules/uranio/.uranio`;
29
35
  const destination_path = `${params.root}/node_modules/.uranio`;
30
- await ray.execute(`rm -rf ${destination_path}`);
31
- await ray.execute(`cp -rf ${dot_path} ${destination_path}`);
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`);
32
41
  }
33
42
 
34
43
  async function _update_dot_uranio(params: GenerateParams){
35
- const tsconfig_path = _resolve_tsconfig_path(params);
36
- const scanned = plutonio.scanner(tsconfig_path);
37
- log.info(scanned);
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`);
38
51
  }
39
52
 
40
53
  async function _build_dot_uranio(params: GenerateParams){
54
+ log.spinner.text(`Transpiling dot uranio files...`);
41
55
  const copied_dot_uranio_tsconfig_path = `${params.root}/node_modules/.uranio/tsconfig.json`;
42
- await ray.execute(`yarn tsc --project ${copied_dot_uranio_tsconfig_path}`);
56
+ await ray.spawn(`yarn tsc --project ${copied_dot_uranio_tsconfig_path}`);
57
+ log.debug(`Transpiled dot uranio files`);
43
58
  }
44
59
 
45
60
  function _resolve_tsconfig_path(params: GenerateParams){
@@ -48,3 +63,106 @@ function _resolve_tsconfig_path(params: GenerateParams){
48
63
  }
49
64
  return `${params.root}/tsconfig.json`;
50
65
  }
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
+ }
@@ -7,6 +7,6 @@
7
7
  */
8
8
 
9
9
  import ion from 'i0n';
10
- export const log = ion.create({
10
+ export const log:ReturnType<typeof ion.create> = ion.create({
11
11
  log_level: ion.LOG_LEVEL.TRACE
12
12
  });