uranio 0.1.3 → 0.1.5

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.
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "client",
3
+ "version": "1.0.0",
4
+ "main": "dist/index.js",
5
+ "license": "MIT",
6
+ "scripts": {
7
+ "build": "yarn tsc -b",
8
+ "dev": "yarn tsc -w"
9
+ },
10
+ "devDependencies": {
11
+ "@types/node": "^20.9.0",
12
+ "@typescript-eslint/eslint-plugin": "^5.59.1",
13
+ "@typescript-eslint/parser": "^5.59.1",
14
+ "eslint": "^8.39.0",
15
+ "eslint-config-prettier": "^8.8.0",
16
+ "eslint-plugin-import": "^2.27.5",
17
+ "eslint-plugin-jest": "^27.2.1",
18
+ "eslint-plugin-json": "^3.1.0",
19
+ "eslint-plugin-node": "^11.1.0",
20
+ "eslint-plugin-promise": "^6.1.1",
21
+ "husky": "^8.0.3",
22
+ "lint-staged": "^13.2.1",
23
+ "prettier": "2.8.7",
24
+ "tsc-watch": "^6.0.0",
25
+ "typescript": "^5.2.2"
26
+ },
27
+ "dependencies": {
28
+ "i0n": "^0.7.3",
29
+ "mongodb": "^6.2.0"
30
+ }
31
+ }
@@ -0,0 +1,132 @@
1
+ /**
2
+ *
3
+ * Atom client module
4
+ *
5
+ */
6
+
7
+ import mongodb from 'mongodb';
8
+
9
+ import {Atom, Query, Shape} from './types';
10
+
11
+ export class AtomClient<S extends Atom> {
12
+
13
+ public collection: mongodb.Collection<S>;
14
+
15
+ constructor(private db: mongodb.Db, public name: string) {
16
+ this.collection = this.db.collection<S>(name);
17
+ }
18
+
19
+ public async get_item(query: Query<S>): Promise<S | null> {
20
+ query = _instance_object_id(query);
21
+ let item = await this.collection.findOne<S>(query as mongodb.Filter<S>);
22
+ if (!item) {
23
+ return null;
24
+ }
25
+ item = _string_id(item) as S;
26
+ return item;
27
+ }
28
+
29
+ public async get_items(query: Query<S>): Promise<S[]> {
30
+ query = _instance_object_id(query);
31
+ let items = await this.collection
32
+ .find<S>(query as mongodb.Filter<S>)
33
+ .toArray();
34
+ for (let item of items) {
35
+ item = _string_id(item) as S;
36
+ }
37
+ return items;
38
+ }
39
+
40
+ public async put_item(shape: Shape<S>): Promise<mongodb.InsertOneResult> {
41
+ shape = _remove_id(shape as Partial<S>);
42
+ const respone_insert = await this.collection.insertOne(
43
+ shape as mongodb.OptionalUnlessRequiredId<S>
44
+ );
45
+ return respone_insert;
46
+ }
47
+
48
+ public async put_items(atoms: Shape<S>[]): Promise<mongodb.InsertManyResult> {
49
+ const atoms_no_ids: mongodb.OptionalUnlessRequiredId<S>[] = [];
50
+ for (const atom of atoms) {
51
+ const atom_no_id = _remove_id(
52
+ atom as Partial<S>
53
+ ) as mongodb.OptionalUnlessRequiredId<S>;
54
+ atoms_no_ids.push(atom_no_id);
55
+ }
56
+ let items = await this.collection.insertMany(atoms_no_ids);
57
+ return items;
58
+ }
59
+
60
+ public async update_item(
61
+ query: Query<S>,
62
+ atom: Partial<S>
63
+ ): Promise<mongodb.UpdateResult> {
64
+ query = _instance_object_id(query);
65
+ atom = _remove_id(atom) as Partial<S>;
66
+ const response_update = await this.collection.updateOne(
67
+ query as mongodb.Filter<S>,
68
+ {$set: atom}
69
+ );
70
+ return response_update;
71
+ }
72
+
73
+ public async update_items(
74
+ query: Query<S>,
75
+ atom: Partial<S>
76
+ ): Promise<mongodb.UpdateResult> {
77
+ query = _instance_object_id(query);
78
+ atom = _remove_id(atom) as Partial<S>;
79
+ const response_update = await this.collection.updateMany(
80
+ query as mongodb.Filter<S>,
81
+ {$set: atom}
82
+ );
83
+ return response_update;
84
+ }
85
+
86
+ public async delete_item(query: Query<S>): Promise<mongodb.DeleteResult> {
87
+ query = _instance_object_id(query);
88
+ const response_delete = await this.collection.deleteOne(
89
+ query as mongodb.Filter<S>
90
+ );
91
+ return response_delete;
92
+ }
93
+
94
+ public async delete_items(query: Query<S>): Promise<mongodb.DeleteResult> {
95
+ query = _instance_object_id(query);
96
+ const response_delete = await this.collection.deleteMany(
97
+ query as mongodb.Filter<S>
98
+ );
99
+ return response_delete;
100
+ }
101
+ }
102
+
103
+ type StringId<T extends unknown> = T extends {_id: any}
104
+ ? Omit<T, '_id'> & {_id: string}
105
+ : T;
106
+
107
+ function _string_id<T extends unknown>(item: T): StringId<T> {
108
+ if (item && typeof item === 'object' && '_id' in item) {
109
+ if (item._id?.toString) {
110
+ item._id = item._id.toString();
111
+ }
112
+ item._id = String(item._id);
113
+ }
114
+ return item as StringId<T>;
115
+ }
116
+
117
+ function _remove_id<A extends Atom>(atom: Partial<A>): Shape<A> {
118
+ delete (atom as any)._id;
119
+ return atom as Shape<A>;
120
+ }
121
+
122
+ function _instance_object_id<A extends Atom>(query: Query<A>): Query<A> {
123
+ for (let [key, value] of Object.entries(query)) {
124
+ if (key === '_id' && typeof value === 'string') {
125
+ query['_id'] = new mongodb.ObjectId(value) as any;
126
+ }
127
+ if (value && typeof value === 'object') {
128
+ value = _instance_object_id(value);
129
+ }
130
+ }
131
+ return query;
132
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ *
3
+ * Client module
4
+ *
5
+ * @packageDocumentation
6
+ *
7
+ */
8
+
9
+ import mongodb, {MongoClient, ServerApiVersion} from 'mongodb';
10
+ import {log} from './log/index';
11
+
12
+ export type ClientParams = {
13
+ uri: string;
14
+ db_name: string;
15
+ };
16
+
17
+ export class Client{
18
+ protected client: mongodb.MongoClient;
19
+ protected db: mongodb.Db;
20
+ constructor(params: ClientParams) {
21
+ this.client = new MongoClient(params.uri, {
22
+ serverApi: {
23
+ version: ServerApiVersion.v1,
24
+ strict: true,
25
+ deprecationErrors: true,
26
+ },
27
+ });
28
+ this.db = this.client.db(params.db_name);
29
+ }
30
+ public async connect() {
31
+ log.trace('Connecting...');
32
+ await this.client.connect();
33
+ log.trace('Connected.');
34
+ }
35
+ public async disconnect() {
36
+ log.trace('Disconnecting...');
37
+ await this.client.close();
38
+ log.trace('Disconnected.');
39
+ }
40
+ }
41
+
42
+
@@ -0,0 +1,14 @@
1
+ /**
2
+ *
3
+ * Index module
4
+ *
5
+ * @packageDocumentation
6
+ *
7
+ */
8
+
9
+ import * as types from './types';
10
+ export {types};
11
+
12
+ import {UranioClient} from './uranio-client';
13
+ export default UranioClient;
14
+
@@ -0,0 +1,10 @@
1
+ /**
2
+ *
3
+ * Log index module
4
+ *
5
+ * @packageDocumentation
6
+ *
7
+ */
8
+
9
+ import ion from 'i0n';
10
+ export const log = ion.create();
@@ -0,0 +1,56 @@
1
+ /**
2
+ *
3
+ * Query type module
4
+ *
5
+ */
6
+
7
+ import {Atom} from './types';
8
+
9
+ export type Query<A extends Atom> = {
10
+ [P in keyof A]?: Condition<A[P]>;
11
+ } & RootFilterOperators<A>;
12
+
13
+ type Condition<T> = AlternativeType<T> | FilterOperators<AlternativeType<T>>;
14
+
15
+ type AlternativeType<T> = T extends ReadonlyArray<infer U>
16
+ ? T | RegExpOrString<U>
17
+ : RegExpOrString<T>;
18
+
19
+ type RegExpOrString<T> = T extends string ? RegExp | T : T;
20
+
21
+ interface FilterOperators<T> {
22
+ $eq?: T;
23
+ $gt?: T;
24
+ $gte?: T;
25
+ $in?: ReadonlyArray<T>;
26
+ $lt?: T;
27
+ $lte?: T;
28
+ $ne?: T;
29
+ $nin?: ReadonlyArray<T>;
30
+ $not?: T extends string ? FilterOperators<T> | RegExp : FilterOperators<T>;
31
+ $exists?: boolean;
32
+ $expr?: Record<string, any>;
33
+ $jsonSchema?: Record<string, any>;
34
+ $mod?: T extends number ? [number, number] : never;
35
+ $regex?: T extends string ? RegExp | string : never;
36
+ $options?: T extends string ? string : never;
37
+ $maxDistance?: number;
38
+ $all?: ReadonlyArray<any>;
39
+ $size?: T extends ReadonlyArray<any> ? number : never;
40
+ $rand?: Record<string, never>;
41
+ }
42
+
43
+ interface RootFilterOperators<A extends Atom> {
44
+ $and?: Query<A>[];
45
+ $nor?: Query<A>[];
46
+ $or?: Query<A>[];
47
+ $text?: {
48
+ $search: string;
49
+ $language?: string;
50
+ $caseSensitive?: boolean;
51
+ $diacriticSensitive?: boolean;
52
+ };
53
+ $where?: string | ((this: A) => boolean);
54
+ $comment?: string;
55
+ }
56
+
@@ -0,0 +1,16 @@
1
+ /**
2
+ *
3
+ * Types module
4
+ *
5
+ * @packageDocumentation
6
+ *
7
+ */
8
+
9
+ import {Query} from './query';
10
+ export {Query};
11
+
12
+ export interface Atom {
13
+ _id: string;
14
+ }
15
+
16
+ export type Shape<A extends Atom> = Omit<A, '_id'>;
@@ -0,0 +1,25 @@
1
+ /**
2
+ *
3
+ * UranioClient module
4
+ *
5
+ * @packageDocumentation
6
+ *
7
+ */
8
+
9
+ import {Client, ClientParams} from './client';
10
+ // import {Atom} from './types';
11
+ // import {AtomClient} from './atom';
12
+
13
+ // interface Product extends Atom {
14
+ // title: string;
15
+ // price: number;
16
+ // }
17
+
18
+ export class UranioClient extends Client{
19
+ // public product: AtomClient<Product>;
20
+ constructor(params: ClientParams) {
21
+ super(params);
22
+ // this.product = new AtomClient<Product>(this.db, 'product');
23
+ }
24
+ }
25
+
@@ -0,0 +1,33 @@
1
+ {
2
+ "compilerOptions": {
3
+ "alwaysStrict": true,
4
+ "allowJs": true,
5
+ "baseUrl": ".",
6
+ "declaration": true,
7
+ "esModuleInterop": true,
8
+ "experimentalDecorators": true,
9
+ "incremental": false,
10
+ "lib": ["ESNext", "ESNext.AsyncIterable", "DOM", "ES2021"],
11
+ "module": "CommonJS",
12
+ "moduleResolution": "node",
13
+ "noEmit": false,
14
+ "noFallthroughCasesInSwitch": true,
15
+ "noImplicitThis": true,
16
+ "noImplicitAny": true,
17
+ "noUncheckedIndexedAccess": true,
18
+ "noUnusedLocals": true,
19
+ "noUnusedParameters": true,
20
+ "outDir": "./dist",
21
+ "rootDir": "./src",
22
+ "skipLibCheck": true,
23
+ "sourceMap": true,
24
+ "strict": true,
25
+ "strictNullChecks": true,
26
+ "strictFunctionTypes": true,
27
+ "strictPropertyInitialization": true,
28
+ "target": "ES2018",
29
+ "typeRoots": ["node_modules/@types"]
30
+ },
31
+ "include": ["src/**/*.ts"],
32
+ "exclude": ["node_modules"]
33
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uranio",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Uranio is a type-safe ODM for MongoDB",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
@@ -38,7 +38,6 @@
38
38
  "typescript": "^5.2.2"
39
39
  },
40
40
  "dependencies": {
41
- "husky": "^8.0.3",
42
41
  "i0n": "^0.7.3",
43
42
  "plutonio": "^0.2.2"
44
43
  }
@@ -1,12 +1,5 @@
1
1
  #!/bin/bash
2
2
 
3
- if [[ `git status --porcelain` ]]; then
4
- echo
5
- echo "Error: Git working directory is not clean. Please commit your changes or stash them."
6
- echo
7
- exit 1
8
- fi;
9
-
10
3
  SEMANTIC_NAME=$1
11
4
 
12
5
  if [ "$SEMANTIC_NAME" == "" ]; then
@@ -19,7 +12,7 @@ fi
19
12
 
20
13
  case "$SEMANTIC_NAME" in
21
14
  patch)
22
- npm version patch --no-git-tag-version
15
+ yarn version --patch --no-git-tag-version
23
16
  break;
24
17
  ;;
25
18
  minor)
@@ -31,7 +24,7 @@ case "$SEMANTIC_NAME" in
31
24
  * ) echo "Please answer [y]es or [n]o.";;
32
25
  esac
33
26
  done
34
- npm version minor --no-git-tag-version
27
+ yarn version --minor --no-git-tag-version
35
28
  break;
36
29
  ;;
37
30
  major)
@@ -43,7 +36,7 @@ case "$SEMANTIC_NAME" in
43
36
  * ) echo "Please answer [y]es or [n]o.";;
44
37
  esac
45
38
  done
46
- npm version major --no-git-tag-version
39
+ yarn version --major --no-git-tag-version
47
40
  break;
48
41
  ;;
49
42
  *)
@@ -55,7 +48,5 @@ case "$SEMANTIC_NAME" in
55
48
  ;;
56
49
  esac
57
50
 
58
- git push origin
59
51
  VERSION=$(node -p "require('./package.json').version")
60
- # git push origin v$VERSION
61
52
  yarn publish --new-version $VERSION --no-git-tag-version