bridge-mongo
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -13,3 +13,7 @@ import { BridgeModelI } from './types'; | ||
create: BridgeModelI<Name, SchemaDef, ModelI, Config, FullModelI>['create']; | ||
find: BridgeModelI<Name, SchemaDef, ModelI, Config, FullModelI>['find']; | ||
findById: BridgeModelI<Name, SchemaDef, ModelI, Config, FullModelI>['findById']; | ||
findOne: BridgeModelI<Name, SchemaDef, ModelI, Config, FullModelI>['findOne']; | ||
findOneAndUpdate: BridgeModelI<Name, SchemaDef, ModelI, Config, FullModelI>['findOneAndUpdate']; | ||
findByIdAndUpdate: BridgeModelI<Name, SchemaDef, ModelI, Config, FullModelI>['findByIdAndUpdate']; | ||
} | ||
@@ -16,0 +20,0 @@ export * from './types'; |
@@ -39,2 +39,3 @@ "use strict"; | ||
}; | ||
// Should we handle some other errors here ? | ||
else | ||
@@ -44,13 +45,74 @@ throw err; | ||
}; | ||
findOne = async (filter, projection) => { | ||
find = async (filter, projection, options) => { | ||
try { | ||
const promise = this.mongooseModel.findOne(filter); | ||
const res = projection ? await promise.select(projection).lean() : await promise.lean(); | ||
return await this.mongooseModel.find(filter, projection || undefined, { | ||
lean: true, | ||
...(options || {}), | ||
}); | ||
} | ||
catch (err) { | ||
// Should we handle some errors here ? | ||
throw err; | ||
} | ||
}; | ||
findById = async (filter, projection, options) => { | ||
try { | ||
const res = await this.mongooseModel.findById(filter, projection || undefined, { | ||
lean: true, | ||
...(options || {}), | ||
}); | ||
if (!res) | ||
return { error: { status: 404, name: 'Document not found' } }; | ||
return { error: { status: 404, name: `${this.modelName} not found` } }; | ||
return res; | ||
} | ||
catch (err) { | ||
return { error: { status: 404, name: `${this.modelName} not found`, data: err } }; | ||
// Should we handle some errors here ? | ||
throw err; | ||
} | ||
}; | ||
findOne = async (filter, projection, options) => { | ||
try { | ||
const res = await this.mongooseModel.findOne(filter, projection || undefined, { | ||
lean: true, | ||
...(options || {}), | ||
}); | ||
if (!res) | ||
return { error: { status: 404, name: `${this.modelName} not found` } }; | ||
return res; | ||
} | ||
catch (err) { | ||
// Should we handle some errors here ? | ||
throw err; | ||
} | ||
}; | ||
findOneAndUpdate = async (filter, updateQuery, options) => { | ||
try { | ||
const res = await this.mongooseModel.findOneAndUpdate(filter, updateQuery, { | ||
lean: true, | ||
...options, | ||
}); | ||
if (!res) | ||
return { error: { status: 404, name: `${this.modelName} not found` } }; | ||
return res; | ||
} | ||
catch (err) { | ||
// Should we handle some errors here ? | ||
throw err; | ||
} | ||
}; | ||
findByIdAndUpdate = async (filter, updateQuery, options) => { | ||
try { | ||
const res = await this.mongooseModel.findByIdAndUpdate(filter, updateQuery, { | ||
lean: true, | ||
...options, | ||
}); | ||
if (!res) | ||
return { error: { status: 404, name: `${this.modelName} not found` } }; | ||
return res; | ||
} | ||
catch (err) { | ||
// Should we handle some errors here ? | ||
throw err; | ||
} | ||
}; | ||
} | ||
@@ -57,0 +119,0 @@ exports.BridgeModel = BridgeModel; |
import { Pretify, StricProperty, PreserverOptionalKeys } from '../utils'; | ||
import { SchemaDefinition, ObjectId, FilterQuery } from 'mongoose'; | ||
import { SchemaDefinition, ObjectId, FilterQuery, SortOrder, ClientSession, UpdateQuery } from 'mongoose'; | ||
import { SchemaToType, SchemaConfig, DefaultValueProperties } from '../schema'; | ||
import { IncludeIdAndTimestamps, CreateReturnWithErrors, Projection, ExtractModelIFromProj } from './utilities'; | ||
import { IncludeIdAndTimestamps, CreateReturnWithErrors, Projection, ExtractModelIFromProj, CompleteProj } from './utilities'; | ||
export interface BridgeModelI<ModelName extends string, SchemaDef extends SchemaDefinition, ModelI extends Pretify<SchemaToType<SchemaDef>>, Config extends SchemaConfig, FullModelI extends Pretify<IncludeIdAndTimestamps<ModelI & Required<Pick<ModelI, DefaultValueProperties<SchemaDef>>>, Config>>> { | ||
create: <CreateData extends ModelI>(data: StricProperty<CreateData, ModelI>) => Promise<Pretify<CreateReturnWithErrors<SchemaDef, IncludeIdAndTimestamps<CreateData & Required<Pick<ModelI, DefaultValueProperties<SchemaDef>>>, Config>, ModelName>>>; | ||
findOne: <Proj extends Projection<Required<FullModelI>> | undefined = undefined>(filer: FilterQuery<FullModelI>, proj?: Proj) => Promise<(Proj extends undefined ? FullModelI : Pretify<PreserverOptionalKeys<ExtractModelIFromProj<Required<FullModelI>, Proj>, FullModelI> & { | ||
create: <CreateData extends ModelI>(data: StricProperty<CreateData, ModelI>, options?: { | ||
session?: ClientSession; | ||
}) => Promise<Pretify<CreateReturnWithErrors<SchemaDef, IncludeIdAndTimestamps<CreateData & Required<Pick<ModelI, DefaultValueProperties<SchemaDef>>>, Config>, ModelName>>>; | ||
find: <Proj extends Projection<Required<FullModelI>> = CompleteProj<FullModelI>>(filer: FilterQuery<FullModelI>, proj?: Proj, options?: { | ||
limit?: number; | ||
skip?: number; | ||
sort?: { | ||
[T in keyof FullModelI]?: SortOrder; | ||
}; | ||
session?: ClientSession; | ||
}) => Promise<Array<Pretify<PreserverOptionalKeys<ExtractModelIFromProj<Required<FullModelI>, Proj>, FullModelI> & { | ||
_id: ObjectId; | ||
}>) | { | ||
}>>>; | ||
findById: <Proj extends Projection<Required<FullModelI>> = CompleteProj<FullModelI>>(filer: string | ObjectId, proj?: Proj, options?: { | ||
session?: ClientSession; | ||
}) => Promise<Pretify<PreserverOptionalKeys<ExtractModelIFromProj<Required<FullModelI>, Proj>, FullModelI> & { | ||
_id: ObjectId; | ||
}> | { | ||
error: { | ||
@@ -16,3 +30,49 @@ status: 404; | ||
}>; | ||
findOne: <Proj extends Projection<Required<FullModelI>> = CompleteProj<FullModelI>>(filer: FilterQuery<FullModelI>, proj?: Proj, options?: { | ||
session?: ClientSession; | ||
}) => Promise<Pretify<PreserverOptionalKeys<ExtractModelIFromProj<Required<FullModelI>, Proj>, FullModelI> & { | ||
_id: ObjectId; | ||
}> | { | ||
error: { | ||
status: 404; | ||
name: `${ModelName} not found`; | ||
}; | ||
}>; | ||
findOneAndUpdate: <Proj extends Projection<Required<FullModelI>> = CompleteProj<FullModelI>>(filter: FilterQuery<FullModelI>, updateQuery: UpdateQuery<ModelI>, options?: { | ||
projection?: Proj; | ||
session?: ClientSession; | ||
returnDocument?: 'before' | 'after'; | ||
new?: boolean; | ||
sort?: { | ||
[T in keyof FullModelI]?: SortOrder; | ||
}; | ||
timestamps?: boolean; | ||
overwrite?: boolean; | ||
}) => Promise<Pretify<PreserverOptionalKeys<ExtractModelIFromProj<Required<FullModelI>, Proj>, FullModelI> & { | ||
_id: ObjectId; | ||
}> | { | ||
error: { | ||
status: 404; | ||
name: `${ModelName} not found`; | ||
}; | ||
}>; | ||
findByIdAndUpdate: <Proj extends Projection<Required<FullModelI>> = CompleteProj<FullModelI>>(filter: string | ObjectId, updateQuery: UpdateQuery<ModelI>, options?: { | ||
projection?: Proj; | ||
session?: ClientSession; | ||
returnDocument?: 'before' | 'after'; | ||
new?: boolean; | ||
sort?: { | ||
[T in keyof FullModelI]?: SortOrder; | ||
}; | ||
timestamps?: boolean; | ||
overwrite?: boolean; | ||
}) => Promise<Pretify<PreserverOptionalKeys<ExtractModelIFromProj<Required<FullModelI>, Proj>, FullModelI> & { | ||
_id: ObjectId; | ||
}> | { | ||
error: { | ||
status: 404; | ||
name: `${ModelName} not found`; | ||
}; | ||
}>; | ||
} | ||
//# sourceMappingURL=types.d.ts.map |
@@ -25,2 +25,5 @@ import { Pretify } from '../utils'; | ||
export type FullModelIGen<ModelI extends Pretify<SchemaToType<SchemaDef>>, SchemaDef extends SchemaDefinition, Config extends SchemaConfig> = Pretify<IncludeIdAndTimestamps<ModelI & Required<Pick<ModelI, DefaultValueProperties<SchemaDef>>>, Config>>; | ||
export type CompleteProj<Model> = { | ||
[T in keyof Required<Model>]: 1; | ||
}; | ||
//# sourceMappingURL=utilities.d.ts.map |
@@ -29,14 +29,18 @@ "use strict"; | ||
}); | ||
const res = DB.test.modelInterface; | ||
const config = DB.test.configInterface; | ||
const test = DB.test.create({ sf: 78 }); | ||
const user = DB.test.findOne({}, { age: 1, sf: 1 }); | ||
DB.user.mongooseModel.create({ name: 'df' }); | ||
// // type Plurial<T extends string> = T extends `${string}${'s' | 'sh' | 'ch' | 'x' | 'z'}` | ||
// // ? `${T}es` | ||
// // : `${T}s`; | ||
// // type Result = Plurial<'chevals'>; // 'a' | ||
const ess = DB.user.mongooseModel.create({ name: 'YO' }).then(async (res) => { | ||
res.name = 'NON'; | ||
await res.save(); | ||
}); | ||
async () => { | ||
const res = DB.test.modelInterface; | ||
const config = DB.test.configInterface; | ||
const test = DB.test.create({ sf: 78 }); | ||
const user = DB.test.findOne({}, { age: 1 }); | ||
DB.user.mongooseModel.create({ name: 'df' }); | ||
const dsf = await DB.test.findOne({}, { age: 1, buffer: 1 }); | ||
// const assdfd = await DB.user.create({'name':}) | ||
// // type Plurial<T extends string> = T extends `${string}${'s' | 'sh' | 'ch' | 'x' | 'z'}` | ||
// // ? `${T}es` | ||
// // : `${T}s`; | ||
// // type Result = Plurial<'chevals'>; // 'a' | ||
const ess = DB.user.mongooseModel.create({ name: 'YO' }).then(async (res) => { | ||
res.name = 'NON'; | ||
await res.save(); | ||
}); | ||
}; |
{ | ||
"name": "bridge-mongo", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A mongodb ORM on top of mongoose that match perfectly with the Bridge framework", | ||
@@ -5,0 +5,0 @@ "main": "dist/source/index.js", |
@@ -1,2 +0,57 @@ | ||
# bridge-mongo | ||
A fully typed mongoose ORM | ||
# Bridge-Mongo | ||
A fully typed mongodb ORM, based on mongoose. The project is under active development. | ||
<h2> <img src="/img/question2.svg" height="20" /> What is Bridge-Mongo? </h2> | ||
Bridge-Mongo is meant to be a type-safe ORM built on top of Mongoose. The main focus is to provide a synthax as close as possible to mongoose, while providing full type-safety and auto-completion throughout all operations. | ||
<img src="/img/bridge-mongo-gif.gif" /> | ||
<h2> <img src="/img/development2.svg" height="20"/> Development </h2> | ||
To follow the development or contribute, join [the Discord](https://discord.gg/yxjrwm7Bfr) server. | ||
- [x] Models | ||
- [x] CRUD | ||
- [ ] Populate | ||
- [ ] Aggregate | ||
<h2> <img src="/img/contributing.svg" height="20" /> Contributing </h2> | ||
If you want any guidance whatsoever with the contribution, don't hesitate to reach out [on Discord](https://discord.gg/yxjrwm7Bfr)! | ||
<h2> <img src="/img/installation.svg" height="20" /> Installation </h2> | ||
You can use your favorite package manager to install **Bridge-Mongo**. | ||
``` | ||
npm i bridge-mongo | ||
``` | ||
### Example | ||
```ts | ||
import { isError, Schema, mongoose, createDB } from "bridge-mongo" | ||
const user = new Schema({ | ||
name: { type: String, required: true }, | ||
email: { type: String, required: true }, | ||
age: Number, | ||
additionnalInformation: { | ||
likeAnimals: Boolean | ||
} | ||
}, { | ||
timestamps: true | ||
}) | ||
const DB = createDB({ | ||
user | ||
}) | ||
async () => { | ||
const newUser = await DB.user.create({ | ||
email: "dave@bridge.codes", | ||
name: "Dave" | ||
}) | ||
} | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
36509
484
58