<commit_before>"
A MaplessMongoRepository is a connection layer between Mapless and the mongo database. Use it using 




"
Class {
	#name : #MaplessMongoRepository,
	#superclass : #MaplessRepository,
	#instVars : [
		'mongoClient',
		'concerns',
		'defaultConcerns'
	],
	#category : #'Mapless-Mongo-Core-Connections'
}

{ #category : #actions }
MaplessMongoRepository class >> getNewId [
	^ OID nextOID
]

{ #category : #accessing }
MaplessMongoRepository >> concerns [
	^ concerns ifNil: [ self initializeConcerns ]
]

{ #category : #accessing }
MaplessMongoRepository >> count: aMaplessClass [
	"Answer what's the number of instances of aMaplessClass in this repository."

	^ self count: aMaplessClass where: {} asDictionary
]

{ #category : #accessing }
MaplessMongoRepository >> count: aMaplessClass where: someConditions [
	"Answer what's the number of instances of aMaplessClass in this repository."

	^ self
		readOnlyDo: [ (self databaseCollectionNamed: aMaplessClass collectionName)
				sizeForQuery: someConditions ]
]

{ #category : #accessing }
MaplessMongoRepository >> database [
	^ mongoClient databaseNamed: databaseName
]

{ #category : #accessing }
MaplessMongoRepository >> databaseCollectionNamed: aCollectionName [
	"Answers the collection for this kind of models hold by the current database"

	^ self database collectionAt: aCollectionName
]

{ #category : #accessing }
MaplessMongoRepository >> defaultConcerns [
	^ defaultConcerns ifNil: [ self initializeDefaultConcerns ]
]

{ #category : #accessing }
MaplessMongoRepository >> defaultDeleteConcern [
	^ self defaultConcerns at: #delete
]

{ #category : #accessing }
MaplessMongoRepository >> defaultDeleteConcern: aConcernOrNil [
	^ self defaultConcerns at: #delete put: aConcernOrNil
]

{ #category : #accessing }
MaplessMongoRepository >> defaultInsertConcern [
	^ self defaultConcerns at: #insert
]

{ #category : #accessing }
MaplessMongoRepository >> defaultInsertConcern: aConcernOrNil [
	^ self defaultConcerns at: #insert put: aConcernOrNil
]

{ #category : #accessing }
MaplessMongoRepository >> defaultUpdateConcern [
	^ self defaultConcerns at: #update
]

{ #category : #accessing }
MaplessMongoRepository >> defaultUpdateConcern: aConcernOrNil [
	^ self defaultConcerns at: #update put: aConcernOrNil
]

{ #category : #accessing }
MaplessMongoRepository >> defaultUpsertConcern [
	^ self defaultConcerns at: #upsert
]

{ #category : #accessing }
MaplessMongoRepository >> defaultUpsertConcern: aConcernOrNil [
	^ self defaultConcerns at: #upsert put: aConcernOrNil
]

{ #category : #accessing }
MaplessMongoRepository >> deleteConcern: aConcern for: aMaplessClass [
	^ (self concerns at: #delete) at: aMaplessClass put: aConcern
]

{ #category : #accessing }
MaplessMongoRepository >> deleteConcernFor: aMaplessClass [
	^ (self concerns at: #delete)
		at: aMaplessClass
		ifPresent: [ :val | val ]
		ifAbsent: [ self defaultDeleteConcern ]
]

{ #category : #actions }
MaplessMongoRepository >> destroy: aMapless [
	"Removes aMapless from its corresponding collection."

	self
		destroy: aMapless
		writeConcern: (self deleteConcernFor: aMapless class)
]

{ #category : #actions }
MaplessMongoRepository >> destroy: aMapless writeConcern: aConcernOrNil [
	"Removes aMapless from its corresponding collection."

	self onBeforeDestroy: aMapless.
	self
		readWriteDo: [ (self databaseCollectionNamed: aMapless class collectionName)
				commandDelete: {(self idPropertyName -> aMapless id)} asDictionary
				limit: 1
				writeConcern: aConcernOrNil ].
	self onAfterDestroy: aMapless
]

{ #category : #actions }
MaplessMongoRepository >> destroyAll: aMaplessClass where: filter writeConcern: aConcernOrNil [
	"Removes the documents matching the filter criteria from the MongoDB collection corresponding to aMaplessClass."

	^ self
		readWriteDo: [ (self databaseCollectionNamed: aMaplessClass collectionName)
				commandDelete: filter
				limit: 0
				writeConcern: aConcernOrNil ]
]

{ #category : #actions }
MaplessMongoRepository >> destroyOne: aMaplessClass atId: anId [
	"Removes the document anId from the MongoDB collection corresponding to aMaplessClass."

	self
		destroyOne: aMaplessClass
		atId: anId
		writeConcern: (self deleteConcernFor: aMaplessClass)
]

{ #category : #actions }
MaplessMongoRepository >> destroyOne: aMaplessClass atId: anId writeConcern: aConcernOrNil [
	"Removes the document anId from the MongoDB collection corresponding to aMaplessClass."

	self
		destroyAll: aMaplessClass
		where: {(self idPropertyName -> anId)} asDictionary
		writeConcern: aConcernOrNil
]

{ #category : #actions }
MaplessMongoRepository >> do: aBlock [
	^ self do: aBlock with: accessor
]

{ #category : #actions }
MaplessMongoRepository >> do: aBlock with: aDatabaseAccessor [
	"Performs aBlock and providing access to the mongo client in use during this thread to 
	anyone calling MaplessCurrentRepository value."

	aDatabaseAccessor
		readWriteDo: [ :client | 
			[ mongoClient := client.
			MaplessCurrentRepository value: self during: [ ^ aBlock value ] ]
				ensure: [ mongoClient := nil ] ]
]
<commit_msg>improves destroy* API <commit_after>"
A MaplessMongoRepository is a connection layer between Mapless and the mongo database. Use it using 




"
Class {
	#name : #MaplessMongoRepository,
	#superclass : #MaplessRepository,
	#instVars : [
		'mongoClient',
		'concerns',
		'defaultConcerns'
	],
	#category : #'Mapless-Mongo-Core-Connections'
}

{ #category : #actions }
MaplessMongoRepository class >> getNewId [
	^ OID nextOID
]

{ #category : #accessing }
MaplessMongoRepository >> concerns [
	^ concerns ifNil: [ self initializeConcerns ]
]

{ #category : #accessing }
MaplessMongoRepository >> count: aMaplessClass [
	"Answer what's the number of instances of aMaplessClass in this repository."

	^ self count: aMaplessClass where: {} asDictionary
]

{ #category : #accessing }
MaplessMongoRepository >> count: aMaplessClass where: someConditions [
	"Answer what's the number of instances of aMaplessClass in this repository."

	^ self
		readOnlyDo: [ (self databaseCollectionNamed: aMaplessClass collectionName)
				sizeForQuery: someConditions ]
]

{ #category : #accessing }
MaplessMongoRepository >> database [
	^ mongoClient databaseNamed: databaseName
]

{ #category : #accessing }
MaplessMongoRepository >> databaseCollectionNamed: aCollectionName [
	"Answers the collection for this kind of models hold by the current database"

	^ self database collectionAt: aCollectionName
]

{ #category : #accessing }
MaplessMongoRepository >> defaultConcerns [
	^ defaultConcerns ifNil: [ self initializeDefaultConcerns ]
]

{ #category : #accessing }
MaplessMongoRepository >> defaultDeleteConcern [
	^ self defaultConcerns at: #delete
]

{ #category : #accessing }
MaplessMongoRepository >> defaultDeleteConcern: aConcernOrNil [
	^ self defaultConcerns at: #delete put: aConcernOrNil
]

{ #category : #accessing }
MaplessMongoRepository >> defaultInsertConcern [
	^ self defaultConcerns at: #insert
]

{ #category : #accessing }
MaplessMongoRepository >> defaultInsertConcern: aConcernOrNil [
	^ self defaultConcerns at: #insert put: aConcernOrNil
]

{ #category : #accessing }
MaplessMongoRepository >> defaultUpdateConcern [
	^ self defaultConcerns at: #update
]

{ #category : #accessing }
MaplessMongoRepository >> defaultUpdateConcern: aConcernOrNil [
	^ self defaultConcerns at: #update put: aConcernOrNil
]

{ #category : #accessing }
MaplessMongoRepository >> defaultUpsertConcern [
	^ self defaultConcerns at: #upsert
]

{ #category : #accessing }
MaplessMongoRepository >> defaultUpsertConcern: aConcernOrNil [
	^ self defaultConcerns at: #upsert put: aConcernOrNil
]

{ #category : #accessing }
MaplessMongoRepository >> deleteConcern: aConcern for: aMaplessClass [
	^ (self concerns at: #delete) at: aMaplessClass put: aConcern
]

{ #category : #accessing }
MaplessMongoRepository >> deleteConcernFor: aMaplessClass [
	^ (self concerns at: #delete)
		at: aMaplessClass
		ifPresent: [ :val | val ]
		ifAbsent: [ self defaultDeleteConcern ]
]

{ #category : #actions }
MaplessMongoRepository >> destroy: aMapless [
	"Removes aMapless from its corresponding collection."

	self
		destroy: aMapless
		writeConcern: (self deleteConcernFor: aMapless class)
]

{ #category : #actions }
MaplessMongoRepository >> destroy: aMapless writeConcern: aConcernOrNil [
	"Removes aMapless from its corresponding collection."

	self onBeforeDestroy: aMapless.
	self
		readWriteDo: [ (self databaseCollectionNamed: aMapless class collectionName)
				commandDelete: {(self idPropertyName -> aMapless id)} asDictionary
				limit: 1
				writeConcern: aConcernOrNil ].
	self onAfterDestroy: aMapless
]

{ #category : #actions }
MaplessMongoRepository >> destroyAll: aMaplessClass where: filter [
	"Remove all the documents matching the filter criteria from the MongoDB collection corresponding to aMaplessClass."

	self
		destroyAll: aMaplessClass
		where: filter
		writeConcern: (self deleteConcernFor: aMaplessClass)
]

{ #category : #actions }
MaplessMongoRepository >> destroyAll: aMaplessClass where: filter writeConcern: aConcernOrNil [
	"Remove all the documents matching the filter criteria from the MongoDB collection corresponding to aMaplessClass."

	self
		readWriteDo: [ (self databaseCollectionNamed: aMaplessClass collectionName)
				commandDelete: filter
				limit: 0
				writeConcern: aConcernOrNil ]
]

{ #category : #actions }
MaplessMongoRepository >> destroyOne: aMaplessClass atId: anId [
	"Removes the document anId from the MongoDB collection corresponding to aMaplessClass."

	self
		destroyOne: aMaplessClass
		atId: anId
		writeConcern: (self deleteConcernFor: aMaplessClass)
]

{ #category : #actions }
MaplessMongoRepository >> destroyOne: aMaplessClass atId: anId writeConcern: aConcernOrNil [
	"Removes the document anId from the MongoDB collection corresponding to aMaplessClass."

	self
		readWriteDo: [ (self databaseCollectionNamed: aMaplessClass collectionName)
				commandDelete: {(self idPropertyName -> anId)} asDictionary
				limit: 1
				writeConcern: aConcernOrNil ]
]

{ #category : #actions }
MaplessMongoRepository >> do: aBlock [
	^ self do: aBlock with: accessor
]

{ #category : #actions }
MaplessMongoRepository >> do: aBlock with: aDatabaseAccessor [
	"Performs aBlock and providing access to the mongo client in use during this thread to 
	anyone calling MaplessCurrentRepository value."

	aDatabaseAccessor
		readWriteDo: [ :client | 
			[ mongoClient := client.
			MaplessCurrentRepository value: self during: [ ^ aBlock value ] ]
				ensure: [ mongoClient := nil ] ]
]

{'fraction_non_alphanumeric': 0.08777547560745903, 'fraction_numerical': 0.00047089847428894333, 'mean_word_length': 5.3321407274895645, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5896623', 'n_tokens_mistral': 3491, 'n_tokens_neox': 3237, 'n_words': 902}