Skip to content

Commit

Permalink
feat: mongoose schema generation supports index
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed Dec 11, 2018
1 parent b79d5fe commit 144e624
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ boolean[]:
+ `@deprecated`: set api as deprecated api
+ `@tags pet`: set api tags, can be seperated by `,`

## mongoose schema only

+ `@index`: set index
+ `@unique`: set unique index
+ `@sparse`: set sparse index

## number type alias

```ts
Expand Down
3 changes: 3 additions & 0 deletions demo/cases.gql
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ type MongooseScheme {
objectId: ObjectId!
date: Date!
decimal128: Decimal128!
index1: String!
index2: String!
index3: String!
}

type OuterType {
Expand Down
3 changes: 3 additions & 0 deletions demo/cases.ml
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ type mongooseScheme = {
objectId: objectId;
date: date;
decimal128: decimal128;
index1: string;
index2: string;
index3: string;
}

type outerType = {
Expand Down
15 changes: 15 additions & 0 deletions demo/cases.mongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,21 @@ export const mongooseSchemeSchema = {
type: Schema.Types.Decimal128,
required: true
},
index1: {
type: String,
required: true,
index: true
},
index2: {
type: String,
required: true,
unique: true
},
index3: {
type: String,
required: true,
sparse: true
},
}

export const outerTypeSchema = {
Expand Down
3 changes: 3 additions & 0 deletions demo/cases.proto
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ message MongooseScheme {
ObjectId objectId = 1;
Date date = 2;
Decimal128 decimal128 = 3;
string index1 = 4;
string index2 = 5;
string index3 = 6;
}

message OuterType {
Expand Down
3 changes: 3 additions & 0 deletions demo/cases.re
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ type mongooseScheme = {
objectId: objectId,
date: date,
decimal128: decimal128,
index1: string,
index2: string,
index3: string,
};

type outerType = {
Expand Down
3 changes: 3 additions & 0 deletions demo/cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ pub struct MongooseScheme {
#[serde(rename = "objectId")] pub object_id: ObjectId,
pub date: Date,
#[serde(rename = "decimal128")] pub decimal_128: Decimal128,
#[serde(rename = "index1")] pub index_1: String,
#[serde(rename = "index2")] pub index_2: String,
#[serde(rename = "index3")] pub index_3: String,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down
12 changes: 12 additions & 0 deletions demo/cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,18 @@ export interface MongooseScheme {
objectId: ObjectId
date: Date
decimal128: Decimal128
/**
* @index
*/
index1: string
/**
* @unique
*/
index2: string
/**
* @sparse
*/
index3: string
}

type Decimal128 = number
40 changes: 38 additions & 2 deletions demo/debug.json
Original file line number Diff line number Diff line change
Expand Up @@ -3301,10 +3301,46 @@
"character": 14
}
}
},
{
"name": "index1",
"type": {
"kind": "string",
"position": {
"file": "demo/cases.ts",
"line": 449,
"character": 10
}
},
"index": true
},
{
"name": "index2",
"type": {
"kind": "string",
"position": {
"file": "demo/cases.ts",
"line": 453,
"character": 10
}
},
"unique": true
},
{
"name": "index3",
"type": {
"kind": "string",
"position": {
"file": "demo/cases.ts",
"line": 457,
"character": 10
}
},
"sparse": true
}
],
"minProperties": 3,
"maxProperties": 3,
"minProperties": 6,
"maxProperties": 6,
"position": {
"file": "demo/cases.ts",
"line": 442,
Expand Down
6 changes: 6 additions & 0 deletions demo/root-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ export interface MongooseScheme<TContext = any> {
objectId: ObjectId<TContext>
date: Date<TContext>
decimal128: Decimal128<TContext>
index1: string
index2: string
index3: string
}

export interface OuterType<TContext = any> {
Expand Down Expand Up @@ -561,6 +564,9 @@ export interface ApolloResolvers<TContext = any> {
objectId?(parent: any, input: {}, context: TContext, info: GraphQLResolveInfo): any,
date?(parent: any, input: {}, context: TContext, info: GraphQLResolveInfo): any,
decimal128?(parent: any, input: {}, context: TContext, info: GraphQLResolveInfo): any,
index1?(parent: any, input: {}, context: TContext, info: GraphQLResolveInfo): any,
index2?(parent: any, input: {}, context: TContext, info: GraphQLResolveInfo): any,
index3?(parent: any, input: {}, context: TContext, info: GraphQLResolveInfo): any,
},
OuterType?: {
outerType?(parent: any, input: {}, context: TContext, info: GraphQLResolveInfo): any,
Expand Down
12 changes: 12 additions & 0 deletions online/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,18 @@ export interface MongooseScheme {
objectId: ObjectId
date: Date
decimal128: Decimal128
/**
* @index
*/
index1: string
/**
* @unique
*/
index2: string
/**
* @sparse
*/
index3: string
}
type Decimal128 = number
Expand Down
22 changes: 20 additions & 2 deletions src/mongoose-schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ ${members.join('\n')}
}

function generateMongooseSchemaOfObjectMember(member: Member) {
const type = generateType(member.type, 2, member.optional)
const type = generateType(member.type, 2, member.optional, member.index, member.unique, member.sparse)
return ` ${member.name}: ${type},`
}

function generateType(type: Type, indentationCount: number, optional?: boolean) {
// tslint:disable-next-line:cognitive-complexity
function generateType(
type: Type,
indentationCount: number,
optional?: boolean,
index?: boolean,
unique?: boolean,
sparse?: boolean
) {
const propertyType = getMongooseSchemaProperty(type)
const properties = [
`type: ${propertyType}`,
Expand Down Expand Up @@ -64,6 +72,16 @@ function generateType(type: Type, indentationCount: number, optional?: boolean)
}
}

if (index) {
properties.push(`index: true`)
}
if (unique) {
properties.push(`unique: true`)
}
if (sparse) {
properties.push(`sparse: true`)
}

const indentation = ' '.repeat(indentationCount)
return `{
${indentation}${properties.join(',\n' + indentation)}
Expand Down
6 changes: 6 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,12 @@ export class Parser {
this.setJsDocTag(propertyJsDoc, member)
} else if (propertyJsDoc.name === 'param') {
this.setJsDocParam(propertyJsDoc, member)
} else if (propertyJsDoc.name === 'index') {
member.index = true
} else if (propertyJsDoc.name === 'unique') {
member.unique = true
} else if (propertyJsDoc.name === 'sparse') {
member.sparse = true
} else {
this.setJsDoc(propertyJsDoc, member.type, sourceFile)
}
Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ export type Member = {
type: Type;
optional?: boolean;
tag?: number;
index?: boolean
unique?: boolean
sparse?: boolean
parameters?: Parameter[];
}

Expand Down

0 comments on commit 144e624

Please sign in to comment.