assai 2.0.0 → 2.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/README.md +148 -148
- package/dist/src/__test/mock_get_collection.d.mts +2 -0
- package/dist/src/factories/create_mongo_collection.d.mts +55 -11
- package/dist/src/usecases/mongo/operation/aggregate.d.mts +16 -0
- package/dist/src/usecases/mongo/operation/bulk_write.d.mts +15 -0
- package/dist/src/usecases/mongo/operation/find_one_and_delete.d.mts +16 -0
- package/dist/src/usecases/mongo/operation/find_one_and_replace.d.mts +18 -0
- package/dist/src/usecases/mongo/operation/find_one_and_update.d.mts +18 -0
- package/dist/src/usecases/mongo/operation/index.d.mts +5 -0
- package/dist/src/usecases/mongo/transformers/id/rename_to_mongo_id.d.mts +3 -0
- package/dist/src/usecases/mongo/transformers/object_id/ids_into_strings.d.mts +5 -0
- package/dist/src/usecases/mongo/transformers/object_id/strings_into_id.d.mts +3 -0
- package/dist/src/usecases/mongo/transformers/timestamps.d.mts +5 -0
- package/index.mjs +2 -2
- package/jsconfig.json +17 -20
- package/jsconfig.prod.json +12 -12
- package/package.json +40 -39
- package/src/__test/manage_mock_database.mjs +24 -24
- package/src/__test/manage_mock_registry.mjs +54 -54
- package/src/__test/mock_get_collection.mjs +22 -20
- package/src/data/errors/operation_fail.mjs +9 -9
- package/src/data/errors/unsupported_error.mjs +9 -9
- package/src/data/interfaces/mongo_operator.mjs +2 -2
- package/src/data/interfaces/native_collection.mjs +27 -27
- package/src/factories/create_mock_collection.mjs +173 -173
- package/src/factories/create_mongo_collection.mjs +224 -181
- package/src/factories/index.mjs +1 -1
- package/src/types.ts +31 -31
- package/src/usecases/index.mjs +1 -1
- package/src/usecases/mongo/generate_new_id.mjs +4 -4
- package/src/usecases/mongo/index.mjs +3 -3
- package/src/usecases/mongo/mongo_client.mjs +36 -36
- package/src/usecases/mongo/operation/additional/delete_one_or_throw.mjs +17 -17
- package/src/usecases/mongo/operation/additional/index.mjs +1 -1
- package/src/usecases/mongo/operation/aggregate.mjs +21 -0
- package/src/usecases/mongo/operation/bulk_write.mjs +54 -0
- package/src/usecases/mongo/operation/count.mjs +16 -16
- package/src/usecases/mongo/operation/delete_many.mjs +15 -15
- package/src/usecases/mongo/operation/delete_one.mjs +16 -16
- package/src/usecases/mongo/operation/find.mjs +34 -34
- package/src/usecases/mongo/operation/find_one.mjs +32 -32
- package/src/usecases/mongo/operation/find_one_and_delete.mjs +23 -0
- package/src/usecases/mongo/operation/find_one_and_replace.mjs +24 -0
- package/src/usecases/mongo/operation/find_one_and_update.mjs +40 -0
- package/src/usecases/mongo/operation/index.mjs +15 -10
- package/src/usecases/mongo/operation/insert_many.mjs +39 -39
- package/src/usecases/mongo/operation/insert_one.mjs +32 -32
- package/src/usecases/mongo/operation/update_many.mjs +39 -39
- package/src/usecases/mongo/operation/update_one.mjs +40 -40
- package/src/usecases/mongo/transformers/id/index.mjs +4 -4
- package/src/usecases/mongo/transformers/id/rename_find_options.mjs +12 -12
- package/src/usecases/mongo/transformers/id/rename_to_dev_id.mjs +14 -13
- package/src/usecases/mongo/transformers/id/rename_to_mongo_id.mjs +34 -13
- package/src/usecases/mongo/transformers/index.mjs +2 -2
- package/src/usecases/mongo/transformers/input_transformer.mjs +32 -32
- package/src/usecases/mongo/transformers/object_id/ids_into_strings.mjs +47 -30
- package/src/usecases/mongo/transformers/object_id/index.mjs +3 -3
- package/src/usecases/mongo/transformers/object_id/strings_into_id.mjs +90 -44
- package/src/usecases/mongo/transformers/output_transformer.mjs +18 -18
- package/src/usecases/mongo/transformers/timestamps.mjs +32 -22
package/package.json
CHANGED
|
@@ -1,39 +1,40 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "assai",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"repository": {
|
|
5
|
-
"url": "https://github.com/TimeLord2010/assai",
|
|
6
|
-
"type": "git"
|
|
7
|
-
},
|
|
8
|
-
"description": "A simple mongodb wrapper to make mongo even easier to work with.",
|
|
9
|
-
"main": "index.mjs",
|
|
10
|
-
"types": "dist/index.d.mts",
|
|
11
|
-
"scripts": {
|
|
12
|
-
"prepare": "rm -rf dist && tsc -p ./jsconfig.prod.json",
|
|
13
|
-
"start": "node --env-file=.env index.mjs",
|
|
14
|
-
"test": "node --test"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"jsconfig.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"@
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "assai",
|
|
3
|
+
"version": "2.1.1",
|
|
4
|
+
"repository": {
|
|
5
|
+
"url": "https://github.com/TimeLord2010/assai",
|
|
6
|
+
"type": "git"
|
|
7
|
+
},
|
|
8
|
+
"description": "A simple mongodb wrapper to make mongo even easier to work with.",
|
|
9
|
+
"main": "index.mjs",
|
|
10
|
+
"types": "dist/index.d.mts",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"prepare": "rm -rf dist && tsc -p ./jsconfig.prod.json",
|
|
13
|
+
"start": "node --env-file=.env index.mjs",
|
|
14
|
+
"test": "node --test",
|
|
15
|
+
"check": "tsc -p jsconfig.json"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"mongodb",
|
|
19
|
+
"orm"
|
|
20
|
+
],
|
|
21
|
+
"files": [
|
|
22
|
+
"index.mjs",
|
|
23
|
+
"src/",
|
|
24
|
+
"dist",
|
|
25
|
+
"!src/**/*.test.mjs",
|
|
26
|
+
"jsconfig.json",
|
|
27
|
+
"jsconfig.prod.json"
|
|
28
|
+
],
|
|
29
|
+
"author": "Vinícius Gabriel",
|
|
30
|
+
"license": "ISC",
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"mongodb": ">=4.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@faker-js/faker": "^8.4.1",
|
|
36
|
+
"@types/node": "^20.12.7",
|
|
37
|
+
"mongodb-memory-server": "^10.0.0",
|
|
38
|
+
"typescript": "^5.4.5"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { MongoMemoryServer } from 'mongodb-memory-server'
|
|
2
|
-
import { after, before } from 'node:test'
|
|
3
|
-
import { closeClient } from '../usecases/mongo/mongo_client.mjs'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Starts a memory version of the mongodb server on a random free port.
|
|
7
|
-
*
|
|
8
|
-
* This method will also automatically close the connection with the database.
|
|
9
|
-
*/
|
|
10
|
-
export function manageMockDatabase() {
|
|
11
|
-
/** @type {null | MongoMemoryServer} */
|
|
12
|
-
let server = null
|
|
13
|
-
|
|
14
|
-
before(async () => {
|
|
15
|
-
server = await MongoMemoryServer.create()
|
|
16
|
-
const uri = server.getUri('test')
|
|
17
|
-
process.env.DATABASE_URL = uri
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
after(async () => {
|
|
21
|
-
await closeClient()
|
|
22
|
-
await server?.stop()
|
|
23
|
-
process.exit()
|
|
24
|
-
})
|
|
1
|
+
import { MongoMemoryServer } from 'mongodb-memory-server'
|
|
2
|
+
import { after, before } from 'node:test'
|
|
3
|
+
import { closeClient } from '../usecases/mongo/mongo_client.mjs'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Starts a memory version of the mongodb server on a random free port.
|
|
7
|
+
*
|
|
8
|
+
* This method will also automatically close the connection with the database.
|
|
9
|
+
*/
|
|
10
|
+
export function manageMockDatabase() {
|
|
11
|
+
/** @type {null | MongoMemoryServer} */
|
|
12
|
+
let server = null
|
|
13
|
+
|
|
14
|
+
before(async () => {
|
|
15
|
+
server = await MongoMemoryServer.create()
|
|
16
|
+
const uri = server.getUri('test')
|
|
17
|
+
process.env.DATABASE_URL = uri
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
after(async () => {
|
|
21
|
+
await closeClient()
|
|
22
|
+
await server?.stop()
|
|
23
|
+
process.exit()
|
|
24
|
+
})
|
|
25
25
|
}
|
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
import { fakerPT_BR } from '@faker-js/faker'
|
|
2
|
-
import { ObjectId } from 'mongodb'
|
|
3
|
-
import { after, before } from 'node:test'
|
|
4
|
-
import { generateNewId } from '../usecases/mongo/generate_new_id.mjs'
|
|
5
|
-
import { closeClient } from '../usecases/mongo/mongo_client.mjs'
|
|
6
|
-
import { deleteOne } from '../usecases/mongo/operation/delete_one.mjs'
|
|
7
|
-
import { insertOne } from '../usecases/mongo/operation/insert_one.mjs'
|
|
8
|
-
import { mockGetCollection } from './mock_get_collection.mjs'
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
*
|
|
12
|
-
* @param {import('./mock_get_collection.mjs').ItestCollection} param
|
|
13
|
-
* @param {object} options
|
|
14
|
-
* @param {boolean} [options.deleteAtEnd]
|
|
15
|
-
*/
|
|
16
|
-
export function manageMockRegistry({ tag, ...rest } = {}, {
|
|
17
|
-
deleteAtEnd = true
|
|
18
|
-
} = {}) {
|
|
19
|
-
|
|
20
|
-
const id = generateNewId()
|
|
21
|
-
const name = rest.name ?? fakerPT_BR.person.fullName()
|
|
22
|
-
|
|
23
|
-
before(async () => {
|
|
24
|
-
/** @type {import('./mock_get_collection.mjs').ItestCollection} */
|
|
25
|
-
const doc = {
|
|
26
|
-
id,
|
|
27
|
-
createdAt: new Date(),
|
|
28
|
-
tag,
|
|
29
|
-
name: name,
|
|
30
|
-
...rest,
|
|
31
|
-
}
|
|
32
|
-
await insertOne({
|
|
33
|
-
// @ts-ignore
|
|
34
|
-
getCollection: mockGetCollection,
|
|
35
|
-
doc: doc,
|
|
36
|
-
})
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
after(async () => {
|
|
40
|
-
if (deleteAtEnd) {
|
|
41
|
-
await deleteOne({
|
|
42
|
-
query: {
|
|
43
|
-
_id: ObjectId.createFromHexString(id)
|
|
44
|
-
},
|
|
45
|
-
// @ts-ignore
|
|
46
|
-
getCollection: mockGetCollection,
|
|
47
|
-
})
|
|
48
|
-
}
|
|
49
|
-
await closeClient()
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
return {
|
|
53
|
-
id, name
|
|
54
|
-
}
|
|
1
|
+
import { fakerPT_BR } from '@faker-js/faker'
|
|
2
|
+
import { ObjectId } from 'mongodb'
|
|
3
|
+
import { after, before } from 'node:test'
|
|
4
|
+
import { generateNewId } from '../usecases/mongo/generate_new_id.mjs'
|
|
5
|
+
import { closeClient } from '../usecases/mongo/mongo_client.mjs'
|
|
6
|
+
import { deleteOne } from '../usecases/mongo/operation/delete_one.mjs'
|
|
7
|
+
import { insertOne } from '../usecases/mongo/operation/insert_one.mjs'
|
|
8
|
+
import { mockGetCollection } from './mock_get_collection.mjs'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* @param {import('./mock_get_collection.mjs').ItestCollection} param
|
|
13
|
+
* @param {object} options
|
|
14
|
+
* @param {boolean} [options.deleteAtEnd]
|
|
15
|
+
*/
|
|
16
|
+
export function manageMockRegistry({ tag, ...rest } = {}, {
|
|
17
|
+
deleteAtEnd = true
|
|
18
|
+
} = {}) {
|
|
19
|
+
|
|
20
|
+
const id = generateNewId()
|
|
21
|
+
const name = rest.name ?? fakerPT_BR.person.fullName()
|
|
22
|
+
|
|
23
|
+
before(async () => {
|
|
24
|
+
/** @type {import('./mock_get_collection.mjs').ItestCollection} */
|
|
25
|
+
const doc = {
|
|
26
|
+
id,
|
|
27
|
+
createdAt: new Date(),
|
|
28
|
+
tag,
|
|
29
|
+
name: name,
|
|
30
|
+
...rest,
|
|
31
|
+
}
|
|
32
|
+
await insertOne({
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
getCollection: mockGetCollection,
|
|
35
|
+
doc: doc,
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
after(async () => {
|
|
40
|
+
if (deleteAtEnd) {
|
|
41
|
+
await deleteOne({
|
|
42
|
+
query: {
|
|
43
|
+
_id: ObjectId.createFromHexString(id)
|
|
44
|
+
},
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
getCollection: mockGetCollection,
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
await closeClient()
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
id, name
|
|
54
|
+
}
|
|
55
55
|
}
|
|
@@ -1,21 +1,23 @@
|
|
|
1
|
-
import { Collection, ObjectId } from 'mongodb'
|
|
2
|
-
import { getClient } from '../usecases/mongo/mongo_client.mjs'
|
|
3
|
-
|
|
4
|
-
export async function mockGetCollection(collectionName = 'test') {
|
|
5
|
-
const client = await getClient()
|
|
6
|
-
const db = client.db()
|
|
7
|
-
/** @type {Collection<ItestCollection>} */
|
|
8
|
-
const collection = db.collection(collectionName)
|
|
9
|
-
return collection
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @typedef {object} ItestCollection
|
|
14
|
-
* @property {ObjectId} [_id]
|
|
15
|
-
* @property {string} [id]
|
|
16
|
-
* @property {string} [name]
|
|
17
|
-
* @property {string | ObjectId} [tag]
|
|
18
|
-
* @property {Date} [createdAt]
|
|
19
|
-
* @property {object[]} [posts]
|
|
20
|
-
* @property {
|
|
1
|
+
import { Collection, ObjectId } from 'mongodb'
|
|
2
|
+
import { getClient } from '../usecases/mongo/mongo_client.mjs'
|
|
3
|
+
|
|
4
|
+
export async function mockGetCollection(collectionName = 'test') {
|
|
5
|
+
const client = await getClient()
|
|
6
|
+
const db = client.db()
|
|
7
|
+
/** @type {Collection<ItestCollection>} */
|
|
8
|
+
const collection = db.collection(collectionName)
|
|
9
|
+
return collection
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {object} ItestCollection
|
|
14
|
+
* @property {ObjectId} [_id]
|
|
15
|
+
* @property {string} [id]
|
|
16
|
+
* @property {string} [name]
|
|
17
|
+
* @property {string | ObjectId} [tag]
|
|
18
|
+
* @property {Date} [createdAt]
|
|
19
|
+
* @property {object[]} [posts]
|
|
20
|
+
* @property {number} [score]
|
|
21
|
+
* @property {string} [group]
|
|
22
|
+
* @property {object} [address]
|
|
21
23
|
*/
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export class OperationFail extends Error {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
*
|
|
5
|
-
* @param {string} msg
|
|
6
|
-
*/
|
|
7
|
-
constructor(msg) {
|
|
8
|
-
super(msg)
|
|
9
|
-
}
|
|
1
|
+
export class OperationFail extends Error {
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param {string} msg
|
|
6
|
+
*/
|
|
7
|
+
constructor(msg) {
|
|
8
|
+
super(msg)
|
|
9
|
+
}
|
|
10
10
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export class NotImplemented extends Error {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
*
|
|
5
|
-
* @param {string} msg
|
|
6
|
-
*/
|
|
7
|
-
constructor(msg) {
|
|
8
|
-
super(msg)
|
|
9
|
-
}
|
|
1
|
+
export class NotImplemented extends Error {
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param {string} msg
|
|
6
|
+
*/
|
|
7
|
+
constructor(msg) {
|
|
8
|
+
super(msg)
|
|
9
|
+
}
|
|
10
10
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef {'$eq' | '$lt' | '$lte'} ImongoOperator
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {'$eq' | '$lt' | '$lte'} ImongoOperator
|
|
3
3
|
*/
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @template {import('mongodb').Document} T
|
|
3
|
-
* @typedef {object} InativeCollection
|
|
4
|
-
* @property {InativeInsertOne<T>} insertOne
|
|
5
|
-
* @property {InativeDeleteOne<T>} deleteOne
|
|
6
|
-
* @property {InativeUpdateOne<T>} updateOne
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @template {import('mongodb').Document} T
|
|
11
|
-
* @callback InativeInsertOne
|
|
12
|
-
* @param {import('../../types.js').Optional<T, '_id'>} data
|
|
13
|
-
* @returns {Promise<any>}
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @template {import('mongodb').Document} T
|
|
18
|
-
* @callback InativeDeleteOne
|
|
19
|
-
* @param {import('mongodb').Filter<T>} filter
|
|
20
|
-
* @returns {Promise<any>}
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* @template {import('mongodb').Document} T
|
|
25
|
-
* @callback InativeUpdateOne
|
|
26
|
-
* @param {import('mongodb').Filter<T>} filter
|
|
27
|
-
* @param {import('mongodb').UpdateFilter<T>} update
|
|
1
|
+
/**
|
|
2
|
+
* @template {import('mongodb').Document} T
|
|
3
|
+
* @typedef {object} InativeCollection
|
|
4
|
+
* @property {InativeInsertOne<T>} insertOne
|
|
5
|
+
* @property {InativeDeleteOne<T>} deleteOne
|
|
6
|
+
* @property {InativeUpdateOne<T>} updateOne
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @template {import('mongodb').Document} T
|
|
11
|
+
* @callback InativeInsertOne
|
|
12
|
+
* @param {import('../../types.js').Optional<T, '_id'>} data
|
|
13
|
+
* @returns {Promise<any>}
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @template {import('mongodb').Document} T
|
|
18
|
+
* @callback InativeDeleteOne
|
|
19
|
+
* @param {import('mongodb').Filter<T>} filter
|
|
20
|
+
* @returns {Promise<any>}
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @template {import('mongodb').Document} T
|
|
25
|
+
* @callback InativeUpdateOne
|
|
26
|
+
* @param {import('mongodb').Filter<T>} filter
|
|
27
|
+
* @param {import('mongodb').UpdateFilter<T>} update
|
|
28
28
|
*/
|