@@ -18,6 +18,7 @@ test('main test', () => {
1818 primary: Boolean
1919 type: String
2020 unique: Boolean
21+ generated: String
2122 ) on OBJECT | FIELD_DEFINITION
2223
2324 # See graphql-directive-private
@@ -117,8 +118,8 @@ test('error multiple primary index', () => {
117118 ${ sqlDirectiveDeclaration }
118119
119120 type User @sql(unicode: true) {
120- userId: String @sql(primary: true)
121- postId: String @sql(primary: true)
121+ userId: String @sql(type: "BINARY(16)", primary: true)
122+ postId: String @sql(type: "BINARY(16)", primary: true)
122123 }
123124 `
124125
@@ -132,3 +133,49 @@ test('error multiple primary index', () => {
132133 } )
133134 } ) . toThrow ( )
134135} )
136+
137+ test ( 'generated' , ( ) => {
138+ const typeDefs = gql `
139+ ${ sqlDirectiveDeclaration }
140+
141+ scalar JSON
142+
143+ type GeneratedTest {
144+ userId: String @sql(type: "BINARY(16)", primary: true)
145+ data: JSON @sql
146+ test1: String
147+ @sql(
148+ type: "VARCHAR(30)"
149+ generated: "GENERATED ALWAYS AS (data->>'$.test') VIRTUAL"
150+ )
151+ test2: String
152+ @sql(type: "VARCHAR(30)", generated: "ALWAYS AS (data->>'$.test')")
153+ test3: String @sql(type: "VARCHAR(30)", generated: "AS (data->>'$.test')")
154+ test4: String
155+ @sql(type: "VARCHAR(30)", generated: "(data->>'$.test')", index: true)
156+ }
157+ `
158+
159+ const outputFilepath = '__tests__/testOutput.sql'
160+ const directives = getSchemaDirectives ( )
161+ makeSqlSchema ( {
162+ typeDefs,
163+ schemaDirectives : directives ,
164+ outputFilepath,
165+ databaseName : 'dbname' ,
166+ tablePrefix : 'test_' ,
167+ } )
168+
169+ const testOutput = fs . readFileSync ( outputFilepath , { encoding : 'utf8' } )
170+ console . log ( 'testOutput' , testOutput )
171+ expect ( testOutput ) . toEqual ( `CREATE TABLE \`dbname\`.\`test_GeneratedTest\` (
172+ \`userId\` BINARY(16) NOT NULL,
173+ \`data\` JSON NOT NULL,
174+ \`test1\` VARCHAR(30) GENERATED ALWAYS AS (data->>'$.test') VIRTUAL NOT NULL,
175+ \`test2\` VARCHAR(30) GENERATED ALWAYS AS (data->>'$.test') NOT NULL,
176+ \`test3\` VARCHAR(30) AS (data->>'$.test') NOT NULL,
177+ \`test4\` VARCHAR(30) AS (data->>'$.test') NOT NULL,
178+ PRIMARY KEY (\`userId\`),
179+ INDEX \`TEST4INDEX\` (\`test4\` ASC)
180+ );` )
181+ } )
0 commit comments