v1.0.3
Extend from deno_mongo, support Schema and extend some API
Attributes
Includes Deno configuration
Repository
Current version released
2 years ago
Versions
- v1.0.3Latest
- v1.0.2
- v1.0.1
- v1.0.0
- v0.11.1
- v0.11.0
- v0.10.5
- v0.10.4
- v0.10.3
- v0.10.2
- v0.10.1
- v0.10.0
- v0.9.2
- v0.9.1
- v0.9.0
- v0.8.5
- v0.8.4
- v0.8.3
- v0.8.2
- v0.8.1
- v0.8.0
- v0.7.5
- v0.7.4
- v0.7.3
- v0.7.2
- v0.7.1
- v0.7.0
- v0.6.6
- v0.6.5
- v0.6.4
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.9
- v0.5.8
- v0.5.7
- v0.5.6
- v0.5.5
- v0.5.4
- v0.5.3
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.0
- v0.3.7
- v0.3.6
- v0.3.5
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.1
- v0.2.0
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.1
deno_mongo_schema
Extend from the Node.js mongodb client v5.3.0, support Schema and extend some API.
Breaking changes on v1.0.0.
I have to switch client from deno_mongo to Node.js. Because it is officially maintained, it looks more robust.
If it is to be used in production, reconnecting after wire breakage is an important function.
hooks
import {
BaseSchema,
getDB,
getModel,
MongoFactory,
MongoHookMethod,
Prop,
Schema,
UpdateExOptions,
} from "https://deno.land/x/deno_mongo_schema@v1.0.3/mod.ts";
import type { Document } from "https://deno.land/x/deno_mongo_schema@v1.0.3/mod.ts";
await MongoFactory.forRoot("mongodb://localhost:27017/test");
@Schema()
class User extends BaseSchema {
@Prop()
age!: number;
@Prop({
required: true,
})
name!: string;
@Prop({
default: Date.now,
expires: 60, // seconds
})
expires?: Date;
}
const UserSchema = SchemaFactory.createForClass(User);
UserSchema.pre(
MongoHookMethod.update,
function (filter: Document, doc: Document, options?: UpdateExOptions) {
console.log("----pre----", filter, doc, options);
if (!doc.$set) {
doc.$set = {};
}
doc.$set.modifyTime = new Date();
},
);
UserSchema.post(MongoHookMethod.findOneAndUpdate, function (doc) {
console.log("----post----", doc);
doc.name = "haha";
});
const userModel = await MongoFactory.getModel(User);
const id = await userModel.insertOne({
"name": "zhangsan",
"age": 18,
});
UserSchema.post(MongoHookMethod.findOne, function (doc) {
console.log("----post---findOne----", doc);
});
// console.log(id);
const info = await userModel.findById(id, {
projection: {
name: 1,
},
});
console.log(info);
UserSchema.post(MongoHookMethod.findMany, function (doc) {
console.log("----post---findMany----", doc);
});
const arr = await userModel.findMany({});
console.log(arr);
UserSchema.post(MongoHookMethod.delete, function (doc) {
console.log("----post---delete----", doc);
});
const del = await userModel.deleteOne({
name: "zhangsan",
});
console.log(del);
const delMulti = await userModel.deleteMany({
name: "zhangsan",
});
console.log(delMulti);
Here are some useful APIs:
- find
- findById
- findOne
- findMany
- countDocuments
- aggregate
- distinct
- insert
- insertOne
- insertMany
- update
- findByIdAndUpdate
- findOneAndUpdate
- updateMany
- updateOne
- delete
- deleteMany
- deleteOne/findOneAndDelete
- deleteById/findByIdAndDelete
- index
- syncIndexes
- dropIndexes
- listIndexes
- createIndexes
- drop collection
- drop
Virtual
Or you can use virtual like this:
import {
BaseSchema,
MongoFactory,
Prop,
Schema,
} from "https://deno.land/x/deno_mongo_schema@v1.0.3/mod.ts";
await MongoFactory.forRoot("mongodb://localhost:27017/test");
@Schema()
class User extends BaseSchema {
@Prop()
group!: string;
@Prop()
title!: string;
}
@Schema()
class Role extends BaseSchema {
@Prop()
userId!: string;
@Prop()
name!: string;
}
const RoleSchema = SchemaFactory.createForClass(Role);
RoleSchema.virtual("user", {
ref: User,
localField: "userId",
foreignField: "_id",
justOne: true,
isTransformLocalFieldToObjectID: true,
});
// Role.populate("user", {
// // _id: 0,
// group: 1,
// // title: 1,
// });
// Role.populate("user", "group");
// Role.populate("user", "-group -createTime");
// Role.populate("user", "title group");
const roleModel = await MongoFactory.getModel(Role);
// roleModel.insertOne({
// userId: id,
// name: "normal",
// });
console.log(
await roleModel.findMany({}, {
projection: {
name: 1,
userId: 1,
},
// skip: 1,
// limit: 1,
populates: {
// user: {
// // _id: 0,
// group: 1,
// title: 1,
// },
// user: "group",
user: true,
// user: "-_id -title",
},
}),
);
Special collection name
If you donnot want to use the default collection name, you must regiter it by yourself.
SchemaFactory.createForClass(Role, "mongo_test_schema_roles");
Then if you still want to use virtual, you must use your registered name instead of Schema Class.
UserSchema.virtual("role", {
ref: "mongo_test_schema_roles",
localField: "roleId",
foreignField: "_id",
justOne: true,
// isTransformLocalFieldToObjectID: true,
// isTransformObjectIDToLocalField: true
});
TODO
- Modify schema as a decorator
- Unit
- Configurable whether to convert _id
- Configurable the createTime and modifyTime
- Switch the underlying layer to the official Node.js version library