-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01dcce6
commit 4db725a
Showing
27 changed files
with
252 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ name: Test Suite | |
on: | ||
push: | ||
tags: | ||
- "v*" | ||
- 'v*' | ||
|
||
workflow_dispatch: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import { setCustomMongooseOptions } from '@kikiutils/mongoose/options'; | ||
import s from '@kikiutils/mongoose/schema-builders'; | ||
import { buildMongooseModel } from '@kikiutils/mongoose/utils'; | ||
import { Schema } from 'mongoose'; | ||
import type { ProjectionType, QueryOptions, Types } from 'mongoose'; | ||
import { env } from 'node:process'; | ||
import type { Except } from 'type-fest'; | ||
|
||
// Load global types | ||
|
@@ -16,7 +19,7 @@ import type {} from '@kikiutils/mongoose/types/data'; | |
* the actual project shouldn't have this line | ||
* (unless you want to handle it the same way as the default). | ||
*/ | ||
process.env.MONGODB_URI ||= 'mongodb://127.0.0.1:27017/kikiutils-mongoose-example?directConnection=true'; | ||
env.MONGODB_URI ||= 'mongodb://127.0.0.1:27017/kikiutils-mongoose-example?directConnection=true'; | ||
|
||
/** | ||
* Set custom mongoose options. | ||
|
@@ -27,7 +30,7 @@ process.env.MONGODB_URI ||= 'mongodb://127.0.0.1:27017/kikiutils-mongoose-exampl | |
* Please make sure to set it before using buildMongooseModel, | ||
* as it will not affect models that have already been built before setting it. | ||
*/ | ||
setCustomMongooseOptions('beforeModelBuild', (schema) => { | ||
setCustomMongooseOptions('beforeModelBuild', (_schema) => { | ||
// console.log('building model with schema: ', schema); | ||
}); | ||
|
||
|
@@ -78,20 +81,20 @@ interface UserMethodsAndOverrides { | |
|
||
interface UserModel extends BaseMongoosePaginateModel<User, UserMethodsAndOverrides> { | ||
// Model static methods | ||
findByAccount(account: string, projection?: ProjectionType<User> | null, options?: QueryOptions<User> | null): MongooseFindOneReturnType<User, UserDocument, {}, UserMethodsAndOverrides>; | ||
findByAccount: (account: string, projection?: ProjectionType<User> | null, options?: QueryOptions<User> | null) => MongooseFindOneReturnType<User, UserDocument, object, UserMethodsAndOverrides>; | ||
} | ||
|
||
type UserDocument = MongooseHydratedDocument<User, UserMethodsAndOverrides>; | ||
export type UserDocument = MongooseHydratedDocument<User, UserMethodsAndOverrides>; | ||
|
||
// Define schema | ||
const userSchema = new Schema<User, UserModel, UserMethodsAndOverrides>({ | ||
account: s.string().maxlength(16).trim.unique.required, | ||
// @ts-expect-error | ||
// @ts-expect-error Ignore this error. | ||
// Use setRoundAndToFixedSetter to round up on save and setToStringGetter to convert to string on get | ||
balance: s.decimal128().setRoundAndToFixedSetter().setToStringGetter.required, | ||
email: s.string().lowercase.trim.nonRequired, | ||
enabled: s.boolean().default(false).required, | ||
password: s.string().private.required | ||
password: s.string().private.required, | ||
}); | ||
|
||
// Set methods | ||
|
@@ -113,7 +116,7 @@ const user = await UserModel.create({ | |
account: Array.from({ length: 8 }, () => String.fromCharCode((Math.random() > 0.5 ? 97 : 65) + Math.floor(Math.random() * 26))).join(''), | ||
balance: '1000.501', | ||
email: '[email protected]', | ||
password: 'test-password' | ||
password: 'test-password', | ||
}); | ||
|
||
console.log('created user: ', user); | ||
|
@@ -129,7 +132,7 @@ console.log('verifying user password'); | |
console.log('verified user password: ', user.verifyPassword('test-password')); | ||
|
||
// Define user log data interface | ||
interface UserLogData extends BaseMongooseModelData<true, false> { | ||
export interface UserLogData extends BaseMongooseModelData<true, false> { | ||
content: string; | ||
type: number; | ||
user: Partial<UserData>; | ||
|
@@ -140,14 +143,14 @@ interface UserLog extends BaseMongooseDocType<Except<UserLogData, 'user'>, true, | |
user: Types.Decimal128; | ||
} | ||
|
||
type UserLogDocument = MongooseHydratedDocument<UserLog>; | ||
type UserLogModel = BaseMongoosePaginateModel<UserLog>; | ||
export type UserLogDocument = MongooseHydratedDocument<UserLog>; | ||
export type UserLogModel = BaseMongoosePaginateModel<UserLog>; | ||
|
||
// Define schema | ||
const userLogSchema = new Schema<UserLog, UserLogModel>({ | ||
content: s.string().trim.required, | ||
type: s.number().required, | ||
user: s.ref('User').required | ||
user: s.ref('User').required, | ||
}); | ||
|
||
// Build model | ||
|
@@ -158,7 +161,7 @@ console.log('creating user log'); | |
const userLog = await UserLogModel.create({ | ||
content: 'test content', | ||
type: 1, | ||
user: user._id | ||
user: user._id, | ||
}); | ||
|
||
console.log('created user log: ', userLog); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
{ | ||
"extends": "@kikiutils/tsconfigs/esnext/es2022.json", | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"paths": { | ||
"@/*": ["./src/*"] | ||
} | ||
}, | ||
"outDir": "./dist" | ||
}, | ||
"include": ["./src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** @type {import('jest').Config} */ | ||
module.exports = { | ||
testEnvironment: 'node', | ||
transform: { '^.+\\.tsx?$': ['ts-jest', { tsconfig: 'tsconfig.jest.json' }] } | ||
transform: { '^.+\\.tsx?$': ['ts-jest', { tsconfig: 'tsconfig.jest.json' }] }, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
import type { Schema } from 'mongoose'; | ||
|
||
export const createBaseSchemaBuilderFactory = <Builder = Readonly<Record<string, any>>>(type: BooleanConstructor | DateConstructor | NumberConstructor | Schema.Types.ObjectId['constructor'] | StringConstructor) => { | ||
const isFunctionKeys = new Set([ | ||
'default', | ||
'enum', | ||
'index', | ||
'max', | ||
'maxlength', | ||
'min', | ||
'minlength', | ||
]); | ||
|
||
export function createBaseSchemaBuilderFactory<Builder = Readonly<Record<string, any>>>(type: BooleanConstructor | DateConstructor | NumberConstructor | Schema.Types.ObjectId['constructor'] | StringConstructor) { | ||
return (schema: Record<string, any> = {}) => { | ||
schema.type = type; | ||
return new Proxy(Object.freeze({}), { | ||
get(_, key, receiver) { | ||
if (typeof key === 'symbol') throw new Error('Cannot use symbol as a schema attribute'); | ||
if (key in schema) throw new Error(`Duplicate schema attribute: ${key}`); | ||
if (isFunctionKeys.has(key)) return (value: any) => ((schema[key] = value), receiver); | ||
if (isFunctionKeys.has(key)) { | ||
return (value: any) => { | ||
schema[key] = value; | ||
return receiver; | ||
}; | ||
} | ||
|
||
if (key === 'nonRequired') return { ...schema }; | ||
if (key === 'required') return { ...schema, required: true }; | ||
schema[key] = true; | ||
return receiver; | ||
} | ||
}, | ||
}) as Builder; | ||
}; | ||
}; | ||
|
||
const isFunctionKeys = new Set([ | ||
'default', | ||
'enum', | ||
'index', | ||
'max', | ||
'maxlength', | ||
'min', | ||
'minlength' | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.