@zenstackhq/better-auth 3.6.3 → 3.7.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ import { DBAdapterDebugLogOption } from "@better-auth/core/db/adapter";
2
+
3
+ //#region src/config.d.ts
4
+ /**
5
+ * Options for the ZenStack adapter factory.
6
+ */
7
+ interface AdapterConfig {
8
+ /**
9
+ * Database provider
10
+ */
11
+ provider: 'sqlite' | 'postgresql';
12
+ /**
13
+ * Enable debug logs for the adapter
14
+ *
15
+ * @default false
16
+ */
17
+ debugLogs?: DBAdapterDebugLogOption | undefined;
18
+ /**
19
+ * Use plural table names
20
+ *
21
+ * @default false
22
+ */
23
+ usePlural?: boolean | undefined;
24
+ /**
25
+ * Preserve Better Auth array fields as native database arrays.
26
+ *
27
+ * Defaults to true for PostgreSQL and false for SQLite.
28
+ */
29
+ supportsArrays?: boolean | undefined;
30
+ }
31
+ //#endregion
32
+ export { AdapterConfig as t };
33
+ //# sourceMappingURL=config-2s6sqz6Y.d.cts.map
@@ -0,0 +1,33 @@
1
+ import { DBAdapterDebugLogOption } from "@better-auth/core/db/adapter";
2
+
3
+ //#region src/config.d.ts
4
+ /**
5
+ * Options for the ZenStack adapter factory.
6
+ */
7
+ interface AdapterConfig {
8
+ /**
9
+ * Database provider
10
+ */
11
+ provider: 'sqlite' | 'postgresql';
12
+ /**
13
+ * Enable debug logs for the adapter
14
+ *
15
+ * @default false
16
+ */
17
+ debugLogs?: DBAdapterDebugLogOption | undefined;
18
+ /**
19
+ * Use plural table names
20
+ *
21
+ * @default false
22
+ */
23
+ usePlural?: boolean | undefined;
24
+ /**
25
+ * Preserve Better Auth array fields as native database arrays.
26
+ *
27
+ * Defaults to true for PostgreSQL and false for SQLite.
28
+ */
29
+ supportsArrays?: boolean | undefined;
30
+ }
31
+ //#endregion
32
+ export { AdapterConfig as t };
33
+ //# sourceMappingURL=config-CjRnH6y3.d.mts.map
@@ -0,0 +1,13 @@
1
+ //#region src/config.ts
2
+ function getSupportsArrays(config) {
3
+ return config.supportsArrays ?? config.provider === "postgresql";
4
+ }
5
+ //#endregion
6
+ Object.defineProperty(exports, "getSupportsArrays", {
7
+ enumerable: true,
8
+ get: function() {
9
+ return getSupportsArrays;
10
+ }
11
+ });
12
+
13
+ //# sourceMappingURL=config-DELckboF.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config-DELckboF.cjs","names":[],"sources":["../src/config.ts"],"sourcesContent":["import type { DBAdapterDebugLogOption } from '@better-auth/core/db/adapter';\n\n/**\n * Options for the ZenStack adapter factory.\n */\nexport interface AdapterConfig {\n /**\n * Database provider\n */\n provider: 'sqlite' | 'postgresql';\n\n /**\n * Enable debug logs for the adapter\n *\n * @default false\n */\n debugLogs?: DBAdapterDebugLogOption | undefined;\n\n /**\n * Use plural table names\n *\n * @default false\n */\n usePlural?: boolean | undefined;\n\n /**\n * Preserve Better Auth array fields as native database arrays.\n *\n * Defaults to true for PostgreSQL and false for SQLite.\n */\n supportsArrays?: boolean | undefined;\n}\n\nexport function getSupportsArrays(config: AdapterConfig) {\n return config.supportsArrays ?? config.provider === 'postgresql';\n}\n"],"mappings":";AAiCA,SAAgB,kBAAkB,QAAuB;AACrD,QAAO,OAAO,kBAAkB,OAAO,aAAa"}
@@ -0,0 +1,8 @@
1
+ //#region src/config.ts
2
+ function getSupportsArrays(config) {
3
+ return config.supportsArrays ?? config.provider === "postgresql";
4
+ }
5
+ //#endregion
6
+ export { getSupportsArrays as t };
7
+
8
+ //# sourceMappingURL=config-PNXbQyyd.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config-PNXbQyyd.mjs","names":[],"sources":["../src/config.ts"],"sourcesContent":["import type { DBAdapterDebugLogOption } from '@better-auth/core/db/adapter';\n\n/**\n * Options for the ZenStack adapter factory.\n */\nexport interface AdapterConfig {\n /**\n * Database provider\n */\n provider: 'sqlite' | 'postgresql';\n\n /**\n * Enable debug logs for the adapter\n *\n * @default false\n */\n debugLogs?: DBAdapterDebugLogOption | undefined;\n\n /**\n * Use plural table names\n *\n * @default false\n */\n usePlural?: boolean | undefined;\n\n /**\n * Preserve Better Auth array fields as native database arrays.\n *\n * Defaults to true for PostgreSQL and false for SQLite.\n */\n supportsArrays?: boolean | undefined;\n}\n\nexport function getSupportsArrays(config: AdapterConfig) {\n return config.supportsArrays ?? config.provider === 'postgresql';\n}\n"],"mappings":";AAiCA,SAAgB,kBAAkB,QAAuB;AACrD,QAAO,OAAO,kBAAkB,OAAO,aAAa"}
package/dist/index.cjs CHANGED
@@ -1,4 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_config = require("./config-DELckboF.cjs");
2
3
  require("./schema-generator.cjs");
3
4
  let _better_auth_core_error = require("@better-auth/core/error");
4
5
  let better_auth_adapters = require("better-auth/adapters");
@@ -142,6 +143,7 @@ const zenstackAdapter = (db, config) => {
142
143
  adapterName: "ZenStack Adapter",
143
144
  usePlural: config.usePlural ?? false,
144
145
  debugLogs: config.debugLogs ?? false,
146
+ supportsArrays: require_config.getSupportsArrays(config),
145
147
  transaction: (cb) => db.$transaction((tx) => {
146
148
  return cb((0, better_auth_adapters.createAdapterFactory)({
147
149
  config: adapterOptions.config,
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["BetterAuthError"],"sources":["../src/adapter.ts"],"sourcesContent":["import type { BetterAuthOptions } from '@better-auth/core';\nimport type { DBAdapter, DBAdapterDebugLogOption, Where } from '@better-auth/core/db/adapter';\nimport { BetterAuthError } from '@better-auth/core/error';\nimport type { ClientContract, ModelOperations, UpdateInput } from '@zenstackhq/orm';\nimport type { GetModels, SchemaDef } from '@zenstackhq/orm/schema';\nimport {\n createAdapterFactory,\n type AdapterFactoryCustomizeAdapterCreator,\n type AdapterFactoryOptions,\n} from 'better-auth/adapters';\n\n/**\n * Options for the ZenStack adapter factory.\n */\nexport interface AdapterConfig {\n /**\n * Database provider\n */\n provider: 'sqlite' | 'postgresql';\n\n /**\n * Enable debug logs for the adapter\n *\n * @default false\n */\n debugLogs?: DBAdapterDebugLogOption | undefined;\n\n /**\n * Use plural table names\n *\n * @default false\n */\n usePlural?: boolean | undefined;\n}\n\n/**\n * Create a Better-Auth adapter for ZenStack ORM.\n * @param db ZenStack ORM client instance\n * @param config adapter configuration options\n */\nexport const zenstackAdapter = <Schema extends SchemaDef>(db: ClientContract<Schema>, config: AdapterConfig) => {\n let lazyOptions: BetterAuthOptions | null = null;\n const createCustomAdapter =\n (db: ClientContract<Schema>): AdapterFactoryCustomizeAdapterCreator =>\n ({ getFieldName, options }) => {\n const convertSelect = (select?: string[], model?: string) => {\n if (!select || !model) return undefined;\n return select.reduce((prev, cur) => {\n return {\n ...prev,\n [getFieldName({ model, field: cur })]: true,\n };\n }, {});\n };\n function operatorToORMOperator(operator: string) {\n switch (operator) {\n case 'starts_with':\n return 'startsWith';\n case 'ends_with':\n return 'endsWith';\n case 'ne':\n return 'not';\n case 'not_in':\n return 'notIn';\n default:\n return operator;\n }\n }\n const convertWhereClause = (model: string, where?: Where[]): any => {\n if (!where || !where.length) return {};\n if (where.length === 1) {\n const w = where[0]!;\n if (!w) {\n throw new BetterAuthError('Invalid where clause');\n }\n return {\n [getFieldName({ model, field: w.field })]:\n w.operator === 'eq' || !w.operator\n ? w.value\n : {\n [operatorToORMOperator(w.operator)]: w.value,\n },\n };\n }\n const and = where.filter((w) => w.connector === 'AND' || !w.connector);\n const or = where.filter((w) => w.connector === 'OR');\n const andClause = and.map((w) => {\n return {\n [getFieldName({ model, field: w.field })]:\n w.operator === 'eq' || !w.operator\n ? w.value\n : {\n [operatorToORMOperator(w.operator)]: w.value,\n },\n };\n });\n const orClause = or.map((w) => {\n return {\n [getFieldName({ model, field: w.field })]:\n w.operator === 'eq' || !w.operator\n ? w.value\n : {\n [operatorToORMOperator(w.operator)]: w.value,\n },\n };\n });\n\n return {\n ...(andClause.length ? { AND: andClause } : {}),\n ...(orClause.length ? { OR: orClause } : {}),\n };\n };\n\n function requireModelDb(db: ClientContract<Schema>, model: string) {\n const modelDb = db[model as keyof typeof db];\n if (!modelDb) {\n throw new BetterAuthError(\n `Model ${model} does not exist in the database. If you haven't generated the ZenStack schema, you need to run 'npx zen generate'`,\n );\n }\n return modelDb as unknown as ModelOperations<SchemaDef, GetModels<SchemaDef>>;\n }\n\n return {\n async create({ model, data: values, select }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n return await modelDb.create({\n data: values,\n select: convertSelect(select, model),\n });\n },\n\n async findOne({ model, where, select }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.findFirst({\n where: whereClause,\n select: convertSelect(select, model),\n });\n },\n\n async findMany({ model, where, limit, offset, sortBy }): Promise<any[]> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.findMany({\n where: whereClause,\n take: limit || 100,\n skip: offset || 0,\n ...(sortBy?.field\n ? {\n orderBy: {\n [getFieldName({ model, field: sortBy.field })]:\n sortBy.direction === 'desc' ? 'desc' : 'asc',\n } as any,\n }\n : {}),\n });\n },\n\n async count({ model, where }) {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.count({\n where: whereClause,\n });\n },\n\n async update({ model, where, update }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.update({\n where: whereClause,\n data: update as UpdateInput<SchemaDef, GetModels<SchemaDef>, any>,\n });\n },\n\n async updateMany({ model, where, update }) {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n const result = await modelDb.updateMany({\n where: whereClause,\n data: update,\n });\n return result ? (result.count as number) : 0;\n },\n\n async delete({ model, where }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n try {\n await modelDb.delete({\n where: whereClause,\n });\n } catch {\n // If the record doesn't exist, we don't want to throw an error\n }\n },\n\n async deleteMany({ model, where }) {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n const result = await modelDb.deleteMany({\n where: whereClause,\n });\n return result ? (result.count as number) : 0;\n },\n\n options: config,\n\n createSchema: async ({ file, tables }) => {\n const generateSchema = (await import('./schema-generator')).generateSchema;\n return generateSchema(file, tables, config, options);\n },\n };\n };\n\n const adapterOptions: AdapterFactoryOptions = {\n config: {\n adapterId: 'zenstack',\n adapterName: 'ZenStack Adapter',\n usePlural: config.usePlural ?? false,\n debugLogs: config.debugLogs ?? false,\n transaction: (cb) =>\n db.$transaction((tx) => {\n const adapter = createAdapterFactory({\n config: adapterOptions!.config,\n adapter: createCustomAdapter(tx as ClientContract<Schema>),\n })(lazyOptions!);\n return cb(adapter);\n }),\n },\n adapter: createCustomAdapter(db),\n };\n\n const adapter = createAdapterFactory(adapterOptions);\n return (options: BetterAuthOptions): DBAdapter<BetterAuthOptions> => {\n lazyOptions = options;\n return adapter(options);\n };\n};\n"],"mappings":";;;;;;;;;;AAwCA,MAAa,mBAA6C,IAA4B,WAA0B;CAC5G,IAAI,cAAwC;CAC5C,MAAM,uBACD,QACA,EAAE,cAAc,cAAc;EAC3B,MAAM,iBAAiB,QAAmB,UAAmB;AACzD,OAAI,CAAC,UAAU,CAAC,MAAO,QAAO,KAAA;AAC9B,UAAO,OAAO,QAAQ,MAAM,QAAQ;AAChC,WAAO;KACH,GAAG;MACF,aAAa;MAAE;MAAO,OAAO;MAAK,CAAC,GAAG;KAC1C;MACF,EAAE,CAAC;;EAEV,SAAS,sBAAsB,UAAkB;AAC7C,WAAQ,UAAR;IACI,KAAK,cACD,QAAO;IACX,KAAK,YACD,QAAO;IACX,KAAK,KACD,QAAO;IACX,KAAK,SACD,QAAO;IACX,QACI,QAAO;;;EAGnB,MAAM,sBAAsB,OAAe,UAAyB;AAChE,OAAI,CAAC,SAAS,CAAC,MAAM,OAAQ,QAAO,EAAE;AACtC,OAAI,MAAM,W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mB,OAAO,MAAM;AACpD,QAAI;AACA,WAAM,QAAQ,OAAO,EACjB,OAAO,aACV,CAAC;YACE;;GAKZ,MAAM,WAAW,EAAE,OAAO,SAAS;IAC/B,MAAM,UAAU,eAAe,IAAI,MAAM;IACzC,MAAM,cAAc,mBAAmB,OAAO,MAAM;IACpD,MAAM,SAAS,MAAM,QAAQ,WAAW,EACpC,OAAO,aACV,CAAC;AACF,WAAO,SAAU,OAAO,QAAmB;;GAG/C,SAAS;GAET,cAAc,OAAO,EAAE,MAAM,aAAa;IACtC,MAAM,kBAAkB,MAAA,QAAA,SAAA,CAAA,WAAA,QAAM,yBAAA,CAAA,EAA8B;AAC5D,WAAO,eAAe,MAAM,QAAQ,QAAQ,QAAQ;;GAE3D;;CAGT,MAAM,iBAAwC;EAC1C,QAAQ;GACJ,WAAW;GACX,aAAa;GACb,WAAW,OAAO,aAAa;GAC/B,WAAW,OAAO,aAAa;GAC/B,cAAc,OACV,GAAG,cAAc,OAAO;AAKpB,WAAO,IAAA,GAAA,qBAAA,sBAJ8B;KACjC,QAAQ,eAAgB;KACxB,SAAS,oBAAoB,GAA6B;KAC7D,CAAC,CAAC,YAAa,CACE;KACpB;GACT;EACD,SAAS,oBAAoB,GAAG;EACnC;CAED,MAAM,WAAA,GAAA,qBAAA,sBAA+B,eAAe;AACpD,SAAQ,YAA6D;AACjE,gBAAc;AACd,SAAO,QAAQ,QAAQ"}
1
+ {"version":3,"file":"index.cjs","names":["BetterAuthError","getSupportsArrays"],"sources":["../src/adapter.ts"],"sourcesContent":["import type { BetterAuthOptions } from '@better-auth/core';\nimport type { DBAdapter, Where } from '@better-auth/core/db/adapter';\nimport { BetterAuthError } from '@better-auth/core/error';\nimport type { ClientContract, ModelOperations, UpdateInput } from '@zenstackhq/orm';\nimport type { GetModels, SchemaDef } from '@zenstackhq/orm/schema';\nimport {\n createAdapterFactory,\n type AdapterFactoryCustomizeAdapterCreator,\n type AdapterFactoryOptions,\n} from 'better-auth/adapters';\nimport { getSupportsArrays, type AdapterConfig } from './config';\n\nexport type { AdapterConfig } from './config';\n\n/**\n * Create a Better-Auth adapter for ZenStack ORM.\n * @param db ZenStack ORM client instance\n * @param config adapter configuration options\n */\nexport const zenstackAdapter = <Schema extends SchemaDef>(db: ClientContract<Schema>, config: AdapterConfig) => {\n let lazyOptions: BetterAuthOptions | null = null;\n const createCustomAdapter =\n (db: ClientContract<Schema>): AdapterFactoryCustomizeAdapterCreator =>\n ({ getFieldName, options }) => {\n const convertSelect = (select?: string[], model?: string) => {\n if (!select || !model) return undefined;\n return select.reduce((prev, cur) => {\n return {\n ...prev,\n [getFieldName({ model, field: cur })]: true,\n };\n }, {});\n };\n function operatorToORMOperator(operator: string) {\n switch (operator) {\n case 'starts_with':\n return 'startsWith';\n case 'ends_with':\n return 'endsWith';\n case 'ne':\n return 'not';\n case 'not_in':\n return 'notIn';\n default:\n return operator;\n }\n }\n const convertWhereClause = (model: string, where?: Where[]): any => {\n if (!where || !where.length) return {};\n if (where.length === 1) {\n const w = where[0]!;\n if (!w) {\n throw new BetterAuthError('Invalid where clause');\n }\n return {\n [getFieldName({ model, field: w.field })]:\n w.operator === 'eq' || !w.operator\n ? w.value\n : {\n [operatorToORMOperator(w.operator)]: w.value,\n },\n };\n }\n const and = where.filter((w) => w.connector === 'AND' || !w.connector);\n const or = where.filter((w) => w.connector === 'OR');\n const andClause = and.map((w) => {\n return {\n [getFieldName({ model, field: w.field })]:\n w.operator === 'eq' || !w.operator\n ? w.value\n : {\n [operatorToORMOperator(w.operator)]: w.value,\n },\n };\n });\n const orClause = or.map((w) => {\n return {\n [getFieldName({ model, field: w.field })]:\n w.operator === 'eq' || !w.operator\n ? w.value\n : {\n [operatorToORMOperator(w.operator)]: w.value,\n },\n };\n });\n\n return {\n ...(andClause.length ? { AND: andClause } : {}),\n ...(orClause.length ? { OR: orClause } : {}),\n };\n };\n\n function requireModelDb(db: ClientContract<Schema>, model: string) {\n const modelDb = db[model as keyof typeof db];\n if (!modelDb) {\n throw new BetterAuthError(\n `Model ${model} does not exist in the database. If you haven't generated the ZenStack schema, you need to run 'npx zen generate'`,\n );\n }\n return modelDb as unknown as ModelOperations<SchemaDef, GetModels<SchemaDef>>;\n }\n\n return {\n async create({ model, data: values, select }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n return await modelDb.create({\n data: values,\n select: convertSelect(select, model),\n });\n },\n\n async findOne({ model, where, select }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.findFirst({\n where: whereClause,\n select: convertSelect(select, model),\n });\n },\n\n async findMany({ model, where, limit, offset, sortBy }): Promise<any[]> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.findMany({\n where: whereClause,\n take: limit || 100,\n skip: offset || 0,\n ...(sortBy?.field\n ? {\n orderBy: {\n [getFieldName({ model, field: sortBy.field })]:\n sortBy.direction === 'desc' ? 'desc' : 'asc',\n } as any,\n }\n : {}),\n });\n },\n\n async count({ model, where }) {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.count({\n where: whereClause,\n });\n },\n\n async update({ model, where, update }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.update({\n where: whereClause,\n data: update as UpdateInput<SchemaDef, GetModels<SchemaDef>, any>,\n });\n },\n\n async updateMany({ model, where, update }) {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n const result = await modelDb.updateMany({\n where: whereClause,\n data: update,\n });\n return result ? (result.count as number) : 0;\n },\n\n async delete({ model, where }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n try {\n await modelDb.delete({\n where: whereClause,\n });\n } catch {\n // If the record doesn't exist, we don't want to throw an error\n }\n },\n\n async deleteMany({ model, where }) {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n const result = await modelDb.deleteMany({\n where: whereClause,\n });\n return result ? (result.count as number) : 0;\n },\n\n options: config,\n\n createSchema: async ({ file, tables }) => {\n const generateSchema = (await import('./schema-generator')).generateSchema;\n return generateSchema(file, tables, config, options);\n },\n };\n };\n\n const adapterOptions: AdapterFactoryOptions = {\n config: {\n adapterId: 'zenstack',\n adapterName: 'ZenStack Adapter',\n usePlural: config.usePlural ?? false,\n debugLogs: config.debugLogs ?? false,\n supportsArrays: getSupportsArrays(config),\n transaction: (cb) =>\n db.$transaction((tx) => {\n const adapter = createAdapterFactory({\n config: adapterOptions!.config,\n adapter: createCustomAdapter(tx as ClientContract<Schema>),\n })(lazyOptions!);\n return cb(adapter);\n }),\n },\n adapter: createCustomAdapter(db),\n };\n\n const adapter = createAdapterFactory(adapterOptions);\n return (options: BetterAuthOptions): DBAdapter<BetterAuthOptions> => {\n lazyOptions = options;\n return adapter(options);\n };\n};\n"],"mappings":";;;;;;;;;;;AAmBA,MAAa,mBAA6C,IAA4B,WAA0B;CAC5G,IAAI,cAAwC;CAC5C,MAAM,uBACD,QACA,EAAE,cAAc,cAAc;EAC3B,MAAM,iBAAiB,QAAmB,UAAmB;AACzD,OAAI,CAAC,UAAU,CAAC,MAAO,QAAO,KAAA;AAC9B,UAAO,OAAO,QAAQ,MAAM,QAAQ;AAChC,WAAO;KACH,GAAG;MACF,aAAa;MAAE;MAAO,OAAO;MAAK,CAAC,GAAG;KAC1C;MACF,EAAE,CAAC;;EAEV,SAAS,sBAAsB,UAAkB;AAC7C,WAAQ,UAAR;IACI,KAAK,cACD,QAAO;IACX,KAAK,YACD,QAAO;IACX,KAAK,KACD,QAAO;IACX,KAAK,SACD,QAAO;IACX,QACI,QAAO;;;EAGnB,MAAM,sBAAsB,OAAe,UAAyB;AAChE,OAAI,CAAC,SAAS,CAAC,MAAM,OAAQ,QAAO,EAAE;AACtC,OAAI,MAAM,WAAW,GAAG;IACpB,MAAM,IAAI,MAAM;AAChB,QAAI,CAAC,EACD,OAAM,IAAIA,wBAAAA,gB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AKZ,MAAM,WAAW,EAAE,OAAO,SAAS;IAC/B,MAAM,UAAU,eAAe,IAAI,MAAM;IACzC,MAAM,cAAc,mBAAmB,OAAO,MAAM;IACpD,MAAM,SAAS,MAAM,QAAQ,WAAW,EACpC,OAAO,aACV,CAAC;AACF,WAAO,SAAU,OAAO,QAAmB;;GAG/C,SAAS;GAET,cAAc,OAAO,EAAE,MAAM,aAAa;IACtC,MAAM,kBAAkB,MAAA,QAAA,SAAA,CAAA,WAAA,QAAM,yBAAA,CAAA,EAA8B;AAC5D,WAAO,eAAe,MAAM,QAAQ,QAAQ,QAAQ;;GAE3D;;CAGT,MAAM,iBAAwC;EAC1C,QAAQ;GACJ,WAAW;GACX,aAAa;GACb,WAAW,OAAO,aAAa;GAC/B,WAAW,OAAO,aAAa;GAC/B,gBAAgBC,eAAAA,kBAAkB,OAAO;GACzC,cAAc,OACV,GAAG,cAAc,OAAO;AAKpB,WAAO,IAAA,GAAA,qBAAA,sBAJ8B;KACjC,QAAQ,eAAgB;KACxB,SAAS,oBAAoB,GAA6B;KAC7D,CAAC,CAAC,YAAa,CACE;KACpB;GACT;EACD,SAAS,oBAAoB,GAAG;EACnC;CAED,MAAM,WAAA,GAAA,qBAAA,sBAA+B,eAAe;AACpD,SAAQ,YAA6D;AACjE,gBAAc;AACd,SAAO,QAAQ,QAAQ"}
package/dist/index.d.cts CHANGED
@@ -1,2 +1,16 @@
1
- import { n as zenstackAdapter, t as AdapterConfig } from "./adapter-Hkvk-P1x.cjs";
2
- export { type AdapterConfig, zenstackAdapter };
1
+ import { t as AdapterConfig } from "./config-2s6sqz6Y.cjs";
2
+ import { BetterAuthOptions } from "@better-auth/core";
3
+ import { DBAdapter } from "@better-auth/core/db/adapter";
4
+ import { ClientContract } from "@zenstackhq/orm";
5
+ import { SchemaDef } from "@zenstackhq/orm/schema";
6
+
7
+ //#region src/adapter.d.ts
8
+ /**
9
+ * Create a Better-Auth adapter for ZenStack ORM.
10
+ * @param db ZenStack ORM client instance
11
+ * @param config adapter configuration options
12
+ */
13
+ declare const zenstackAdapter: <Schema extends SchemaDef>(db: ClientContract<Schema>, config: AdapterConfig) => (options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>;
14
+ //#endregion
15
+ export { type AdapterConfig, zenstackAdapter };
16
+ //# sourceMappingURL=index.d.cts.map
package/dist/index.d.mts CHANGED
@@ -1,2 +1,16 @@
1
- import { n as zenstackAdapter, t as AdapterConfig } from "./adapter-4J4FE5_e.mjs";
2
- export { type AdapterConfig, zenstackAdapter };
1
+ import { t as AdapterConfig } from "./config-CjRnH6y3.mjs";
2
+ import { BetterAuthOptions } from "@better-auth/core";
3
+ import { DBAdapter } from "@better-auth/core/db/adapter";
4
+ import { ClientContract } from "@zenstackhq/orm";
5
+ import { SchemaDef } from "@zenstackhq/orm/schema";
6
+
7
+ //#region src/adapter.d.ts
8
+ /**
9
+ * Create a Better-Auth adapter for ZenStack ORM.
10
+ * @param db ZenStack ORM client instance
11
+ * @param config adapter configuration options
12
+ */
13
+ declare const zenstackAdapter: <Schema extends SchemaDef>(db: ClientContract<Schema>, config: AdapterConfig) => (options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>;
14
+ //#endregion
15
+ export { type AdapterConfig, zenstackAdapter };
16
+ //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { t as getSupportsArrays } from "./config-PNXbQyyd.mjs";
1
2
  import { BetterAuthError } from "@better-auth/core/error";
2
3
  import { createAdapterFactory } from "better-auth/adapters";
3
4
  //#region src/adapter.ts
@@ -140,6 +141,7 @@ const zenstackAdapter = (db, config) => {
140
141
  adapterName: "ZenStack Adapter",
141
142
  usePlural: config.usePlural ?? false,
142
143
  debugLogs: config.debugLogs ?? false,
144
+ supportsArrays: getSupportsArrays(config),
143
145
  transaction: (cb) => db.$transaction((tx) => {
144
146
  return cb(createAdapterFactory({
145
147
  config: adapterOptions.config,
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/adapter.ts"],"sourcesContent":["import type { BetterAuthOptions } from '@better-auth/core';\nimport type { DBAdapter, DBAdapterDebugLogOption, Where } from '@better-auth/core/db/adapter';\nimport { BetterAuthError } from '@better-auth/core/error';\nimport type { ClientContract, ModelOperations, UpdateInput } from '@zenstackhq/orm';\nimport type { GetModels, SchemaDef } from '@zenstackhq/orm/schema';\nimport {\n createAdapterFactory,\n type AdapterFactoryCustomizeAdapterCreator,\n type AdapterFactoryOptions,\n} from 'better-auth/adapters';\n\n/**\n * Options for the ZenStack adapter factory.\n */\nexport interface AdapterConfig {\n /**\n * Database provider\n */\n provider: 'sqlite' | 'postgresql';\n\n /**\n * Enable debug logs for the adapter\n *\n * @default false\n */\n debugLogs?: DBAdapterDebugLogOption | undefined;\n\n /**\n * Use plural table names\n *\n * @default false\n */\n usePlural?: boolean | undefined;\n}\n\n/**\n * Create a Better-Auth adapter for ZenStack ORM.\n * @param db ZenStack ORM client instance\n * @param config adapter configuration options\n */\nexport const zenstackAdapter = <Schema extends SchemaDef>(db: ClientContract<Schema>, config: AdapterConfig) => {\n let lazyOptions: BetterAuthOptions | null = null;\n const createCustomAdapter =\n (db: ClientContract<Schema>): AdapterFactoryCustomizeAdapterCreator =>\n ({ getFieldName, options }) => {\n const convertSelect = (select?: string[], model?: string) => {\n if (!select || !model) return undefined;\n return select.reduce((prev, cur) => {\n return {\n ...prev,\n [getFieldName({ model, field: cur })]: true,\n };\n }, {});\n };\n function operatorToORMOperator(operator: string) {\n switch (operator) {\n case 'starts_with':\n return 'startsWith';\n case 'ends_with':\n return 'endsWith';\n case 'ne':\n return 'not';\n case 'not_in':\n return 'notIn';\n default:\n return operator;\n }\n }\n const convertWhereClause = (model: string, where?: Where[]): any => {\n if (!where || !where.length) return {};\n if (where.length === 1) {\n const w = where[0]!;\n if (!w) {\n throw new BetterAuthError('Invalid where clause');\n }\n return {\n [getFieldName({ model, field: w.field })]:\n w.operator === 'eq' || !w.operator\n ? w.value\n : {\n [operatorToORMOperator(w.operator)]: w.value,\n },\n };\n }\n const and = where.filter((w) => w.connector === 'AND' || !w.connector);\n const or = where.filter((w) => w.connector === 'OR');\n const andClause = and.map((w) => {\n return {\n [getFieldName({ model, field: w.field })]:\n w.operator === 'eq' || !w.operator\n ? w.value\n : {\n [operatorToORMOperator(w.operator)]: w.value,\n },\n };\n });\n const orClause = or.map((w) => {\n return {\n [getFieldName({ model, field: w.field })]:\n w.operator === 'eq' || !w.operator\n ? w.value\n : {\n [operatorToORMOperator(w.operator)]: w.value,\n },\n };\n });\n\n return {\n ...(andClause.length ? { AND: andClause } : {}),\n ...(orClause.length ? { OR: orClause } : {}),\n };\n };\n\n function requireModelDb(db: ClientContract<Schema>, model: string) {\n const modelDb = db[model as keyof typeof db];\n if (!modelDb) {\n throw new BetterAuthError(\n `Model ${model} does not exist in the database. If you haven't generated the ZenStack schema, you need to run 'npx zen generate'`,\n );\n }\n return modelDb as unknown as ModelOperations<SchemaDef, GetModels<SchemaDef>>;\n }\n\n return {\n async create({ model, data: values, select }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n return await modelDb.create({\n data: values,\n select: convertSelect(select, model),\n });\n },\n\n async findOne({ model, where, select }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.findFirst({\n where: whereClause,\n select: convertSelect(select, model),\n });\n },\n\n async findMany({ model, where, limit, offset, sortBy }): Promise<any[]> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.findMany({\n where: whereClause,\n take: limit || 100,\n skip: offset || 0,\n ...(sortBy?.field\n ? {\n orderBy: {\n [getFieldName({ model, field: sortBy.field })]:\n sortBy.direction === 'desc' ? 'desc' : 'asc',\n } as any,\n }\n : {}),\n });\n },\n\n async count({ model, where }) {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.count({\n where: whereClause,\n });\n },\n\n async update({ model, where, update }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.update({\n where: whereClause,\n data: update as UpdateInput<SchemaDef, GetModels<SchemaDef>, any>,\n });\n },\n\n async updateMany({ model, where, update }) {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n const result = await modelDb.updateMany({\n where: whereClause,\n data: update,\n });\n return result ? (result.count as number) : 0;\n },\n\n async delete({ model, where }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n try {\n await modelDb.delete({\n where: whereClause,\n });\n } catch {\n // If the record doesn't exist, we don't want to throw an error\n }\n },\n\n async deleteMany({ model, where }) {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n const result = await modelDb.deleteMany({\n where: whereClause,\n });\n return result ? (result.count as number) : 0;\n },\n\n options: config,\n\n createSchema: async ({ file, tables }) => {\n const generateSchema = (await import('./schema-generator')).generateSchema;\n return generateSchema(file, tables, config, options);\n },\n };\n };\n\n const adapterOptions: AdapterFactoryOptions = {\n config: {\n adapterId: 'zenstack',\n adapterName: 'ZenStack Adapter',\n usePlural: config.usePlural ?? false,\n debugLogs: config.debugLogs ?? false,\n transaction: (cb) =>\n db.$transaction((tx) => {\n const adapter = createAdapterFactory({\n config: adapterOptions!.config,\n adapter: createCustomAdapter(tx as ClientContract<Schema>),\n })(lazyOptions!);\n return cb(adapter);\n }),\n },\n adapter: createCustomAdapter(db),\n };\n\n const adapter = createAdapterFactory(adapterOptions);\n return (options: BetterAuthOptions): DBAdapter<BetterAuthOptions> => {\n lazyOptions = options;\n return adapter(options);\n };\n};\n"],"mappings":";;;;;;;;AAwCA,MAAa,mBAA6C,IAA4B,WAA0B;CAC5G,IAAI,cAAwC;CAC5C,MAAM,uBACD,QACA,EAAE,cAAc,cAAc;EAC3B,MAAM,iBAAiB,QAAmB,UAAmB;AACzD,OAAI,CAAC,UAAU,CAAC,MAAO,QAAO,KAAA;AAC9B,UAAO,OAAO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import type { BetterAuthOptions } from '@better-auth/core';\nimport type { DBAdapter, Where } from '@better-auth/core/db/adapter';\nimport { BetterAuthError } from '@better-auth/core/error';\nimport type { ClientContract, ModelOperations, UpdateInput } from '@zenstackhq/orm';\nimport type { GetModels, SchemaDef } from '@zenstackhq/orm/schema';\nimport {\n createAdapterFactory,\n type AdapterFactoryCustomizeAdapterCreator,\n type AdapterFactoryOptions,\n} from 'better-auth/adapters';\nimport { getSupportsArrays, type AdapterConfig } from './config';\n\nexport type { AdapterConfig } from './config';\n\n/**\n * Create a Better-Auth adapter for ZenStack ORM.\n * @param db ZenStack ORM client instance\n * @param config adapter configuration options\n */\nexport const zenstackAdapter = <Schema extends SchemaDef>(db: ClientContract<Schema>, config: AdapterConfig) => {\n let lazyOptions: BetterAuthOptions | null = null;\n const createCustomAdapter =\n (db: ClientContract<Schema>): AdapterFactoryCustomizeAdapterCreator =>\n ({ getFieldName, options }) => {\n const convertSelect = (select?: string[], model?: string) => {\n if (!select || !model) return undefined;\n return select.reduce((prev, cur) => {\n return {\n ...prev,\n [getFieldName({ model, field: cur })]: true,\n };\n }, {});\n };\n function operatorToORMOperator(operator: string) {\n switch (operator) {\n case 'starts_with':\n return 'startsWith';\n case 'ends_with':\n return 'endsWith';\n case 'ne':\n return 'not';\n case 'not_in':\n return 'notIn';\n default:\n return operator;\n }\n }\n const convertWhereClause = (model: string, where?: Where[]): any => {\n if (!where || !where.length) return {};\n if (where.length === 1) {\n const w = where[0]!;\n if (!w) {\n throw new BetterAuthError('Invalid where clause');\n }\n return {\n [getFieldName({ model, field: w.field })]:\n w.operator === 'eq' || !w.operator\n ? w.value\n : {\n [operatorToORMOperator(w.operator)]: w.value,\n },\n };\n }\n const and = where.filter((w) => w.connector === 'AND' || !w.connector);\n const or = where.filter((w) => w.connector === 'OR');\n const andClause = and.map((w) => {\n return {\n [getFieldName({ model, field: w.field })]:\n w.operator === 'eq' || !w.operator\n ? w.value\n : {\n [operatorToORMOperator(w.operator)]: w.value,\n },\n };\n });\n const orClause = or.map((w) => {\n return {\n [getFieldName({ model, field: w.field })]:\n w.operator === 'eq' || !w.operator\n ? w.value\n : {\n [operatorToORMOperator(w.operator)]: w.value,\n },\n };\n });\n\n return {\n ...(andClause.length ? { AND: andClause } : {}),\n ...(orClause.length ? { OR: orClause } : {}),\n };\n };\n\n function requireModelDb(db: ClientContract<Schema>, model: string) {\n const modelDb = db[model as keyof typeof db];\n if (!modelDb) {\n throw new BetterAuthError(\n `Model ${model} does not exist in the database. If you haven't generated the ZenStack schema, you need to run 'npx zen generate'`,\n );\n }\n return modelDb as unknown as ModelOperations<SchemaDef, GetModels<SchemaDef>>;\n }\n\n return {\n async create({ model, data: values, select }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n return await modelDb.create({\n data: values,\n select: convertSelect(select, model),\n });\n },\n\n async findOne({ model, where, select }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.findFirst({\n where: whereClause,\n select: convertSelect(select, model),\n });\n },\n\n async findMany({ model, where, limit, offset, sortBy }): Promise<any[]> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.findMany({\n where: whereClause,\n take: limit || 100,\n skip: offset || 0,\n ...(sortBy?.field\n ? {\n orderBy: {\n [getFieldName({ model, field: sortBy.field })]:\n sortBy.direction === 'desc' ? 'desc' : 'asc',\n } as any,\n }\n : {}),\n });\n },\n\n async count({ model, where }) {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.count({\n where: whereClause,\n });\n },\n\n async update({ model, where, update }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n return await modelDb.update({\n where: whereClause,\n data: update as UpdateInput<SchemaDef, GetModels<SchemaDef>, any>,\n });\n },\n\n async updateMany({ model, where, update }) {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n const result = await modelDb.updateMany({\n where: whereClause,\n data: update,\n });\n return result ? (result.count as number) : 0;\n },\n\n async delete({ model, where }): Promise<any> {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n try {\n await modelDb.delete({\n where: whereClause,\n });\n } catch {\n // If the record doesn't exist, we don't want to throw an error\n }\n },\n\n async deleteMany({ model, where }) {\n const modelDb = requireModelDb(db, model);\n const whereClause = convertWhereClause(model, where);\n const result = await modelDb.deleteMany({\n where: whereClause,\n });\n return result ? (result.count as number) : 0;\n },\n\n options: config,\n\n createSchema: async ({ file, tables }) => {\n const generateSchema = (await import('./schema-generator')).generateSchema;\n return generateSchema(file, tables, config, options);\n },\n };\n };\n\n const adapterOptions: AdapterFactoryOptions = {\n config: {\n adapterId: 'zenstack',\n adapterName: 'ZenStack Adapter',\n usePlural: config.usePlural ?? false,\n debugLogs: config.debugLogs ?? false,\n supportsArrays: getSupportsArrays(config),\n transaction: (cb) =>\n db.$transaction((tx) => {\n const adapter = createAdapterFactory({\n config: adapterOptions!.config,\n adapter: createCustomAdapter(tx as ClientContract<Schema>),\n })(lazyOptions!);\n return cb(adapter);\n }),\n },\n adapter: createCustomAdapter(db),\n };\n\n const adapter = createAdapterFactory(adapterOptions);\n return (options: BetterAuthOptions): DBAdapter<BetterAuthOptions> => {\n lazyOptions = options;\n return adapter(options);\n };\n};\n"],"mappings":";;;;;;;;;AAmBA,MAAa,mBAA6C,IAA4B,WAA0B;CAC5G,IAAI,cAAwC;CAC5C,MAAM,uBACD,QACA,EAAE,cAAc,cAAc;EAC3B,MAAM,iBAAiB,QAAmB,UAAmB;AACzD,OAAI,CAAC,UAAU,CAAC,MAAO,QAAO,KAAA;AAC9B,UAAO,OAAO,QAAQ,MAAM,QAAQ;AAChC,WAAO;KACH,GAAG;MACF,aAAa;MAAE;MAAO,OAAO;MAAK,CAAC,GAAG;KAC1C;MACF,EAAE,CAAC;;EAEV,SAAS,sBAAsB,UAAkB;AAC7C,WAAQ,UAAR;IACI,KAAK,cACD,QAAO;I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Q,WAAW;KACpC,OAAO;KACP,MAAM;KACT,CAAC;AACF,WAAO,SAAU,OAAO,QAAmB;;GAG/C,MAAM,OAAO,EAAE,OAAO,SAAuB;IACzC,MAAM,UAAU,eAAe,IAAI,MAAM;IACzC,MAAM,cAAc,mBAAmB,OAAO,MAAM;AACpD,QAAI;AACA,WAAM,QAAQ,OAAO,EACjB,OAAO,aACV,CAAC;YACE;;GAKZ,MAAM,WAAW,EAAE,OAAO,SAAS;IAC/B,MAAM,UAAU,eAAe,IAAI,MAAM;IACzC,MAAM,cAAc,mBAAmB,OAAO,MAAM;IACpD,MAAM,SAAS,MAAM,QAAQ,WAAW,EACpC,OAAO,aACV,CAAC;AACF,WAAO,SAAU,OAAO,QAAmB;;GAG/C,SAAS;GAET,cAAc,OAAO,EAAE,MAAM,aAAa;IACtC,MAAM,kBAAkB,MAAM,OAAO,2BAAuB;AAC5D,WAAO,eAAe,MAAM,QAAQ,QAAQ,QAAQ;;GAE3D;;CAGT,MAAM,iBAAwC;EAC1C,QAAQ;GACJ,WAAW;GACX,aAAa;GACb,WAAW,OAAO,aAAa;GAC/B,WAAW,OAAO,aAAa;GAC/B,gBAAgB,kBAAkB,OAAO;GACzC,cAAc,OACV,GAAG,cAAc,OAAO;AAKpB,WAAO,GAJS,qBAAqB;KACjC,QAAQ,eAAgB;KACxB,SAAS,oBAAoB,GAA6B;KAC7D,CAAC,CAAC,YAAa,CACE;KACpB;GACT;EACD,SAAS,oBAAoB,GAAG;EACnC;CAED,MAAM,UAAU,qBAAqB,eAAe;AACpD,SAAQ,YAA6D;AACjE,gBAAc;AACd,SAAO,QAAQ,QAAQ"}
@@ -21,6 +21,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
21
21
  enumerable: true
22
22
  }) : target, mod));
23
23
  //#endregion
24
+ const require_config = require("./config-DELckboF.cjs");
24
25
  let _zenstackhq_common_helpers = require("@zenstackhq/common-helpers");
25
26
  let _zenstackhq_language = require("@zenstackhq/language");
26
27
  let _zenstackhq_language_ast = require("@zenstackhq/language/ast");
@@ -64,8 +65,9 @@ async function updateSchema(schemaPath, tables, config, options) {
64
65
  }
65
66
  }
66
67
  let changed = false;
68
+ const supportsArrays = require_config.getSupportsArrays(config);
67
69
  for (const [name, table] of Object.entries(tables)) {
68
- const c = addOrUpdateModel(name, table, zmodel, tables, toManyRelations, !!options.advanced?.database?.useNumberId);
70
+ const c = addOrUpdateModel(name, table, zmodel, tables, toManyRelations, supportsArrays, !!options.advanced?.database?.useNumberId);
69
71
  changed = changed || c;
70
72
  }
71
73
  if (!changed) return;
@@ -170,20 +172,20 @@ function initializeZmodel(config) {
170
172
  ds.fields.push(urlField);
171
173
  return zmodel;
172
174
  }
173
- function getMappedFieldType({ bigint, type }) {
174
- return (0, ts_pattern.match)(type).with("string", () => ({ type: "String" })).with("number", () => bigint ? { type: "BigInt" } : { type: "Int" }).with("boolean", () => ({ type: "Boolean" })).with("date", () => ({ type: "DateTime" })).with("json", () => ({ type: "Json" })).with("string[]", () => ({
175
+ function getMappedFieldType({ bigint, type }, supportsArrays) {
176
+ return (0, ts_pattern.match)(type).with("string", () => ({ type: "String" })).with("number", () => bigint ? { type: "BigInt" } : { type: "Int" }).with("boolean", () => ({ type: "Boolean" })).with("date", () => ({ type: "DateTime" })).with("json", () => ({ type: "Json" })).with("string[]", () => supportsArrays ? {
175
177
  type: "String",
176
178
  array: true
177
- })).with("number[]", () => ({
179
+ } : { type: "Json" }).with("number[]", () => supportsArrays ? {
178
180
  type: "Int",
179
181
  array: true
180
- })).when((v) => Array.isArray(v) && v.every((e) => typeof e === "string"), () => {
182
+ } : { type: "Json" }).when((v) => Array.isArray(v) && v.every((e) => typeof e === "string"), () => {
181
183
  return { type: "String" };
182
184
  }).otherwise(() => {
183
185
  throw new Error(`Unsupported field type: ${type}`);
184
186
  });
185
187
  }
186
- function addOrUpdateModel(tableName, table, zmodel, tables, toManyRelations, numericId) {
188
+ function addOrUpdateModel(tableName, table, zmodel, tables, toManyRelations, supportsArrays, numericId) {
187
189
  let changed = false;
188
190
  const modelName = (0, _zenstackhq_common_helpers.upperCaseFirst)(tables[tableName]?.modelName ?? tableName);
189
191
  let dataModel = zmodel.declarations.find((d) => (0, _zenstackhq_language_ast.isDataModel)(d) && d.name === modelName);
@@ -198,7 +200,7 @@ function addOrUpdateModel(tableName, table, zmodel, tables, toManyRelations, num
198
200
  if (dataModel.fields.some((f) => f.name === fieldName)) continue;
199
201
  changed = true;
200
202
  if (!field.references) {
201
- const { array, type } = getMappedFieldType(field);
203
+ const { array, type } = getMappedFieldType(field, supportsArrays);
202
204
  const df = {
203
205
  $type: "DataField",
204
206
  name: fieldName,
@@ -1 +1 @@
1
- {"version":3,"file":"schema-generator.cjs","names":["fs","ZModelCodeGenerator"],"sources":["../src/schema-generator.ts"],"sourcesContent":["import { lowerCaseFirst, upperCaseFirst } from '@zenstackhq/common-helpers';\nimport { formatDocument, loadDocument, ZModelCodeGenerator } from '@zenstackhq/language';\nimport {\n Argument,\n ArrayExpr,\n AttributeArg,\n BooleanLiteral,\n ConfigExpr,\n ConfigField,\n DataField,\n DataFieldAttribute,\n DataFieldType,\n DataModel,\n DataModelAttribute,\n DataSource,\n InvocationExpr,\n isDataModel,\n Model,\n NumberLiteral,\n ReferenceExpr,\n StringLiteral,\n} from '@zenstackhq/language/ast';\nimport { hasAttribute } from '@zenstackhq/language/utils';\nimport { type BetterAuthOptions } from 'better-auth';\nimport type { DBAdapterSchemaCreation } from 'better-auth/adapters';\nimport type { BetterAuthDBSchema, DBFieldAttribute, DBFieldType } from 'better-auth/db';\nimport fs from 'node:fs';\nimport { match } from 'ts-pattern';\nimport type { AdapterConfig } from './adapter';\n\nexport async function generateSchema(\n file: string | undefined,\n tables: BetterAuthDBSchema,\n config: AdapterConfig,\n options: BetterAuthOptions,\n): Promise<DBAdapterSchemaCreation> {\n let filePath = file;\n\n if (!filePath) {\n // TODO: respect \"zenstack\" entry in package.json for default schema file path\n if (fs.existsSync('./schema.zmodel')) {\n filePath = './schema.zmodel';\n } else {\n filePath = './zenstack/schema.zmodel';\n }\n }\n\n const schemaExists = fs.existsSync(filePath);\n\n const schema = await updateSchema(filePath, tables, config, options);\n\n return {\n code: schema ?? '',\n path: filePath,\n overwrite: schemaExists && !!schema,\n };\n}\n\nasync function updateSchema(\n schemaPath: string,\n tables: BetterAuthDBSchema,\n config: AdapterConfig,\n options: BetterAuthOptions,\n) {\n let zmodel: Model | undefined;\n if (fs.existsSync(schemaPath)) {\n const loadResult = await loadDocument(schemaPath);\n if (!loadResult.success) {\n throw new Error(`Failed to load existing schema at ${schemaPath}: ${loadResult.errors.join(', ')}`);\n }\n zmodel = loadResult.model;\n } else {\n zmodel = initializeZmodel(config);\n }\n\n // collect to-many relations\n const toManyRelations = new Map();\n for (const [tableName, table] of Object.entries(tables)) {\n const fields = tables[tableName]?.fields;\n for (const field in fields) {\n const attr = fields[field]!;\n if (attr.references) {\n const referencedOriginalModel = attr.references.model;\n const referencedCustomModel = tables[referencedOriginalModel]?.modelName || referencedOriginalModel;\n const referencedModelNameCap = upperCaseFirst(referencedCustomModel);\n if (!toManyRelations.has(referencedModelNameCap)) {\n toManyRelations.set(referencedModelNameCap, new Set());\n }\n const currentCustomModel = table.modelName ?? tableName;\n const currentModelNameCap = upperCaseFirst(currentCustomModel);\n toManyRelations.get(referencedModelNameCap).add(currentModelNameCap);\n }\n }\n }\n\n let changed = false;\n\n for (const [name, table] of Object.entries(tables)) {\n const c = addOrUpdateModel(\n name,\n table,\n zmodel,\n tables,\n toManyRelations,\n !!options.advanced?.database?.useNumberId,\n );\n changed = changed || c;\n }\n\n if (!changed) {\n return undefined;\n }\n\n const generator = new ZModelCodeGenerator();\n let content = generator.generate(zmodel);\n\n try {\n content = await formatDocument(content);\n } catch {\n // ignore formatting errors\n }\n\n return content;\n}\n\n// @default(now())\nfunction addDefaultNow(df: DataField) {\n const nowArg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const nowExpr: InvocationExpr = {\n $type: 'InvocationExpr',\n function: { $refText: 'now' },\n args: [],\n $container: nowArg,\n };\n nowArg.value = nowExpr;\n addFieldAttribute(df, '@default', [nowArg]);\n}\n\nfunction createDataModel(modelName: string, zmodel: Model, numericId: boolean) {\n const dataModel: DataModel = {\n $type: 'DataModel',\n name: modelName,\n fields: [],\n attributes: [],\n mixins: [],\n comments: [],\n isView: false,\n $container: zmodel,\n };\n\n let idField: DataField;\n if (numericId) {\n idField = addModelField(dataModel, 'id', 'Int', false, false);\n } else {\n idField = addModelField(dataModel, 'id', 'String', false, false);\n }\n addFieldAttribute(idField, '@id');\n\n return dataModel;\n}\n\nfunction addModelField(dataModel: DataModel, fieldName: string, fieldType: string, array: boolean, optional: boolean) {\n const field: DataField = {\n $type: 'DataField',\n name: fieldName,\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n field.type = {\n $type: 'DataFieldType',\n type: fieldType as any,\n array,\n optional,\n $container: field,\n };\n dataModel.fields.push(field);\n return field;\n}\n\nfunction initializeZmodel(config: AdapterConfig) {\n const zmodel: Model = {\n $type: 'Model',\n declarations: [],\n imports: [],\n };\n\n // datasource db { ... }\n const ds: DataSource = {\n $type: 'DataSource',\n name: 'db',\n fields: [],\n $container: zmodel,\n };\n zmodel.declarations.push(ds);\n\n // provider = 'sqlite' | 'postgresql'\n const providerField: ConfigField = {\n $type: 'ConfigField',\n name: 'provider',\n $container: ds,\n } as any;\n providerField.value = {\n $type: 'StringLiteral',\n value: config.provider,\n $container: providerField,\n } satisfies ConfigExpr;\n\n const urlField: ConfigField = {\n $type: 'ConfigField',\n name: 'url',\n $container: ds,\n } as any;\n\n // env('DATABASE_URL')\n const envCall: InvocationExpr = {\n $type: 'InvocationExpr',\n function: {\n $refText: 'env',\n },\n args: [],\n $container: urlField,\n };\n\n // 'DATABASE_URL' arg\n const dbUrlArg: Argument = {\n $type: 'Argument',\n } as any;\n dbUrlArg.value = {\n $type: 'StringLiteral',\n value: 'DATABASE_URL',\n $container: dbUrlArg,\n } satisfies ConfigExpr;\n\n envCall.args = [dbUrlArg];\n\n urlField.value =\n config.provider === 'sqlite'\n ? {\n $type: 'StringLiteral',\n value: 'file:./dev.db',\n $container: urlField,\n }\n : envCall;\n\n ds.fields.push(providerField);\n ds.fields.push(urlField);\n\n return zmodel;\n}\n\nfunction getMappedFieldType({ bigint, type }: DBFieldAttribute) {\n return match<DBFieldType, { type: string; array?: boolean }>(type)\n .with('string', () => ({ type: 'String' }))\n .with('number', () => (bigint ? { type: 'BigInt' } : { type: 'Int' }))\n .with('boolean', () => ({ type: 'Boolean' }))\n .with('date', () => ({ type: 'DateTime' }))\n .with('json', () => ({ type: 'Json' }))\n .with('string[]', () => ({ type: 'String', array: true }))\n .with('number[]', () => ({ type: 'Int', array: true }))\n .when(\n (v) => Array.isArray(v) && v.every((e) => typeof e === 'string'),\n () => {\n // Handle enum types (e.g., ['user', 'admin']), map them to String type for now\n return { type: 'String' };\n },\n )\n .otherwise(() => {\n throw new Error(`Unsupported field type: ${type}`);\n });\n}\n\nfunction addOrUpdateModel(\n tableName: string,\n table: BetterAuthDBSchema[string],\n zmodel: Model,\n tables: BetterAuthDBSchema,\n toManyRelations: Map<string, Set<string>>,\n numericId: boolean,\n): boolean {\n let changed = false;\n const customModelName = tables[tableName]?.modelName ?? tableName;\n const modelName = upperCaseFirst(customModelName);\n\n let dataModel = zmodel.declarations.find((d): d is DataModel => isDataModel(d) && d.name === modelName);\n if (!dataModel) {\n changed = true;\n dataModel = createDataModel(modelName, zmodel, numericId);\n zmodel.declarations.push(dataModel);\n }\n\n if (modelName !== tableName && !hasAttribute(dataModel, '@@map')) {\n addModelAttribute(dataModel, '@@map', [createStringAttributeArg(tableName)]);\n }\n\n for (const [fName, field] of Object.entries(table.fields)) {\n const fieldName = field.fieldName ?? fName;\n if (dataModel.fields.some((f) => f.name === fieldName)) {\n continue;\n }\n\n changed = true;\n\n if (!field.references) {\n // scalar field\n const { array, type } = getMappedFieldType(field);\n\n const df: DataField = {\n $type: 'DataField',\n name: fieldName,\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n df.type = {\n $type: 'DataFieldType',\n type: type as any,\n array: !!array,\n optional: !field.required,\n $container: df,\n };\n dataModel.fields.push(df);\n\n // @id\n if (fieldName === 'id') {\n addFieldAttribute(df, '@id');\n }\n\n // @unique\n if (field.unique) {\n addFieldAttribute(df, '@unique');\n }\n\n // @default\n if (field.defaultValue !== undefined) {\n if (fieldName === 'createdAt') {\n // @default(now())\n addDefaultNow(df);\n } else if (typeof field.defaultValue === 'boolean') {\n addFieldAttribute(df, '@default', [createBooleanAttributeArg(field.defaultValue)]);\n } else if (typeof field.defaultValue === 'string') {\n addFieldAttribute(df, '@default', [createStringAttributeArg(field.defaultValue)]);\n } else if (typeof field.defaultValue === 'number') {\n addFieldAttribute(df, '@default', [createNumberAttributeArg(field.defaultValue)]);\n } else if (typeof field.defaultValue === 'function') {\n // For other function-based defaults, we'll need to check what they return\n const defaultVal = field.defaultValue();\n if (defaultVal instanceof Date) {\n // @default(now())\n addDefaultNow(df);\n } else {\n console.warn(\n `Warning: Unsupported default function for field ${fieldName} in model ${table.modelName}. Please adjust manually.`,\n );\n }\n }\n }\n\n // This is a special handling for updatedAt fields\n if (fieldName === 'updatedAt' && field.onUpdate) {\n addFieldAttribute(df, '@updatedAt');\n } else if (field.onUpdate) {\n console.warn(\n `Warning: 'onUpdate' is only supported on 'updatedAt' fields. Please adjust manually for field ${fieldName} in model ${table.modelName}.`,\n );\n }\n } else {\n // relation\n\n // fk field\n addModelField(dataModel, fieldName, numericId ? 'Int' : 'String', false, !field.required);\n\n // relation field\n const referencedOriginalModelName = field.references.model;\n const referencedCustomModelName =\n tables[referencedOriginalModelName]?.modelName || referencedOriginalModelName;\n\n const relationField: DataField = {\n $type: 'DataField',\n name: lowerCaseFirst(referencedCustomModelName),\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n relationField.type = {\n $type: 'DataFieldType',\n reference: {\n $refText: upperCaseFirst(referencedCustomModelName),\n },\n array: (field.type as string).endsWith('[]'),\n optional: !field.required,\n $container: relationField,\n } satisfies DataFieldType;\n\n let action = 'Cascade';\n if (field.references.onDelete === 'no action') action = 'NoAction';\n else if (field.references.onDelete === 'set null') action = 'SetNull';\n else if (field.references.onDelete === 'set default') action = 'SetDefault';\n else if (field.references.onDelete === 'restrict') action = 'Restrict';\n\n // @relation(fields: [field], references: [referencedField], onDelete: action)\n const relationAttr: DataFieldAttribute = {\n $type: 'DataFieldAttribute',\n decl: {\n $refText: '@relation',\n },\n args: [],\n $container: relationField,\n };\n\n // fields: [field]\n const fieldsArg: AttributeArg = {\n $type: 'AttributeArg',\n name: 'fields',\n $container: relationAttr,\n } as any;\n const fieldsExpr: ArrayExpr = {\n $type: 'ArrayExpr',\n items: [],\n $container: fieldsArg,\n };\n const fkRefExpr: ReferenceExpr = {\n $type: 'ReferenceExpr',\n args: [],\n $container: fieldsExpr,\n target: {\n $refText: fieldName,\n },\n };\n fieldsExpr.items.push(fkRefExpr);\n fieldsArg.value = fieldsExpr;\n\n // references: [referencedField]\n const referencesArg: AttributeArg = {\n $type: 'AttributeArg',\n name: 'references',\n $container: relationAttr,\n } as any;\n const referencesExpr: ArrayExpr = {\n $type: 'ArrayExpr',\n items: [],\n $container: referencesArg,\n };\n const pkRefExpr: ReferenceExpr = {\n $type: 'ReferenceExpr',\n args: [],\n $container: referencesExpr,\n target: {\n $refText: field.references.field,\n },\n };\n referencesExpr.items.push(pkRefExpr);\n referencesArg.value = referencesExpr;\n\n // onDelete: action\n const onDeleteArg: AttributeArg = {\n $type: 'AttributeArg',\n name: 'onDelete',\n $container: relationAttr,\n } as any;\n const onDeleteValueExpr: ReferenceExpr = {\n $type: 'ReferenceExpr',\n target: { $refText: action },\n args: [],\n $container: onDeleteArg,\n };\n onDeleteArg.value = onDeleteValueExpr;\n\n relationAttr.args.push(...[fieldsArg, referencesArg, onDeleteArg]);\n relationField.attributes.push(relationAttr);\n\n dataModel.fields.push(relationField);\n }\n }\n\n // add to-many relations\n if (toManyRelations.has(modelName)) {\n const relations = toManyRelations.get(modelName)!;\n for (const relatedModel of relations) {\n const relationName = `${lowerCaseFirst(relatedModel)}s`;\n if (!dataModel.fields.some((f) => f.name === relationName)) {\n const relationField: DataField = {\n $type: 'DataField',\n name: relationName,\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n const relationType: DataFieldType = {\n $type: 'DataFieldType',\n reference: {\n $refText: relatedModel,\n },\n array: true,\n optional: false,\n $container: relationField,\n };\n relationField.type = relationType;\n dataModel.fields.push(relationField);\n }\n }\n }\n\n return changed;\n}\n\nfunction addModelAttribute(dataModel: DataModel, name: string, args: Omit<AttributeArg, '$container'>[] = []) {\n const attr: DataModelAttribute = {\n $type: 'DataModelAttribute',\n decl: { $refText: name },\n $container: dataModel,\n args: [],\n };\n const finalArgs = args.map((arg) => ({\n ...arg,\n $container: attr,\n }));\n attr.args.push(...finalArgs);\n dataModel.attributes.push(attr);\n}\n\nfunction addFieldAttribute(dataField: DataField, name: string, args: Omit<AttributeArg, '$container'>[] = []) {\n const attr: DataFieldAttribute = {\n $type: 'DataFieldAttribute',\n decl: { $refText: name },\n $container: dataField,\n args: [],\n };\n const finalArgs = args.map((arg) => ({\n ...arg,\n $container: attr,\n }));\n attr.args.push(...finalArgs);\n dataField.attributes.push(attr);\n}\n\nfunction createBooleanAttributeArg(value: boolean) {\n const arg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const expr: BooleanLiteral = {\n $type: 'BooleanLiteral',\n value,\n $container: arg,\n };\n arg.value = expr;\n return arg;\n}\n\nfunction createNumberAttributeArg(value: number) {\n const arg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const expr: NumberLiteral = {\n $type: 'NumberLiteral',\n value: value.toString(),\n $container: arg,\n };\n arg.value = expr;\n return arg;\n}\n\nfunction createStringAttributeArg(value: string) {\n const arg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const expr: StringLiteral = {\n $type: 'StringLiteral',\n value,\n $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EAAE;CAC1G,MAAM,OAA2B;EAC7B,OAAO;EACP,MAAM,EAAE,UAAU,MAAM;EACxB,YAAY;EACZ,MAAM,EAAE;EACX;CACD,MAAM,YAAY,KAAK,KAAK,SAAS;EACjC,GAAG;EACH,YAAY;EACf,EAAE;AACH,MAAK,KAAK,KAAK,GAAG,UAAU;AAC5B,WAAU,WAAW,KAAK,KAAK;;AAGnC,SAAS,kBAAkB,WAAsB,MAAc,OAA2C,EAAE,EAAE;CAC1G,MAAM,OAA2B;EAC7B,OAAO;EACP,MAAM,EAAE,UAAU,MAAM;EACxB,YAAY;EACZ,MAAM,EAAE;EACX;CACD,MAAM,YAAY,KAAK,KAAK,SAAS;EACjC,GAAG;EACH,YAAY;EACf,EAAE;AACH,MAAK,KAAK,KAAK,GAAG,UAAU;AAC5B,WAAU,WAAW,KAAK,KAAK;;AAGnC,SAAS,0BAA0B,OAAgB;CAC/C,MAAM,MAAoB,EACtB,OAAO,gBACV;AAMD,KAAI,QALyB;EACzB,OAAO;EACP;EACA,YAAY;EACf;AAED,QAAO;;AAGX,SAAS,yBAAyB,OAAe;CAC7C,MAAM,MAAoB,EACtB,OAAO,gBACV;AAMD,KAAI,QALwB;EACxB,OAAO;EACP,OAAO,MAAM,UAAU;EACvB,YAAY;EACf;AAED,QAAO;;AAGX,SAAS,yBAAyB,OAAe;CAC7C,MAAM,MAAoB,EACtB,OAAO,gBACV;AAMD,KAAI,QALwB;EACxB,OAAO;EACP;EACA,YAAY;EACf;AAED,QAAO"}
1
+ {"version":3,"file":"schema-generator.cjs","names":["fs","getSupportsArrays","ZModelCodeGenerator"],"sources":["../src/schema-generator.ts"],"sourcesContent":["import { lowerCaseFirst, upperCaseFirst } from '@zenstackhq/common-helpers';\nimport { formatDocument, loadDocument, ZModelCodeGenerator } from '@zenstackhq/language';\nimport {\n Argument,\n ArrayExpr,\n AttributeArg,\n BooleanLiteral,\n ConfigExpr,\n ConfigField,\n DataField,\n DataFieldAttribute,\n DataFieldType,\n DataModel,\n DataModelAttribute,\n DataSource,\n InvocationExpr,\n isDataModel,\n Model,\n NumberLiteral,\n ReferenceExpr,\n StringLiteral,\n} from '@zenstackhq/language/ast';\nimport { hasAttribute } from '@zenstackhq/language/utils';\nimport { type BetterAuthOptions } from 'better-auth';\nimport type { DBAdapterSchemaCreation } from 'better-auth/adapters';\nimport type { BetterAuthDBSchema, DBFieldAttribute, DBFieldType } from 'better-auth/db';\nimport fs from 'node:fs';\nimport { match } from 'ts-pattern';\nimport { getSupportsArrays, type AdapterConfig } from './config';\n\nexport async function generateSchema(\n file: string | undefined,\n tables: BetterAuthDBSchema,\n config: AdapterConfig,\n options: BetterAuthOptions,\n): Promise<DBAdapterSchemaCreation> {\n let filePath = file;\n\n if (!filePath) {\n // TODO: respect \"zenstack\" entry in package.json for default schema file path\n if (fs.existsSync('./schema.zmodel')) {\n filePath = './schema.zmodel';\n } else {\n filePath = './zenstack/schema.zmodel';\n }\n }\n\n const schemaExists = fs.existsSync(filePath);\n\n const schema = await updateSchema(filePath, tables, config, options);\n\n return {\n code: schema ?? '',\n path: filePath,\n overwrite: schemaExists && !!schema,\n };\n}\n\nasync function updateSchema(\n schemaPath: string,\n tables: BetterAuthDBSchema,\n config: AdapterConfig,\n options: BetterAuthOptions,\n) {\n let zmodel: Model | undefined;\n if (fs.existsSync(schemaPath)) {\n const loadResult = await loadDocument(schemaPath);\n if (!loadResult.success) {\n throw new Error(`Failed to load existing schema at ${schemaPath}: ${loadResult.errors.join(', ')}`);\n }\n zmodel = loadResult.model;\n } else {\n zmodel = initializeZmodel(config);\n }\n\n // collect to-many relations\n const toManyRelations = new Map();\n for (const [tableName, table] of Object.entries(tables)) {\n const fields = tables[tableName]?.fields;\n for (const field in fields) {\n const attr = fields[field]!;\n if (attr.references) {\n const referencedOriginalModel = attr.references.model;\n const referencedCustomModel = tables[referencedOriginalModel]?.modelName || referencedOriginalModel;\n const referencedModelNameCap = upperCaseFirst(referencedCustomModel);\n if (!toManyRelations.has(referencedModelNameCap)) {\n toManyRelations.set(referencedModelNameCap, new Set());\n }\n const currentCustomModel = table.modelName ?? tableName;\n const currentModelNameCap = upperCaseFirst(currentCustomModel);\n toManyRelations.get(referencedModelNameCap).add(currentModelNameCap);\n }\n }\n }\n\n let changed = false;\n\n const supportsArrays = getSupportsArrays(config);\n for (const [name, table] of Object.entries(tables)) {\n const c = addOrUpdateModel(\n name,\n table,\n zmodel,\n tables,\n toManyRelations,\n supportsArrays,\n !!options.advanced?.database?.useNumberId,\n );\n changed = changed || c;\n }\n\n if (!changed) {\n return undefined;\n }\n\n const generator = new ZModelCodeGenerator();\n let content = generator.generate(zmodel);\n\n try {\n content = await formatDocument(content);\n } catch {\n // ignore formatting errors\n }\n\n return content;\n}\n\n// @default(now())\nfunction addDefaultNow(df: DataField) {\n const nowArg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const nowExpr: InvocationExpr = {\n $type: 'InvocationExpr',\n function: { $refText: 'now' },\n args: [],\n $container: nowArg,\n };\n nowArg.value = nowExpr;\n addFieldAttribute(df, '@default', [nowArg]);\n}\n\nfunction createDataModel(modelName: string, zmodel: Model, numericId: boolean) {\n const dataModel: DataModel = {\n $type: 'DataModel',\n name: modelName,\n fields: [],\n attributes: [],\n mixins: [],\n comments: [],\n isView: false,\n $container: zmodel,\n };\n\n let idField: DataField;\n if (numericId) {\n idField = addModelField(dataModel, 'id', 'Int', false, false);\n } else {\n idField = addModelField(dataModel, 'id', 'String', false, false);\n }\n addFieldAttribute(idField, '@id');\n\n return dataModel;\n}\n\nfunction addModelField(dataModel: DataModel, fieldName: string, fieldType: string, array: boolean, optional: boolean) {\n const field: DataField = {\n $type: 'DataField',\n name: fieldName,\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n field.type = {\n $type: 'DataFieldType',\n type: fieldType as any,\n array,\n optional,\n $container: field,\n };\n dataModel.fields.push(field);\n return field;\n}\n\nfunction initializeZmodel(config: AdapterConfig) {\n const zmodel: Model = {\n $type: 'Model',\n declarations: [],\n imports: [],\n };\n\n // datasource db { ... }\n const ds: DataSource = {\n $type: 'DataSource',\n name: 'db',\n fields: [],\n $container: zmodel,\n };\n zmodel.declarations.push(ds);\n\n // provider = 'sqlite' | 'postgresql'\n const providerField: ConfigField = {\n $type: 'ConfigField',\n name: 'provider',\n $container: ds,\n } as any;\n providerField.value = {\n $type: 'StringLiteral',\n value: config.provider,\n $container: providerField,\n } satisfies ConfigExpr;\n\n const urlField: ConfigField = {\n $type: 'ConfigField',\n name: 'url',\n $container: ds,\n } as any;\n\n // env('DATABASE_URL')\n const envCall: InvocationExpr = {\n $type: 'InvocationExpr',\n function: {\n $refText: 'env',\n },\n args: [],\n $container: urlField,\n };\n\n // 'DATABASE_URL' arg\n const dbUrlArg: Argument = {\n $type: 'Argument',\n } as any;\n dbUrlArg.value = {\n $type: 'StringLiteral',\n value: 'DATABASE_URL',\n $container: dbUrlArg,\n } satisfies ConfigExpr;\n\n envCall.args = [dbUrlArg];\n\n urlField.value =\n config.provider === 'sqlite'\n ? {\n $type: 'StringLiteral',\n value: 'file:./dev.db',\n $container: urlField,\n }\n : envCall;\n\n ds.fields.push(providerField);\n ds.fields.push(urlField);\n\n return zmodel;\n}\n\nfunction getMappedFieldType({ bigint, type }: DBFieldAttribute, supportsArrays: boolean) {\n return match<DBFieldType, { type: string; array?: boolean }>(type)\n .with('string', () => ({ type: 'String' }))\n .with('number', () => (bigint ? { type: 'BigInt' } : { type: 'Int' }))\n .with('boolean', () => ({ type: 'Boolean' }))\n .with('date', () => ({ type: 'DateTime' }))\n .with('json', () => ({ type: 'Json' }))\n .with('string[]', () => (supportsArrays ? { type: 'String', array: true } : { type: 'Json' }))\n .with('number[]', () => (supportsArrays ? { type: 'Int', array: true } : { type: 'Json' }))\n .when(\n (v) => Array.isArray(v) && v.every((e) => typeof e === 'string'),\n () => {\n // Handle enum types (e.g., ['user', 'admin']), map them to String type for now\n return { type: 'String' };\n },\n )\n .otherwise(() => {\n throw new Error(`Unsupported field type: ${type}`);\n });\n}\n\nfunction addOrUpdateModel(\n tableName: string,\n table: BetterAuthDBSchema[string],\n zmodel: Model,\n tables: BetterAuthDBSchema,\n toManyRelations: Map<string, Set<string>>,\n supportsArrays: boolean,\n numericId: boolean,\n): boolean {\n let changed = false;\n const customModelName = tables[tableName]?.modelName ?? tableName;\n const modelName = upperCaseFirst(customModelName);\n\n let dataModel = zmodel.declarations.find((d): d is DataModel => isDataModel(d) && d.name === modelName);\n if (!dataModel) {\n changed = true;\n dataModel = createDataModel(modelName, zmodel, numericId);\n zmodel.declarations.push(dataModel);\n }\n\n if (modelName !== tableName && !hasAttribute(dataModel, '@@map')) {\n addModelAttribute(dataModel, '@@map', [createStringAttributeArg(tableName)]);\n }\n\n for (const [fName, field] of Object.entries(table.fields)) {\n const fieldName = field.fieldName ?? fName;\n if (dataModel.fields.some((f) => f.name === fieldName)) {\n continue;\n }\n\n changed = true;\n\n if (!field.references) {\n // scalar field\n const { array, type } = getMappedFieldType(field, supportsArrays);\n\n const df: DataField = {\n $type: 'DataField',\n name: fieldName,\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n df.type = {\n $type: 'DataFieldType',\n type: type as any,\n array: !!array,\n optional: !field.required,\n $container: df,\n };\n dataModel.fields.push(df);\n\n // @id\n if (fieldName === 'id') {\n addFieldAttribute(df, '@id');\n }\n\n // @unique\n if (field.unique) {\n addFieldAttribute(df, '@unique');\n }\n\n // @default\n if (field.defaultValue !== undefined) {\n if (fieldName === 'createdAt') {\n // @default(now())\n addDefaultNow(df);\n } else if (typeof field.defaultValue === 'boolean') {\n addFieldAttribute(df, '@default', [createBooleanAttributeArg(field.defaultValue)]);\n } else if (typeof field.defaultValue === 'string') {\n addFieldAttribute(df, '@default', [createStringAttributeArg(field.defaultValue)]);\n } else if (typeof field.defaultValue === 'number') {\n addFieldAttribute(df, '@default', [createNumberAttributeArg(field.defaultValue)]);\n } else if (typeof field.defaultValue === 'function') {\n // For other function-based defaults, we'll need to check what they return\n const defaultVal = field.defaultValue();\n if (defaultVal instanceof Date) {\n // @default(now())\n addDefaultNow(df);\n } else {\n console.warn(\n `Warning: Unsupported default function for field ${fieldName} in model ${table.modelName}. Please adjust manually.`,\n );\n }\n }\n }\n\n // This is a special handling for updatedAt fields\n if (fieldName === 'updatedAt' && field.onUpdate) {\n addFieldAttribute(df, '@updatedAt');\n } else if (field.onUpdate) {\n console.warn(\n `Warning: 'onUpdate' is only supported on 'updatedAt' fields. Please adjust manually for field ${fieldName} in model ${table.modelName}.`,\n );\n }\n } else {\n // relation\n\n // fk field\n addModelField(dataModel, fieldName, numericId ? 'Int' : 'String', false, !field.required);\n\n // relation field\n const referencedOriginalModelName = field.references.model;\n const referencedCustomModelName =\n tables[referencedOriginalModelName]?.modelName || referencedOriginalModelName;\n\n const relationField: DataField = {\n $type: 'DataField',\n name: lowerCaseFirst(referencedCustomModelName),\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n relationField.type = {\n $type: 'DataFieldType',\n reference: {\n $refText: upperCaseFirst(referencedCustomModelName),\n },\n array: (field.type as string).endsWith('[]'),\n optional: !field.required,\n $container: relationField,\n } satisfies DataFieldType;\n\n let action = 'Cascade';\n if (field.references.onDelete === 'no action') action = 'NoAction';\n else if (field.references.onDelete === 'set null') action = 'SetNull';\n else if (field.references.onDelete === 'set default') action = 'SetDefault';\n else if (field.references.onDelete === 'restrict') action = 'Restrict';\n\n // @relation(fields: [field], references: [referencedField], onDelete: action)\n const relationAttr: DataFieldAttribute = {\n $type: 'DataFieldAttribute',\n decl: {\n $refText: '@relation',\n },\n args: [],\n $container: relationField,\n };\n\n // fields: [field]\n const fieldsArg: AttributeArg = {\n $type: 'AttributeArg',\n name: 'fields',\n $container: relationAttr,\n } as any;\n const fieldsExpr: ArrayExpr = {\n $type: 'ArrayExpr',\n items: [],\n $container: fieldsArg,\n };\n const fkRefExpr: ReferenceExpr = {\n $type: 'ReferenceExpr',\n args: [],\n $container: fieldsExpr,\n target: {\n $refText: fieldName,\n },\n };\n fieldsExpr.items.push(fkRefExpr);\n fieldsArg.value = fieldsExpr;\n\n // references: [referencedField]\n const referencesArg: AttributeArg = {\n $type: 'AttributeArg',\n name: 'references',\n $container: relationAttr,\n } as any;\n const referencesExpr: ArrayExpr = {\n $type: 'ArrayExpr',\n items: [],\n $container: referencesArg,\n };\n const pkRefExpr: ReferenceExpr = {\n $type: 'ReferenceExpr',\n args: [],\n $container: referencesExpr,\n target: {\n $refText: field.references.field,\n },\n };\n referencesExpr.items.push(pkRefExpr);\n referencesArg.value = referencesExpr;\n\n // onDelete: action\n const onDeleteArg: AttributeArg = {\n $type: 'AttributeArg',\n name: 'onDelete',\n $container: relationAttr,\n } as any;\n const onDeleteValueExpr: ReferenceExpr = {\n $type: 'ReferenceExpr',\n target: { $refText: action },\n args: [],\n $container: onDeleteArg,\n };\n onDeleteArg.value = onDeleteValueExpr;\n\n relationAttr.args.push(...[fieldsArg, referencesArg, onDeleteArg]);\n relationField.attributes.push(relationAttr);\n\n dataModel.fields.push(relationField);\n }\n }\n\n // add to-many relations\n if (toManyRelations.has(modelName)) {\n const relations = toManyRelations.get(modelName)!;\n for (const relatedModel of relations) {\n const relationName = `${lowerCaseFirst(relatedModel)}s`;\n if (!dataModel.fields.some((f) => f.name === relationName)) {\n const relationField: DataField = {\n $type: 'DataField',\n name: relationName,\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n const relationType: DataFieldType = {\n $type: 'DataFieldType',\n reference: {\n $refText: relatedModel,\n },\n array: true,\n optional: false,\n $container: relationField,\n };\n relationField.type = relationType;\n dataModel.fields.push(relationField);\n }\n }\n }\n\n return changed;\n}\n\nfunction addModelAttribute(dataModel: DataModel, name: string, args: Omit<AttributeArg, '$container'>[] = []) {\n const attr: DataModelAttribute = {\n $type: 'DataModelAttribute',\n decl: { $refText: name },\n $container: dataModel,\n args: [],\n };\n const finalArgs = args.map((arg) => ({\n ...arg,\n $container: attr,\n }));\n attr.args.push(...finalArgs);\n dataModel.attributes.push(attr);\n}\n\nfunction addFieldAttribute(dataField: DataField, name: string, args: Omit<AttributeArg, '$container'>[] = []) {\n const attr: DataFieldAttribute = {\n $type: 'DataFieldAttribute',\n decl: { $refText: name },\n $container: dataField,\n args: [],\n };\n const finalArgs = args.map((arg) => ({\n ...arg,\n $container: attr,\n }));\n attr.args.push(...finalArgs);\n dataField.attributes.push(attr);\n}\n\nfunction createBooleanAttributeArg(value: boolean) {\n const arg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const expr: BooleanLiteral = {\n $type: 'BooleanLiteral',\n value,\n $container: arg,\n };\n arg.value = expr;\n return arg;\n}\n\nfunction createNumberAttributeArg(value: number) {\n const arg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const expr: NumberLiteral = {\n $type: 'NumberLiteral',\n value: value.toString(),\n $container: arg,\n };\n arg.value = expr;\n return arg;\n}\n\nfunction createStringAttributeArg(value: string) {\n const arg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const expr: StringLiteral = {\n $type: 'StringLiteral',\n value,\n $container: arg,\n };\n arg.value = expr;\n return arg;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,eAAsB,eAClB,MACA,QACA,QACA,SACgC;CAChC,IAAI,WAAW;AAEf,KAAI,CAAC,SAED,KAAIA,QAAAA,QAAG,WAAW,kBAAkB,CAChC,YAAW;KAEX,YAAW;CAInB,MAAM,eAAeA,QAAAA,QAAG,WAAW,SAAS;CAE5C,MAAM,SAAS,MAAM,aAAa,UAAU,QAAQ,QAAQ,QAAQ;AAEpE,QAAO;EACH,MAAM,UAAU;EAChB,MAAM;EACN,WAAW,gBAAgB,CAAC,CAAC;EAChC;;AAGL,eAAe,aACX,YACA,QACA,QACA,SACF;CACE,IAAI;AACJ,KAAIA,QAAAA,QAAG,WAAW,WAAW,EAAE;EAC3B,MAAM,aAAa,OAAA,GAAA,qBAAA,cAAmB,WAAW;AACjD,MAAI,CAAC,WAAW,QACZ,OAAM,IAAI,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EAAE;AACH,MAAK,KAAK,KAAK,GAAG,UAAU;AAC5B,WAAU,WAAW,KAAK,KAAK;;AAGnC,SAAS,0BAA0B,OAAgB;CAC/C,MAAM,MAAoB,EACtB,OAAO,gBACV;AAMD,KAAI,QALyB;EACzB,OAAO;EACP;EACA,YAAY;EACf;AAED,QAAO;;AAGX,SAAS,yBAAyB,OAAe;CAC7C,MAAM,MAAoB,EACtB,OAAO,gBACV;AAMD,KAAI,QALwB;EACxB,OAAO;EACP,OAAO,MAAM,UAAU;EACvB,YAAY;EACf;AAED,QAAO;;AAGX,SAAS,yBAAyB,OAAe;CAC7C,MAAM,MAAoB,EACtB,OAAO,gBACV;AAMD,KAAI,QALwB;EACxB,OAAO;EACP;EACA,YAAY;EACf;AAED,QAAO"}
@@ -1,4 +1,4 @@
1
- import { t as AdapterConfig } from "./adapter-Hkvk-P1x.cjs";
1
+ import { t as AdapterConfig } from "./config-2s6sqz6Y.cjs";
2
2
  import { BetterAuthOptions } from "better-auth";
3
3
  import { DBAdapterSchemaCreation } from "better-auth/adapters";
4
4
  import { BetterAuthDBSchema } from "better-auth/db";
@@ -1,4 +1,4 @@
1
- import { t as AdapterConfig } from "./adapter-4J4FE5_e.mjs";
1
+ import { t as AdapterConfig } from "./config-CjRnH6y3.mjs";
2
2
  import { DBAdapterSchemaCreation } from "better-auth/adapters";
3
3
  import { BetterAuthOptions } from "better-auth";
4
4
  import { BetterAuthDBSchema } from "better-auth/db";
@@ -1,3 +1,4 @@
1
+ import { t as getSupportsArrays } from "./config-PNXbQyyd.mjs";
1
2
  import { lowerCaseFirst, upperCaseFirst } from "@zenstackhq/common-helpers";
2
3
  import { ZModelCodeGenerator, formatDocument, loadDocument } from "@zenstackhq/language";
3
4
  import { isDataModel } from "@zenstackhq/language/ast";
@@ -40,8 +41,9 @@ async function updateSchema(schemaPath, tables, config, options) {
40
41
  }
41
42
  }
42
43
  let changed = false;
44
+ const supportsArrays = getSupportsArrays(config);
43
45
  for (const [name, table] of Object.entries(tables)) {
44
- const c = addOrUpdateModel(name, table, zmodel, tables, toManyRelations, !!options.advanced?.database?.useNumberId);
46
+ const c = addOrUpdateModel(name, table, zmodel, tables, toManyRelations, supportsArrays, !!options.advanced?.database?.useNumberId);
45
47
  changed = changed || c;
46
48
  }
47
49
  if (!changed) return;
@@ -146,20 +148,20 @@ function initializeZmodel(config) {
146
148
  ds.fields.push(urlField);
147
149
  return zmodel;
148
150
  }
149
- function getMappedFieldType({ bigint, type }) {
150
- return match(type).with("string", () => ({ type: "String" })).with("number", () => bigint ? { type: "BigInt" } : { type: "Int" }).with("boolean", () => ({ type: "Boolean" })).with("date", () => ({ type: "DateTime" })).with("json", () => ({ type: "Json" })).with("string[]", () => ({
151
+ function getMappedFieldType({ bigint, type }, supportsArrays) {
152
+ return match(type).with("string", () => ({ type: "String" })).with("number", () => bigint ? { type: "BigInt" } : { type: "Int" }).with("boolean", () => ({ type: "Boolean" })).with("date", () => ({ type: "DateTime" })).with("json", () => ({ type: "Json" })).with("string[]", () => supportsArrays ? {
151
153
  type: "String",
152
154
  array: true
153
- })).with("number[]", () => ({
155
+ } : { type: "Json" }).with("number[]", () => supportsArrays ? {
154
156
  type: "Int",
155
157
  array: true
156
- })).when((v) => Array.isArray(v) && v.every((e) => typeof e === "string"), () => {
158
+ } : { type: "Json" }).when((v) => Array.isArray(v) && v.every((e) => typeof e === "string"), () => {
157
159
  return { type: "String" };
158
160
  }).otherwise(() => {
159
161
  throw new Error(`Unsupported field type: ${type}`);
160
162
  });
161
163
  }
162
- function addOrUpdateModel(tableName, table, zmodel, tables, toManyRelations, numericId) {
164
+ function addOrUpdateModel(tableName, table, zmodel, tables, toManyRelations, supportsArrays, numericId) {
163
165
  let changed = false;
164
166
  const modelName = upperCaseFirst(tables[tableName]?.modelName ?? tableName);
165
167
  let dataModel = zmodel.declarations.find((d) => isDataModel(d) && d.name === modelName);
@@ -174,7 +176,7 @@ function addOrUpdateModel(tableName, table, zmodel, tables, toManyRelations, num
174
176
  if (dataModel.fields.some((f) => f.name === fieldName)) continue;
175
177
  changed = true;
176
178
  if (!field.references) {
177
- const { array, type } = getMappedFieldType(field);
179
+ const { array, type } = getMappedFieldType(field, supportsArrays);
178
180
  const df = {
179
181
  $type: "DataField",
180
182
  name: fieldName,
@@ -1 +1 @@
1
- {"version":3,"file":"schema-generator.mjs","names":[],"sources":["../src/schema-generator.ts"],"sourcesContent":["import { lowerCaseFirst, upperCaseFirst } from '@zenstackhq/common-helpers';\nimport { formatDocument, loadDocument, ZModelCodeGenerator } from '@zenstackhq/language';\nimport {\n Argument,\n ArrayExpr,\n AttributeArg,\n BooleanLiteral,\n ConfigExpr,\n ConfigField,\n DataField,\n DataFieldAttribute,\n DataFieldType,\n DataModel,\n DataModelAttribute,\n DataSource,\n InvocationExpr,\n isDataModel,\n Model,\n NumberLiteral,\n ReferenceExpr,\n StringLiteral,\n} from '@zenstackhq/language/ast';\nimport { hasAttribute } from '@zenstackhq/language/utils';\nimport { type BetterAuthOptions } from 'better-auth';\nimport type { DBAdapterSchemaCreation } from 'better-auth/adapters';\nimport type { BetterAuthDBSchema, DBFieldAttribute, DBFieldType } from 'better-auth/db';\nimport fs from 'node:fs';\nimport { match } from 'ts-pattern';\nimport type { AdapterConfig } from './adapter';\n\nexport async function generateSchema(\n file: string | undefined,\n tables: BetterAuthDBSchema,\n config: AdapterConfig,\n options: BetterAuthOptions,\n): Promise<DBAdapterSchemaCreation> {\n let filePath = file;\n\n if (!filePath) {\n // TODO: respect \"zenstack\" entry in package.json for default schema file path\n if (fs.existsSync('./schema.zmodel')) {\n filePath = './schema.zmodel';\n } else {\n filePath = './zenstack/schema.zmodel';\n }\n }\n\n const schemaExists = fs.existsSync(filePath);\n\n const schema = await updateSchema(filePath, tables, config, options);\n\n return {\n code: schema ?? '',\n path: filePath,\n overwrite: schemaExists && !!schema,\n };\n}\n\nasync function updateSchema(\n schemaPath: string,\n tables: BetterAuthDBSchema,\n config: AdapterConfig,\n options: BetterAuthOptions,\n) {\n let zmodel: Model | undefined;\n if (fs.existsSync(schemaPath)) {\n const loadResult = await loadDocument(schemaPath);\n if (!loadResult.success) {\n throw new Error(`Failed to load existing schema at ${schemaPath}: ${loadResult.errors.join(', ')}`);\n }\n zmodel = loadResult.model;\n } else {\n zmodel = initializeZmodel(config);\n }\n\n // collect to-many relations\n const toManyRelations = new Map();\n for (const [tableName, table] of Object.entries(tables)) {\n const fields = tables[tableName]?.fields;\n for (const field in fields) {\n const attr = fields[field]!;\n if (attr.references) {\n const referencedOriginalModel = attr.references.model;\n const referencedCustomModel = tables[referencedOriginalModel]?.modelName || referencedOriginalModel;\n const referencedModelNameCap = upperCaseFirst(referencedCustomModel);\n if (!toManyRelations.has(referencedModelNameCap)) {\n toManyRelations.set(referencedModelNameCap, new Set());\n }\n const currentCustomModel = table.modelName ?? tableName;\n const currentModelNameCap = upperCaseFirst(currentCustomModel);\n toManyRelations.get(referencedModelNameCap).add(currentModelNameCap);\n }\n }\n }\n\n let changed = false;\n\n for (const [name, table] of Object.entries(tables)) {\n const c = addOrUpdateModel(\n name,\n table,\n zmodel,\n tables,\n toManyRelations,\n !!options.advanced?.database?.useNumberId,\n );\n changed = changed || c;\n }\n\n if (!changed) {\n return undefined;\n }\n\n const generator = new ZModelCodeGenerator();\n let content = generator.generate(zmodel);\n\n try {\n content = await formatDocument(content);\n } catch {\n // ignore formatting errors\n }\n\n return content;\n}\n\n// @default(now())\nfunction addDefaultNow(df: DataField) {\n const nowArg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const nowExpr: InvocationExpr = {\n $type: 'InvocationExpr',\n function: { $refText: 'now' },\n args: [],\n $container: nowArg,\n };\n nowArg.value = nowExpr;\n addFieldAttribute(df, '@default', [nowArg]);\n}\n\nfunction createDataModel(modelName: string, zmodel: Model, numericId: boolean) {\n const dataModel: DataModel = {\n $type: 'DataModel',\n name: modelName,\n fields: [],\n attributes: [],\n mixins: [],\n comments: [],\n isView: false,\n $container: zmodel,\n };\n\n let idField: DataField;\n if (numericId) {\n idField = addModelField(dataModel, 'id', 'Int', false, false);\n } else {\n idField = addModelField(dataModel, 'id', 'String', false, false);\n }\n addFieldAttribute(idField, '@id');\n\n return dataModel;\n}\n\nfunction addModelField(dataModel: DataModel, fieldName: string, fieldType: string, array: boolean, optional: boolean) {\n const field: DataField = {\n $type: 'DataField',\n name: fieldName,\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n field.type = {\n $type: 'DataFieldType',\n type: fieldType as any,\n array,\n optional,\n $container: field,\n };\n dataModel.fields.push(field);\n return field;\n}\n\nfunction initializeZmodel(config: AdapterConfig) {\n const zmodel: Model = {\n $type: 'Model',\n declarations: [],\n imports: [],\n };\n\n // datasource db { ... }\n const ds: DataSource = {\n $type: 'DataSource',\n name: 'db',\n fields: [],\n $container: zmodel,\n };\n zmodel.declarations.push(ds);\n\n // provider = 'sqlite' | 'postgresql'\n const providerField: ConfigField = {\n $type: 'ConfigField',\n name: 'provider',\n $container: ds,\n } as any;\n providerField.value = {\n $type: 'StringLiteral',\n value: config.provider,\n $container: providerField,\n } satisfies ConfigExpr;\n\n const urlField: ConfigField = {\n $type: 'ConfigField',\n name: 'url',\n $container: ds,\n } as any;\n\n // env('DATABASE_URL')\n const envCall: InvocationExpr = {\n $type: 'InvocationExpr',\n function: {\n $refText: 'env',\n },\n args: [],\n $container: urlField,\n };\n\n // 'DATABASE_URL' arg\n const dbUrlArg: Argument = {\n $type: 'Argument',\n } as any;\n dbUrlArg.value = {\n $type: 'StringLiteral',\n value: 'DATABASE_URL',\n $container: dbUrlArg,\n } satisfies ConfigExpr;\n\n envCall.args = [dbUrlArg];\n\n urlField.value =\n config.provider === 'sqlite'\n ? {\n $type: 'StringLiteral',\n value: 'file:./dev.db',\n $container: urlField,\n }\n : envCall;\n\n ds.fields.push(providerField);\n ds.fields.push(urlField);\n\n return zmodel;\n}\n\nfunction getMappedFieldType({ bigint, type }: DBFieldAttribute) {\n return match<DBFieldType, { type: string; array?: boolean }>(type)\n .with('string', () => ({ type: 'String' }))\n .with('number', () => (bigint ? { type: 'BigInt' } : { type: 'Int' }))\n .with('boolean', () => ({ type: 'Boolean' }))\n .with('date', () => ({ type: 'DateTime' }))\n .with('json', () => ({ type: 'Json' }))\n .with('string[]', () => ({ type: 'String', array: true }))\n .with('number[]', () => ({ type: 'Int', array: true }))\n .when(\n (v) => Array.isArray(v) && v.every((e) => typeof e === 'string'),\n () => {\n // Handle enum types (e.g., ['user', 'admin']), map them to String type for now\n return { type: 'String' };\n },\n )\n .otherwise(() => {\n throw new Error(`Unsupported field type: ${type}`);\n });\n}\n\nfunction addOrUpdateModel(\n tableName: string,\n table: BetterAuthDBSchema[string],\n zmodel: Model,\n tables: BetterAuthDBSchema,\n toManyRelations: Map<string, Set<string>>,\n numericId: boolean,\n): boolean {\n let changed = false;\n const customModelName = tables[tableName]?.modelName ?? tableName;\n const modelName = upperCaseFirst(customModelName);\n\n let dataModel = zmodel.declarations.find((d): d is DataModel => isDataModel(d) && d.name === modelName);\n if (!dataModel) {\n changed = true;\n dataModel = createDataModel(modelName, zmodel, numericId);\n zmodel.declarations.push(dataModel);\n }\n\n if (modelName !== tableName && !hasAttribute(dataModel, '@@map')) {\n addModelAttribute(dataModel, '@@map', [createStringAttributeArg(tableName)]);\n }\n\n for (const [fName, field] of Object.entries(table.fields)) {\n const fieldName = field.fieldName ?? fName;\n if (dataModel.fields.some((f) => f.name === fieldName)) {\n continue;\n }\n\n changed = true;\n\n if (!field.references) {\n // scalar field\n const { array, type } = getMappedFieldType(field);\n\n const df: DataField = {\n $type: 'DataField',\n name: fieldName,\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n df.type = {\n $type: 'DataFieldType',\n type: type as any,\n array: !!array,\n optional: !field.required,\n $container: df,\n };\n dataModel.fields.push(df);\n\n // @id\n if (fieldName === 'id') {\n addFieldAttribute(df, '@id');\n }\n\n // @unique\n if (field.unique) {\n addFieldAttribute(df, '@unique');\n }\n\n // @default\n if (field.defaultValue !== undefined) {\n if (fieldName === 'createdAt') {\n // @default(now())\n addDefaultNow(df);\n } else if (typeof field.defaultValue === 'boolean') {\n addFieldAttribute(df, '@default', [createBooleanAttributeArg(field.defaultValue)]);\n } else if (typeof field.defaultValue === 'string') {\n addFieldAttribute(df, '@default', [createStringAttributeArg(field.defaultValue)]);\n } else if (typeof field.defaultValue === 'number') {\n addFieldAttribute(df, '@default', [createNumberAttributeArg(field.defaultValue)]);\n } else if (typeof field.defaultValue === 'function') {\n // For other function-based defaults, we'll need to check what they return\n const defaultVal = field.defaultValue();\n if (defaultVal instanceof Date) {\n // @default(now())\n addDefaultNow(df);\n } else {\n console.warn(\n `Warning: Unsupported default function for field ${fieldName} in model ${table.modelName}. Please adjust manually.`,\n );\n }\n }\n }\n\n // This is a special handling for updatedAt fields\n if (fieldName === 'updatedAt' && field.onUpdate) {\n addFieldAttribute(df, '@updatedAt');\n } else if (field.onUpdate) {\n console.warn(\n `Warning: 'onUpdate' is only supported on 'updatedAt' fields. Please adjust manually for field ${fieldName} in model ${table.modelName}.`,\n );\n }\n } else {\n // relation\n\n // fk field\n addModelField(dataModel, fieldName, numericId ? 'Int' : 'String', false, !field.required);\n\n // relation field\n const referencedOriginalModelName = field.references.model;\n const referencedCustomModelName =\n tables[referencedOriginalModelName]?.modelName || referencedOriginalModelName;\n\n const relationField: DataField = {\n $type: 'DataField',\n name: lowerCaseFirst(referencedCustomModelName),\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n relationField.type = {\n $type: 'DataFieldType',\n reference: {\n $refText: upperCaseFirst(referencedCustomModelName),\n },\n array: (field.type as string).endsWith('[]'),\n optional: !field.required,\n $container: relationField,\n } satisfies DataFieldType;\n\n let action = 'Cascade';\n if (field.references.onDelete === 'no action') action = 'NoAction';\n else if (field.references.onDelete === 'set null') action = 'SetNull';\n else if (field.references.onDelete === 'set default') action = 'SetDefault';\n else if (field.references.onDelete === 'restrict') action = 'Restrict';\n\n // @relation(fields: [field], references: [referencedField], onDelete: action)\n const relationAttr: DataFieldAttribute = {\n $type: 'DataFieldAttribute',\n decl: {\n $refText: '@relation',\n },\n args: [],\n $container: relationField,\n };\n\n // fields: [field]\n const fieldsArg: AttributeArg = {\n $type: 'AttributeArg',\n name: 'fields',\n $container: relationAttr,\n } as any;\n const fieldsExpr: ArrayExpr = {\n $type: 'ArrayExpr',\n items: [],\n $container: fieldsArg,\n };\n const fkRefExpr: ReferenceExpr = {\n $type: 'ReferenceExpr',\n args: [],\n $container: fieldsExpr,\n target: {\n $refText: fieldName,\n },\n };\n fieldsExpr.items.push(fkRefExpr);\n fieldsArg.value = fieldsExpr;\n\n // references: [referencedField]\n const referencesArg: AttributeArg = {\n $type: 'AttributeArg',\n name: 'references',\n $container: relationAttr,\n } as any;\n const referencesExpr: ArrayExpr = {\n $type: 'ArrayExpr',\n items: [],\n $container: referencesArg,\n };\n const pkRefExpr: ReferenceExpr = {\n $type: 'ReferenceExpr',\n args: [],\n $container: referencesExpr,\n target: {\n $refText: field.references.field,\n },\n };\n referencesExpr.items.push(pkRefExpr);\n referencesArg.value = referencesExpr;\n\n // onDelete: action\n const onDeleteArg: AttributeArg = {\n $type: 'AttributeArg',\n name: 'onDelete',\n $container: relationAttr,\n } as any;\n const onDeleteValueExpr: ReferenceExpr = {\n $type: 'ReferenceExpr',\n target: { $refText: action },\n args: [],\n $container: onDeleteArg,\n };\n onDeleteArg.value = onDeleteValueExpr;\n\n relationAttr.args.push(...[fieldsArg, referencesArg, onDeleteArg]);\n relationField.attributes.push(relationAttr);\n\n dataModel.fields.push(relationField);\n }\n }\n\n // add to-many relations\n if (toManyRelations.has(modelName)) {\n const relations = toManyRelations.get(modelName)!;\n for (const relatedModel of relations) {\n const relationName = `${lowerCaseFirst(relatedModel)}s`;\n if (!dataModel.fields.some((f) => f.name === relationName)) {\n const relationField: DataField = {\n $type: 'DataField',\n name: relationName,\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n const relationType: DataFieldType = {\n $type: 'DataFieldType',\n reference: {\n $refText: relatedModel,\n },\n array: true,\n optional: false,\n $container: relationField,\n };\n relationField.type = relationType;\n dataModel.fields.push(relationField);\n }\n }\n }\n\n return changed;\n}\n\nfunction addModelAttribute(dataModel: DataModel, name: string, args: Omit<AttributeArg, '$container'>[] = []) {\n const attr: DataModelAttribute = {\n $type: 'DataModelAttribute',\n decl: { $refText: name },\n $container: dataModel,\n args: [],\n };\n const finalArgs = args.map((arg) => ({\n ...arg,\n $container: attr,\n }));\n attr.args.push(...finalArgs);\n dataModel.attributes.push(attr);\n}\n\nfunction addFieldAttribute(dataField: DataField, name: string, args: Omit<AttributeArg, '$container'>[] = []) {\n const attr: DataFieldAttribute = {\n $type: 'DataFieldAttribute',\n decl: { $refText: name },\n $container: dataField,\n args: [],\n };\n const finalArgs = args.map((arg) => ({\n ...arg,\n $container: attr,\n }));\n attr.args.push(...finalArgs);\n dataField.attributes.push(attr);\n}\n\nfunction createBooleanAttributeArg(value: boolean) {\n const arg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const expr: BooleanLiteral = {\n $type: 'BooleanLiteral',\n value,\n $container: arg,\n };\n arg.value = expr;\n return arg;\n}\n\nfunction createNumberAttributeArg(value: number) {\n const arg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const expr: NumberLiteral = {\n $type: 'NumberLiteral',\n value: value.toString(),\n $container: arg,\n };\n arg.value = expr;\n return arg;\n}\n\nfunction createStringAttributeArg(value: string) {\n const arg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const expr: StringLiteral = {\n $type: 'StringLiteral',\n value,\n $container: arg,\n };\n arg.value = expr;\n return arg;\n}\n"],"mappings":";;;;;;;;AA8BA,eAAsB,eAClB,MACA,QACA,QACA,SACgC;CAChC,IAAI,WAAW;AAEf,KAAI,CAAC,SAED,KAAI,GAAG,WAAW,kBAAkB,CAChC,YAAW;KAEX,YAAW;CAInB,MAAM,eAAe,GAAG,WAAW,SAAS;CAE5C,MAAM,SAAS,MAAM,aAAa,UAAU,QAA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G,UAAU;AAC5B,WAAU,WAAW,KAAK,KAAK;;AAGnC,SAAS,0BAA0B,OAAgB;CAC/C,MAAM,MAAoB,EACtB,OAAO,gBACV;AAMD,KAAI,QALyB;EACzB,OAAO;EACP;EACA,YAAY;EACf;AAED,QAAO;;AAGX,SAAS,yBAAyB,OAAe;CAC7C,MAAM,MAAoB,EACtB,OAAO,gBACV;AAMD,KAAI,QALwB;EACxB,OAAO;EACP,OAAO,MAAM,UAAU;EACvB,YAAY;EACf;AAED,QAAO;;AAGX,SAAS,yBAAyB,OAAe;CAC7C,MAAM,MAAoB,EACtB,OAAO,gBACV;AAMD,KAAI,QALwB;EACxB,OAAO;EACP;EACA,YAAY;EACf;AAED,QAAO"}
1
+ {"version":3,"file":"schema-generator.mjs","names":[],"sources":["../src/schema-generator.ts"],"sourcesContent":["import { lowerCaseFirst, upperCaseFirst } from '@zenstackhq/common-helpers';\nimport { formatDocument, loadDocument, ZModelCodeGenerator } from '@zenstackhq/language';\nimport {\n Argument,\n ArrayExpr,\n AttributeArg,\n BooleanLiteral,\n ConfigExpr,\n ConfigField,\n DataField,\n DataFieldAttribute,\n DataFieldType,\n DataModel,\n DataModelAttribute,\n DataSource,\n InvocationExpr,\n isDataModel,\n Model,\n NumberLiteral,\n ReferenceExpr,\n StringLiteral,\n} from '@zenstackhq/language/ast';\nimport { hasAttribute } from '@zenstackhq/language/utils';\nimport { type BetterAuthOptions } from 'better-auth';\nimport type { DBAdapterSchemaCreation } from 'better-auth/adapters';\nimport type { BetterAuthDBSchema, DBFieldAttribute, DBFieldType } from 'better-auth/db';\nimport fs from 'node:fs';\nimport { match } from 'ts-pattern';\nimport { getSupportsArrays, type AdapterConfig } from './config';\n\nexport async function generateSchema(\n file: string | undefined,\n tables: BetterAuthDBSchema,\n config: AdapterConfig,\n options: BetterAuthOptions,\n): Promise<DBAdapterSchemaCreation> {\n let filePath = file;\n\n if (!filePath) {\n // TODO: respect \"zenstack\" entry in package.json for default schema file path\n if (fs.existsSync('./schema.zmodel')) {\n filePath = './schema.zmodel';\n } else {\n filePath = './zenstack/schema.zmodel';\n }\n }\n\n const schemaExists = fs.existsSync(filePath);\n\n const schema = await updateSchema(filePath, tables, config, options);\n\n return {\n code: schema ?? '',\n path: filePath,\n overwrite: schemaExists && !!schema,\n };\n}\n\nasync function updateSchema(\n schemaPath: string,\n tables: BetterAuthDBSchema,\n config: AdapterConfig,\n options: BetterAuthOptions,\n) {\n let zmodel: Model | undefined;\n if (fs.existsSync(schemaPath)) {\n const loadResult = await loadDocument(schemaPath);\n if (!loadResult.success) {\n throw new Error(`Failed to load existing schema at ${schemaPath}: ${loadResult.errors.join(', ')}`);\n }\n zmodel = loadResult.model;\n } else {\n zmodel = initializeZmodel(config);\n }\n\n // collect to-many relations\n const toManyRelations = new Map();\n for (const [tableName, table] of Object.entries(tables)) {\n const fields = tables[tableName]?.fields;\n for (const field in fields) {\n const attr = fields[field]!;\n if (attr.references) {\n const referencedOriginalModel = attr.references.model;\n const referencedCustomModel = tables[referencedOriginalModel]?.modelName || referencedOriginalModel;\n const referencedModelNameCap = upperCaseFirst(referencedCustomModel);\n if (!toManyRelations.has(referencedModelNameCap)) {\n toManyRelations.set(referencedModelNameCap, new Set());\n }\n const currentCustomModel = table.modelName ?? tableName;\n const currentModelNameCap = upperCaseFirst(currentCustomModel);\n toManyRelations.get(referencedModelNameCap).add(currentModelNameCap);\n }\n }\n }\n\n let changed = false;\n\n const supportsArrays = getSupportsArrays(config);\n for (const [name, table] of Object.entries(tables)) {\n const c = addOrUpdateModel(\n name,\n table,\n zmodel,\n tables,\n toManyRelations,\n supportsArrays,\n !!options.advanced?.database?.useNumberId,\n );\n changed = changed || c;\n }\n\n if (!changed) {\n return undefined;\n }\n\n const generator = new ZModelCodeGenerator();\n let content = generator.generate(zmodel);\n\n try {\n content = await formatDocument(content);\n } catch {\n // ignore formatting errors\n }\n\n return content;\n}\n\n// @default(now())\nfunction addDefaultNow(df: DataField) {\n const nowArg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const nowExpr: InvocationExpr = {\n $type: 'InvocationExpr',\n function: { $refText: 'now' },\n args: [],\n $container: nowArg,\n };\n nowArg.value = nowExpr;\n addFieldAttribute(df, '@default', [nowArg]);\n}\n\nfunction createDataModel(modelName: string, zmodel: Model, numericId: boolean) {\n const dataModel: DataModel = {\n $type: 'DataModel',\n name: modelName,\n fields: [],\n attributes: [],\n mixins: [],\n comments: [],\n isView: false,\n $container: zmodel,\n };\n\n let idField: DataField;\n if (numericId) {\n idField = addModelField(dataModel, 'id', 'Int', false, false);\n } else {\n idField = addModelField(dataModel, 'id', 'String', false, false);\n }\n addFieldAttribute(idField, '@id');\n\n return dataModel;\n}\n\nfunction addModelField(dataModel: DataModel, fieldName: string, fieldType: string, array: boolean, optional: boolean) {\n const field: DataField = {\n $type: 'DataField',\n name: fieldName,\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n field.type = {\n $type: 'DataFieldType',\n type: fieldType as any,\n array,\n optional,\n $container: field,\n };\n dataModel.fields.push(field);\n return field;\n}\n\nfunction initializeZmodel(config: AdapterConfig) {\n const zmodel: Model = {\n $type: 'Model',\n declarations: [],\n imports: [],\n };\n\n // datasource db { ... }\n const ds: DataSource = {\n $type: 'DataSource',\n name: 'db',\n fields: [],\n $container: zmodel,\n };\n zmodel.declarations.push(ds);\n\n // provider = 'sqlite' | 'postgresql'\n const providerField: ConfigField = {\n $type: 'ConfigField',\n name: 'provider',\n $container: ds,\n } as any;\n providerField.value = {\n $type: 'StringLiteral',\n value: config.provider,\n $container: providerField,\n } satisfies ConfigExpr;\n\n const urlField: ConfigField = {\n $type: 'ConfigField',\n name: 'url',\n $container: ds,\n } as any;\n\n // env('DATABASE_URL')\n const envCall: InvocationExpr = {\n $type: 'InvocationExpr',\n function: {\n $refText: 'env',\n },\n args: [],\n $container: urlField,\n };\n\n // 'DATABASE_URL' arg\n const dbUrlArg: Argument = {\n $type: 'Argument',\n } as any;\n dbUrlArg.value = {\n $type: 'StringLiteral',\n value: 'DATABASE_URL',\n $container: dbUrlArg,\n } satisfies ConfigExpr;\n\n envCall.args = [dbUrlArg];\n\n urlField.value =\n config.provider === 'sqlite'\n ? {\n $type: 'StringLiteral',\n value: 'file:./dev.db',\n $container: urlField,\n }\n : envCall;\n\n ds.fields.push(providerField);\n ds.fields.push(urlField);\n\n return zmodel;\n}\n\nfunction getMappedFieldType({ bigint, type }: DBFieldAttribute, supportsArrays: boolean) {\n return match<DBFieldType, { type: string; array?: boolean }>(type)\n .with('string', () => ({ type: 'String' }))\n .with('number', () => (bigint ? { type: 'BigInt' } : { type: 'Int' }))\n .with('boolean', () => ({ type: 'Boolean' }))\n .with('date', () => ({ type: 'DateTime' }))\n .with('json', () => ({ type: 'Json' }))\n .with('string[]', () => (supportsArrays ? { type: 'String', array: true } : { type: 'Json' }))\n .with('number[]', () => (supportsArrays ? { type: 'Int', array: true } : { type: 'Json' }))\n .when(\n (v) => Array.isArray(v) && v.every((e) => typeof e === 'string'),\n () => {\n // Handle enum types (e.g., ['user', 'admin']), map them to String type for now\n return { type: 'String' };\n },\n )\n .otherwise(() => {\n throw new Error(`Unsupported field type: ${type}`);\n });\n}\n\nfunction addOrUpdateModel(\n tableName: string,\n table: BetterAuthDBSchema[string],\n zmodel: Model,\n tables: BetterAuthDBSchema,\n toManyRelations: Map<string, Set<string>>,\n supportsArrays: boolean,\n numericId: boolean,\n): boolean {\n let changed = false;\n const customModelName = tables[tableName]?.modelName ?? tableName;\n const modelName = upperCaseFirst(customModelName);\n\n let dataModel = zmodel.declarations.find((d): d is DataModel => isDataModel(d) && d.name === modelName);\n if (!dataModel) {\n changed = true;\n dataModel = createDataModel(modelName, zmodel, numericId);\n zmodel.declarations.push(dataModel);\n }\n\n if (modelName !== tableName && !hasAttribute(dataModel, '@@map')) {\n addModelAttribute(dataModel, '@@map', [createStringAttributeArg(tableName)]);\n }\n\n for (const [fName, field] of Object.entries(table.fields)) {\n const fieldName = field.fieldName ?? fName;\n if (dataModel.fields.some((f) => f.name === fieldName)) {\n continue;\n }\n\n changed = true;\n\n if (!field.references) {\n // scalar field\n const { array, type } = getMappedFieldType(field, supportsArrays);\n\n const df: DataField = {\n $type: 'DataField',\n name: fieldName,\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n df.type = {\n $type: 'DataFieldType',\n type: type as any,\n array: !!array,\n optional: !field.required,\n $container: df,\n };\n dataModel.fields.push(df);\n\n // @id\n if (fieldName === 'id') {\n addFieldAttribute(df, '@id');\n }\n\n // @unique\n if (field.unique) {\n addFieldAttribute(df, '@unique');\n }\n\n // @default\n if (field.defaultValue !== undefined) {\n if (fieldName === 'createdAt') {\n // @default(now())\n addDefaultNow(df);\n } else if (typeof field.defaultValue === 'boolean') {\n addFieldAttribute(df, '@default', [createBooleanAttributeArg(field.defaultValue)]);\n } else if (typeof field.defaultValue === 'string') {\n addFieldAttribute(df, '@default', [createStringAttributeArg(field.defaultValue)]);\n } else if (typeof field.defaultValue === 'number') {\n addFieldAttribute(df, '@default', [createNumberAttributeArg(field.defaultValue)]);\n } else if (typeof field.defaultValue === 'function') {\n // For other function-based defaults, we'll need to check what they return\n const defaultVal = field.defaultValue();\n if (defaultVal instanceof Date) {\n // @default(now())\n addDefaultNow(df);\n } else {\n console.warn(\n `Warning: Unsupported default function for field ${fieldName} in model ${table.modelName}. Please adjust manually.`,\n );\n }\n }\n }\n\n // This is a special handling for updatedAt fields\n if (fieldName === 'updatedAt' && field.onUpdate) {\n addFieldAttribute(df, '@updatedAt');\n } else if (field.onUpdate) {\n console.warn(\n `Warning: 'onUpdate' is only supported on 'updatedAt' fields. Please adjust manually for field ${fieldName} in model ${table.modelName}.`,\n );\n }\n } else {\n // relation\n\n // fk field\n addModelField(dataModel, fieldName, numericId ? 'Int' : 'String', false, !field.required);\n\n // relation field\n const referencedOriginalModelName = field.references.model;\n const referencedCustomModelName =\n tables[referencedOriginalModelName]?.modelName || referencedOriginalModelName;\n\n const relationField: DataField = {\n $type: 'DataField',\n name: lowerCaseFirst(referencedCustomModelName),\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n relationField.type = {\n $type: 'DataFieldType',\n reference: {\n $refText: upperCaseFirst(referencedCustomModelName),\n },\n array: (field.type as string).endsWith('[]'),\n optional: !field.required,\n $container: relationField,\n } satisfies DataFieldType;\n\n let action = 'Cascade';\n if (field.references.onDelete === 'no action') action = 'NoAction';\n else if (field.references.onDelete === 'set null') action = 'SetNull';\n else if (field.references.onDelete === 'set default') action = 'SetDefault';\n else if (field.references.onDelete === 'restrict') action = 'Restrict';\n\n // @relation(fields: [field], references: [referencedField], onDelete: action)\n const relationAttr: DataFieldAttribute = {\n $type: 'DataFieldAttribute',\n decl: {\n $refText: '@relation',\n },\n args: [],\n $container: relationField,\n };\n\n // fields: [field]\n const fieldsArg: AttributeArg = {\n $type: 'AttributeArg',\n name: 'fields',\n $container: relationAttr,\n } as any;\n const fieldsExpr: ArrayExpr = {\n $type: 'ArrayExpr',\n items: [],\n $container: fieldsArg,\n };\n const fkRefExpr: ReferenceExpr = {\n $type: 'ReferenceExpr',\n args: [],\n $container: fieldsExpr,\n target: {\n $refText: fieldName,\n },\n };\n fieldsExpr.items.push(fkRefExpr);\n fieldsArg.value = fieldsExpr;\n\n // references: [referencedField]\n const referencesArg: AttributeArg = {\n $type: 'AttributeArg',\n name: 'references',\n $container: relationAttr,\n } as any;\n const referencesExpr: ArrayExpr = {\n $type: 'ArrayExpr',\n items: [],\n $container: referencesArg,\n };\n const pkRefExpr: ReferenceExpr = {\n $type: 'ReferenceExpr',\n args: [],\n $container: referencesExpr,\n target: {\n $refText: field.references.field,\n },\n };\n referencesExpr.items.push(pkRefExpr);\n referencesArg.value = referencesExpr;\n\n // onDelete: action\n const onDeleteArg: AttributeArg = {\n $type: 'AttributeArg',\n name: 'onDelete',\n $container: relationAttr,\n } as any;\n const onDeleteValueExpr: ReferenceExpr = {\n $type: 'ReferenceExpr',\n target: { $refText: action },\n args: [],\n $container: onDeleteArg,\n };\n onDeleteArg.value = onDeleteValueExpr;\n\n relationAttr.args.push(...[fieldsArg, referencesArg, onDeleteArg]);\n relationField.attributes.push(relationAttr);\n\n dataModel.fields.push(relationField);\n }\n }\n\n // add to-many relations\n if (toManyRelations.has(modelName)) {\n const relations = toManyRelations.get(modelName)!;\n for (const relatedModel of relations) {\n const relationName = `${lowerCaseFirst(relatedModel)}s`;\n if (!dataModel.fields.some((f) => f.name === relationName)) {\n const relationField: DataField = {\n $type: 'DataField',\n name: relationName,\n attributes: [],\n comments: [],\n $container: dataModel,\n } as any;\n const relationType: DataFieldType = {\n $type: 'DataFieldType',\n reference: {\n $refText: relatedModel,\n },\n array: true,\n optional: false,\n $container: relationField,\n };\n relationField.type = relationType;\n dataModel.fields.push(relationField);\n }\n }\n }\n\n return changed;\n}\n\nfunction addModelAttribute(dataModel: DataModel, name: string, args: Omit<AttributeArg, '$container'>[] = []) {\n const attr: DataModelAttribute = {\n $type: 'DataModelAttribute',\n decl: { $refText: name },\n $container: dataModel,\n args: [],\n };\n const finalArgs = args.map((arg) => ({\n ...arg,\n $container: attr,\n }));\n attr.args.push(...finalArgs);\n dataModel.attributes.push(attr);\n}\n\nfunction addFieldAttribute(dataField: DataField, name: string, args: Omit<AttributeArg, '$container'>[] = []) {\n const attr: DataFieldAttribute = {\n $type: 'DataFieldAttribute',\n decl: { $refText: name },\n $container: dataField,\n args: [],\n };\n const finalArgs = args.map((arg) => ({\n ...arg,\n $container: attr,\n }));\n attr.args.push(...finalArgs);\n dataField.attributes.push(attr);\n}\n\nfunction createBooleanAttributeArg(value: boolean) {\n const arg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const expr: BooleanLiteral = {\n $type: 'BooleanLiteral',\n value,\n $container: arg,\n };\n arg.value = expr;\n return arg;\n}\n\nfunction createNumberAttributeArg(value: number) {\n const arg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const expr: NumberLiteral = {\n $type: 'NumberLiteral',\n value: value.toString(),\n $container: arg,\n };\n arg.value = expr;\n return arg;\n}\n\nfunction createStringAttributeArg(value: string) {\n const arg: AttributeArg = {\n $type: 'AttributeArg',\n } as any;\n const expr: StringLiteral = {\n $type: 'StringLiteral',\n value,\n $container: arg,\n };\n arg.value = expr;\n return arg;\n}\n"],"mappings":";;;;;;;;;AA8BA,eAAsB,eAClB,MACA,QACA,QACA,SACgC;CAChC,IAAI,WAAW;AAEf,KAAI,CAAC,SAED,KAAI,GAAG,WAAW,kBAAkB,CAChC,YAAW;KAEX,YAAW;CAInB,MAAM,eAAe,GAAG,WAAW,SAAS;CAE5C,MAAM,SAAS,MAAM,aAAa,UAAU,QAAQ,QAAQ,QAAQ;AAEpE,QAAO;EACH,MAAM,UAAU;EAChB,MAAM;EACN,WAAW,gBAAgB,CAAC,CAAC;EAChC;;AAGL,eAAe,aACX,YACA,QACA,QACA,SACF;CACE,IAAI;AACJ,KAAI,GAAG,WAAW,WAAW,EAAE;EAC3B,MAAM,aAAa,MAAM,aAAa,WAAW;AACjD,MAAI,CAAC,WAAW,QACZ,OAAM,IAAI,MAAM,qCAAqC,WAAW,IAAI,WAAW,OAAO,KAAK,KAAK,GAAG;AAEvG,WAAS,WAAW;OAEpB,UAAS,iBAAiB,OAAO;CAIrC,MAAM,kCAAkB,IAAI,KAAK;AACjC,MAAK,MAAM,CAAC,WAAW,UAAU,OAAO,QAAQ,OAAO,EAAE;EACrD,MAAM,SAAS,OAAO,YAAY;AAClC,OAAK,MAAM,SAAS,QAAQ;GACxB,MAAM,OAAO,OAAO;AACpB,OAAI,KAAK,YAAY;IACjB,MAAM,0BAA0B,KAAK,WAAW;IAEhD,MAAM,yBAAyB,eADD,OAAO,0BAA0B,aAAa,wBACR;AACpE,QAAI,CAAC,gBAAgB,IAAI,uBAAuB,CAC5C,iBAAgB,IAAI,wCAAwB,IAAI,KAAK,CAAC;IAG1D,MAAM,sBAAsB,eADD,MAAM,aAAa,UACgB;AAC9D,oBAAgB,IAAI,uBAAuB,CAAC,IAAI,oBAAoB;;;;CA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AkB,WAAsB,MAAc,OAA2C,EAAE,EAAE;CAC1G,MAAM,OAA2B;EAC7B,OAAO;EACP,MAAM,EAAE,UAAU,MAAM;EACxB,YAAY;EACZ,MAAM,EAAE;EACX;CACD,MAAM,YAAY,KAAK,KAAK,SAAS;EACjC,GAAG;EACH,YAAY;EACf,EAAE;AACH,MAAK,KAAK,KAAK,GAAG,UAAU;AAC5B,WAAU,WAAW,KAAK,KAAK;;AAGnC,SAAS,kBAAkB,WAAsB,MAAc,OAA2C,EAAE,EAAE;CAC1G,MAAM,OAA2B;EAC7B,OAAO;EACP,MAAM,EAAE,UAAU,MAAM;EACxB,YAAY;EACZ,MAAM,EAAE;EACX;CACD,MAAM,YAAY,KAAK,KAAK,SAAS;EACjC,GAAG;EACH,YAAY;EACf,EAAE;AACH,MAAK,KAAK,KAAK,GAAG,UAAU;AAC5B,WAAU,WAAW,KAAK,KAAK;;AAGnC,SAAS,0BAA0B,OAAgB;CAC/C,MAAM,MAAoB,EACtB,OAAO,gBACV;AAMD,KAAI,QALyB;EACzB,OAAO;EACP;EACA,YAAY;EACf;AAED,QAAO;;AAGX,SAAS,yBAAyB,OAAe;CAC7C,MAAM,MAAoB,EACtB,OAAO,gBACV;AAMD,KAAI,QALwB;EACxB,OAAO;EACP,OAAO,MAAM,UAAU;EACvB,YAAY;EACf;AAED,QAAO;;AAGX,SAAS,yBAAyB,OAAe;CAC7C,MAAM,MAAoB,EACtB,OAAO,gBACV;AAMD,KAAI,QALwB;EACxB,OAAO;EACP;EACA,YAAY;EACf;AAED,QAAO"}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@zenstackhq/better-auth",
3
3
  "displayName": "ZenStack Better Auth Adapter",
4
4
  "description": "ZenStack Better Auth Adapter. This adapter is modified from better-auth's Prisma adapter.",
5
- "version": "3.6.3",
5
+ "version": "3.7.0-beta.1",
6
6
  "type": "module",
7
7
  "author": {
8
8
  "name": "ZenStack Team",
@@ -49,9 +49,9 @@
49
49
  },
50
50
  "dependencies": {
51
51
  "ts-pattern": "^5.7.1",
52
- "@zenstackhq/orm": "3.6.3",
53
- "@zenstackhq/common-helpers": "3.6.3",
54
- "@zenstackhq/language": "3.6.3"
52
+ "@zenstackhq/language": "3.7.0-beta.1",
53
+ "@zenstackhq/common-helpers": "3.7.0-beta.1",
54
+ "@zenstackhq/orm": "3.7.0-beta.1"
55
55
  },
56
56
  "peerDependencies": {
57
57
  "@better-auth/core": "^1.3.0",
@@ -64,11 +64,11 @@
64
64
  "better-auth": "1.4.19",
65
65
  "kysely": "~0.28.16",
66
66
  "tmp": "^0.2.4",
67
- "@zenstackhq/cli": "3.6.3",
68
- "@zenstackhq/tsdown-config": "3.6.3",
69
- "@zenstackhq/typescript-config": "3.6.3",
70
- "@zenstackhq/vitest-config": "3.6.3",
71
- "@zenstackhq/eslint-config": "3.6.3"
67
+ "@zenstackhq/cli": "3.7.0-beta.1",
68
+ "@zenstackhq/eslint-config": "3.7.0-beta.1",
69
+ "@zenstackhq/tsdown-config": "3.7.0-beta.1",
70
+ "@zenstackhq/typescript-config": "3.7.0-beta.1",
71
+ "@zenstackhq/vitest-config": "3.7.0-beta.1"
72
72
  },
73
73
  "funding": "https://github.com/sponsors/zenstackhq",
74
74
  "scripts": {
@@ -1,36 +0,0 @@
1
- import { BetterAuthOptions } from "@better-auth/core";
2
- import { DBAdapter, DBAdapterDebugLogOption } from "@better-auth/core/db/adapter";
3
- import { ClientContract } from "@zenstackhq/orm";
4
- import { SchemaDef } from "@zenstackhq/orm/schema";
5
-
6
- //#region src/adapter.d.ts
7
- /**
8
- * Options for the ZenStack adapter factory.
9
- */
10
- interface AdapterConfig {
11
- /**
12
- * Database provider
13
- */
14
- provider: 'sqlite' | 'postgresql';
15
- /**
16
- * Enable debug logs for the adapter
17
- *
18
- * @default false
19
- */
20
- debugLogs?: DBAdapterDebugLogOption | undefined;
21
- /**
22
- * Use plural table names
23
- *
24
- * @default false
25
- */
26
- usePlural?: boolean | undefined;
27
- }
28
- /**
29
- * Create a Better-Auth adapter for ZenStack ORM.
30
- * @param db ZenStack ORM client instance
31
- * @param config adapter configuration options
32
- */
33
- declare const zenstackAdapter: <Schema extends SchemaDef>(db: ClientContract<Schema>, config: AdapterConfig) => (options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>;
34
- //#endregion
35
- export { zenstackAdapter as n, AdapterConfig as t };
36
- //# sourceMappingURL=adapter-4J4FE5_e.d.mts.map
@@ -1,36 +0,0 @@
1
- import { BetterAuthOptions } from "@better-auth/core";
2
- import { DBAdapter, DBAdapterDebugLogOption } from "@better-auth/core/db/adapter";
3
- import { ClientContract } from "@zenstackhq/orm";
4
- import { SchemaDef } from "@zenstackhq/orm/schema";
5
-
6
- //#region src/adapter.d.ts
7
- /**
8
- * Options for the ZenStack adapter factory.
9
- */
10
- interface AdapterConfig {
11
- /**
12
- * Database provider
13
- */
14
- provider: 'sqlite' | 'postgresql';
15
- /**
16
- * Enable debug logs for the adapter
17
- *
18
- * @default false
19
- */
20
- debugLogs?: DBAdapterDebugLogOption | undefined;
21
- /**
22
- * Use plural table names
23
- *
24
- * @default false
25
- */
26
- usePlural?: boolean | undefined;
27
- }
28
- /**
29
- * Create a Better-Auth adapter for ZenStack ORM.
30
- * @param db ZenStack ORM client instance
31
- * @param config adapter configuration options
32
- */
33
- declare const zenstackAdapter: <Schema extends SchemaDef>(db: ClientContract<Schema>, config: AdapterConfig) => (options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>;
34
- //#endregion
35
- export { zenstackAdapter as n, AdapterConfig as t };
36
- //# sourceMappingURL=adapter-Hkvk-P1x.d.cts.map