@zenstackhq/better-auth 3.7.0-beta.1 → 3.7.0
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.
- package/dist/index.cjs +7 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -4
- package/dist/index.d.mts +33 -4
- package/dist/index.mjs +6 -2
- package/dist/index.mjs.map +1 -1
- package/dist/schema-generator.cjs +6 -3
- package/dist/schema-generator.cjs.map +1 -1
- package/dist/schema-generator.d.cts +30 -1
- package/dist/schema-generator.d.mts +31 -2
- package/dist/schema-generator.mjs +5 -1
- package/dist/schema-generator.mjs.map +1 -1
- package/package.json +10 -10
- package/dist/config-2s6sqz6Y.d.cts +0 -33
- package/dist/config-CjRnH6y3.d.mts +0 -33
- package/dist/config-DELckboF.cjs +0 -13
- package/dist/config-DELckboF.cjs.map +0 -1
- package/dist/config-PNXbQyyd.mjs +0 -8
- package/dist/config-PNXbQyyd.mjs.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_config = require("./config-DELckboF.cjs");
|
|
3
|
-
require("./schema-generator.cjs");
|
|
4
2
|
let _better_auth_core_error = require("@better-auth/core/error");
|
|
5
3
|
let better_auth_adapters = require("better-auth/adapters");
|
|
4
|
+
//#region src/config.ts
|
|
5
|
+
function getSupportsArrays(config) {
|
|
6
|
+
return config.supportsArrays ?? config.provider === "postgresql";
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
6
9
|
//#region src/adapter.ts
|
|
7
10
|
/**
|
|
8
11
|
* Create a Better-Auth adapter for ZenStack ORM.
|
|
@@ -132,7 +135,7 @@ const zenstackAdapter = (db, config) => {
|
|
|
132
135
|
},
|
|
133
136
|
options: config,
|
|
134
137
|
createSchema: async ({ file, tables }) => {
|
|
135
|
-
const generateSchema = (await
|
|
138
|
+
const generateSchema = (await import("@zenstackhq/better-auth/schema-generator")).generateSchema;
|
|
136
139
|
return generateSchema(file, tables, config, options);
|
|
137
140
|
}
|
|
138
141
|
};
|
|
@@ -143,7 +146,7 @@ const zenstackAdapter = (db, config) => {
|
|
|
143
146
|
adapterName: "ZenStack Adapter",
|
|
144
147
|
usePlural: config.usePlural ?? false,
|
|
145
148
|
debugLogs: config.debugLogs ?? false,
|
|
146
|
-
supportsArrays:
|
|
149
|
+
supportsArrays: getSupportsArrays(config),
|
|
147
150
|
transaction: (cb) => db.$transaction((tx) => {
|
|
148
151
|
return cb((0, better_auth_adapters.createAdapterFactory)({
|
|
149
152
|
config: adapterOptions.config,
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,gBAAgB,uBAAuB;AAErD,WAAO,GACF,aAAa;KAAE;KAAO,OAAO,EAAE;KAAO,CAAC,GACpC,EAAE,aAAa,QAAQ,CAAC,EAAE,WACpB,EAAE,QACF,GACK,sBAAsB,EAAE,SAAS,GAAG,EAAE,OAC1C,EACd;;GAEL,MAAM,MAAM,MAAM,QAAQ,MAAM,EAAE,cAAc,SAAS,CAAC,EAAE,UAAU;GACtE,MAAM,KAAK,MAAM,QAAQ,MAAM,EAAE,cAAc,KAAK;GACpD,MAAM,YAAY,IAAI,KAAK,MAAM;AAC7B,WAAO,GACF,aAAa;KAAE;KAAO,OAAO,EAAE;KAAO,CAAC,GACpC,EAAE,aAAa,Q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J,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"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["BetterAuthError"],"sources":["../src/config.ts","../src/adapter.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","import type { BetterAuthOptions, Where } from 'better-auth';\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 // Self-import via package subpath (not a relative './schema-generator') so the\n // bundler treats it as external and keeps it lazy in the CJS output — see tsdown.config.ts.\n const generateSchema = (await import('@zenstackhq/better-auth/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) => {\n lazyOptions = options;\n return adapter(options);\n };\n};\n"],"mappings":";;;;AAiCA,SAAgB,kBAAkB,QAAuB;AACrD,QAAO,OAAO,kBAAkB,OAAO,aAAa;;;;;;;;;AChBxD,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,MA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D,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;IAGtC,MAAM,kBAAkB,MAAM,OAAO,6CAA6C;AAClF,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,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,YAA+B;AACnC,gBAAc;AACd,SAAO,QAAQ,QAAQ"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,16 +1,45 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { BetterAuthOptions } from "
|
|
3
|
-
import { DBAdapter } from "@better-auth/core/db/adapter";
|
|
1
|
+
import * as _$better_auth0 from "better-auth";
|
|
2
|
+
import { BetterAuthOptions } from "better-auth";
|
|
4
3
|
import { ClientContract } from "@zenstackhq/orm";
|
|
5
4
|
import { SchemaDef } from "@zenstackhq/orm/schema";
|
|
5
|
+
import { DBAdapterDebugLogOption } from "@better-auth/core/db/adapter";
|
|
6
6
|
|
|
7
|
+
//#region src/config.d.ts
|
|
8
|
+
/**
|
|
9
|
+
* Options for the ZenStack adapter factory.
|
|
10
|
+
*/
|
|
11
|
+
interface AdapterConfig {
|
|
12
|
+
/**
|
|
13
|
+
* Database provider
|
|
14
|
+
*/
|
|
15
|
+
provider: 'sqlite' | 'postgresql';
|
|
16
|
+
/**
|
|
17
|
+
* Enable debug logs for the adapter
|
|
18
|
+
*
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
21
|
+
debugLogs?: DBAdapterDebugLogOption | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Use plural table names
|
|
24
|
+
*
|
|
25
|
+
* @default false
|
|
26
|
+
*/
|
|
27
|
+
usePlural?: boolean | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Preserve Better Auth array fields as native database arrays.
|
|
30
|
+
*
|
|
31
|
+
* Defaults to true for PostgreSQL and false for SQLite.
|
|
32
|
+
*/
|
|
33
|
+
supportsArrays?: boolean | undefined;
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
7
36
|
//#region src/adapter.d.ts
|
|
8
37
|
/**
|
|
9
38
|
* Create a Better-Auth adapter for ZenStack ORM.
|
|
10
39
|
* @param db ZenStack ORM client instance
|
|
11
40
|
* @param config adapter configuration options
|
|
12
41
|
*/
|
|
13
|
-
declare const zenstackAdapter: <Schema extends SchemaDef>(db: ClientContract<Schema>, config: AdapterConfig) => (options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>;
|
|
42
|
+
declare const zenstackAdapter: <Schema extends SchemaDef>(db: ClientContract<Schema>, config: AdapterConfig) => (options: BetterAuthOptions) => _$better_auth0.DBAdapter<BetterAuthOptions>;
|
|
14
43
|
//#endregion
|
|
15
44
|
export { type AdapterConfig, zenstackAdapter };
|
|
16
45
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.mts
CHANGED
|
@@ -1,16 +1,45 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { BetterAuthOptions } from "
|
|
3
|
-
import { DBAdapter } from "@better-auth/core/db/adapter";
|
|
1
|
+
import * as _$better_auth0 from "better-auth";
|
|
2
|
+
import { BetterAuthOptions } from "better-auth";
|
|
4
3
|
import { ClientContract } from "@zenstackhq/orm";
|
|
5
4
|
import { SchemaDef } from "@zenstackhq/orm/schema";
|
|
5
|
+
import { DBAdapterDebugLogOption } from "@better-auth/core/db/adapter";
|
|
6
6
|
|
|
7
|
+
//#region src/config.d.ts
|
|
8
|
+
/**
|
|
9
|
+
* Options for the ZenStack adapter factory.
|
|
10
|
+
*/
|
|
11
|
+
interface AdapterConfig {
|
|
12
|
+
/**
|
|
13
|
+
* Database provider
|
|
14
|
+
*/
|
|
15
|
+
provider: 'sqlite' | 'postgresql';
|
|
16
|
+
/**
|
|
17
|
+
* Enable debug logs for the adapter
|
|
18
|
+
*
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
21
|
+
debugLogs?: DBAdapterDebugLogOption | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Use plural table names
|
|
24
|
+
*
|
|
25
|
+
* @default false
|
|
26
|
+
*/
|
|
27
|
+
usePlural?: boolean | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Preserve Better Auth array fields as native database arrays.
|
|
30
|
+
*
|
|
31
|
+
* Defaults to true for PostgreSQL and false for SQLite.
|
|
32
|
+
*/
|
|
33
|
+
supportsArrays?: boolean | undefined;
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
7
36
|
//#region src/adapter.d.ts
|
|
8
37
|
/**
|
|
9
38
|
* Create a Better-Auth adapter for ZenStack ORM.
|
|
10
39
|
* @param db ZenStack ORM client instance
|
|
11
40
|
* @param config adapter configuration options
|
|
12
41
|
*/
|
|
13
|
-
declare const zenstackAdapter: <Schema extends SchemaDef>(db: ClientContract<Schema>, config: AdapterConfig) => (options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>;
|
|
42
|
+
declare const zenstackAdapter: <Schema extends SchemaDef>(db: ClientContract<Schema>, config: AdapterConfig) => (options: BetterAuthOptions) => _$better_auth0.DBAdapter<BetterAuthOptions>;
|
|
14
43
|
//#endregion
|
|
15
44
|
export { type AdapterConfig, zenstackAdapter };
|
|
16
45
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import { t as getSupportsArrays } from "./config-PNXbQyyd.mjs";
|
|
2
1
|
import { BetterAuthError } from "@better-auth/core/error";
|
|
3
2
|
import { createAdapterFactory } from "better-auth/adapters";
|
|
3
|
+
//#region src/config.ts
|
|
4
|
+
function getSupportsArrays(config) {
|
|
5
|
+
return config.supportsArrays ?? config.provider === "postgresql";
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
4
8
|
//#region src/adapter.ts
|
|
5
9
|
/**
|
|
6
10
|
* Create a Better-Auth adapter for ZenStack ORM.
|
|
@@ -130,7 +134,7 @@ const zenstackAdapter = (db, config) => {
|
|
|
130
134
|
},
|
|
131
135
|
options: config,
|
|
132
136
|
createSchema: async ({ file, tables }) => {
|
|
133
|
-
const generateSchema = (await import("
|
|
137
|
+
const generateSchema = (await import("@zenstackhq/better-auth/schema-generator")).generateSchema;
|
|
134
138
|
return generateSchema(file, tables, config, options);
|
|
135
139
|
}
|
|
136
140
|
};
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"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,IAAI,gBAAgB,uBAAuB;AAErD,WAAO,GACF,aAAa;KAAE;KAAO,OAAO,EAAE;KAAO,CAAC,GACpC,EAAE,aAAa,QAAQ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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"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/config.ts","../src/adapter.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","import type { BetterAuthOptions, Where } from 'better-auth';\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 // Self-import via package subpath (not a relative './schema-generator') so the\n // bundler treats it as external and keeps it lazy in the CJS output — see tsdown.config.ts.\n const generateSchema = (await import('@zenstackhq/better-auth/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) => {\n lazyOptions = options;\n return adapter(options);\n };\n};\n"],"mappings":";;;AAiCA,SAAgB,kBAAkB,QAAuB;AACrD,QAAO,OAAO,kBAAkB,OAAO,aAAa;;;;;;;;;AChBxD,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;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,MAAM,SAAS,MAAM,QAAQ,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;IAGtC,MAAM,kBAAkB,MAAM,OAAO,6CAA6C;AAClF,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,YAA+B;AACnC,gBAAc;AACd,SAAO,QAAQ,QAAQ"}
|
|
@@ -21,7 +21,6 @@ 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");
|
|
25
24
|
let _zenstackhq_common_helpers = require("@zenstackhq/common-helpers");
|
|
26
25
|
let _zenstackhq_language = require("@zenstackhq/language");
|
|
27
26
|
let _zenstackhq_language_ast = require("@zenstackhq/language/ast");
|
|
@@ -30,6 +29,11 @@ require("better-auth");
|
|
|
30
29
|
let node_fs = require("node:fs");
|
|
31
30
|
node_fs = __toESM(node_fs, 1);
|
|
32
31
|
let ts_pattern = require("ts-pattern");
|
|
32
|
+
//#region src/config.ts
|
|
33
|
+
function getSupportsArrays(config) {
|
|
34
|
+
return config.supportsArrays ?? config.provider === "postgresql";
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
33
37
|
//#region src/schema-generator.ts
|
|
34
38
|
async function generateSchema(file, tables, config, options) {
|
|
35
39
|
let filePath = file;
|
|
@@ -65,7 +69,7 @@ async function updateSchema(schemaPath, tables, config, options) {
|
|
|
65
69
|
}
|
|
66
70
|
}
|
|
67
71
|
let changed = false;
|
|
68
|
-
const supportsArrays =
|
|
72
|
+
const supportsArrays = getSupportsArrays(config);
|
|
69
73
|
for (const [name, table] of Object.entries(tables)) {
|
|
70
74
|
const c = addOrUpdateModel(name, table, zmodel, tables, toManyRelations, supportsArrays, !!options.advanced?.database?.useNumberId);
|
|
71
75
|
changed = changed || c;
|
|
@@ -394,7 +398,6 @@ function createStringAttributeArg(value) {
|
|
|
394
398
|
return arg;
|
|
395
399
|
}
|
|
396
400
|
//#endregion
|
|
397
|
-
exports.__toESM = __toESM;
|
|
398
401
|
exports.generateSchema = generateSchema;
|
|
399
402
|
|
|
400
403
|
//# sourceMappingURL=schema-generator.cjs.map
|
|
@@ -1 +1 @@
|
|
|
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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E;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","ZModelCodeGenerator"],"sources":["../src/config.ts","../src/schema-generator.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","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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA,SAAgB,kBAAkB,QAAuB;AACrD,QAAO,OAAO,kBAAkB,OAAO,aAAa;;;;ACJxD,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,QAA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U,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,8 +1,37 @@
|
|
|
1
|
-
import { t as AdapterConfig } from "./config-2s6sqz6Y.cjs";
|
|
2
1
|
import { BetterAuthOptions } from "better-auth";
|
|
3
2
|
import { DBAdapterSchemaCreation } from "better-auth/adapters";
|
|
4
3
|
import { BetterAuthDBSchema } from "better-auth/db";
|
|
4
|
+
import { DBAdapterDebugLogOption } from "@better-auth/core/db/adapter";
|
|
5
5
|
|
|
6
|
+
//#region src/config.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
|
+
* Preserve Better Auth array fields as native database arrays.
|
|
29
|
+
*
|
|
30
|
+
* Defaults to true for PostgreSQL and false for SQLite.
|
|
31
|
+
*/
|
|
32
|
+
supportsArrays?: boolean | undefined;
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
6
35
|
//#region src/schema-generator.d.ts
|
|
7
36
|
declare function generateSchema(file: string | undefined, tables: BetterAuthDBSchema, config: AdapterConfig, options: BetterAuthOptions): Promise<DBAdapterSchemaCreation>;
|
|
8
37
|
//#endregion
|
|
@@ -1,8 +1,37 @@
|
|
|
1
|
-
import { t as AdapterConfig } from "./config-CjRnH6y3.mjs";
|
|
2
|
-
import { DBAdapterSchemaCreation } from "better-auth/adapters";
|
|
3
1
|
import { BetterAuthOptions } from "better-auth";
|
|
2
|
+
import { DBAdapterSchemaCreation } from "better-auth/adapters";
|
|
4
3
|
import { BetterAuthDBSchema } from "better-auth/db";
|
|
4
|
+
import { DBAdapterDebugLogOption } from "@better-auth/core/db/adapter";
|
|
5
5
|
|
|
6
|
+
//#region src/config.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
|
+
* Preserve Better Auth array fields as native database arrays.
|
|
29
|
+
*
|
|
30
|
+
* Defaults to true for PostgreSQL and false for SQLite.
|
|
31
|
+
*/
|
|
32
|
+
supportsArrays?: boolean | undefined;
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
6
35
|
//#region src/schema-generator.d.ts
|
|
7
36
|
declare function generateSchema(file: string | undefined, tables: BetterAuthDBSchema, config: AdapterConfig, options: BetterAuthOptions): Promise<DBAdapterSchemaCreation>;
|
|
8
37
|
//#endregion
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { t as getSupportsArrays } from "./config-PNXbQyyd.mjs";
|
|
2
1
|
import { lowerCaseFirst, upperCaseFirst } from "@zenstackhq/common-helpers";
|
|
3
2
|
import { ZModelCodeGenerator, formatDocument, loadDocument } from "@zenstackhq/language";
|
|
4
3
|
import { isDataModel } from "@zenstackhq/language/ast";
|
|
@@ -6,6 +5,11 @@ import { hasAttribute } from "@zenstackhq/language/utils";
|
|
|
6
5
|
import "better-auth";
|
|
7
6
|
import fs from "node:fs";
|
|
8
7
|
import { match } from "ts-pattern";
|
|
8
|
+
//#region src/config.ts
|
|
9
|
+
function getSupportsArrays(config) {
|
|
10
|
+
return config.supportsArrays ?? config.provider === "postgresql";
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
9
13
|
//#region src/schema-generator.ts
|
|
10
14
|
async function generateSchema(file, tables, config, options) {
|
|
11
15
|
let filePath = file;
|
|
@@ -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 { 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;AACj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AAO,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/config.ts","../src/schema-generator.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","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":";;;;;;;;AAiCA,SAAgB,kBAAkB,QAAuB;AACrD,QAAO,OAAO,kBAAkB,OAAO,aAAa;;;;ACJxD,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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,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.7.0
|
|
5
|
+
"version": "3.7.0",
|
|
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/
|
|
53
|
-
"@zenstackhq/
|
|
54
|
-
"@zenstackhq/
|
|
52
|
+
"@zenstackhq/common-helpers": "3.7.0",
|
|
53
|
+
"@zenstackhq/orm": "3.7.0",
|
|
54
|
+
"@zenstackhq/language": "3.7.0"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
57
|
"@better-auth/core": "^1.3.0",
|
|
@@ -62,13 +62,13 @@
|
|
|
62
62
|
"@better-auth/core": "1.4.19",
|
|
63
63
|
"@types/tmp": "^0.2.6",
|
|
64
64
|
"better-auth": "1.4.19",
|
|
65
|
-
"kysely": "~0.
|
|
65
|
+
"kysely": "~0.29.0",
|
|
66
66
|
"tmp": "^0.2.4",
|
|
67
|
-
"@zenstackhq/cli": "3.7.0
|
|
68
|
-
"@zenstackhq/
|
|
69
|
-
"@zenstackhq/
|
|
70
|
-
"@zenstackhq/
|
|
71
|
-
"@zenstackhq/vitest-config": "3.7.0
|
|
67
|
+
"@zenstackhq/cli": "3.7.0",
|
|
68
|
+
"@zenstackhq/tsdown-config": "3.7.0",
|
|
69
|
+
"@zenstackhq/typescript-config": "3.7.0",
|
|
70
|
+
"@zenstackhq/eslint-config": "3.7.0",
|
|
71
|
+
"@zenstackhq/vitest-config": "3.7.0"
|
|
72
72
|
},
|
|
73
73
|
"funding": "https://github.com/sponsors/zenstackhq",
|
|
74
74
|
"scripts": {
|
|
@@ -1,33 +0,0 @@
|
|
|
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
|
|
@@ -1,33 +0,0 @@
|
|
|
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
|
package/dist/config-DELckboF.cjs
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/dist/config-PNXbQyyd.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"}
|