assai 0.4.2 → 0.4.3

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.
Files changed (32) hide show
  1. package/dist/index.d.mts +2 -0
  2. package/dist/src/data/errors/operation_fail.d.mts +7 -0
  3. package/dist/src/factories/create_mongo_collection.d.mts +95 -0
  4. package/dist/src/factories/index.d.mts +1 -0
  5. package/dist/src/types.d.ts +21 -0
  6. package/dist/src/usecases/index.d.mts +1 -0
  7. package/dist/src/usecases/mongo/generate_new_id.d.mts +1 -0
  8. package/dist/src/usecases/mongo/index.d.mts +3 -0
  9. package/dist/src/usecases/mongo/mongo_client.d.mts +10 -0
  10. package/dist/src/usecases/mongo/operation/additional/delete_one_or_throw.d.mts +11 -0
  11. package/dist/src/usecases/mongo/operation/additional/index.d.mts +1 -0
  12. package/dist/src/usecases/mongo/operation/count.d.mts +11 -0
  13. package/dist/src/usecases/mongo/operation/delete_many.d.mts +11 -0
  14. package/dist/src/usecases/mongo/operation/delete_one.d.mts +12 -0
  15. package/dist/src/usecases/mongo/operation/find.d.mts +15 -0
  16. package/dist/src/usecases/mongo/operation/find_one.d.mts +14 -0
  17. package/dist/src/usecases/mongo/operation/index.d.mts +10 -0
  18. package/dist/src/usecases/mongo/operation/insert_many.d.mts +12 -0
  19. package/dist/src/usecases/mongo/operation/insert_one.d.mts +12 -0
  20. package/dist/src/usecases/mongo/operation/update_many.d.mts +13 -0
  21. package/dist/src/usecases/mongo/operation/update_one.d.mts +13 -0
  22. package/dist/src/usecases/mongo/test/manage_mock_registry.d.mts +12 -0
  23. package/dist/src/usecases/mongo/test/mock_get_collection.d.mts +12 -0
  24. package/dist/src/usecases/mongo/transformers/id/index.d.mts +3 -0
  25. package/dist/src/usecases/mongo/transformers/id/rename_find_options.d.mts +5 -0
  26. package/dist/src/usecases/mongo/transformers/id/rename_to_dev_id.d.mts +6 -0
  27. package/dist/src/usecases/mongo/transformers/id/rename_to_mongo_id.d.mts +6 -0
  28. package/dist/src/usecases/mongo/transformers/index.d.mts +2 -0
  29. package/dist/src/usecases/mongo/transformers/object_id/ids_into_strings.d.mts +7 -0
  30. package/dist/src/usecases/mongo/transformers/object_id/index.d.mts +2 -0
  31. package/dist/src/usecases/mongo/transformers/object_id/strings_into_id.d.mts +11 -0
  32. package/package.json +3 -2
@@ -0,0 +1,2 @@
1
+ export * from "./src/factories/index.mjs";
2
+ export * from "./src/usecases/index.mjs";
@@ -0,0 +1,7 @@
1
+ export class OperationFail extends Error {
2
+ /**
3
+ *
4
+ * @param {string} msg
5
+ */
6
+ constructor(msg: string);
7
+ }
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Generates a collection object that automatically manage ObjectId conversion to string.
3
+ *
4
+ * This method will read the string DATABASE_URL to create a connection. If you have it in another
5
+ * location, you will need to pass it at `connectionString` property inside the options parameter.
6
+ *
7
+ * The connection is cached by default. Use `collectionGetter` and `cachedCollectionGetter` to
8
+ * customize this behavior.
9
+ * @template {import('../types.js').MongoDocument} T
10
+ * @param {string} name
11
+ * @param {IcreateCollectionOptions<T>} [options]
12
+ */
13
+ export function createMongoCollection<T extends import("../types.js").MongoDocument>(name: string, options?: IcreateCollectionOptions<T> | undefined): Promise<{
14
+ /**
15
+ * Returns the native driver.
16
+ */
17
+ getCollection: () => Promise<Collection<T>>;
18
+ /**
19
+ * @param {import('mongodb').Filter<T>} query
20
+ */
21
+ count: (query?: import("mongodb").Filter<T>) => Promise<number>;
22
+ /**
23
+ * @template {import('../types.js').Projection<T>} K
24
+ * @param {import('mongodb').Filter<T>} query
25
+ * @param {import('../types.js').FindOptions<T, K>} options
26
+ * @returns {Promise<T[]>}
27
+ */
28
+ find: <K extends import("../types.js").Projection<T>>(query: import("mongodb").Filter<T>, options?: import("../types.js").FindOptions<T, K>) => Promise<T[]>;
29
+ /**
30
+ * @template {import('../types.js').Projection<T> | undefined} K
31
+ * @param {import('mongodb').Filter<T>} query
32
+ * @param {import('../types.js').FindOptions<T,K>} options
33
+ * @returns {Promise<T | null>}
34
+ */
35
+ findOne: <K_1 extends import("../types.js").Projection<T>>(query: import("mongodb").Filter<T>, options?: import("../types.js").FindOptions<T, K_1>) => Promise<T | null>;
36
+ /**
37
+ * @param {import('../types.js').Optional<T, 'id'>} doc
38
+ * @returns {Promise<T>}
39
+ */
40
+ insertOne: (doc: import("../types.js").Optional<T, "id">) => Promise<T>;
41
+ /**
42
+ *
43
+ * @param {import('../types.js').Optional<T, 'id'>[]} docs
44
+ * @returns {Promise<T[]>}
45
+ */
46
+ insertMany: (docs: import("../types.js").Optional<T, "id">[]) => Promise<T[]>;
47
+ /**
48
+ * @param {import('mongodb').Filter<T>} query
49
+ * @returns {Promise<boolean>}
50
+ */
51
+ deleteOne: (query: import("mongodb").Filter<T>) => Promise<boolean>;
52
+ /**
53
+ * Deletes the first document to match the query.
54
+ * This method will throw an error if no documents were deleted.
55
+ * @param {import('mongodb').Filter<T>} query
56
+ * @throw {@link OperationFail} If not document was deleted
57
+ */
58
+ deleteOneOrThrow: (query: import("mongodb").Filter<T>) => Promise<void>;
59
+ /**
60
+ *
61
+ * @param {import('mongodb').Filter<T>} query
62
+ */
63
+ deleteMany: (query: import("mongodb").Filter<T>) => Promise<boolean>;
64
+ /**
65
+ * @param {import('mongodb').Filter<T>} query
66
+ * @param {import('mongodb').UpdateFilter<T>} update
67
+ */
68
+ updateOne: (query: import("mongodb").Filter<T>, update: import("mongodb").UpdateFilter<T>) => Promise<boolean>;
69
+ /**
70
+ * @param {import('mongodb').Filter<T>} query
71
+ * @param {import('mongodb').UpdateFilter<T>} update
72
+ */
73
+ updateMany: (query: import("mongodb").Filter<T>, update: import("mongodb").UpdateFilter<T>) => Promise<boolean>;
74
+ }>;
75
+ export type IcollectionGetter<T extends import("../types.js").MongoDocument> = () => Promise<Collection<T>>;
76
+ export type IcreateCollectionOptions<T extends import("../types.js").MongoDocument> = {
77
+ /**
78
+ * A custom function to get the collection object from mongodb driver.
79
+ *
80
+ * This will be not be cached and will be used on every operation such as insertOne or findOne.
81
+ *
82
+ * If you wish to cache the collection object, see `cachableCollectionGetter`.
83
+ */
84
+ collectionGetter?: IcollectionGetter<T> | undefined;
85
+ /**
86
+ * Same as `collectionGetter`. But the object is cached.
87
+ */
88
+ cachableCollectionGetter?: IcollectionGetter<T> | undefined;
89
+ /**
90
+ * A custom connection string. If none is given, `process.env.DATABASE_URL` will be used.
91
+ */
92
+ connectionString?: string | undefined;
93
+ };
94
+ export type ICollection<T extends import("../types.js").MongoDocument> = Awaited<ReturnType<typeof createMongoCollection<T>>>;
95
+ import { Collection } from 'mongodb';
@@ -0,0 +1 @@
1
+ export * from "./create_mongo_collection.mjs";
@@ -0,0 +1,21 @@
1
+ import { FindOptions as FO } from 'mongodb';
2
+ export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
3
+ export type MongoDocument = {
4
+ id: string;
5
+ [key: string]: any;
6
+ };
7
+ export type RequiredIfPresent<T, U> = {
8
+ [P in keyof T]: P extends keyof U ? Required<T[P]> : T[P];
9
+ };
10
+ type NonNullProjection<T extends MongoDocument> = Partial<Record<keyof T, 1 | 0>>;
11
+ export type Projection<T extends MongoDocument> = NonNullProjection<T> | undefined;
12
+ type GivenProjectionReturnType<T extends MongoDocument, P extends NonNullProjection<T>> = P extends Partial<Record<keyof T, 0>> ? {
13
+ [K in keyof T as P[K] extends 0 ? never : K]: T[K];
14
+ } : {
15
+ [K in keyof T as P[K] extends 1 ? K : never]: T[K];
16
+ };
17
+ export type ProjectionReturnType<T extends MongoDocument, P extends Projection<T>> = P extends NonNullProjection<T> ? GivenProjectionReturnType<T, P> : T;
18
+ export type FindOptions<T extends MongoDocument, K extends Projection<T>> = Omit<FO<T>, 'projection'> & {
19
+ projection?: K;
20
+ };
21
+ export {};
@@ -0,0 +1 @@
1
+ export * as mongo from "./mongo/index.mjs";
@@ -0,0 +1 @@
1
+ export function generateNewId(): string;
@@ -0,0 +1,3 @@
1
+ export * from "./generate_new_id.mjs";
2
+ export * from "./mongo_client.mjs";
3
+ export * from "./operation/index.mjs";
@@ -0,0 +1,10 @@
1
+ /**
2
+ *
3
+ * @param {object} params
4
+ * @param {string} [params.connectionString]
5
+ */
6
+ export function getClient({ connectionString, }?: {
7
+ connectionString?: string | undefined;
8
+ }): Promise<MongoClient>;
9
+ export function closeClient(): Promise<void>;
10
+ import { MongoClient } from 'mongodb';
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @template {import('../../../../types.js').MongoDocument} T
3
+ * @param {object} param
4
+ * @param {import('mongodb').Filter<T>} param.query
5
+ * @param {() => Promise<Collection<T>>} param.getCollection
6
+ */
7
+ export function deleteOneOrThrow<T extends import("../../../../types.js").MongoDocument>({ query, getCollection, }: {
8
+ query: import("mongodb").Filter<T>;
9
+ getCollection: () => Promise<Collection<T>>;
10
+ }): Promise<void>;
11
+ import { Collection } from 'mongodb';
@@ -0,0 +1 @@
1
+ export * from "./delete_one_or_throw.mjs";
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @template {import('../../../types.js').MongoDocument} T
3
+ * @param {object} params
4
+ * @param {() => Promise<Collection<T>>} params.getCollection
5
+ * @param {import('mongodb').Filter<T>} [params.query]
6
+ */
7
+ export function count<T extends import("../../../types.js").MongoDocument>({ getCollection, query }: {
8
+ getCollection: () => Promise<Collection<T>>;
9
+ query?: import("mongodb").Filter<T> | undefined;
10
+ }): Promise<number>;
11
+ import { Collection } from 'mongodb';
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @template {import('../../../types.js').MongoDocument} T
3
+ * @param {object} parameter
4
+ * @param {import('mongodb').Filter<T>} parameter.query
5
+ * @param {() => Promise<Collection<T>>} parameter.getCollection
6
+ */
7
+ export function deleteMany<T extends import("../../../types.js").MongoDocument>({ query, getCollection }: {
8
+ query: import("mongodb").Filter<T>;
9
+ getCollection: () => Promise<Collection<T>>;
10
+ }): Promise<boolean>;
11
+ import { Collection } from 'mongodb';
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @template {import('../../../types.js').MongoDocument} T
3
+ * @param {object} parameter
4
+ * @param {import('mongodb').Filter<T>} parameter.query
5
+ * @param {() => Promise<Collection<T>>} parameter.getCollection
6
+ * @returns {Promise<boolean>}
7
+ */
8
+ export function deleteOne<T extends import("../../../types.js").MongoDocument>({ query, getCollection }: {
9
+ query: import("mongodb").Filter<T>;
10
+ getCollection: () => Promise<Collection<T>>;
11
+ }): Promise<boolean>;
12
+ import { Collection } from 'mongodb';
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @template {import('../../../types.js').MongoDocument} T
3
+ * @template {import('../../../types.js').Projection<T>} K
4
+ * @param {object} parameter
5
+ * @param {() => Promise<Collection<T>>} parameter.getCollection
6
+ * @param {import('mongodb').Filter<T>} parameter.query
7
+ * @param {import('../../../types.js').FindOptions<T, K>} [parameter.options]
8
+ * @returns {Promise<T[]>}
9
+ */
10
+ export function find<T extends import("../../../types.js").MongoDocument, K extends import("../../../types.js").Projection<T>>({ getCollection, query, options }: {
11
+ getCollection: () => Promise<Collection<T>>;
12
+ query: import("mongodb").Filter<T>;
13
+ options?: import("../../../types.js").FindOptions<T, K> | undefined;
14
+ }): Promise<T[]>;
15
+ import { Collection } from 'mongodb';
@@ -0,0 +1,14 @@
1
+ /**
2
+ * @template {import('../../../types.js').MongoDocument} T
3
+ * @template {import('../../../types.js').Projection<T> | undefined} K
4
+ * @param {object} param
5
+ * @param {import('mongodb').Filter<T>} param.query
6
+ * @param {import('../../../types.js').FindOptions<T, K>} [param.options]
7
+ * @param {() => Promise<Collection<T>>} param.getCollection
8
+ */
9
+ export function findOne<T extends import("../../../types.js").MongoDocument, K extends import("../../../types.js").Projection<T>>({ query, options, getCollection }: {
10
+ query: import("mongodb").Filter<T>;
11
+ options?: import("../../../types.js").FindOptions<T, K> | undefined;
12
+ getCollection: () => Promise<Collection<T>>;
13
+ }): Promise<T | null>;
14
+ import { Collection } from 'mongodb';
@@ -0,0 +1,10 @@
1
+ export * from "./additional/index.mjs";
2
+ export * from "./count.mjs";
3
+ export * from "./delete_many.mjs";
4
+ export * from "./delete_one.mjs";
5
+ export * from "./find.mjs";
6
+ export * from "./find_one.mjs";
7
+ export * from "./insert_many.mjs";
8
+ export * from "./insert_one.mjs";
9
+ export * from "./update_many.mjs";
10
+ export * from "./update_one.mjs";
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @template {import('../../../types.js').MongoDocument} T
3
+ * @param {object} param
4
+ * @param {import('../../../types.js').Optional<T, 'id'>[]} param.docs
5
+ * @param {() => Promise<Collection<T>>} param.getCollection
6
+ * @returns {Promise<T[]>}
7
+ */
8
+ export function insertMany<T extends import("../../../types.js").MongoDocument>({ docs, getCollection }: {
9
+ docs: import("../../../types.js").Optional<T, "id">[];
10
+ getCollection: () => Promise<Collection<T>>;
11
+ }): Promise<T[]>;
12
+ import { Collection } from 'mongodb';
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @template {import('../../../types.js').MongoDocument} T
3
+ * @param {object} param
4
+ * @param {import('../../../types.js').Optional<T, 'id'>} param.doc
5
+ * @param {() => Promise<Collection<T>>} param.getCollection
6
+ * @returns {Promise<T>}
7
+ */
8
+ export function insertOne<T extends import("../../../types.js").MongoDocument>({ doc, getCollection }: {
9
+ doc: import("../../../types.js").Optional<T, "id">;
10
+ getCollection: () => Promise<Collection<T>>;
11
+ }): Promise<T>;
12
+ import { Collection } from 'mongodb';
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @template {import('../../../types.js').MongoDocument} T
3
+ * @param {object} param
4
+ * @param {import('mongodb').Filter<T>} param.query
5
+ * @param {import('mongodb').UpdateFilter<T>} param.update
6
+ * @param {() => Promise<Collection<T>>} param.getCollection
7
+ */
8
+ export function updateMany<T extends import("../../../types.js").MongoDocument>({ query, update, getCollection }: {
9
+ query: import("mongodb").Filter<T>;
10
+ update: import("mongodb").UpdateFilter<T>;
11
+ getCollection: () => Promise<Collection<T>>;
12
+ }): Promise<boolean>;
13
+ import { Collection } from 'mongodb';
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @template {import('../../../types.js').MongoDocument} T
3
+ * @param {object} param
4
+ * @param {import('mongodb').Filter<T>} param.query
5
+ * @param {import('mongodb').UpdateFilter<T>} param.update
6
+ * @param {() => Promise<Collection<T>>} param.getCollection
7
+ */
8
+ export function updateOne<T extends import("../../../types.js").MongoDocument>({ query, update, getCollection }: {
9
+ query: import("mongodb").Filter<T>;
10
+ update: import("mongodb").UpdateFilter<T>;
11
+ getCollection: () => Promise<Collection<T>>;
12
+ }): Promise<boolean>;
13
+ import { Collection } from 'mongodb';
@@ -0,0 +1,12 @@
1
+ /**
2
+ *
3
+ * @param {import('./mock_get_collection.mjs').ItestCollection} param
4
+ * @param {object} options
5
+ * @param {boolean} [options.deleteAtEnd]
6
+ */
7
+ export function manageMockRegistry({ tag, ...rest }?: import('./mock_get_collection.mjs').ItestCollection, { deleteAtEnd }?: {
8
+ deleteAtEnd?: boolean | undefined;
9
+ }): {
10
+ id: string;
11
+ name: string;
12
+ };
@@ -0,0 +1,12 @@
1
+ export function mockGetCollection(collectionName?: string): Promise<Collection<ItestCollection>>;
2
+ export type ItestCollection = {
3
+ _id?: ObjectId | undefined;
4
+ id?: string | undefined;
5
+ name?: string | undefined;
6
+ tag?: string | ObjectId | undefined;
7
+ createdAt?: Date | undefined;
8
+ posts?: any[] | undefined;
9
+ address?: object;
10
+ };
11
+ import { Collection } from 'mongodb';
12
+ import { ObjectId } from 'mongodb';
@@ -0,0 +1,3 @@
1
+ export * from "./rename_find_options.mjs";
2
+ export * from "./rename_to_dev_id.mjs";
3
+ export * from "./rename_to_mongo_id.mjs";
@@ -0,0 +1,5 @@
1
+ /**
2
+ *
3
+ * @param {import('../../../../types.js').FindOptions<any ,any>} options
4
+ */
5
+ export function renameFindOptions(options?: import('../../../../types.js').FindOptions<any, any>): void;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * ```
3
+ * "_id" -> "id"
4
+ * ```
5
+ */
6
+ export function renameToDevId(obj: any): any;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * ```
3
+ * "id" -> "_id"
4
+ * ```
5
+ */
6
+ export function renameToMongoId(obj: any): any;
@@ -0,0 +1,2 @@
1
+ export * from "./id/index.mjs";
2
+ export * from "./object_id/index.mjs";
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Convert the parameter from `ObjectId` into string whenever possible.
3
+ *
4
+ * If an object or array is given, this function will be applied recursively.
5
+ * @param {*} obj
6
+ */
7
+ export function idsIntoString(obj: any): any;
@@ -0,0 +1,2 @@
1
+ export * from "./ids_into_strings.mjs";
2
+ export * from "./strings_into_id.mjs";
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Converts the input value into an `ObjectId`, doing a recursive internal search if possible.
3
+ *
4
+ * This function will do a recursive operation if the parameter is any of the following:
5
+ * - An object;
6
+ * - An array;
7
+ *
8
+ * If the parameter is a string, then it is converted to `ObjectId` if possible.
9
+ * @param {*} obj
10
+ */
11
+ export function stringsIntoId(obj: any): any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "assai",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "repository": {
5
5
  "url": "https://github.com/TimeLord2010/assai",
6
6
  "type": "git"
@@ -9,7 +9,7 @@
9
9
  "main": "index.mjs",
10
10
  "types": "dist/index.d.mts",
11
11
  "scripts": {
12
- "postinstall": "tsc -p ./jsconfig.prod.json",
12
+ "prepare": "tsc -p ./jsconfig.prod.json",
13
13
  "start": "node --env-file=.env index.mjs",
14
14
  "test": "node --env-file=.env --test"
15
15
  },
@@ -20,6 +20,7 @@
20
20
  "files": [
21
21
  "index.mjs",
22
22
  "src/",
23
+ "dist",
23
24
  "!src/**/*.test.mjs",
24
25
  "jsconfig.json",
25
26
  "jsconfig.prod.json"